简体   繁体   English

从Package.swift运行shell脚本

[英]Run shell script from Package.swift

I'm trying to include a hardcoded version string generated by git describe within a CLI I'm writing in Swift. 我试图在我用Swift编写的CLI中包含git describe生成的硬编码版本字符串。

The idea was to have a shell script that executes git describe and writes that as a global constant into a file called Version.swift , which I can then reference from within my tool. 这个想法是要有一个执行git describe并将其作为全局常量写入到一个名为Version.swift的文件中的shell脚本,然后可以从我的工具中引用该文件。

I don't want to have to think about running this script before every build though or building the project via another shell script. 我不想在每次构建之前都考虑运行此脚本,也不必通过另一个Shell脚本构建项目。 The ideal way would be to keep using swift build and having Swift itself invoke said script when it compiles and evaluates Package.swift . 理想的方法是继续使用swift build并让Swift自身在编译和评估Package.swift时调用所述脚本。

So I included the following in my package manifest. 因此,我在包装清单中包括以下内容。

import Foundation

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

Whilst importing Foundation and initializing the task seems to work just fine, actually launching it crashes and outputs the following stack trace. 虽然导入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

The content of update_version.sh is the following update_version.sh的内容如下

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

What could I do here to get this to work? 我在这里可以做什么使它起作用? Does this have to do with the package manifest being executed within a sandbox? 这是否与在沙箱中执行的包清单有关? And if so, what options do I have to accomplish what I'm trying to do? 如果是这样,我要完成我想做的事情有什么选择? If in any way possible I'd really love to just run swift build and have the rest just work . 如果可能的话,我真的很希望运行swift build ,并使其余的工作正常

the exception has NOTHING with your approach, the system raised it because your launchPath is NOT a valid launch path :-). 异常与您的方法无关,系统将其引发,因为您的launchPath不是有效的启动路径:-)。 You have to be ABSOLUTELY sure about the validity of your launchPath, otherwise, the execution will be terminated with some uncaught exception. 您必须绝对确定launchPath的有效性,否则,执行将因未捕获的异常而终止。 For a solution see how to solve it 有关解决方案,请参阅解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到 Package.swift 清单(“没有 Package.Z818056DBD7E2012432Z06C 清单 11848C...” - Package.swift manifest not found ("has no Package.swift manifest for ...") 从现有的 Xcode 项目中为 Swift Package Manager 导出 Package.swift - Exporting Package.swift for Swift Package Manager from existing Xcode project 预处理器指令在 Package.swift 中不起作用? - Preprocessor directives not working in Package.swift? 将 Package.swift 文件转换为二进制 XCFramework - Turn Package.swift file into binary XCFramework 在没有 package.swift 的情况下安装第三方 Swift ZEFE90A8E604A7C840E88D03AD6 - Install a third party without package.swift with the Swift package manager 安装时出错 Swift Package - 无法访问/Package.swift - Error while installing Swift Package - unable to access /Package.swift 是否需要 package.swift 和 package.resolved? - Is there the need for both package.swift and package.resolved? 如何在Package.swift中指定模块的alpha版本? - How to specify alpha version of a module in Package.swift? Package.swift 平台从 v13 更改为 v14 时出现 Xcode 错误“v14 不可用” - Xcode error when Package.swift platform changed from v13 to v14 “v14 unavailable” 如何使用 Swift 从可可应用程序运行 shell 脚本? - How to run a shell script from cocoa application using Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM