-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
264 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,12 +317,12 @@ Spectator.describe MStrap::Configuration do | |
end | ||
end | ||
|
||
describe "#runtime_manager" do | ||
describe "#default_runtime_manager" do | ||
context "for v1.0 configs" do | ||
subject { MStrap::Configuration.new(config_def_v1_0) } | ||
|
||
it "defaults to asdf" do | ||
expect(subject.runtime_manager).to be_a(MStrap::RuntimeManagers::ASDF) | ||
expect(subject.default_runtime_manager).to be_a(MStrap::RuntimeManagers::ASDF) | ||
end | ||
end | ||
|
||
|
@@ -350,7 +350,120 @@ Spectator.describe MStrap::Configuration do | |
subject { MStrap::Configuration.new(config_def_v1_1) } | ||
|
||
it "can be set through runtimes.default_manager" do | ||
expect(subject.runtime_manager).to be_a(MStrap::RuntimeManagers::Mise) | ||
expect(subject.default_runtime_manager).to be_a(MStrap::RuntimeManagers::Mise) | ||
end | ||
end | ||
end | ||
|
||
describe "#runtime_managers" do | ||
context "for v1.0 configs" do | ||
subject { MStrap::Configuration.new(config_def_v1_0) } | ||
|
||
it "defaults to [asdf]" do | ||
expect(subject.runtime_managers).to eq([MStrap::RuntimeManager.for("asdf")]) | ||
end | ||
end | ||
|
||
context "for v1.1 configs" do | ||
let(config_def_v1_1) do | ||
MStrap::Defs::ConfigDef.from_hcl(<<-HCL) | ||
version = "1.1" | ||
runtimes { | ||
default_manager = "mise" | ||
runtime "rust" { | ||
manager = "rustup" | ||
} | ||
} | ||
user { | ||
name = "Reginald Testington" | ||
email = "[email protected]" | ||
} | ||
profile "personal" { | ||
url = "ssh://[email protected]/reggiemctest/mstrap-personal.git" | ||
} | ||
HCL | ||
end | ||
|
||
subject { MStrap::Configuration.new(config_def_v1_1) } | ||
|
||
it "it derived through runtimes.default_manager and runtimes.runtime[*].manager" do | ||
expect(subject.runtime_managers).to eq([ | ||
MStrap::RuntimeManager.for("mise"), | ||
MStrap::RuntimeManager.for("rustup"), | ||
]) | ||
end | ||
end | ||
end | ||
|
||
describe "#runtimes" do | ||
context "for v1.0 configs" do | ||
subject { MStrap::Configuration.new(config_def_v1_0) } | ||
|
||
it "defaults all to ASDF-provided runtimes" do | ||
%w(crystal go node php python ruby rust).each do |language_name| | ||
expect(subject.runtimes[language_name].runtime_manager).to be_a(MStrap::RuntimeManagers::ASDF) | ||
end | ||
end | ||
|
||
it "returns the expected language runtimes" do | ||
expect(subject.runtimes["crystal"]).to be_a(MStrap::Runtimes::Crystal) | ||
expect(subject.runtimes["go"]).to be_a(MStrap::Runtimes::Go) | ||
expect(subject.runtimes["node"]).to be_a(MStrap::Runtimes::Node) | ||
expect(subject.runtimes["php"]).to be_a(MStrap::Runtimes::Php) | ||
expect(subject.runtimes["python"]).to be_a(MStrap::Runtimes::Python) | ||
expect(subject.runtimes["ruby"]).to be_a(MStrap::Runtimes::Ruby) | ||
expect(subject.runtimes["rust"]).to be_a(MStrap::Runtimes::Rust) | ||
end | ||
end | ||
|
||
context "for v1.1 configs" do | ||
let(config_def_v1_1) do | ||
MStrap::Defs::ConfigDef.from_hcl(<<-HCL) | ||
version = "1.1" | ||
runtimes { | ||
default_manager = "mise" | ||
runtime "rust" { | ||
manager = "rustup" | ||
} | ||
} | ||
user { | ||
name = "Reginald Testington" | ||
email = "[email protected]" | ||
} | ||
profile "personal" { | ||
url = "ssh://[email protected]/reggiemctest/mstrap-personal.git" | ||
} | ||
HCL | ||
end | ||
|
||
subject { MStrap::Configuration.new(config_def_v1_1) } | ||
|
||
it "allows overriding language manager for specific languages" do | ||
%w(crystal go node php python ruby).each do |language_name| | ||
expect(subject.runtimes[language_name].runtime_manager).to be_a(MStrap::RuntimeManagers::Mise) | ||
end | ||
|
||
expect(subject.runtimes["rust"].runtime_manager).to be_a(MStrap::RuntimeManagers::Rustup) | ||
end | ||
|
||
it "returns the expected language runtimes" do | ||
expect(subject.runtimes["crystal"]).to be_a(MStrap::Runtimes::Crystal) | ||
expect(subject.runtimes["go"]).to be_a(MStrap::Runtimes::Go) | ||
expect(subject.runtimes["node"]).to be_a(MStrap::Runtimes::Node) | ||
expect(subject.runtimes["php"]).to be_a(MStrap::Runtimes::Php) | ||
expect(subject.runtimes["python"]).to be_a(MStrap::Runtimes::Python) | ||
expect(subject.runtimes["ruby"]).to be_a(MStrap::Runtimes::Ruby) | ||
expect(subject.runtimes["rust"]).to be_a(MStrap::Runtimes::Rust) | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module MStrap | ||
module RuntimeManagers | ||
class Rustup < RuntimeManager | ||
def current_version(language_name : String) : String? | ||
current_toolchain = `rustup show active-toolchain`.chomp | ||
extract_rust_version_from_toolchain(current_toolchain) | ||
end | ||
|
||
# Execute a command using a specific language runtime version | ||
def runtime_exec(language_name : String, command : String, args : Array(String)? = nil, runtime_version : String? = nil) | ||
exec_args = [] of String | ||
exec_args << runtime_version if runtime_version | ||
|
||
cmd_args = ["run"] + exec_args + ["--", command] | ||
cmd_args += args if args | ||
|
||
if command && (!args || args.empty?) | ||
cmd "rustup #{cmd_args.join(' ')}", quiet: true | ||
else | ||
cmd "rustup", cmd_args, quiet: true | ||
end | ||
end | ||
|
||
# Returns whether the mise plugin has been installed for a language runtime | ||
# or not | ||
def has_plugin?(language_name : String) : Bool | ||
language_name == "rust" | ||
end | ||
|
||
def install_plugin(language_name : String) : Bool | ||
language_name == "rust" | ||
end | ||
|
||
def install_version(language_name : String, version : String) : Bool | ||
cmd("rustup toolchain install #{version}", quiet: true) | ||
end | ||
|
||
# Returns a list of the versions of the language runtime installed | ||
# by mise. | ||
def installed_versions(language_name : String) : Array(String) | ||
rust_versions_list = `rustup toolchain list`.chomp.split("\n").map do |version| | ||
extract_rust_version_from_toolchain(version) | ||
end | ||
|
||
rust_versions_list | ||
end | ||
|
||
# Rustup doesn't support querying this, but "stable" is _probably_ safe | ||
def latest_version(language_name : String) : String | ||
"stable" | ||
end | ||
|
||
# Rust is the only language managed by rustup, so this is always "rust"! | ||
def plugin_name(language_name : String) : String? | ||
"rust" | ||
end | ||
|
||
def set_version(language_name : String, version : String?) : Bool | ||
cmd "rustup override set #{version}", quiet: true | ||
end | ||
|
||
def set_global_version(language_name, version : String) : Bool | ||
cmd "rustup default #{version}", quiet: true | ||
end | ||
|
||
def shell_activation(shell_name : String) : String | ||
<<-SHELL | ||
# Activate rustup for Rust compiler version management | ||
export PATH="$HOME/.cargo/bin:$PATH" | ||
SHELL | ||
end | ||
|
||
def supported_languages : Array(String) | ||
%w(rust) | ||
end | ||
|
||
private def extract_rust_version_from_toolchain(rustup_toolchain : String) | ||
version, _, _ = rustup_toolchain.partition('-') | ||
version | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module MStrap | ||
# Manages the install of rustup for managing Rust | ||
# | ||
# NOTE: See `MStrap::RuntimeManagers::Rustup` for how rustup is integrated | ||
class RustupInstaller | ||
include DSL | ||
|
||
# :nodoc: | ||
RUSTUP_INIT_SH_PATH = File.join(MStrap::Paths::RC_DIR, "vendor", "rustup-init.sh") | ||
|
||
# :nodoc: | ||
RUSTUP_INIT_SH_URL = "https://sh.rustup.rs" | ||
|
||
# :nodoc: | ||
RUSTUP_BIN_DIR_PATH = File.join(ENV["HOME"], ".cargo", "bin") | ||
|
||
def install! | ||
fetch_installer! | ||
|
||
install_args = [RUSTUP_INIT_SH_PATH, "--no-modify-path"] | ||
|
||
if MStrap.debug? | ||
install_args << "--verbose" | ||
else | ||
install_args << "--quiet" | ||
install_args << "-y" | ||
end | ||
|
||
unless cmd "sh", install_args | ||
logc "Failed to install rustup" | ||
end | ||
|
||
# "Activate" it | ||
path = ENV["PATH"] | ||
ENV["PATH"] = "#{RUSTUP_BIN_DIR_PATH}:#{path}" | ||
end | ||
|
||
def installed? | ||
`command -v rustup` && $?.success? | ||
end | ||
|
||
private def fetch_installer! | ||
HTTP::Client.get(RUSTUP_INIT_SH_URL, tls: MStrap.tls_client) do |response| | ||
File.write(RUSTUP_INIT_SH_PATH, response.body_io.gets_to_end) | ||
end | ||
end | ||
end | ||
end |