@@ -11,11 +11,39 @@ See http://swift.org/CONTRIBUTORS.txt for Swift project authors
1111import ArgumentParser
1212import Basics
1313import PackageModel
14- import TSCBasic
1514import ScriptParse
1615import ScriptingCore
16+ import TSCBasic
1717import Workspace
1818
19+ protocol ScriptCommand : ParsableCommand {
20+ var swiftOptions : SwiftToolOptions { get }
21+ var options : ScriptToolOptions { get }
22+
23+ func run( _ swiftTool: SwiftTool , as productName: String , at cacheDirPath: AbsolutePath ) throws
24+ }
25+
26+ extension ScriptCommand {
27+ public func run( ) throws {
28+ guard let file = options. file else {
29+ throw ScriptError . fileNotFound ( " " )
30+ }
31+ let ( productName, cacheDirPath) = try checkAndPerformCache ( for: file)
32+
33+ var swiftOptions = swiftOptions
34+ swiftOptions. packagePath = cacheDirPath
35+ swiftOptions. buildPath = nil
36+ let swiftTool = try SwiftTool ( options: swiftOptions)
37+
38+ try self . run ( swiftTool, as: productName, at: cacheDirPath)
39+ if swiftTool. diagnostics. hasErrors || swiftTool. executionStatus == . failure {
40+ throw ExitCode . failure
41+ }
42+ }
43+
44+ public static var _errorLabel : String { " error " }
45+ }
46+
1947struct ScriptToolOptions : ParsableArguments {
2048 /// If the executable product should be built before running.
2149 @Flag ( name: . customLong( " skip-build " ) , help: " Skip building the executable product " )
@@ -55,10 +83,6 @@ public struct SwiftScriptTool: ParsableCommand {
5583 public static var _errorLabel : String { " error " }
5684}
5785
58- extension SwiftScriptTool {
59- static var cacheDir : AbsolutePath { localFileSystem. dotSwiftPM. appending ( component: " scripts " ) }
60- }
61-
6286/// swift-run tool namespace
6387extension SwiftScriptTool {
6488 struct Run : ScriptCommand {
@@ -74,7 +98,7 @@ extension SwiftScriptTool {
7498 /// Whether to print build progress.
7599 @Flag ( help: " Print build progress " )
76100 var quiet : Bool = false
77-
101+
78102 func run( _ swiftTool: SwiftTool , as productName: String , at cacheDirPath: AbsolutePath ) throws {
79103 let output = BufferedOutputByteStream ( )
80104 if quiet {
0 commit comments