use crate::cli::args::ToolArg;
use crate::config::{Config, Settings};
use crate::file::display_path;
use crate::install_context::InstallContext;
use crate::toolset::ToolsetBuilder;
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::prompt;
use clap::ValueHint;
use console::style;
use eyre::{Result, bail, eyre};
use path_absolutize::Absolutize;
use std::{
path::{Path, PathBuf},
sync::Arc,
};
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct InstallInto {
#[clap(value_name = "TOOL@VERSION")]
tool: ToolArg,
#[clap(value_hint = ValueHint::DirPath)]
path: PathBuf,
}
impl InstallInto {
pub async fn run(self) -> Result<()> {
let install_path = self.path.absolutize()?.into_owned();
let config = Config::get().await?;
let ts = Arc::new(
ToolsetBuilder::new()
.with_args(std::slice::from_ref(&self.tool))
.build(&config)
.await?,
);
let mut tv = ts
.versions
.get(self.tool.ba.as_ref())
.ok_or_else(|| eyre!("Tool not found"))?
.versions
.first()
.unwrap()
.clone();
let before_date = tv.before_date;
let backend = tv.backend()?;
let mpr = MultiProgressReport::get();
let install_ctx = InstallContext {
config: config.clone(),
ts: ts.clone(),
pr: mpr.add(&tv.style()),
force: true,
dry_run: false,
locked: false, before_date,
};
tv.install_path = Some(install_path.clone());
tv.install_path_is_exact = true;
tv.install_path_is_explicit = true;
if path_has_contents(&install_path) {
let proceed = Settings::get().yes
|| prompt::confirm_with_default(
format!(
"{} is not empty; install-into will delete its contents. Continue?",
display_path(&install_path)
),
false,
)?;
if !proceed {
bail!(
"refusing to overwrite non-empty directory {}; pass {} or choose an empty/new path",
display_path(&install_path),
style("--yes").yellow().for_stderr()
);
}
}
backend.install_version(install_ctx, tv).await?;
Ok(())
}
}
fn path_has_contents(path: &Path) -> bool {
match std::fs::read_dir(path) {
Ok(mut entries) => entries.next().is_some(), Err(e) if e.kind() == std::io::ErrorKind::NotFound => false, Err(_) => path.exists(),
}
}
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
# install node@20.0.0 into ./mynode
$ <bold>mise install-into node@20.0.0 ./mynode && ./mynode/bin/node -v</bold>
20.0.0
"#
);