diff --git a/Package.swift b/Package.swift index c23e9da59..2f26d25da 100644 --- a/Package.swift +++ b/Package.swift @@ -17,12 +17,21 @@ let package = Package( .package(url: "https://github.com/apple/swift-package-manager", from: "0.2.0"), ], targets: [ + .target( + name: "xpmcore", + dependencies: ["Utility"]), + .target( + name: "xpmcoreTesting", + dependencies: ["xpmcore"]), + .testTarget( + name: "xpmcoreTests", + dependencies: ["xpmcore"]), .target( name: "xpmkit", - dependencies: ["xcodeproj", "Utility"]), + dependencies: ["xcodeproj", "Utility", "xpmcore"]), .testTarget( name: "xpmkitTests", - dependencies: ["xpmkit"]), + dependencies: ["xpmkit", "xpmcoreTesting"]), .target( name: "xpm", dependencies: ["xpmkit"]), @@ -31,10 +40,10 @@ let package = Package( dependencies: ["xpmkit"]), .target( name: "xpmenvkit", - dependencies: ["Utility"]), + dependencies: ["Utility", "xpmcore"]), .testTarget( name: "xpmenvkitTests", - dependencies: ["xpm"]), + dependencies: ["xpm", "xpmcoreTesting"]), .target( name: "xpmenv", dependencies: ["xpmenvkit"]), diff --git a/Sources/xpmkit/Utils/Printer.swift b/Sources/xpmcore/Utils/Printer.swift similarity index 91% rename from Sources/xpmkit/Utils/Printer.swift rename to Sources/xpmcore/Utils/Printer.swift index 8d48883d3..f6f86121a 100644 --- a/Sources/xpmkit/Utils/Printer.swift +++ b/Sources/xpmcore/Utils/Printer.swift @@ -2,7 +2,7 @@ import Basic import Foundation /// Protocol that represents an object that can print messages. -protocol Printing: AnyObject { +public protocol Printing: AnyObject { /// Prints a message on the console. /// /// - Parameter text: message to be printed. @@ -25,15 +25,17 @@ protocol Printing: AnyObject { } /// Default printer that conforms the printing protocol. -class Printer: Printing { +public class Printer: Printing { // swiftlint:disable force_cast let terminalController: TerminalController = TerminalController(stream: stdoutStream as! LocalFileOutputByteStream)! // swiftlint:enable force_cast + public init() {} + /// Prints a message on the console. /// /// - Parameter text: message to be printed. - func print(_ text: String) { + public func print(_ text: String) { let writer = InteractiveWriter.stdout writer.write(text) writer.write("\n") @@ -42,7 +44,7 @@ class Printer: Printing { /// Prints an error. /// /// - Parameter error: error to be printed. - func print(error: Error) { + public func print(error: Error) { let writer = InteractiveWriter.stderr writer.write("Error: ", inColor: .red, bold: true) writer.write(error.localizedDescription) @@ -52,7 +54,7 @@ class Printer: Printing { /// Prints an error message. /// /// - Parameter errorMessage: error message. - func print(errorMessage: String) { + public func print(errorMessage: String) { let writer = InteractiveWriter.stderr writer.write("Error: ", inColor: .red, bold: true) writer.write(errorMessage) @@ -62,7 +64,7 @@ class Printer: Printing { /// Prints an error. /// /// - Parameter error: error to be printed. - func print(section: String) { + public func print(section: String) { let writer = InteractiveWriter.stdout writer.write("\(section)", inColor: .green, bold: true) writer.write("\n") diff --git a/Sources/xpmcoreTesting/Utils/MockPrinter.swift b/Sources/xpmcoreTesting/Utils/MockPrinter.swift new file mode 100644 index 000000000..0fe4f9027 --- /dev/null +++ b/Sources/xpmcoreTesting/Utils/MockPrinter.swift @@ -0,0 +1,25 @@ +import Foundation +@testable import xpmcore + +public final class MockPrinter: Printing { + public var printArgs: [String] = [] + public var printErrorArgs: [Error] = [] + public var printSectionArgs: [String] = [] + public var printErrorMessageArgs: [String] = [] + + public func print(_ text: String) { + printArgs.append(text) + } + + public func print(section: String) { + printSectionArgs.append(section) + } + + public func print(errorMessage: String) { + printErrorMessageArgs.append(errorMessage) + } + + public func print(error: Error) { + printErrorArgs.append(error) + } +} diff --git a/Sources/xpmenv/main.swift b/Sources/xpmenv/main.swift index 3376a05d8..df6a9a4eb 100644 --- a/Sources/xpmenv/main.swift +++ b/Sources/xpmenv/main.swift @@ -1,4 +1,5 @@ import Foundation import xpmenvkit -XPMEnvCommand().execute() +var registry = CommandRegistry() +try registry.run() diff --git a/Sources/xpmenvkit/Commands/Command.swift b/Sources/xpmenvkit/Commands/Command.swift new file mode 100644 index 000000000..9fc8d729e --- /dev/null +++ b/Sources/xpmenvkit/Commands/Command.swift @@ -0,0 +1,9 @@ +import Foundation +import Utility + +protocol Command { + static var command: String { get } + static var overview: String { get } + init(parser: ArgumentParser) + func run(with arguments: ArgumentParser.Result) throws +} diff --git a/Sources/xpmenvkit/Commands/CommandRegistry.swift b/Sources/xpmenvkit/Commands/CommandRegistry.swift new file mode 100644 index 000000000..0db9d4133 --- /dev/null +++ b/Sources/xpmenvkit/Commands/CommandRegistry.swift @@ -0,0 +1,13 @@ +import Basic +import Foundation +import Utility + +public final class CommandRegistry { + public init() { + } + + public func run() throws { +// let parsedArguments = try parse() +// try process(arguments: parsedArguments) + } +} diff --git a/Sources/xpmenvkit/Commands/GlobalCommand.swift b/Sources/xpmenvkit/Commands/GlobalCommand.swift new file mode 100644 index 000000000..5d0fb7af6 --- /dev/null +++ b/Sources/xpmenvkit/Commands/GlobalCommand.swift @@ -0,0 +1,8 @@ +// +// GlobalCommand.swift +// xpmenvkit +// +// Created by Pedro Piñera Buendía on 14.07.18. +// + +import Foundation diff --git a/Sources/xpmenvkit/Command.swift b/Sources/xpmenvkit/Commands/ToolCommand.swift similarity index 85% rename from Sources/xpmenvkit/Command.swift rename to Sources/xpmenvkit/Commands/ToolCommand.swift index 8637556f0..b74f95f12 100644 --- a/Sources/xpmenvkit/Command.swift +++ b/Sources/xpmenvkit/Commands/ToolCommand.swift @@ -58,9 +58,17 @@ public class XPMEnvCommand { let cliPath = path.appending(component: "xpm") + var arguments: [String] = [cliPath.asString] /// We drop the first element, which is the path to this executable. - let exitStatus = Process.run(path: cliPath.asString, arguments: Array(CommandLine.arguments.dropFirst())) - exit(exitStatus) + arguments.append(contentsOf: Array(CommandLine.arguments.dropFirst())) + let status: ProcessResult.ExitStatus = try Process.popen(arguments: arguments).exitStatus + if case let ProcessResult.ExitStatus.signalled(code) = status { + exit(code) + } else if case let ProcessResult.ExitStatus.terminated(code) = status { + exit(code) + } else { + exit(0) + } } catch let error as FatalError { let message = """ \("Error:".bold().red()) \(error.errorDescription) diff --git a/Sources/xpmenvkit/Commands/UpdateCommand.swift b/Sources/xpmenvkit/Commands/UpdateCommand.swift new file mode 100644 index 000000000..fecc4ab44 --- /dev/null +++ b/Sources/xpmenvkit/Commands/UpdateCommand.swift @@ -0,0 +1 @@ +import Foundation diff --git a/Sources/xpmenvkit/LocalEnvironmentController.swift b/Sources/xpmenvkit/Controllers/LocalEnvironmentController.swift similarity index 100% rename from Sources/xpmenvkit/LocalEnvironmentController.swift rename to Sources/xpmenvkit/Controllers/LocalEnvironmentController.swift diff --git a/Sources/xpmenvkit/LocalVersionsController.swift b/Sources/xpmenvkit/Controllers/LocalVersionsController.swift similarity index 100% rename from Sources/xpmenvkit/LocalVersionsController.swift rename to Sources/xpmenvkit/Controllers/LocalVersionsController.swift diff --git a/Sources/xpmenvkit/ReleaseDownloader.swift b/Sources/xpmenvkit/Controllers/ReleaseDownloader.swift similarity index 100% rename from Sources/xpmenvkit/ReleaseDownloader.swift rename to Sources/xpmenvkit/Controllers/ReleaseDownloader.swift diff --git a/Sources/xpmenvkit/UpdatesController.swift b/Sources/xpmenvkit/Controllers/UpdatesController.swift similarity index 100% rename from Sources/xpmenvkit/UpdatesController.swift rename to Sources/xpmenvkit/Controllers/UpdatesController.swift diff --git a/Sources/xpmenvkit/FatalError.swift b/Sources/xpmenvkit/Errors/FatalError.swift similarity index 100% rename from Sources/xpmenvkit/FatalError.swift rename to Sources/xpmenvkit/Errors/FatalError.swift diff --git a/Sources/xpmenvkit/Process.swift b/Sources/xpmenvkit/Process.swift deleted file mode 100644 index 60d06821e..000000000 --- a/Sources/xpmenvkit/Process.swift +++ /dev/null @@ -1,39 +0,0 @@ -import Foundation - -extension Process { - /// Runs the file at the given path with the given arguments. - /// It forwards the intputs/outputs from and to the standard input and output. - /// This method blocks the thread until the execution of the process finishes, and then returns the termination status. - /// - /// - Parameters: - /// - path: path to the file to be executed. - /// - arguments: arguments to be passed. - /// - Returns: the termination status. - static func run(path: String, arguments: [String] = []) -> Int32 { - let process = Process() - process.launchPath = path - process.arguments = arguments - - let outputPipe = Pipe() - let errorPipe = Pipe() - let inputPipe = Pipe() - process.standardOutput = outputPipe - process.standardError = errorPipe - process.standardInput = inputPipe - - outputPipe.fileHandleForReading.readabilityHandler = { handler in - FileHandle.standardOutput.write(handler.availableData) - } - errorPipe.fileHandleForReading.readabilityHandler = { handler in - FileHandle.standardError.write(handler.availableData) - } - FileHandle.standardInput.readabilityHandler = { handler in - inputPipe.fileHandleForWriting.write(handler.availableData) - } - - process.launch() - process.waitUntilExit() - - return process.terminationStatus - } -} diff --git a/Sources/xpmenvkit/VersionResolver.swift b/Sources/xpmenvkit/Utils/VersionResolver.swift similarity index 100% rename from Sources/xpmenvkit/VersionResolver.swift rename to Sources/xpmenvkit/Utils/VersionResolver.swift diff --git a/Sources/xpmkit/Commands/CommandsContext.swift b/Sources/xpmkit/Commands/CommandsContext.swift index 0865af3f2..fcc8859f5 100644 --- a/Sources/xpmkit/Commands/CommandsContext.swift +++ b/Sources/xpmkit/Commands/CommandsContext.swift @@ -1,4 +1,5 @@ import Foundation +import xpmcore /// Utils class that contains dependencies used by commands. protocol CommandsContexting: Contexting { diff --git a/Sources/xpmkit/Errors/ErrorHandler.swift b/Sources/xpmkit/Errors/ErrorHandler.swift index a63436d51..39cebe8f3 100644 --- a/Sources/xpmkit/Errors/ErrorHandler.swift +++ b/Sources/xpmkit/Errors/ErrorHandler.swift @@ -1,4 +1,5 @@ import Foundation +import xpmcore /// Error handling protocol. protocol ErrorHandling: AnyObject { diff --git a/Sources/xpmkit/Generator/GeneratorContext.swift b/Sources/xpmkit/Generator/GeneratorContext.swift index 31a3a40a9..4771161d0 100644 --- a/Sources/xpmkit/Generator/GeneratorContext.swift +++ b/Sources/xpmkit/Generator/GeneratorContext.swift @@ -1,4 +1,5 @@ import Foundation +import xpmcore /// Generator context protocol. protocol GeneratorContexting: Contexting { diff --git a/Sources/xpmkit/Loader/GraphLoaderContext.swift b/Sources/xpmkit/Loader/GraphLoaderContext.swift index c061b63ed..c45ea2ade 100644 --- a/Sources/xpmkit/Loader/GraphLoaderContext.swift +++ b/Sources/xpmkit/Loader/GraphLoaderContext.swift @@ -1,5 +1,6 @@ import Basic import Foundation +import xpmcore /// Protocol that defines the interface of the context that is used during the graph loading. protocol GraphLoaderContexting: Contexting { diff --git a/Sources/xpmkit/Utils/Context.swift b/Sources/xpmkit/Utils/Context.swift index f18531617..d428aeaaf 100644 --- a/Sources/xpmkit/Utils/Context.swift +++ b/Sources/xpmkit/Utils/Context.swift @@ -1,4 +1,5 @@ import Foundation +import xpmcore /// Context protocol. protocol Contexting: AnyObject { diff --git a/Sources/xpmtools/main.swift b/Sources/xpmtools/main.swift index 2b9430a8e..ede7e5ed4 100644 --- a/Sources/xpmtools/main.swift +++ b/Sources/xpmtools/main.swift @@ -1,4 +1,5 @@ import Foundation +import xpmkit var registry = CommandRegistry() try registry.run() diff --git a/Tests/xpmcoreTests/CoreTests.swift b/Tests/xpmcoreTests/CoreTests.swift new file mode 100644 index 000000000..e69de29bb diff --git a/Tests/xpmkitTests/Commands/DumpCommandTests.swift b/Tests/xpmkitTests/Commands/DumpCommandTests.swift index 980159c32..78a78163c 100644 --- a/Tests/xpmkitTests/Commands/DumpCommandTests.swift +++ b/Tests/xpmkitTests/Commands/DumpCommandTests.swift @@ -2,6 +2,7 @@ import Basic import Foundation import Utility import XCTest +@testable import xpmcoreTesting @testable import xpmkit final class DumpCommandTests: XCTestCase { diff --git a/Tests/xpmkitTests/Commands/GenerateCommandTests.swift b/Tests/xpmkitTests/Commands/GenerateCommandTests.swift index a3d94a0d8..1c78770d3 100644 --- a/Tests/xpmkitTests/Commands/GenerateCommandTests.swift +++ b/Tests/xpmkitTests/Commands/GenerateCommandTests.swift @@ -3,6 +3,7 @@ import Foundation import Utility @testable import xcodeproj import XCTest +@testable import xpmcoreTesting @testable import xpmkit final class GenerateCommandTests: XCTestCase { diff --git a/Tests/xpmkitTests/Errors/ErrorHandlerTests.swift b/Tests/xpmkitTests/Errors/ErrorHandlerTests.swift index 947822739..1dad14704 100644 --- a/Tests/xpmkitTests/Errors/ErrorHandlerTests.swift +++ b/Tests/xpmkitTests/Errors/ErrorHandlerTests.swift @@ -1,5 +1,6 @@ import Foundation import XCTest +@testable import xpmcoreTesting @testable import xpmkit fileprivate struct TestError: FatalError { diff --git a/Tests/xpmkitTests/Utils/Mocks/MockPrinter.swift b/Tests/xpmkitTests/Utils/Mocks/MockPrinter.swift deleted file mode 100644 index 51800689b..000000000 --- a/Tests/xpmkitTests/Utils/Mocks/MockPrinter.swift +++ /dev/null @@ -1,25 +0,0 @@ -import Foundation -@testable import xpmkit - -final class MockPrinter: Printing { - var printArgs: [String] = [] - var printErrorArgs: [Error] = [] - var printSectionArgs: [String] = [] - var printErrorMessageArgs: [String] = [] - - func print(_ text: String) { - printArgs.append(text) - } - - func print(section: String) { - printSectionArgs.append(section) - } - - func print(errorMessage: String) { - printErrorMessageArgs.append(errorMessage) - } - - func print(error: Error) { - printErrorArgs.append(error) - } -}