-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from tsirysndr/feat/rustup-extension
feat: add builtin extension for `rustup`
- Loading branch information
Showing
11 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,115 @@ | ||
use std::{ | ||
env, | ||
process::{Command, Stdio}, | ||
}; | ||
|
||
use anyhow::Error; | ||
use envhub_types::configuration::Configuration; | ||
|
||
use crate::Extension; | ||
|
||
pub struct Rustup {} | ||
|
||
impl Rustup { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
pub fn set_default_toolchain(&self, toolchain: &str) -> Result<(), Error> { | ||
let mut child = Command::new("sh") | ||
.arg("-c") | ||
.arg(format!( | ||
"rustup toolchain install {} && rustup default {}", | ||
toolchain, toolchain | ||
)) | ||
.stdin(Stdio::inherit()) | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.spawn()?; | ||
child.wait()?; | ||
Ok(()) | ||
} | ||
|
||
pub fn install_toolchains(&self, toolchains: Vec<String>) -> Result<(), Error> { | ||
for toolchain in toolchains { | ||
let mut child = Command::new("sh") | ||
.arg("-c") | ||
.arg(format!("rustup toolchain install {}", toolchain)) | ||
.stdin(Stdio::inherit()) | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.spawn()?; | ||
child.wait()?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn install_components(&self, components: Vec<String>) -> Result<(), Error> { | ||
for component in components { | ||
let mut child = Command::new("sh") | ||
.arg("-c") | ||
.arg(format!("rustup component add {}", component)) | ||
.stdin(Stdio::inherit()) | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.spawn()?; | ||
child.wait()?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn add_targets(&self, targets: Vec<String>) -> Result<(), Error> { | ||
for target in targets { | ||
let mut child = Command::new("sh") | ||
.arg("-c") | ||
.arg(format!("rustup target add {}", target)) | ||
.stdin(Stdio::inherit()) | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.spawn()?; | ||
child.wait()?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Extension for Rustup { | ||
fn load(&self, config: &Configuration) -> Result<(), Error> { | ||
self.setup()?; | ||
match config.rustup { | ||
Some(ref rustup) => { | ||
if let Some(value) = &rustup.default { | ||
self.set_default_toolchain(value)?; | ||
} | ||
if let Some(value) = &rustup.toolchains { | ||
self.install_toolchains(value.clone())?; | ||
} | ||
if let Some(value) = &rustup.components { | ||
self.install_components(value.clone())?; | ||
} | ||
if let Some(value) = &rustup.targets { | ||
self.add_targets(value.clone())?; | ||
} | ||
} | ||
None => {} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn setup(&self) -> Result<(), Error> { | ||
env::set_var( | ||
"PATH", | ||
format!("{}/.cargo/bin:{}", env::var("HOME")?, env::var("PATH")?), | ||
); | ||
let mut child = Command::new("sh") | ||
.arg("-c") | ||
.arg("type rustup > /dev/null || curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh") | ||
.stdin(Stdio::inherit()) | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.spawn()?; | ||
child.wait()?; | ||
|
||
Ok(()) | ||
} | ||
} |
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,17 @@ | ||
packages = [ | ||
"hello" | ||
] | ||
|
||
rustup { | ||
default = "stable" | ||
toolchains = [ | ||
"nightly" | ||
] | ||
components = [ | ||
"clippy", | ||
"llvm-tools" | ||
] | ||
targets = [ | ||
"wasm32-wasi", | ||
] | ||
} |
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