Skip to content

Add --install-system-deps flag to automatically install system dependencies #352

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions Sources/Swiftly/Install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ struct Install: SwiftlyCommand {
))
var postInstallFile: FilePath?

@Flag(name: .shortAndLong, help: "Automatically install system dependencies for the newly installed toolchain.")
var installSystemDeps: Bool = false

@OptionGroup var root: GlobalOptions

private enum CodingKeys: String, CodingKey {
case version, use, verify, postInstallFile, root
case version, use, verify, postInstallFile, installSystemDeps, root
}

mutating func run() async throws {
Expand Down Expand Up @@ -124,16 +127,36 @@ struct Install: SwiftlyCommand {
}

if let postInstallScript {
if installSystemDeps {
// Auto install system dependencies if dpecified
await ctx.print("\nThe following command will be executed to install system dependencies for the installed toolchain:")
await ctx.print(postInstallScript)

if !self.root.assumeYes {
guard await ctx.promptForConfirmation(defaultBehavior: true) else {
throw SwiftlyError(message: "System dependency installation has been cancelled")
}
}

try Swiftly.currentPlatform.runProgram(postInstallScript.components(separatedBy: " "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (blocking): Generally, the post install script will be just "apt-get" or "yum" without the "sudo" or top-level command that runs the script as root. I don't think this will work unless the user is already root.

}

guard let postInstallFile = self.postInstallFile else {
throw SwiftlyError(
message: """
// If the user isn't auto installing the deps and they didn't specify a post install file, display error. If they are
// auto installing the system deps without a postInstallFile exit early as this is considered a valid use case.
if installSystemDeps {
return
} else {
throw SwiftlyError(
message: """

There are some dependencies that should be installed before using this toolchain.
You can run the following script as the system administrator (e.g. root) to prepare
your system:
There are some dependencies that should be installed before using this toolchain.
You can run the following script as the system administrator (e.g. root) to prepare
your system:

\(postInstallScript)
""")
\(postInstallScript)
""")
}
}

try Data(postInstallScript.utf8).write(
Expand Down
Loading