-
-
Notifications
You must be signed in to change notification settings - Fork 524
/
SwiftPackageManagerController.swift
145 lines (118 loc) · 5.29 KB
/
SwiftPackageManagerController.swift
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import Foundation
import TSCBasic
import TSCUtility
/// Protocol that defines an interface to interact with the Swift Package Manager.
public protocol SwiftPackageManagerControlling {
/// Resolves package dependencies.
/// - Parameters:
/// - path: Directory where the `Package.swift` is defined.
/// - printOutput: When true it prints the Swift Package Manager's output.
func resolve(at path: AbsolutePath, printOutput: Bool) throws
/// Updates package dependencies.
/// - Parameters:
/// - path: Directory where the `Package.swift` is defined.
/// - printOutput: When true it prints the Swift Package Manager's output.
func update(at path: AbsolutePath, printOutput: Bool) throws
/// Gets the tools version of the package at the given path
/// - Parameter path: Directory where the `Package.swift` is defined.
/// - Returns: Version of tools.
func getToolsVersion(at path: AbsolutePath) throws -> Version
/// Sets tools version of package to the given value.
/// - Parameter path: Directory where the `Package.swift` is defined.
/// - Parameter version: Version of tools. When `nil` then the environment’s version will be set.
func setToolsVersion(at path: AbsolutePath, to version: Version) throws
/// Loads the information from the package.
/// - Parameter path: Directory where the `Package.swift` is defined.
func loadPackageInfo(at path: AbsolutePath) throws -> PackageInfo
/// Builds a release binary containing release binaries compatible with arm64 and x86.
/// - Parameters:
/// - packagePath: Directory where the `Package.swift` is defined.
/// - product: Name of the product to be built.
/// - buildPath: Directory where the intermediary build products should be saved.
/// - outputPath: Directory where the fat binaries should be saved to.
func buildFatReleaseBinary(
packagePath: AbsolutePath,
product: String,
buildPath: AbsolutePath,
outputPath: AbsolutePath
) throws
}
public final class SwiftPackageManagerController: SwiftPackageManagerControlling {
public init() {}
public func resolve(at path: AbsolutePath, printOutput: Bool) throws {
let command = buildSwiftPackageCommand(packagePath: path, extraArguments: ["resolve"])
printOutput ?
try System.shared.runAndPrint(command) :
try System.shared.run(command)
}
public func update(at path: AbsolutePath, printOutput: Bool) throws {
let command = buildSwiftPackageCommand(packagePath: path, extraArguments: ["update"])
printOutput ?
try System.shared.runAndPrint(command) :
try System.shared.run(command)
}
public func setToolsVersion(at path: AbsolutePath, to version: Version) throws {
let extraArguments = ["tools-version", "--set", "\(version.major).\(version.minor)"]
let command = buildSwiftPackageCommand(packagePath: path, extraArguments: extraArguments)
try System.shared.run(command)
}
public func getToolsVersion(at path: AbsolutePath) throws -> Version {
let extraArguments = ["tools-version"]
let command = buildSwiftPackageCommand(packagePath: path, extraArguments: extraArguments)
let rawVersion = try System.shared.capture(command).trimmingCharacters(in: .whitespacesAndNewlines)
return try Version(versionString: rawVersion)
}
public func loadPackageInfo(at path: AbsolutePath) throws -> PackageInfo {
let command = buildSwiftPackageCommand(packagePath: path, extraArguments: ["dump-package"])
let json = try System.shared.capture(command)
let data = Data(json.utf8)
let decoder = JSONDecoder()
return try decoder.decode(PackageInfo.self, from: data)
}
public func buildFatReleaseBinary(
packagePath: AbsolutePath,
product: String,
buildPath: AbsolutePath,
outputPath: AbsolutePath
) throws {
let buildCommand: [String] = [
"swift", "build",
"--configuration", "release",
"--disable-sandbox",
"--package-path", packagePath.pathString,
"--product", product,
"--build-path", buildPath.pathString,
"--triple",
]
let arm64Target = "arm64-apple-macosx"
let x64Target = "x86_64-apple-macosx"
try System.shared.run(
buildCommand + [
arm64Target,
]
)
try System.shared.run(
buildCommand + [
x64Target,
]
)
if !FileHandler.shared.exists(outputPath) {
try FileHandler.shared.createFolder(outputPath)
}
try System.shared.run([
"lipo", "-create", "-output", outputPath.appending(component: product).pathString,
buildPath.appending(components: arm64Target, "release", product).pathString,
buildPath.appending(components: x64Target, "release", product).pathString,
])
}
// MARK: - Helpers
private func buildSwiftPackageCommand(packagePath: AbsolutePath, extraArguments: [String]) -> [String] {
[
"swift",
"package",
"--package-path",
packagePath.pathString,
]
+ extraArguments
}
}