Skip to content

Commit

Permalink
feat: added postinstall hook (#2654)
Browse files Browse the repository at this point in the history
Fixes #1014
  • Loading branch information
jdx authored Sep 25, 2024
1 parent 6cb1c60 commit 90c72dc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
7 changes: 7 additions & 0 deletions docs/dev-tools/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,12 @@ mise plugins may accept configuration in the form of tool options specified in `
mytool = { version = '3.10', foo = 'bar' }
```

All tools can accept a `postinstall` option which is a shell command to run after the tool is installed:

```toml
[tools]
node = { version = '20', postinstall = 'corepack enable' }
```

Unfortunately at the time of this writing, it's not possible to specify this via the CLI in
`mise use` or other commands though. See <https://github.com/jdx/mise/issues/2309>
2 changes: 1 addition & 1 deletion e2e/plugins/test_tiny
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

cat >.mise.toml <<EOF
[tools]
tiny = "latest"
tiny = {version = "latest", postinstall = "echo 'postinstall'"}
[plugins]
tiny-ref = "https://github.com/mise-plugins/rtx-tiny#c532b140abd4ca00d3e76651b9bd32a980bd483c"
EOF
Expand Down
26 changes: 21 additions & 5 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ use regex::Regex;
use strum::IntoEnumIterator;
use versions::Versioning;

use self::backend_meta::BackendMeta;
use crate::cli::args::{BackendArg, ToolVersionType};
use crate::config::{Config, Settings};
use crate::cmd::CmdLineRunner;
use crate::config::{Config, Settings, CONFIG};
use crate::file::{display_path, remove_all, remove_all_with_warning};
use crate::install_context::InstallContext;
use crate::plugins::core::CORE_PLUGINS;
use crate::plugins::core::{CorePlugin, CORE_PLUGINS};
use crate::plugins::{Plugin, PluginType, VERSION_REGEX};
use crate::runtime_symlinks::is_runtime_symlink;
use crate::toolset::{ToolRequest, ToolVersion, Toolset};
use crate::ui::progress_report::SingleReport;
use crate::{dirs, file, lock_file};

use self::backend_meta::BackendMeta;
use crate::{dirs, env, file, lock_file};

pub mod asdf;
pub mod backend_meta;
Expand Down Expand Up @@ -396,10 +396,26 @@ pub trait Backend: Debug + Send + Sync {
if let Err(err) = file::remove_file(self.incomplete_file_path(&ctx.tv)) {
debug!("error removing incomplete file: {:?}", err);
}
if let Some(script) = ctx.tv.request.options().get("postinstall") {
ctx.pr
.finish_with_message("running custom postinstall hook".to_string());
self.run_postinstall_hook(&ctx, script)?;
}
ctx.pr.finish_with_message("installed".to_string());

Ok(())
}

fn run_postinstall_hook(&self, ctx: &InstallContext, script: &str) -> eyre::Result<()> {
CmdLineRunner::new(&*env::SHELL)
.env(&*env::PATH_KEY, CorePlugin::path_env_with_tv_path(&ctx.tv)?)
.with_pr(ctx.pr.as_ref())
.arg("-c")
.arg(script)
.envs(self.exec_env(&CONFIG, ctx.ts, &ctx.tv)?)
.execute()?;
Ok(())
}
fn install_version_impl(&self, ctx: &InstallContext) -> eyre::Result<()>;
fn uninstall_version(
&self,
Expand Down
9 changes: 5 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ pub struct Config {
tool_request_set: OnceCell<ToolRequestSet>,
}

static CONFIG: RwLock<Option<Arc<Config>>> = RwLock::new(None);
pub static CONFIG: Lazy<Arc<Config>> = Lazy::new(Config::get);
static _CONFIG: RwLock<Option<Arc<Config>>> = RwLock::new(None);

impl Config {
pub fn get() -> Arc<Self> {
Self::try_get().unwrap()
}
pub fn try_get() -> Result<Arc<Self>> {
if let Some(config) = &*CONFIG.read().unwrap() {
if let Some(config) = &*_CONFIG.read().unwrap() {
return Ok(config.clone());
}
let config = Arc::new(Self::load()?);
*CONFIG.write().unwrap() = Some(config.clone());
*_CONFIG.write().unwrap() = Some(config.clone());
Ok(config)
}
pub fn load() -> Result<Self> {
Expand Down Expand Up @@ -416,7 +417,7 @@ impl Config {
#[cfg(test)]
pub fn reset() {
Settings::reset(None);
CONFIG.write().unwrap().take();
_CONFIG.write().unwrap().take();
}
}

Expand Down

0 comments on commit 90c72dc

Please sign in to comment.