Skip to content

Commit

Permalink
Merge pull request #17 from tsirysndr/feat/rtx-extension
Browse files Browse the repository at this point in the history
feat: add rtx extension
  • Loading branch information
tsirysndr authored Oct 10, 2023
2 parents 638140a + dbc099d commit 939712e
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 13 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ license = "MIT"
name = "envhub"
readme = "../../README.md"
repository = "https://github.com/tsirysndr/envhub"
version = "0.1.0"
version = "0.2.0"

# 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-hm = {path = "../hm", version = "0.1.0"}
envhub-ext = {path = "../ext", version = "0.1.0"}
envhub-hm = {path = "../hm", version = "0.1.2"}
envhub-pkgs = {path = "../pkgs", version = "0.1.0"}
envhub-providers = {path = "../providers", version = "0.1.0"}
envhub-providers = {path = "../providers", version = "0.2.0"}
envhub-stow = {path = "../stow", version = "0.1.0"}
envhub-types = {path = "../types", version = "0.1.0"}
envhub-types = {path = "../types", version = "0.2.0"}
hcl-rs = "0.14.2"
owo-colors = "3.5.0"
tokio = {version = "1.28.2", features = ["full"]}
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/cmd/use.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs;

use anyhow::Error;
use envhub_ext::{rtx::Rtx, 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 @@ -41,6 +42,11 @@ pub fn use_environment(name: &str) -> Result<(), Error> {
install_packages(&config)?;
}

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

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

let state = toml::to_string(&config)?;
Expand Down
16 changes: 16 additions & 0 deletions crates/ext/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors = ["Tsiry Sandratraina <[email protected]>"]
categories = ["command-line-utilities"]
description = "Manage your dotfiles and packages with ease."
edition = "2021"
keywords = ["nix", "shell", "environment", "dotfiles"]
license = "MIT"
name = "envhub-ext"
repository = "https://github.com/tsirysndr/envhub"
version = "0.1.0"

# 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"}
9 changes: 9 additions & 0 deletions crates/ext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use anyhow::Error;
use envhub_types::configuration::Configuration;

pub mod rtx;

pub trait Extension {
fn load(&self, config: &Configuration) -> Result<(), Error>;
fn setup(&self) -> Result<(), Error>;
}
75 changes: 75 additions & 0 deletions crates/ext/src/rtx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::{
env, fs,
process::{Command, Stdio},
};

use anyhow::Error;
use envhub_types::configuration::Configuration;

use crate::Extension;

pub struct Rtx {}

impl Rtx {
pub fn new() -> Self {
Self {}
}

pub fn install(&self, package: &str) -> Result<(), Error> {
let mut child = Command::new("sh")
.arg("-c")
.arg(format!("rtx install {}", package))
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?;
child.wait()?;
Ok(())
}
}

impl Extension for Rtx {
fn load(&self, config: &Configuration) -> Result<(), Error> {
self.setup()?;
match config.rtx {
Some(ref rtx) => {
for package in &rtx.packages {
self.install(package)?;
}
}
None => {}
}
Ok(())
}

fn setup(&self) -> Result<(), Error> {
env::set_var(
"PATH",
format!(
"{}/.local/share/rtx/bin:{}",
env::var("HOME")?,
env::var("PATH")?
),
);
env::set_var(
"PATH",
format!(
"{}/.local/share/rtx/shims:{}",
env::var("HOME")?,
env::var("PATH")?
),
);
let mut child = Command::new("sh")
.arg("-c")
.arg("type rtx > /dev/null || curl https://rtx.pub/install.sh | sh")
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?;
child.wait()?;

fs::create_dir_all(format!("{}/.local/share/rtx/shims", env::var("HOME")?))?;

Ok(())
}
}
4 changes: 2 additions & 2 deletions crates/hm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ keywords = ["nix", "shell", "environment", "dotfiles"]
license = "MIT"
name = "envhub-hm"
repository = "https://github.com/tsirysndr/envhub"
version = "0.1.0"
version = "0.1.2"

# 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.1.0", path = "../types"}
envhub-types = {version = "0.2.0", path = "../types"}
indexmap = {version = "1.9.3", features = ["serde"]}
nix-editor = "0.3.0"
rnix = "0.11.0"
2 changes: 1 addition & 1 deletion crates/providers/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-providers"
repository = "https://github.com/tsirysndr/envhub"
version = "0.1.0"
version = "0.2.0"

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

Expand Down
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.1.0"
version = "0.2.0"

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

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

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct RtxParams {
pub packages: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Configuration {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -58,4 +63,9 @@ pub struct Configuration {
pub symlink_manager: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub package_manager: Option<String>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "hcl::ser::block"
)]
pub rtx: Option<RtxParams>,
}
9 changes: 9 additions & 0 deletions examples/rtx/envhub.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
packages = [
"hello"
]

rtx {
packages = [
"[email protected]"
]
}
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.1.0";
version = "0.2.0";
cargoExtraArgs = "--package=envhub";

buildInputs = [
Expand Down

0 comments on commit 939712e

Please sign in to comment.