Skip to content

Commit

Permalink
perf: memoize CLI loading (#2826)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Oct 26, 2024
1 parent 353ee94 commit e908af4
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 74 deletions.
6 changes: 3 additions & 3 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ usage "Usage: mise [OPTIONS] <COMMAND>"
flag "-C --cd" help="Change directory before running command" global=true {
arg "<DIR>"
}
flag "-P --profile" help="Set the profile (environment)" global=true {
arg "<PROFILE>"
}
flag "--debug" help="Sets log level to debug" hide=true global=true
flag "--log-level" help="Set the log output verbosity" hide=true global=true {
arg "<LEVEL>" {
choices "error" "warn" "info" "debug" "trace"
}
}
flag "-P --profile" help="Set the profile (environment)" global=true {
arg "<PROFILE>"
}
flag "-q --quiet" help="Suppress non-error messages" global=true
flag "--trace" help="Sets log level to trace" hide=true global=true
flag "-v --verbose" help="Show extra output (use -vv for even more)" var=true global=true count=true
Expand Down
4 changes: 4 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@
"description": "How long to wait before updating plugins automatically (note this isn't currently implemented).",
"type": "string"
},
"profile": {
"description": "Profile to use for mise.${MISE_ENV}.toml files.",
"type": "string"
},
"python": {
"additionalProperties": false,
"properties": {
Expand Down
6 changes: 6 additions & 0 deletions settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ type = "String"
default = "7d"
description = "How long to wait before updating plugins automatically (note this isn't currently implemented)."

[profile]
env = "MISE_PROFILE"
type = "String"
description = "Profile to use for mise.${MISE_ENV}.toml files."
optional = true

[python.compile]
env = "MISE_PYTHON_COMPILE"
type = "Bool"
Expand Down
5 changes: 4 additions & 1 deletion src/cli/args/cd_arg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::path::PathBuf;

use clap::{Arg, ArgAction};
use once_cell::sync::Lazy;

#[derive(Clone)]
pub struct CdArg;

pub static CD_ARG: Lazy<Arg> = Lazy::new(CdArg::arg);

impl CdArg {
pub fn arg() -> Arg {
fn arg() -> Arg {
Arg::new("cd")
.value_parser(clap::value_parser!(PathBuf))
.short('C')
Expand Down
11 changes: 8 additions & 3 deletions src/cli/args/log_level_arg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use clap::{Arg, ArgAction};
use once_cell::sync::Lazy;

pub static LOG_LEVEL_ARG: Lazy<Arg> = Lazy::new(LogLevelArg::arg);
pub static DEBUG_ARG: Lazy<Arg> = Lazy::new(DebugArg::arg);
pub static TRACE_ARG: Lazy<Arg> = Lazy::new(TraceArg::arg);

#[derive(Clone)]
pub struct LogLevelArg;

impl LogLevelArg {
pub fn arg() -> clap::Arg {
fn arg() -> clap::Arg {
Arg::new("log-level")
.long("log-level")
.value_name("LEVEL")
Expand All @@ -18,7 +23,7 @@ impl LogLevelArg {
pub struct DebugArg;

impl DebugArg {
pub fn arg() -> clap::Arg {
fn arg() -> clap::Arg {
Arg::new("debug")
.long("debug")
.help("Sets log level to debug")
Expand All @@ -31,7 +36,7 @@ impl DebugArg {
pub struct TraceArg;

impl TraceArg {
pub fn arg() -> clap::Arg {
fn arg() -> clap::Arg {
Arg::new("trace")
.long("trace")
.help("Sets log level to trace")
Expand Down
12 changes: 6 additions & 6 deletions src/cli/args/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pub use backend_arg::BackendArg;
pub use cd_arg::CdArg;
pub use cd_arg::CD_ARG;
pub use env_var_arg::EnvVarArg;
pub use log_level_arg::{DebugArg, LogLevelArg, TraceArg};
pub use profile_arg::ProfileArg;
pub use quiet_arg::QuietArg;
pub use log_level_arg::{DEBUG_ARG, LOG_LEVEL_ARG, TRACE_ARG};
pub use profile_arg::PROFILE_ARG;
pub use quiet_arg::QUIET_ARG;
pub use tool_arg::{ToolArg, ToolVersionType};
pub use verbose_arg::VerboseArg;
pub use yes_arg::YesArg;
pub use verbose_arg::VERBOSE_ARG;
pub use yes_arg::YES_ARG;

mod backend_arg;
mod cd_arg;
Expand Down
5 changes: 4 additions & 1 deletion src/cli/args/profile_arg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use clap::{Arg, ArgAction};
use once_cell::sync::Lazy;

#[derive(Clone, Debug)]
pub struct ProfileArg;

pub static PROFILE_ARG: Lazy<Arg> = Lazy::new(ProfileArg::arg);

impl ProfileArg {
pub fn arg() -> Arg {
fn arg() -> Arg {
Arg::new("profile")
.short('P')
.long("profile")
Expand Down
5 changes: 4 additions & 1 deletion src/cli/args/quiet_arg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use clap::{Arg, ArgAction};
use once_cell::sync::Lazy;

#[derive(Clone)]
pub struct QuietArg;

pub static QUIET_ARG: Lazy<Arg> = Lazy::new(QuietArg::arg);

impl QuietArg {
pub fn arg() -> Arg {
fn arg() -> Arg {
Arg::new("quiet")
.short('q')
.long("quiet")
Expand Down
5 changes: 4 additions & 1 deletion src/cli/args/verbose_arg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use clap::{Arg, ArgAction};
use once_cell::sync::Lazy;

#[derive(Clone)]
pub struct VerboseArg;

pub static VERBOSE_ARG: Lazy<clap::Arg> = Lazy::new(VerboseArg::arg);

impl VerboseArg {
pub fn arg() -> clap::Arg {
fn arg() -> clap::Arg {
Arg::new("verbose")
.short('v')
.long("verbose")
Expand Down
5 changes: 4 additions & 1 deletion src/cli/args/yes_arg.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use clap::{Arg, ArgAction};
use once_cell::sync::Lazy;

pub struct YesArg;

pub static YES_ARG: Lazy<Arg> = Lazy::new(YesArg::arg);

impl YesArg {
pub fn arg() -> Arg {
fn arg() -> Arg {
Arg::new("yes")
.short('y')
.long("yes")
Expand Down
22 changes: 13 additions & 9 deletions src/cli/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use console::style;
use eyre::{ensure, Result};
use indexmap::IndexMap;
use itertools::Itertools;
use rayon::prelude::*;
use serde_derive::Serialize;
use tabled::{Table, Tabled};
use versions::Versioning;
Expand Down Expand Up @@ -161,15 +162,18 @@ impl Ls {
// .into_iter()
// .map(|(plugin, tv, source)| (plugin.to_string(), tv.to_string()))
// .collect_vec();
let rows = runtimes.into_iter().map(|(p, tv, source)| Row {
tool: p.clone(),
version: (p.as_ref(), &tv, &source).into(),
requested: match source.is_some() {
true => Some(tv.request.version()),
false => None,
},
source,
});
let rows = runtimes
.into_par_iter()
.map(|(p, tv, source)| Row {
tool: p.clone(),
version: (p.as_ref(), &tv, &source).into(),
requested: match source.is_some() {
true => Some(tv.request.version()),
false => None,
},
source,
})
.collect::<Vec<_>>();
let mut table = Table::new(rows);
table::default_style(&mut table, self.no_header);
miseprintln!("{}", table.to_string());
Expand Down
62 changes: 30 additions & 32 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::{FromArgMatches, Subcommand};
use color_eyre::Result;
use indoc::indoc;

use crate::config::Settings;
use crate::ui::ctrlc;
use crate::{eager, logger, migrate, shims};
use clap::{FromArgMatches, Subcommand};
use color_eyre::Result;
use indoc::indoc;
use once_cell::sync::Lazy;

mod activate;
mod alias;
Expand Down Expand Up @@ -179,28 +179,28 @@ impl Commands {
}
}

impl Cli {
pub fn command() -> clap::Command {
Commands::augment_subcommands(
clap::Command::new("mise")
.version(version::VERSION.to_string())
.about(env!("CARGO_PKG_DESCRIPTION"))
.author("Jeff Dickey <@jdx>")
.long_about(LONG_ABOUT)
.arg_required_else_help(true)
.subcommand_required(true)
.after_long_help(AFTER_LONG_HELP)
.arg(args::CdArg::arg())
.arg(args::ProfileArg::arg())
.arg(args::DebugArg::arg())
.arg(args::LogLevelArg::arg())
.arg(args::QuietArg::arg())
.arg(args::TraceArg::arg())
.arg(args::VerboseArg::arg())
.arg(args::YesArg::arg()),
)
}
pub static CLI: Lazy<clap::Command> = Lazy::new(|| {
Commands::augment_subcommands(
clap::Command::new("mise")
.version(version::VERSION.to_string())
.about(env!("CARGO_PKG_DESCRIPTION"))
.author("Jeff Dickey <@jdx>")
.long_about(LONG_ABOUT)
.arg_required_else_help(true)
.subcommand_required(true)
.after_long_help(AFTER_LONG_HELP)
.arg(args::CD_ARG.clone())
.arg(args::DEBUG_ARG.clone())
.arg(args::LOG_LEVEL_ARG.clone())
.arg(args::PROFILE_ARG.clone())
.arg(args::QUIET_ARG.clone())
.arg(args::TRACE_ARG.clone())
.arg(args::VERBOSE_ARG.clone())
.arg(args::YES_ARG.clone()),
)
});

impl Cli {
pub fn run(args: &Vec<String>) -> Result<()> {
crate::env::ARGS.write().unwrap().clone_from(args);
time!("run init");
Expand All @@ -209,13 +209,11 @@ impl Cli {
ctrlc::init()?;
version::print_version_if_requested(args)?;

let matches = Self::command()
.try_get_matches_from(args)
.unwrap_or_else(|_| {
Self::command()
.subcommands(external::commands())
.get_matches_from(args)
});
let matches = CLI.clone().try_get_matches_from(args).unwrap_or_else(|_| {
CLI.clone()
.subcommands(external::commands())
.get_matches_from(args)
});
time!("run get_matches_from");
Settings::add_cli_matches(&matches);
time!("run add_cli_matches");
Expand Down
5 changes: 3 additions & 2 deletions src/cli/render_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use eyre::Result;
use indoc::formatdoc;
use itertools::Itertools;

use crate::cli::Cli;
use crate::cli::CLI;
use crate::file;

/// internal command to generate markdown from help
Expand Down Expand Up @@ -34,7 +34,8 @@ fn render_command_ts() -> String {
}};
"#});
doc.push_str("export const commands: { [key: string]: Command } = {\n");
let mut cli = Cli::command()
let mut cli = CLI
.clone()
.term_width(80)
.max_term_width(80)
.disable_help_subcommand(true)
Expand Down
5 changes: 3 additions & 2 deletions src/cli/render_mangen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{env, fs};
use eyre::Result;
use xx::file;

use crate::cli::{version, Cli};
use crate::cli::{version, CLI};

/// internal command to generate markdown from help
#[derive(Debug, clap::Args)]
Expand All @@ -13,7 +13,8 @@ pub struct RenderMangen {}

impl RenderMangen {
pub fn run(self) -> Result<()> {
let cli = Cli::command()
let cli = CLI
.clone()
.version(version::V.to_string())
.disable_colored_help(true);

Expand Down
5 changes: 2 additions & 3 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Mutex;
use std::time::SystemTime;

use super::args::ToolArg;
use crate::cli::Cli;
use crate::cli::CLI;
use crate::cmd::CmdLineRunner;
use crate::config::{CONFIG, SETTINGS};
use crate::errors::Error;
Expand Down Expand Up @@ -140,8 +140,7 @@ impl Run {
}

fn get_clap_command(&self) -> clap::Command {
Cli::command()
.get_subcommands()
CLI.get_subcommands()
.find(|s| s.get_name() == "run")
.unwrap()
.clone()
Expand Down
4 changes: 2 additions & 2 deletions src/cli/usage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::builder::Resettable;
use eyre::Result;

use crate::cli;
use crate::cli::CLI;

/// Generate a usage CLI spec
///
Expand All @@ -12,7 +12,7 @@ pub struct Usage {}

impl Usage {
pub fn run(self) -> Result<()> {
let cli = cli::Cli::command().version(Resettable::Reset);
let cli = CLI.clone().version(Resettable::Reset);
let mut spec: usage::Spec = cli.into();
let run = spec.cmd.subcommands.get_mut("run").unwrap();
run.args = vec![];
Expand Down
13 changes: 6 additions & 7 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use std::string::ToString;
use std::sync::RwLock;
use std::{path, process};

use itertools::Itertools;
use log::LevelFilter;
use once_cell::sync::Lazy;

use crate::cli::args::ProfileArg;
use crate::cli::args::PROFILE_ARG;
use crate::env_diff::{EnvDiff, EnvDiffOperation, EnvDiffPatches};
use crate::file::replace_path;
use crate::hook_env::{deserialize_watches, HookEnvWatches};
use itertools::Itertools;
use log::LevelFilter;
use once_cell::sync::Lazy;

pub static ARGS: RwLock<Vec<String>> = RwLock::new(vec![]);
#[cfg(unix)]
Expand Down Expand Up @@ -343,8 +342,8 @@ fn prefer_stale(args: &[String]) -> bool {
}

fn environment(args: &[String]) -> Option<String> {
let long_arg = format!("--{}", ProfileArg::arg().get_long().unwrap_or_default());
let short_arg = format!("-{}", ProfileArg::arg().get_short().unwrap_or_default());
let long_arg = format!("--{}", PROFILE_ARG.get_long().unwrap_or_default());
let short_arg = format!("-{}", PROFILE_ARG.get_short().unwrap_or_default());

args.windows(2)
.find_map(|window| {
Expand Down

0 comments on commit e908af4

Please sign in to comment.