@@ -17,8 +17,60 @@ let currentPlatform: Platform = MacOS.currentPlatform
17
17
#error("Unsupported platform")
18
18
#endif
19
19
20
+ typealias sys = SwiftlyCore . SystemCommand
20
21
typealias fs = SwiftlyCore . FileSystem
21
22
23
+ func sh( executable: Executable = ShCommand . defaultExecutable, _ options: ShCommand . Option ... ) -> ShCommand {
24
+ sh ( executable: executable, options)
25
+ }
26
+
27
+ func sh( executable: Executable = ShCommand . defaultExecutable, _ options: [ ShCommand . Option ] ) -> ShCommand {
28
+ ShCommand ( executable: executable, options)
29
+ }
30
+
31
+ struct ShCommand {
32
+ static var defaultExecutable : Executable { . name( " sh " ) }
33
+
34
+ var executable : Executable
35
+
36
+ var options : [ Option ]
37
+
38
+ enum Option {
39
+ case login
40
+ case command( String )
41
+
42
+ func args( ) -> [ String ] {
43
+ switch self {
44
+ case . login:
45
+ [ " -l " ]
46
+ case let . command( command) :
47
+ [ " -c " , command]
48
+ }
49
+ }
50
+ }
51
+
52
+ init ( executable: Executable , _ options: [ Option ] ) {
53
+ self . executable = executable
54
+ self . options = options
55
+ }
56
+
57
+ func config( ) -> Configuration {
58
+ var args : [ String ] = [ ]
59
+
60
+ for opt in self . options {
61
+ args. append ( contentsOf: opt. args ( ) )
62
+ }
63
+
64
+ return Configuration (
65
+ executable: self . executable,
66
+ arguments: Arguments ( args) ,
67
+ environment: . inherit
68
+ )
69
+ }
70
+ }
71
+
72
+ extension ShCommand : Runnable { }
73
+
22
74
@main
23
75
struct TestSwiftly: AsyncParsableCommand {
24
76
@Flag ( name: [ . customShort( " y " ) , . long] , help: " Disable confirmation prompts by assuming 'yes' " )
@@ -40,11 +92,13 @@ struct TestSwiftly: AsyncParsableCommand {
40
92
Foundation . exit ( 2 )
41
93
}
42
94
95
+ guard case let swiftlyArchive = FilePath ( swiftlyArchive) else { fatalError ( " " ) }
96
+
43
97
print ( " Extracting swiftly release " )
44
98
#if os(Linux)
45
- try currentPlatform . runProgram ( " tar " , " -zxvf " , swiftlyArchive, quiet: false )
99
+ try await sys . tar ( ) . extract ( . verbose , . compressed , . archive ( swiftlyArchive) ) . run ( currentPlatform , quiet: false )
46
100
#elseif os(macOS)
47
- try currentPlatform . runProgram ( " installer " , " - pkg" , swiftlyArchive, " - target" , " CurrentUserHomeDirectory " , quiet: false )
101
+ try await sys . installer ( . verbose , pkg: swiftlyArchive, target: " CurrentUserHomeDirectory " ) . run ( currentPlatform , quiet: false )
48
102
#endif
49
103
50
104
#if os(Linux)
@@ -54,7 +108,7 @@ struct TestSwiftly: AsyncParsableCommand {
54
108
#endif
55
109
56
110
var env = ProcessInfo . processInfo. environment
57
- let shell = try await currentPlatform. getShell ( )
111
+ let shell = FilePath ( try await currentPlatform. getShell ( ) )
58
112
var customLoc : FilePath ?
59
113
60
114
if self . customLocation {
@@ -66,37 +120,39 @@ struct TestSwiftly: AsyncParsableCommand {
66
120
env [ " SWIFTLY_TOOLCHAINS_DIR " ] = ( customLoc! / " toolchains " ) . string
67
121
68
122
try currentPlatform. runProgram ( extractedSwiftly. string, " init " , " --assume-yes " , " --no-modify-profile " , " --skip-install " , quiet: false , env: env)
69
- try currentPlatform . runProgram ( shell, " -l " , " -c " , " . \" \( customLoc! / " env.sh " ) \" && swiftly install --assume-yes latest --post-install-file=./post-install.sh " , quiet : false , env : env )
123
+ try await sh ( executable : . path ( shell) , . login , . command ( " . \" \( customLoc! / " env.sh " ) \" && swiftly install --assume-yes latest --post-install-file=./post-install.sh " ) ) . run ( currentPlatform , env : env , quiet : false )
70
124
} else {
71
125
print ( " Installing swiftly to the default location. " )
72
126
// Setting this environment helps to ensure that the profile gets sourced with bash, even if it is not in an interactive shell
73
- if shell. hasSuffix ( " bash " ) {
127
+ if shell. ends ( with : " bash " ) {
74
128
env [ " BASH_ENV " ] = ( fs. home / " .profile " ) . string
75
- } else if shell. hasSuffix ( " zsh " ) {
129
+ } else if shell. ends ( with : " zsh " ) {
76
130
env [ " ZDOTDIR " ] = fs. home. string
77
- } else if shell. hasSuffix ( " fish " ) {
131
+ } else if shell. ends ( with : " fish " ) {
78
132
env [ " XDG_CONFIG_HOME " ] = ( fs. home / " .config " ) . string
79
133
}
80
134
81
135
try currentPlatform. runProgram ( extractedSwiftly. string, " init " , " --assume-yes " , " --skip-install " , quiet: false , env: env)
82
- try currentPlatform . runProgram ( shell, " -l " , " -c " , " swiftly install --assume-yes latest --post-install-file=./post-install.sh " , quiet : false , env : env )
136
+ try await sh ( executable : . path ( shell) , . login , . command ( " swiftly install --assume-yes latest --post-install-file=./post-install.sh " ) ) . run ( currentPlatform , env : env , quiet : false )
83
137
}
84
138
85
139
var swiftReady = false
86
140
87
- if NSUserName ( ) == " root " && FileManager . default. fileExists ( atPath: " ./post-install.sh " ) {
88
- try currentPlatform. runProgram ( shell, " ./post-install.sh " , quiet: false )
141
+ if NSUserName ( ) == " root " {
142
+ if try await fs. exists ( atPath: " ./post-install.sh " ) {
143
+ try currentPlatform. runProgram ( shell. string, " ./post-install.sh " , quiet: false )
144
+ }
89
145
swiftReady = true
90
- } else if FileManager . default . fileExists ( atPath: " ./post-install.sh " ) {
146
+ } else if try await fs . exists ( atPath: " ./post-install.sh " ) {
91
147
print ( " WARNING: not running as root, so skipping the post installation steps and final swift verification. " )
92
148
} else {
93
149
swiftReady = true
94
150
}
95
151
96
152
if let customLoc = customLoc, swiftReady {
97
- try currentPlatform . runProgram ( shell, " -l " , " -c " , " . \" \( customLoc / " env.sh " ) \" && swift --version " , quiet : false , env : env )
153
+ try await sh ( executable : . path ( shell) , . login , . command ( " . \" \( customLoc / " env.sh " ) \" && swift --version " ) ) . run ( currentPlatform , env : env , quiet : false )
98
154
} else if swiftReady {
99
- try currentPlatform . runProgram ( shell, " -l " , " -c " , " swift --version " , quiet : false , env : env )
155
+ try await sh ( executable : . path ( shell) , . login , . command ( " swift --version " ) ) . run ( currentPlatform , env : env , quiet : false )
100
156
}
101
157
}
102
158
}
0 commit comments