从Package.swift运行shell脚本
我试图在用Swift编写的CLI中包含由git descripe生成的硬编码版本字符串 其想法是让一个shell脚本执行从Package.swift运行shell脚本,swift,swift-package-manager,Swift,Swift Package Manager,我试图在用Swift编写的CLI中包含由git descripe生成的硬编码版本字符串 其想法是让一个shell脚本执行git descripe,并将其作为全局常量写入名为Version.swift的文件中,然后我可以从工具中引用该文件 我不想在每次构建之前都考虑运行这个脚本,或者通过另一个shell脚本构建项目。理想的方法是继续使用swift build,并让swift自己在编译和评估Package.swift时调用所述脚本 所以我在我的包裹清单中包括了以下内容 import Foundati
git descripe
,并将其作为全局常量写入名为Version.swift
的文件中,然后我可以从工具中引用该文件
我不想在每次构建之前都考虑运行这个脚本,或者通过另一个shell脚本构建项目。理想的方法是继续使用swift build
,并让swift自己在编译和评估Package.swift
时调用所述脚本
所以我在我的包裹清单中包括了以下内容
import Foundation
let task = Process()
task.launchPath = "./"
task.arguments = ["update_version.sh"]
task.launch()
task.waitUntilExit()
导入基础和初始化任务似乎工作正常,实际上启动它崩溃并输出下面的堆栈跟踪。
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
我能在这里做些什么来让它工作?这与在沙箱中执行的包清单有关吗?如果是这样,我有什么选择来完成我想做的事情?如果可能的话,我真的很想只运行
swift build
,让其余部分正常工作。异常与您的方法无关,系统引发了异常,因为您的启动路径不是有效的启动路径:-)。您必须绝对确定launchPath的有效性,否则,执行将以一些未捕获的异常终止。有关解决方案,请参见您是否考虑过makefile
?您可以设置一个运行脚本的生成规则,然后调用swift build
。我没有。这实际上比自定义构建脚本更有意义,但方向是相同的。但无论如何,感谢您的建议,makefile似乎是一个明智的退路。
echo "// THIS FILE IS AUTO-GENERATED\npublic let VERSION = \""$(git describe)"\"" > Sources/target/Version.swift