Skip to content

Commit

Permalink
feat(use): add ability to switch package variants
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Oct 10, 2024
1 parent 4a6e3c4 commit de2264d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Commands:
list List all available packages
inspect Inspect package build log
run Run packages without installing to PATH [aliases: exec]
use Use different variant of a package
help Print this message or the help of the given subcommand(s)

Options:
Expand Down
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ pub enum Commands {
#[arg(required = true)]
command: Vec<String>,
},

/// Use different variant of a package
#[command(arg_required_else_help = true)]
#[clap(name = "use")]
Use {
/// The package to use
#[arg(required = true)]
package: String,
},
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub async fn init() -> Result<()> {
Commands::Run { command } => {
registry.run(command.as_ref()).await?;
}
Commands::Use { package } => {
registry.use_package(&package).await?;
}
};

Ok(())
Expand Down
27 changes: 26 additions & 1 deletion src/registry/installed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::fs;

use crate::{
core::{
constant::INSTALL_TRACK_PATH,
constant::{BIN_PATH, INSTALL_TRACK_PATH},
util::{format_bytes, parse_size},
},
registry::package::parse_package_query,
Expand Down Expand Up @@ -229,4 +229,29 @@ impl InstalledPackages {
None
}
}

pub async fn use_package(&self, resolved_package: &ResolvedPackage) -> Result<()> {
if let Some(installed) = self.find_package(resolved_package) {
let install_path = resolved_package
.package
.get_install_path(&installed.checksum);
let symlink_path = &BIN_PATH.join(&installed.bin_name);

if symlink_path.exists() {
fs::remove_file(symlink_path).await?;
}

fs::symlink(&install_path, symlink_path)
.await
.context(format!(
"Failed to link {} to {}",
install_path.to_string_lossy(),
symlink_path.to_string_lossy()
))?;
} else {
return Err(anyhow::anyhow!("NOT_INSTALLED"));
}

Ok(())
}
}
31 changes: 31 additions & 0 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,37 @@ impl PackageRegistry {
pub async fn run(&self, command: &[String]) -> Result<()> {
self.storage.run(command).await
}

pub async fn use_package(&self, package_name: &str) -> Result<()> {
let installed_guard = self.installed_packages.lock().await;
let resolved_package = self.storage.resolve_package(package_name)?;
let result = installed_guard.use_package(&resolved_package).await;
drop(installed_guard);
match result {
Ok(_) => {
println!("{} linked to binary path", package_name);
Ok(())
}
Err(e) => {
if e.to_string() == "NOT_INSTALLED" {
println!("Package is not yet installed.");
let package_name = resolved_package.package.full_name('/');
self.storage
.install_packages(
&[package_name.to_owned()],
true,
false,
self.installed_packages.clone(),
)
.await?;

Ok(())
} else {
Err(e)
}
}
}
}
}

pub fn select_package_variant(packages: &[ResolvedPackage]) -> Result<&ResolvedPackage> {
Expand Down
1 change: 0 additions & 1 deletion src/registry/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl Installer {
.join("tmp")
.join(package.package.full_name('-'))
.with_extension("part");
println!("TMP PATH: {:#?}", temp_path);
Self {
resolved_package: package.to_owned(),
install_path,
Expand Down

0 comments on commit de2264d

Please sign in to comment.