Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a swift-run tool to run a package executable
- Loading branch information
Showing
6 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
Copyright 2015 - 2016 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import class Foundation.ProcessInfo | ||
|
||
import Basic | ||
import Build | ||
import Utility | ||
import PackageGraph | ||
import PackageModel | ||
|
||
import func POSIX.chdir | ||
import func POSIX.getcwd | ||
|
||
private enum RunError: Swift.Error { | ||
case noExecutableFound | ||
case executableNotFound(String) | ||
case multipleExecutables | ||
} | ||
|
||
extension RunError: CustomStringConvertible { | ||
var description: String { | ||
switch self { | ||
case .noExecutableFound: | ||
return "no executable product found" | ||
case .executableNotFound(let executable): | ||
return "could not find executable '\(executable)'" | ||
case .multipleExecutables: | ||
return "multiple products defined" | ||
} | ||
} | ||
} | ||
|
||
public class RunToolOptions: ToolOptions { | ||
/// Returns the mode in with the tool command should run. | ||
var mode: RunMode { | ||
// If we got version option, just print the version and exit. | ||
if shouldPrintVersion { | ||
return .version | ||
} | ||
|
||
return .run | ||
} | ||
|
||
/// If the executable product should be built before running. | ||
var shouldBuild = true | ||
|
||
/// The custom working directory the executable should be run in. | ||
var workingDirectory: AbsolutePath? | ||
|
||
/// The executable product to run. | ||
var executable: String? | ||
|
||
/// The arguments to pass to the executable. | ||
var arguments: [String] = [] | ||
} | ||
|
||
public enum RunMode { | ||
case version | ||
case run | ||
} | ||
|
||
/// swift-run tool namespace | ||
public class SwiftRunTool: SwiftTool<RunToolOptions> { | ||
|
||
public convenience init(args: [String]) { | ||
self.init( | ||
toolName: "run", | ||
usage: "[options] [executable <arguments>]", | ||
overview: "Build and run an executable product", | ||
args: args | ||
) | ||
} | ||
|
||
override func runImpl() throws { | ||
switch options.mode { | ||
case .version: | ||
print(Versioning.currentVersion.completeDisplayString) | ||
|
||
case .run: | ||
let plan = try buildPlan() | ||
|
||
if options.shouldBuild { | ||
try build(plan: plan, includingTests: false) | ||
} | ||
|
||
let product = try findExecutable(in: plan) | ||
let path = plan.buildParameters.buildPath.appending(component: product.name) | ||
try run(at: path) | ||
} | ||
} | ||
|
||
/// Find executable product based on options. | ||
private func findExecutable(in plan: BuildPlan) throws -> ResolvedProduct { | ||
let executableProducts = plan.graph.products.filter({ $0.type == .executable }) | ||
|
||
guard executableProducts.count > 0 else { | ||
throw RunError.noExecutableFound | ||
} | ||
|
||
if let executable = options.executable { | ||
guard let executableProduct = executableProducts.first(where: { $0.name == executable }) else { | ||
throw RunError.executableNotFound(executable) | ||
} | ||
|
||
return executableProduct | ||
} else { | ||
guard executableProducts.count == 1 else { | ||
throw RunError.multipleExecutables | ||
} | ||
|
||
return executableProducts.first! | ||
} | ||
} | ||
|
||
/// Executes and returns execution status. Prints output on standard streams. | ||
private func run(at path: AbsolutePath) throws { | ||
if let workingDirectory = options.workingDirectory { | ||
try POSIX.chdir(workingDirectory.asString) | ||
} | ||
|
||
let relativePath = path.relative(to: currentWorkingDirectory) | ||
try exec(path: path.asString, args: [relativePath.asString] + options.arguments) | ||
} | ||
|
||
override class func defineArguments(parser: ArgumentParser, binder: ArgumentBinder<RunToolOptions>) { | ||
binder.bind( | ||
option: parser.add(option: "--skip-build", kind: Bool.self, | ||
usage: "Skip building the executable product"), | ||
to: { $0.shouldBuild = !$1 }) | ||
|
||
binder.bindArray( | ||
positional: parser.add(positional: "executable", kind: [String].self, optional: true, strategy: .remaining, | ||
usage: "The executable to run"), | ||
to: { | ||
$0.executable = $1.first! | ||
$0.arguments = Array($1.dropFirst()) | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Commands | ||
|
||
let tool = SwiftRunTool(args: Array(CommandLine.arguments.dropFirst())) | ||
tool.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import XCTest | ||
|
||
import TestSupport | ||
import Commands | ||
|
||
final class RunToolTests: XCTestCase { | ||
private func execute(_ args: [String]) throws -> String { | ||
return try SwiftPMProduct.SwiftRun.execute(args, printIfError: true) | ||
} | ||
|
||
func testUsage() throws { | ||
XCTAssert(try execute(["--help"]).contains("USAGE: swift run [options] [executable <arguments>]")) | ||
} | ||
|
||
func testVersion() throws { | ||
XCTAssert(try execute(["--version"]).contains("Swift Package Manager")) | ||
} | ||
|
||
static var allTests = [ | ||
("testUsage", testUsage), | ||
("testVersion", testVersion), | ||
] | ||
} |