2

我试图git describe在我用 Swift 编写的 CLI 中包含一个硬编码的版本字符串。

这个想法是有一个 shell 脚本来执行git describe并将其作为一个全局常量写入一个名为 的文件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,让其余的工作

4

1 回答 1

0

该异常与您的方法无关,系统引发了它,因为您的 launchPath 不是有效的启动路径:-)。您必须绝对确定您的启动路径的有效性,否则,执行将因一些未捕获的异常而终止。有关解决方案,请参阅如何解决

于 2017-07-29T07:04:54.910 回答