@@ -21,6 +21,9 @@ struct TestSwiftly: AsyncParsableCommand {
2121 @Flag ( name: [ . customShort( " y " ) , . long] , help: " Disable confirmation prompts by assuming 'yes' " )
2222 var assumeYes : Bool = false
2323
24+ @Flag ( help: " Install swiftly to a custom location, not added to the user profile. " )
25+ var customLocation : Bool = false
26+
2427 @Argument var swiftlyArchive : String ? = nil
2528
2629 mutating func run( ) async throws {
@@ -41,30 +44,40 @@ struct TestSwiftly: AsyncParsableCommand {
4144 try currentPlatform. runProgram ( " installer " , " -pkg " , swiftlyArchive, " -target " , " CurrentUserHomeDirectory " , quiet: false )
4245#endif
4346
44- print ( " Running 'swiftly init --assume-yes --verbose' to install swiftly and the latest toolchain " )
45-
4647#if os(Linux)
4748 let extractedSwiftly = " ./swiftly "
4849#elseif os(macOS)
4950 let extractedSwiftly = FileManager . default. homeDirectoryForCurrentUser. appendingPathComponent ( " .swiftly/bin/swiftly " ) . path
5051#endif
5152
52- try currentPlatform. runProgram ( extractedSwiftly, " init " , " --assume-yes " , " --skip-install " , quiet: false )
53-
53+ var env = ProcessInfo . processInfo. environment
5454 let shell = try await currentPlatform. getShell ( )
55+ var customLoc : URL ?
5556
56- var env = ProcessInfo . processInfo. environment
57+ if self . customLocation {
58+ let customLoc = currentPlatform. getTempFilePath ( )
5759
58- // Setting this environment helps to ensure that the profile gets sourced with bash, even if it is not in an interactive shell
59- if shell. hasSuffix ( " bash " ) {
60- env [ " BASH_ENV " ] = FileManager . default. homeDirectoryForCurrentUser. appendingPathComponent ( " .profile " ) . path
61- } else if shell. hasSuffix ( " zsh " ) {
62- env [ " ZDOTDIR " ] = FileManager . default. homeDirectoryForCurrentUser. path
63- } else if shell. hasSuffix ( " fish " ) {
64- env [ " XDG_CONFIG_HOME " ] = FileManager . default. homeDirectoryForCurrentUser. appendingPathComponent ( " .config " ) . path
65- }
60+ print ( " Installing swiftly to custom location \( customLoc. path) . " )
61+ env [ " SWIFTLY_HOME_DIR " ] = customLoc. path
62+ env [ " SWIFTLY_BIN_DIR " ] = customLoc. appendingPathComponent ( " bin " ) . path
63+ env [ " SWIFTLY_TOOLCHAIN_DIR " ] = customLoc. appendingPathComponent ( " toolchains " ) . path
6664
67- try currentPlatform. runProgram ( shell, " -l " , " -c " , " swiftly install --assume-yes latest --post-install-file=./post-install.sh " , quiet: false , env: env)
65+ try currentPlatform. runProgram ( extractedSwiftly, " init " , " --assume-yes " , " --no-modify-profile " , " --skip-install " , quiet: false , env: env)
66+ try currentPlatform. runProgram ( shell, " -l " , " -c " , " . \" \( customLoc. path) /env.sh \" && swiftly install --assume-yes latest --post-install-file=./post-install.sh " , quiet: false , env: env)
67+ } else {
68+ print ( " Installing swiftly to the default location. " )
69+ // Setting this environment helps to ensure that the profile gets sourced with bash, even if it is not in an interactive shell
70+ if shell. hasSuffix ( " bash " ) {
71+ env [ " BASH_ENV " ] = FileManager . default. homeDirectoryForCurrentUser. appendingPathComponent ( " .profile " ) . path
72+ } else if shell. hasSuffix ( " zsh " ) {
73+ env [ " ZDOTDIR " ] = FileManager . default. homeDirectoryForCurrentUser. path
74+ } else if shell. hasSuffix ( " fish " ) {
75+ env [ " XDG_CONFIG_HOME " ] = FileManager . default. homeDirectoryForCurrentUser. appendingPathComponent ( " .config " ) . path
76+ }
77+
78+ try currentPlatform. runProgram ( extractedSwiftly, " init " , " --assume-yes " , " --skip-install " , quiet: false , env: env)
79+ try currentPlatform. runProgram ( shell, " -l " , " -c " , " swiftly install --assume-yes latest --post-install-file=./post-install.sh " , quiet: false , env: env)
80+ }
6881
6982 var swiftReady = false
7083
@@ -77,7 +90,9 @@ struct TestSwiftly: AsyncParsableCommand {
7790 swiftReady = true
7891 }
7992
80- if swiftReady {
93+ if let customLoc = customLoc, swiftReady {
94+ try currentPlatform. runProgram ( shell, " -l " , " -c " , " . \" \( customLoc. path) /env.sh \" && swift --version " , quiet: false , env: env)
95+ } else if swiftReady {
8196 try currentPlatform. runProgram ( shell, " -l " , " -c " , " swift --version " , quiet: false , env: env)
8297 }
8398 }
0 commit comments