从Package.swift运行shell脚本

时间:2017-07-28 13:17:02

标签: swift swift-package-manager

我试图在我在Swift中编写的CLI中包含由git describe生成的硬编码版本字符串。

我的想法是创建一个执行git describe的shell脚本,并将其作为全局常量写入名为Version.swift的文件中,然后我可以在我的工具中引用它。

我不想在每次构建之前考虑运行此脚本,或者通过另一个shell脚本构建项目。理想的方法是继续使用swift build并让Swift在编译和评估Package.swift时调用所述脚本。

所以我在我的包清单中包含以下内容。

import Foundation

let task = Process()
task.launchPath = "./"
task.arguments = ["update_version.sh"]
task.launch()
task.waitUntilExit()

虽然导入Foundation并初始化任务似乎工作正常,但实际启动它会崩溃并输出以下堆栈跟踪。

error: manifest parse error(s):
2017-07-28 15:07:43.711 swift[39828:16163314] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't posix_spawn: error 13'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff9be232cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffb0c5d48d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff9bea1c3d +[NSException raise:format:] + 205
    3   Foundation                          0x00007fff9d84c54e -[NSConcreteTask launchWithDictionary:] + 3134
    4   ???                                 0x0000000109ccb1c3 0x0 + 4459377091
    5   swift                               0x00000001045fe84f _ZN4llvm5MCJIT11runFunctionEPNS_8FunctionENS_8ArrayRefINS_12GenericValueEEE + 655
    6   swift                               0x0000000104604ab3 _ZN4llvm15ExecutionEngine17runFunctionAsMainEPNS_8FunctionERKNSt3__16vectorINS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEENS8_ISA_EEEEPKPKc + 707
    7   swift                               0x00000001039f4512 _ZL14performCompileRN5swift16CompilerInstanceERNS_18CompilerInvocationEN4llvm8ArrayRefIPKcEERiPNS_16FrontendObserverEPNS_20UnifiedStatsReporterE + 22802
    8   swift                               0x00000001039ed394 _ZN5swift15performFrontendEN4llvm8ArrayRefIPKcEES3_PvPNS_16FrontendObserverE + 7332
    9   swift                               0x00000001039a2ad8 main + 12248
    10  libdyld.dylib                       0x00007fffb1542235 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
0  swift                    0x0000000107073eaa PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x00000001070732e6 SignalHandler(int) + 662
2  libsystem_platform.dylib 0x00007fffb1751b3a _sigtramp + 26
3  libsystem_platform.dylib 0x00000003ba442240 _sigtramp + 147785504
4  libsystem_c.dylib        0x00007fffb15d6420 abort + 129
5  libc++abi.dylib          0x00007fffb012c84a __cxa_bad_cast + 0
6  libc++abi.dylib          0x00007fffb0151c4f default_unexpected_handler() + 0
7  libobjc.A.dylib          0x00007fffb0c5f6fe _objc_terminate() + 103
8  libc++abi.dylib          0x00007fffb014ed69 std::__terminate(void (*)()) + 8
9  libc++abi.dylib          0x00007fffb014e9f2 __cxa_rethrow + 99
10 libobjc.A.dylib          0x00007fffb0c5d94a objc_exception_rethrow + 40
11 Foundation               0x00007fff9d84c884 -[NSConcreteTask launchWithDictionary:] + 3956
12 Foundation               0x0000000109ccb1c3 -[NSConcreteTask launchWithDictionary:] + 1816656051
13 swift                    0x00000001045fe84f llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 655
14 swift                    0x0000000104604ab3 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, char const* const*) + 707
15 swift                    0x00000001039f4512 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 22802
16 swift                    0x00000001039ed394 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7332
17 swift                    0x00000001039a2ad8 main + 12248
18 libdyld.dylib            0x00007fffb1542235 start + 1
Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret /Users/kilian/dev/apodidae/Package.swift -target x86_64-apple-macosx10.10 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -I /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4 -swift-version 4 -module-name Package -lPackageDescription -- -fileno 5

update_version.sh的内容如下

echo "// THIS FILE IS AUTO-GENERATED\npublic let VERSION = \""$(git describe)"\"" > Sources/target/Version.swift

我能在这做什么才能让它发挥作用?这是否与在沙箱中执行的包清单有关?如果是这样,我有什么选择来完成我想要做的事情?如果以任何可能的方式,我真的很乐意只运行swift build并让其他正常工作

1 个答案:

答案 0 :(得分:0)

异常没有你的方法,系统提出它因为你的launchPath不是一个有效的启动路径:-)。您必须绝对确定您的launchPath的有效性,否则,执行将以一些未捕获的异常终止。有关解决方案,请参阅how to solve it