From 9c0498f87d8af314bebb746db03032406aefc113 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Mon, 3 Dec 2018 21:06:18 +0000 Subject: [PATCH 1/3] Print an error if there is no configuration --- Sources/Rocket/main.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Rocket/main.swift b/Sources/Rocket/main.swift index 0629462..8dbfbae 100644 --- a/Sources/Rocket/main.swift +++ b/Sources/Rocket/main.swift @@ -26,6 +26,8 @@ if FileManager.default.fileExists(atPath: rocketYamlPath) { stepsDictionary = loadedDictionary } else if let rocketConfig = getPackageConfig()["rocket"] as? [String: Any] { stepsDictionary = rocketConfig +} else { + logger.logError("Steps not found") } let versionExporter = VersionExporter() From 5f4c215278b6ba057b01ec404d92e33857017b82 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Tue, 4 Dec 2018 16:54:56 +0000 Subject: [PATCH 2/3] Added swift scripts support --- .../Executors/SwiftScriptExecutor.swift | 47 ++++++++++++++++++ .../Parameters/SwiftScriptParameters.swift | 12 +++++ Sources/RocketLib/Step.swift | 3 ++ .../SwiftScriptExecutorTests.swift | 48 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 Sources/RocketLib/Executors/SwiftScriptExecutor.swift create mode 100644 Sources/RocketLib/Parameters/SwiftScriptParameters.swift create mode 100644 Tests/RocketTests/SwiftScriptExecutorTests.swift diff --git a/Sources/RocketLib/Executors/SwiftScriptExecutor.swift b/Sources/RocketLib/Executors/SwiftScriptExecutor.swift new file mode 100644 index 0000000..8b1331d --- /dev/null +++ b/Sources/RocketLib/Executors/SwiftScriptExecutor.swift @@ -0,0 +1,47 @@ +import Foundation +import Logger + +final class SwiftScriptExecutor: DefaultExecutor { + lazy var fileManager: FileManager = .default + lazy var processLauncher: ProcessLaunching = ProcessLauncher() + + override func executeStep(version: String, logger: Logger) { + guard let scriptPath = parameters.scriptPath else { + logger.logError("Invalid script path for the Swift script step") + return + } + + let supportedSwiftCPaths = ["/usr/bin/swiftc", "/home/travis/.swiftenv/shims/swiftc"] + + let swiftCPath = supportedSwiftCPaths.first { fileManager.fileExists(atPath: $0) } + let swiftC = swiftCPath ?? "swiftc" + let args = [ + "--driver-mode=swift", + scriptPath, + version, + ] + + logger.logInfo("Running: \(swiftC) \(args.joined(separator: " "))") + + let process = Process() + process.launchPath = swiftC + process.arguments = args + + let standardOutput = FileHandle.standardOutput + process.standardOutput = standardOutput + process.standardError = standardOutput + + processLauncher.launchProcess(process: process) + } +} + +protocol ProcessLaunching { + func launchProcess(process: Process) +} + +struct ProcessLauncher: ProcessLaunching { + func launchProcess(process: Process) { + process.launch() + process.waitUntilExit() + } +} diff --git a/Sources/RocketLib/Parameters/SwiftScriptParameters.swift b/Sources/RocketLib/Parameters/SwiftScriptParameters.swift new file mode 100644 index 0000000..65c5218 --- /dev/null +++ b/Sources/RocketLib/Parameters/SwiftScriptParameters.swift @@ -0,0 +1,12 @@ + +struct SwiftScriptParameters: StepParameters { + let scriptPath: String? + + enum CodingKeys: String { + case scriptPath = "script_path" + } + + init(dictionary: [String: Any]?) { + scriptPath = dictionary?[CodingKeys.scriptPath] + } +} diff --git a/Sources/RocketLib/Step.swift b/Sources/RocketLib/Step.swift index d04646b..1506a9e 100644 --- a/Sources/RocketLib/Step.swift +++ b/Sources/RocketLib/Step.swift @@ -7,6 +7,7 @@ public enum Step: String { case gitAdd = "git_add" case hideDependencies = "hide_dev_dependencies" case unhideDependencies = "unhide_dev_dependencies" + case swiftScript = "swift_script" func executor(dictionary: [String: Any]?) -> StepExecutor { return executorType.init(dictionary: dictionary) @@ -28,6 +29,8 @@ public enum Step: String { return HideDevDependenciesExecutor.self case .unhideDependencies: return UnhideDevDependenciesExecutor.self + case .swiftScript: + return SwiftScriptExecutor.self } } } diff --git a/Tests/RocketTests/SwiftScriptExecutorTests.swift b/Tests/RocketTests/SwiftScriptExecutorTests.swift new file mode 100644 index 0000000..140f6fc --- /dev/null +++ b/Tests/RocketTests/SwiftScriptExecutorTests.swift @@ -0,0 +1,48 @@ +import Logger +import Nimble +@testable import RocketLib +import XCTest + +final class SwiftScriptExecutorTests: XCTestCase { + func testItCreatesTheCorrectProcess() { + let testPath = "TestPath.swift" + let executor = SwiftScriptExecutor(dictionary: ["script_path": testPath]) + + executor.fileManager = DummyFileManager() + let processLauncher = SpyProcessLauncher() + executor.processLauncher = processLauncher + + executor.executeStep(version: "1.0.0", logger: Logger.testLogger) + + expect(processLauncher.receivedProcess?.launchPath) == "/usr/bin/swiftc" + expect(processLauncher.receivedProcess?.arguments) == [ + "--driver-mode=swift", + testPath, + "1.0.0", + ] + } + + func testItDoesntCreateTheProcessIfThereIsNoScriptPath() { + let executor = SwiftScriptExecutor(dictionary: nil) + let processLauncher = SpyProcessLauncher() + executor.processLauncher = processLauncher + + executor.executeStep(version: "1.0.0", logger: Logger.testLogger) + + expect(processLauncher.receivedProcess?.launchPath).to(beNil()) + } +} + +fileprivate class DummyFileManager: FileManager { + override func fileExists(atPath _: String) -> Bool { + return true + } +} + +fileprivate class SpyProcessLauncher: ProcessLaunching { + var receivedProcess: Process? + + func launchProcess(process: Process) { + receivedProcess = process + } +} From 198597d497815924053bda7ebb3165ed5f4494ac Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Tue, 4 Dec 2018 16:56:30 +0000 Subject: [PATCH 3/3] Update Linux tests --- Tests/RocketTests/XCTestManifests.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/RocketTests/XCTestManifests.swift b/Tests/RocketTests/XCTestManifests.swift index aba92b4..3655d2f 100644 --- a/Tests/RocketTests/XCTestManifests.swift +++ b/Tests/RocketTests/XCTestManifests.swift @@ -54,6 +54,13 @@ extension StepsParserTests { ] } +extension SwiftScriptExecutorTests { + static let __allTests = [ + ("testItCreatesTheCorrectProcess", testItCreatesTheCorrectProcess), + ("testItDoesntCreateTheProcessIfThereIsNoScriptPath", testItDoesntCreateTheProcessIfThereIsNoScriptPath), + ] +} + extension TagExecutorTests { static let __allTests = [ ("testItSendsTheCorrectScriptToTheScriptLauncher", testItSendsTheCorrectScriptToTheScriptLauncher), @@ -85,6 +92,7 @@ extension VersionExporterTests { testCase(PushExecutorTests.__allTests), testCase(ScriptExecutorTests.__allTests), testCase(StepsParserTests.__allTests), + testCase(SwiftScriptExecutorTests.__allTests), testCase(TagExecutorTests.__allTests), testCase(UnhideDevDependenciesExecutorTests.__allTests), testCase(VersionExporterTests.__allTests),