Skip to content

Commit

Permalink
Merge pull request #40 from tsirysndr/feat/rustup-extension
Browse files Browse the repository at this point in the history
feat: add builtin extension for `rustup`
  • Loading branch information
tsirysndr authored Feb 23, 2024
2 parents da9b66d + 499c3ac commit c476755
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ You can use EnvHub as a [GitHub Action](https://github.com/tsirysndr/setup-envhu
```yaml
- uses: tsirysndr/setup-envhub@v1
with:
version: 'v0.2.11'
version: 'v0.2.12'
- run: envhub --help
```
6 changes: 3 additions & 3 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ license = "MIT"
name = "envhub"
readme = "../../README.md"
repository = "https://github.com/tsirysndr/envhub"
version = "0.2.11"
version = "0.2.12"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.71"
clap = "3.2.20"
envhub-ext = {path = "../ext", version = "0.1.0"}
envhub-ext = {path = "../ext", version = "0.1.1"}
envhub-hm = {path = "../hm", version = "0.2.0"}
envhub-pkgs = {path = "../pkgs", version = "0.1.2"}
envhub-providers = {path = "../providers", version = "0.2.0"}
envhub-stow = {path = "../stow", version = "0.1.0"}
envhub-types = {path = "../types", version = "0.2.1"}
envhub-types = {path = "../types", version = "0.2.2"}
hcl-rs = "0.14.2"
indexmap = "2.2.3"
inquire = "0.6.2"
Expand Down
7 changes: 6 additions & 1 deletion crates/cli/src/cmd/use.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs;

use anyhow::Error;
use envhub_ext::{rtx::Rtx, Extension};
use envhub_ext::{rtx::Rtx, rustup::Rustup, Extension};
use envhub_hm::switch::switch_env;
use envhub_providers::{github::Github, local::Local, s3::S3, Provider};
use envhub_stow::stow::stow;
Expand Down Expand Up @@ -49,6 +49,11 @@ pub fn use_environment(name: &str, backup: bool) -> Result<(), Error> {
rtx.load(&config)?;
}

if config.rustup.is_some() {
let rustup: Box<dyn Extension> = Box::new(Rustup::new());
rustup.load(&config)?;
}

switch_env(Some(&home_manager_dir), &config, backup)?;

let state = toml::to_string(&config)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ keywords = ["nix", "shell", "environment", "dotfiles"]
license = "MIT"
name = "envhub-ext"
repository = "https://github.com/tsirysndr/envhub"
version = "0.1.0"
version = "0.1.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.71"
envhub-types = {version = "0.2.0", path = "../types"}
envhub-types = {version = "0.2.2", path = "../types"}
1 change: 1 addition & 0 deletions crates/ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Error;
use envhub_types::configuration::Configuration;

pub mod rtx;
pub mod rustup;

pub trait Extension {
fn load(&self, config: &Configuration) -> Result<(), Error>;
Expand Down
115 changes: 115 additions & 0 deletions crates/ext/src/rustup.rs
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(())
}
}
2 changes: 1 addition & 1 deletion crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords = ["nix", "shell", "environment", "dotfiles"]
license = "MIT"
name = "envhub-types"
repository = "https://github.com/tsirysndr/envhub"
version = "0.2.1"
version = "0.2.2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
17 changes: 17 additions & 0 deletions crates/types/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ pub struct RtxParams {
pub packages: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct RustupParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub toolchains: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub components: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub targets: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Configuration {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -70,4 +82,9 @@ pub struct Configuration {
serialize_with = "hcl::ser::block"
)]
pub rtx: Option<RtxParams>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "hcl::ser::block"
)]
pub rustup: Option<RustupParams>,
}
17 changes: 17 additions & 0 deletions examples/rustup/envhub.hcl
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",
]
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
inherit src;

pname = "envhub";
version = "0.2.9";
version = "0.2.12";
cargoExtraArgs = "--package=envhub";

buildInputs = [
Expand Down

0 comments on commit c476755

Please sign in to comment.