Skip to content

Commit 58ab36b

Browse files
committed
Model make, strip, and sha256sum
1 parent 3d7bb4b commit 58ab36b

File tree

4 files changed

+159
-23
lines changed

4 files changed

+159
-23
lines changed

Sources/SwiftlyCore/Commands.swift

+127
Original file line numberDiff line numberDiff line change
@@ -946,3 +946,130 @@ extension SystemCommand.SwiftCommand.PackageCommand.InitCommand: Runnable {}
946946
extension SystemCommand.SwiftCommand.SdkCommand.InstallCommand: Runnable {}
947947
extension SystemCommand.SwiftCommand.SdkCommand.RemoveCommand: Runnable {}
948948
extension SystemCommand.SwiftCommand.BuildCommand: Runnable {}
949+
950+
extension SystemCommand {
951+
public static func make(executable: Executable = MakeCommand.defaultExecutable) -> MakeCommand {
952+
MakeCommand(executable: executable)
953+
}
954+
955+
public struct MakeCommand {
956+
public static var defaultExecutable: Executable { .name("make") }
957+
958+
public var executable: Executable
959+
960+
public init(executable: Executable) {
961+
self.executable = executable
962+
}
963+
964+
public func config() -> Configuration {
965+
var args: [String] = []
966+
967+
return Configuration(
968+
executable: self.executable,
969+
arguments: Arguments(args),
970+
environment: .inherit
971+
)
972+
}
973+
974+
public func install() -> InstallCommand {
975+
InstallCommand(self)
976+
}
977+
978+
public struct InstallCommand {
979+
var make: MakeCommand
980+
981+
init(_ make: MakeCommand) {
982+
self.make = make
983+
}
984+
985+
public func config() -> Configuration {
986+
var c = self.make.config()
987+
988+
var args = c.arguments.storage.map(\.description)
989+
990+
args.append("install")
991+
992+
c.arguments = .init(args)
993+
994+
return c
995+
}
996+
}
997+
}
998+
}
999+
1000+
extension SystemCommand.MakeCommand: Runnable {}
1001+
extension SystemCommand.MakeCommand.InstallCommand: Runnable {}
1002+
1003+
extension SystemCommand {
1004+
public static func strip(executable: Executable = StripCommand.defaultExecutable, names: FilePath...) -> StripCommand {
1005+
self.strip(executable: executable, names: names)
1006+
}
1007+
1008+
public static func strip(executable: Executable = StripCommand.defaultExecutable, names: [FilePath]) -> StripCommand {
1009+
StripCommand(executable: executable, names: names)
1010+
}
1011+
1012+
public struct StripCommand {
1013+
public static var defaultExecutable: Executable { .name("strip") }
1014+
1015+
public var executable: Executable
1016+
1017+
public var names: [FilePath]
1018+
1019+
public init(executable: Executable, names: [FilePath]) {
1020+
self.executable = executable
1021+
self.names = names
1022+
}
1023+
1024+
public func config() -> Configuration {
1025+
var args: [String] = []
1026+
1027+
args.append(contentsOf: self.names.map(\.string))
1028+
1029+
return Configuration(
1030+
executable: self.executable,
1031+
arguments: Arguments(args),
1032+
environment: .inherit
1033+
)
1034+
}
1035+
}
1036+
}
1037+
1038+
extension SystemCommand.StripCommand: Runnable {}
1039+
1040+
extension SystemCommand {
1041+
public static func sha256sum(executable: Executable = Sha256SumCommand.defaultExecutable, files: FilePath...) -> Sha256SumCommand {
1042+
self.sha256sum(executable: executable, files: files)
1043+
}
1044+
1045+
public static func sha256sum(executable: Executable, files: [FilePath]) -> Sha256SumCommand {
1046+
Sha256SumCommand(executable: executable, files: files)
1047+
}
1048+
1049+
public struct Sha256SumCommand {
1050+
public static var defaultExecutable: Executable { .name("sha256sum") }
1051+
1052+
public var executable: Executable
1053+
1054+
public var files: [FilePath]
1055+
1056+
public init(executable: Executable, files: [FilePath]) {
1057+
self.executable = executable
1058+
self.files = files
1059+
}
1060+
1061+
public func config() -> Configuration {
1062+
var args: [String] = []
1063+
1064+
args.append(contentsOf: self.files.map(\.string))
1065+
1066+
return Configuration(
1067+
executable: self.executable,
1068+
arguments: Arguments(args),
1069+
environment: .inherit
1070+
)
1071+
}
1072+
}
1073+
}
1074+
1075+
extension SystemCommand.Sha256SumCommand: Output {}

Sources/SwiftlyCore/ModeledCommandLine.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public protocol Runnable {
154154
}
155155

156156
extension Runnable {
157-
public func run(_ p: Platform, quiet: Bool = false) async throws {
157+
public func run(_ p: Platform, env: [String: String] = ProcessInfo.processInfo.environment, quiet: Bool = false) async throws {
158158
let c = self.config()
159159
let executable = switch c.executable.storage {
160160
case let .executable(name):
@@ -163,16 +163,19 @@ extension Runnable {
163163
p.string
164164
}
165165
let args = c.arguments.storage.map(\.description)
166-
var env: [String: String] = ProcessInfo.processInfo.environment
166+
167+
var newEnv: [String: String] = env
168+
167169
switch c.environment.config {
168170
case let .inherit(newValue):
169171
for (key, value) in newValue {
170-
env[key] = value
172+
newEnv[key] = value
171173
}
172174
case let .custom(newValue):
173-
env = newValue
175+
newEnv = newValue
174176
}
175-
try await p.runProgram([executable] + args, quiet: quiet, env: env)
177+
178+
try await p.runProgram([executable] + args, quiet: quiet, env: newEnv)
176179
}
177180
}
178181

Tests/SwiftlyTests/CommandLineTests.swift

+15
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,19 @@ public struct CommandLineTests {
194194
try await sys.swift().package()._init(.packagePath(tmp), .type("executable")).run(Swiftly.currentPlatform)
195195
try await sys.swift().build(.packagePath(tmp), .configuration("release"))
196196
}
197+
198+
@Test func testMake() async throws {
199+
var config = sys.make().install().config()
200+
#expect(String(describing: config) == "make install")
201+
}
202+
203+
@Test func testStrip() async throws {
204+
var config = sys.strip(names: FilePath("foo")).config()
205+
#expect(String(describing: config) == "strip foo")
206+
}
207+
208+
@Test func testSha256Sum() async throws {
209+
var config = sys.sha256sum(files: FilePath("abcde")).config()
210+
#expect(String(describing: config) == "sha256sum abcde")
211+
}
197212
}

Tools/build-swiftly-release/BuildSwiftlyRelease.swift

+9-18
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
213213
return nil
214214
}
215215

216-
func checkGitRepoStatus(_: String) async throws {
216+
func checkGitRepoStatus() async throws {
217217
guard !self.skip else {
218218
return
219219
}
@@ -241,12 +241,8 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
241241
func buildLinuxRelease() async throws {
242242
// TODO: turn these into checks that the system meets the criteria for being capable of using the toolchain + checking for packages, not tools
243243
let curl = try await self.assertTool("curl", message: "Please install curl with `yum install curl`")
244-
let make = try await self.assertTool("make", message: "Please install make with `yum install make`")
245-
let git = try await self.assertTool("git", message: "Please install git with `yum install git`")
246-
let strip = try await self.assertTool("strip", message: "Please install strip with `yum install binutils`")
247-
let sha256sum = try await self.assertTool("sha256sum", message: "Please install sha256sum with `yum install coreutils`")
248244

249-
try await self.checkGitRepoStatus(git)
245+
try await self.checkGitRepoStatus()
250246

251247
// Start with a fresh SwiftPM package
252248
try await sys.swift().package().reset().run(currentPlatform)
@@ -264,7 +260,7 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
264260

265261
try? FileManager.default.removeItem(atPath: libArchivePath)
266262
try runProgram(curl, "-L", "-o", "\(buildCheckoutsDir + "/libarchive-\(libArchiveVersion).tar.gz")", "--remote-name", "--location", "https://github.com/libarchive/libarchive/releases/download/v\(libArchiveVersion)/libarchive-\(libArchiveVersion).tar.gz")
267-
let libArchiveTarShaActual = try await runProgramOutput(sha256sum, "\(buildCheckoutsDir)/libarchive-\(libArchiveVersion).tar.gz")
263+
let libArchiveTarShaActual = try await sys.sha256sum(files: FilePath("\(buildCheckoutsDir)/libarchive-\(libArchiveVersion).tar.gz")).output(currentPlatform)
268264
guard let libArchiveTarShaActual, libArchiveTarShaActual.starts(with: libArchiveTarSha) else {
269265
let shaActual = libArchiveTarShaActual ?? "none"
270266
throw Error(message: "The libarchive tar.gz file sha256sum is \(shaActual), but expected \(libArchiveTarSha)")
@@ -330,9 +326,9 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
330326
env: customEnv
331327
)
332328

333-
try runProgramEnv(make, env: customEnv)
329+
try await sys.make().run(currentPlatform, env: customEnv)
334330

335-
try runProgram(make, "install")
331+
try await sys.make().install().run(currentPlatform)
336332

337333
FileManager.default.changeCurrentDirectoryPath(cwd)
338334

@@ -341,7 +337,7 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
341337
let releaseDir = cwd + "/.build/release"
342338

343339
// Strip the symbols from the binary to decrease its size
344-
try runProgram(strip, releaseDir + "/swiftly")
340+
try await sys.strip(names: FilePath(releaseDir) / "swiftly").run(currentPlatform)
345341

346342
try await self.collectLicenses(releaseDir)
347343

@@ -374,18 +370,13 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
374370
}
375371

376372
func buildMacOSRelease(cert: String?, identifier: String) async throws {
377-
// Check system requirements
378-
let git = try await self.assertTool("git", message: "Please install git with either `xcode-select --install` or `brew install git`")
379-
380-
try await self.checkGitRepoStatus(git)
381-
382-
let strip = try await self.assertTool("strip", message: "In order to strip binaries there needs to be the `strip` tool that is installed on macOS.")
373+
try await self.checkGitRepoStatus()
383374

384375
try await sys.swift().package().clean().run(currentPlatform)
385376

386377
for arch in ["x86_64", "arm64"] {
387378
try await sys.swift().build(.product("swiftly"), .configuration("release"), .arch("\(arch)")).run(currentPlatform)
388-
try runProgram(strip, ".build/\(arch)-apple-macosx/release/swiftly")
379+
try await sys.strip(names: FilePath(".build") / "\(arch)-apple-macosx/release/swiftly").run(currentPlatform)
389380
}
390381

391382
let swiftlyBinDir = fs.cwd / ".build/release/.swiftly/bin"
@@ -450,7 +441,7 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
450441
if self.test {
451442
for arch in ["x86_64", "arm64"] {
452443
try await sys.swift().build(.product("test-swiftly"), .configuration("debug"), .arch("\(arch)")).run(currentPlatform)
453-
try runProgram(strip, ".build/\(arch)-apple-macosx/release/swiftly")
444+
try await sys.strip(names: FilePath(".build") / "\(arch)-apple-macosx/release/swiftly").run(currentPlatform)
454445
}
455446

456447
let testArchive = releaseDir.appendingPathComponent("test-swiftly-macos.tar.gz")

0 commit comments

Comments
 (0)