@@ -11,11 +11,39 @@ See http://swift.org/CONTRIBUTORS.txt for Swift project authors
11
11
import ArgumentParser
12
12
import Basics
13
13
import PackageModel
14
- import TSCBasic
15
14
import ScriptParse
16
15
import ScriptingCore
16
+ import TSCBasic
17
17
import Workspace
18
18
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
+
19
47
struct ScriptToolOptions : ParsableArguments {
20
48
/// If the executable product should be built before running.
21
49
@Flag ( name: . customLong( " skip-build " ) , help: " Skip building the executable product " )
@@ -55,10 +83,6 @@ public struct SwiftScriptTool: ParsableCommand {
55
83
public static var _errorLabel : String { " error " }
56
84
}
57
85
58
- extension SwiftScriptTool {
59
- static var cacheDir : AbsolutePath { localFileSystem. dotSwiftPM. appending ( component: " scripts " ) }
60
- }
61
-
62
86
/// swift-run tool namespace
63
87
extension SwiftScriptTool {
64
88
struct Run : ScriptCommand {
@@ -74,7 +98,7 @@ extension SwiftScriptTool {
74
98
/// Whether to print build progress.
75
99
@Flag ( help: " Print build progress " )
76
100
var quiet : Bool = false
77
-
101
+
78
102
func run( _ swiftTool: SwiftTool , as productName: String , at cacheDirPath: AbsolutePath ) throws {
79
103
let output = BufferedOutputByteStream ( )
80
104
if quiet {
0 commit comments