【注意】最后更新于 October 16, 2018,文中内容可能已过时,请谨慎使用。
问题
#3707
1
  | dyld: Library not loaded: @rpath/FilesProvider.framework/Versions/A/FilesProvider
  | 
结论:cocoa pods不支持Command Line Tool项目
Cocoapods + Command Line Tool - dyld: Library not loaded: @rpath/Realm.framework/Versions/A/Realm
cocoa pods不支持Command Line Tool项目
Command Line Tool + CocoaPods frameworks breaks
采用SPM来管理依赖库
- 使用SPM创建可执行模版
 
1
2
  | $ mkdir SPMCmdLineTool
$ swift package init --type=executable
  | 
- 配置
Package.swift 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
  | import PackageDescription
let package = Package(
    name: "SPMCmdLineTool",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(path: "/Users/admin/hsg/FileProvider")
    ],
    .target(
        name: "SPMCmdLineTool",
        dependencies: ["FilesProvider"]),
        
    .testTarget(
        name: "SPMCmdLineToolTests",
        dependencies: ["SPMCmdLineTool"]),
    ]
)
  | 
- 使用workspace管理
先生成一个Xcode工程文件:
SPMCmdLineTool.xcodeproj 
1
  | $ swift package generate-xcodeproj
  | 
再把SPMCmdLineTool.xcodeproj拖入现有的workspace中来管理。
每次执行swift build ,须重新生成xcodeproj工程文件,再重新打开workspace即可。
命令行的单元测试赏析
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  | final class SPMCmdLineToolTests: XCTestCase {
    func testExample() throws {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct
        // results.
        // Some of the APIs that we use below are available in macOS 10.13 and above.
        guard #available(macOS 10.13, *) else {
            return
        }
        // 运行可执行文件
        let fooBinary = productsDirectory.appendingPathComponent("SPMCmdLineTool")
        let process = Process()
        // 指定可执行文件
        process.executableURL = fooBinary
        let pipe = Pipe()
        process.standardOutput = pipe
        //开始运行
        try process.run()
        process.waitUntilExit()
        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        let output = String(data: data, encoding: .utf8)
        XCTAssertEqual(output, "Hello, world!\n")
    }
    /// Returns path to the built products directory.
    var productsDirectory: URL {
        #if os(macOS)
            for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
                return bundle.bundleURL.deletingLastPathComponent()
            }
            fatalError("couldn't find the products directory")
        #else
            return Bundle.main.bundleURL
        #endif
    }
    static var allTests = [
        ("testExample", testExample),
    ]
}
  |