-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implementation for the swift run
command
#1187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,11 @@ | ||
// swift-tools-version:4.0 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "EchoExecutable", | ||
products: [ | ||
.executable(name: "secho", targets: ["secho"]) | ||
], | ||
targets: [ | ||
.target(name: "secho", dependencies: []) | ||
]) |
2 changes: 2 additions & 0 deletions
2
Fixtures/Miscellaneous/EchoExecutable/Sources/secho/main.swift
This file contains hidden or 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,2 @@ | ||
let arguments = Array(CommandLine.arguments.dropFirst()) | ||
print(arguments.map({ "\"\($0)\"" }).joined(separator: " ")) |
This file contains hidden or 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,13 @@ | ||
// swift-tools-version:4.0 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "EchoExecutable", | ||
products: [ | ||
.executable(name: "exec1", targets: ["exec1"]), | ||
.executable(name: "exec2", targets: ["exec2"]) | ||
], | ||
targets: [ | ||
.target(name: "exec1", dependencies: []), | ||
.target(name: "exec2", dependencies: []) | ||
]) |
1 change: 1 addition & 0 deletions
1
Fixtures/Miscellaneous/MultipleExecutables/Sources/exec1/main.swift
This file contains hidden or 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 @@ | ||
print("1") |
1 change: 1 addition & 0 deletions
1
Fixtures/Miscellaneous/MultipleExecutables/Sources/exec2/main.swift
This file contains hidden or 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 @@ | ||
print("2") |
This file contains hidden or 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 hidden or 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,151 @@ | ||
/* | ||
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 Basic | ||
import Build | ||
import Utility | ||
import PackageGraph | ||
import PackageModel | ||
|
||
import func POSIX.chdir | ||
import func POSIX.getcwd | ||
|
||
/// An enumeration of the errors that can be generated by the run tool. | ||
private enum RunError: Swift.Error { | ||
/// The package manifest has no executable product. | ||
case noExecutableFound | ||
|
||
/// Could not find a specific executable in the package manifest. | ||
case executableNotFound(String) | ||
|
||
/// There are multiple executables and one must be chosen. | ||
case multipleExecutables([String]) | ||
} | ||
|
||
extension RunError: CustomStringConvertible { | ||
var description: String { | ||
switch self { | ||
case .noExecutableFound: | ||
return "no executable product in the package" | ||
case .executableNotFound(let executable): | ||
return "could not find executable product '\(executable)' in the package" | ||
case .multipleExecutables(let executables): | ||
let joinedExecutables = executables.joined(separator: ", ") | ||
return "multiple executable products in the package. Use `swift run ` followed by one of: \(joinedExecutables)" | ||
} | ||
} | ||
} | ||
|
||
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 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) | ||
} | ||
|
||
try run(findExecutable(in: plan)) | ||
} | ||
} | ||
|
||
/// Returns the path to the correct executable based on options. | ||
private func findExecutable(in plan: BuildPlan) throws -> AbsolutePath { | ||
let executableProducts = plan.graph.products.filter({ $0.type == .executable }) | ||
|
||
// Error out if the product contains no executable. | ||
guard executableProducts.count > 0 else { | ||
throw RunError.noExecutableFound | ||
} | ||
|
||
let product: ResolvedProduct | ||
if let executable = options.executable { | ||
// If the exectuable is explicitly specified, verify that it exists. | ||
guard let executableProduct = executableProducts.first(where: { $0.name == executable }) else { | ||
throw RunError.executableNotFound(executable) | ||
} | ||
|
||
product = executableProduct | ||
} else { | ||
// Only implicitly deduce the executable if it is the only one. | ||
guard executableProducts.count == 1 else { | ||
throw RunError.multipleExecutables(executableProducts.map({ $0.name })) | ||
} | ||
|
||
product = executableProducts[0] | ||
} | ||
|
||
// Build the path to the executable from the build directory. | ||
return plan.buildParameters.buildPath.appending(component: product.name) | ||
} | ||
|
||
/// Executes the executable at the specified path. | ||
private func run(_ excutablePath: AbsolutePath) throws { | ||
let pathRelativeToWorkingDirectory = excutablePath.relative(to: originalWorkingDirectory) | ||
try exec(path: excutablePath.asString, args: [pathRelativeToWorkingDirectory.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 hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
/* | ||
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 | ||
import Basic | ||
|
||
final class RunToolTests: XCTestCase { | ||
private func execute(_ args: [String], packagePath: AbsolutePath? = nil) throws -> String { | ||
return try SwiftPMProduct.SwiftRun.execute(args, packagePath: packagePath, 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")) | ||
} | ||
|
||
func testFunctional() throws { | ||
fixture(name: "Miscellaneous/EchoExecutable") { path in | ||
do { | ||
_ = try execute(["unknown"], packagePath: path) | ||
XCTFail("Unexpected success") | ||
} catch SwiftPMProductError.executionFailure(_, _, let stderr) { | ||
XCTAssertEqual(stderr, "error: could not find executable product 'unknown' in the package\n") | ||
} | ||
|
||
let runOutput = try execute(["secho", "1", "--hello", "world"], packagePath: path) | ||
let outputLines = runOutput.split(separator: "\n") | ||
XCTAssertEqual(outputLines.last!, "\"1\" \"--hello\" \"world\"") | ||
} | ||
|
||
fixture(name: "Miscellaneous/MultipleExecutables") { path in | ||
do { | ||
_ = try execute([], packagePath: path) | ||
XCTFail("Unexpected success") | ||
} catch SwiftPMProductError.executionFailure(_, _, let stderr) { | ||
XCTAssertEqual(stderr, "error: multiple executable products in the package. Use `swift run ` followed by one of: exec1, exec2\n") | ||
} | ||
|
||
var runOutput = try execute(["exec1"], packagePath: path) | ||
var outputLines = runOutput.split(separator: "\n") | ||
XCTAssertEqual(outputLines.last!, "1") | ||
runOutput = try execute(["exec2"], packagePath: path) | ||
outputLines = runOutput.split(separator: "\n") | ||
XCTAssertEqual(outputLines.last!, "2") | ||
} | ||
} | ||
|
||
static var allTests = [ | ||
("testUsage", testUsage), | ||
("testVersion", testVersion), | ||
("testFunctional", testFunctional) | ||
] | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a basic functional test. We can extend one of the executable related functional test.