Skip to content

Commit

Permalink
feat: added backend aliases
Browse files Browse the repository at this point in the history
Fixes #2885
  • Loading branch information
jdx committed Nov 10, 2024
1 parent a86e372 commit dccbcde
Show file tree
Hide file tree
Showing 33 changed files with 204 additions and 198 deletions.
3 changes: 0 additions & 3 deletions .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ ripgrep = "latest"
[plugins]
nnnn = 'https://github.com/mise-plugins/rtx-nodejs#main'

[alias.tiny]
abc = '1'

[task_config]
includes = ["tasks"]

Expand Down
2 changes: 1 addition & 1 deletion docs/cli/alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Don't show table header
## Subcommands

- [`mise alias get <PLUGIN> <ALIAS>`](/cli/alias/get.md)
- [`mise alias ls [--no-header] [PLUGIN]`](/cli/alias/ls.md)
- [`mise alias ls [--no-header] [TOOL]`](/cli/alias/ls.md)
- [`mise alias set <ARGS>…`](/cli/alias/set.md)
- [`mise alias unset <PLUGIN> <ALIAS>`](/cli/alias/unset.md)
8 changes: 4 additions & 4 deletions docs/cli/alias/ls.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `mise alias ls`

- **Usage**: `mise alias ls [--no-header] [PLUGIN]`
- **Usage**: `mise alias ls [--no-header] [TOOL]`
- **Aliases**: `list`
- **Source code**: [`src/cli/alias/ls.rs`](https://github.com/jdx/mise/blob/main/src/cli/alias/ls.rs)

Expand All @@ -10,14 +10,14 @@ These can come from user config or from plugins in `bin/list-aliases`.

For user config, aliases are defined like the following in `~/.config/mise/config.toml`:

[alias.node]
[alias.node.versions]
lts = "22.0.0"

## Arguments

### `[PLUGIN]`
### `[TOOL]`

Show aliases for &lt;PLUGIN>
Show aliases for &lt;TOOL>

## Flags

Expand Down
2 changes: 1 addition & 1 deletion docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Answer yes to all confirmation prompts
- [`mise activate [--shims] [-q --quiet] [SHELL_TYPE]`](/cli/activate.md)
- [`mise alias [-p --plugin <PLUGIN>] [--no-header] <SUBCOMMAND>`](/cli/alias.md)
- [`mise alias get <PLUGIN> <ALIAS>`](/cli/alias/get.md)
- [`mise alias ls [--no-header] [PLUGIN]`](/cli/alias/ls.md)
- [`mise alias ls [--no-header] [TOOL]`](/cli/alias/ls.md)
- [`mise alias set <ARGS>…`](/cli/alias/set.md)
- [`mise alias unset <PLUGIN> <ALIAS>`](/cli/alias/unset.md)
- [`mise backends <SUBCOMMAND>`](/cli/backends.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ node = ['16', 'prefix:20', 'ref:master', 'path:~/.nodes/14']
# note this will only be used if the plugin does not already exist
python = 'https://github.com/asdf-community/asdf-python'

[alias.node] # project-local aliases
[alias.node.versions] # project-local aliases
my_custom_node = '20'

[tasks.build]
Expand Down
4 changes: 2 additions & 2 deletions docs/dev-tools/aliases.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ so you can use set it with `node lts-hydrogen` in `.tool-versions`/`mise.toml`.
User aliases can be created by adding an `alias.<PLUGIN>` section to `~/.config/mise/config.toml`:

```toml
[alias.node]
[alias.node.versions]
my_custom_20 = '20'
```

Expand All @@ -32,6 +32,6 @@ plugin currently, but plugin authors can add this script without impacting asdf
Alias values can be templates, see [Templates](/templates) for details.

```toml
[alias.node]
[alias.node.versions]
current = "{{exec(command='node --version')}}"
```
2 changes: 1 addition & 1 deletion docs/mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ These can come from user config or from plugins in `bin/list-aliases`.

For user config, aliases are defined like the following in `~/.config/mise/config.toml`:

[alias.node]
[alias.node.versions]
lts = "20.0.0""#
after_long_help r"Examples:

Expand Down
2 changes: 1 addition & 1 deletion docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Mise offers additional functions:
A example of function using `exec`:

```toml
[alias.node]
[alias.node.versions]
current = "{{ exec(command='node --version') }}"
```

Expand Down
6 changes: 6 additions & 0 deletions e2e/cli/test_alias
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

mise alias set tiny xxx 2
assert_contains "mise alias ls tiny" "tiny xxx 2"
mise alias unset tiny xxx
assert_not_contains "mise alias ls" "tiny xxx"
14 changes: 14 additions & 0 deletions e2e/tools/test_tools_alias
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

cat <<EOF >mise.toml
tools.mytool = "2"
tools.mytool-lts = "lts"
alias.mytool = "asdf:tiny"
[alias.mytool-lts]
full = "asdf:tiny"
version = "1.0.1"
EOF

assert "mise x mytool -- rtx-tiny" "2"
assert "mise x mytool-lts -- rtx-tiny" "1.0.1"
4 changes: 2 additions & 2 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ These can come from user config or from plugins in `bin/list-aliases`.

For user config, aliases are defined like the following in `~/.config/mise/config.toml`:

[alias.node]
[alias.node.versions]
lts = "22.0.0""#
after_long_help r"Examples:

$ mise aliases
node lts-jod 22
"
flag "--no-header" help="Don't show table header"
arg "[PLUGIN]" help="Show aliases for <PLUGIN>"
arg "[TOOL]" help="Show aliases for <TOOL>"
}
cmd "set" help="Add/update an alias for a plugin" {
alias "add" "create"
Expand Down
2 changes: 1 addition & 1 deletion src/cli/alias/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl AliasGet {
pub fn run(self) -> Result<()> {
let config = Config::try_get()?;
match config.get_all_aliases().get(&self.plugin) {
Some(plugin) => match plugin.get(&self.alias) {
Some(alias) => match alias.versions.get(&self.alias) {
Some(alias) => {
miseprintln!("{alias}");
Ok(())
Expand Down
55 changes: 9 additions & 46 deletions src/cli/alias/ls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use eyre::Result;
use itertools::Itertools;
use tabled::Tabled;

use crate::cli::args::BackendArg;
Expand All @@ -11,14 +12,14 @@ use crate::ui::table;
///
/// For user config, aliases are defined like the following in `~/.config/mise/config.toml`:
///
/// [alias.node]
/// [alias.node.versions]
/// lts = "22.0.0"
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "list", after_long_help = AFTER_LONG_HELP, verbatim_doc_comment)]
pub struct AliasLs {
/// Show aliases for <PLUGIN>
/// Show aliases for <TOOL>
#[clap()]
pub plugin: Option<BackendArg>,
pub tool: Option<BackendArg>,

/// Don't show table header
#[clap(long)]
Expand All @@ -31,15 +32,15 @@ impl AliasLs {
let rows = config
.get_all_aliases()
.iter()
.filter(|(fa, _)| {
self.plugin.is_none() || self.plugin.as_ref().is_some_and(|f| &f == fa)
})
.filter(|(fa, _)| self.tool.is_none() || self.tool.as_ref().is_some_and(|f| &f == fa))
.sorted_by(|(a, _), (b, _)| a.name.cmp(&b.name))
.flat_map(|(fa, aliases)| {
aliases
.versions
.iter()
.filter(|(from, _to)| fa.name != "node" || !from.starts_with("lts/"))
.map(|(from, to)| Row {
plugin: fa.to_string(),
tool: fa.to_string(),
alias: from.clone(),
version: to.clone(),
})
Expand All @@ -55,7 +56,7 @@ impl AliasLs {

#[derive(Tabled)]
struct Row {
plugin: String,
tool: String,
alias: String,
version: String,
}
Expand All @@ -67,41 +68,3 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(
node lts-jod 22
"#
);

#[cfg(test)]
mod tests {
use crate::test::reset;
use test_log::test;

#[test]
fn test_alias_ls() {
reset();
assert_cli_snapshot!("aliases", @r"
java lts 21
node lts 22
node lts-argon 4
node lts-boron 6
node lts-carbon 8
node lts-dubnium 10
node lts-erbium 12
node lts-fermium 14
node lts-gallium 16
node lts-hydrogen 18
node lts-iron 20
node lts-jod 22
tiny lts 3.1.0
tiny lts-prev 2.0.0
tiny my/alias 3.0
");
}

#[test]
fn test_alias_ls_filter() {
reset();
assert_cli_snapshot!("aliases", "ls", "tiny", @r"
tiny lts 3.1.0
tiny lts-prev 2.0.0
tiny my/alias 3.0
");
}
}
2 changes: 1 addition & 1 deletion src/cli/alias/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Commands {
impl Alias {
pub fn run(self) -> Result<()> {
let cmd = self.command.unwrap_or(Commands::Ls(ls::AliasLs {
plugin: self.plugin,
tool: self.plugin,
no_header: self.no_header,
}));

Expand Down
30 changes: 0 additions & 30 deletions src/cli/alias/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,3 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(
$ <bold>mise alias set node lts-jod 22.0.0</bold>
"#
);

#[cfg(test)]
pub mod tests {
use crate::test::reset;

#[test]
fn test_alias_set() {
reset();
assert_cli!("alias", "set", "tiny", "my/alias", "3.0");

assert_cli_snapshot!("aliases", @r"
java lts 21
node lts 22
node lts-argon 4
node lts-boron 6
node lts-carbon 8
node lts-dubnium 10
node lts-erbium 12
node lts-fermium 14
node lts-gallium 16
node lts-hydrogen 18
node lts-iron 20
node lts-jod 22
tiny lts 3.1.0
tiny lts-prev 2.0.0
tiny my/alias 3.0
");
reset();
}
}
30 changes: 0 additions & 30 deletions src/cli/alias/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,3 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(
$ <bold>mise alias unset node lts-jod</bold>
"#
);

#[cfg(test)]
mod tests {
use crate::test::reset;

#[test]
fn test_alias_unset() {
reset();

assert_cli!("alias", "unset", "tiny", "my/alias");
assert_cli_snapshot!("aliases", @r"
java lts 21
node lts 22
node lts-argon 4
node lts-boron 6
node lts-carbon 8
node lts-dubnium 10
node lts-erbium 12
node lts-fermium 14
node lts-gallium 16
node lts-hydrogen 18
node lts-iron 20
node lts-jod 22
tiny lts 3.1.0
tiny lts-prev 2.0.0
");

reset();
}
}
2 changes: 1 addition & 1 deletion src/cli/config/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ConfigGenerate {
# # note this will only be used if the plugin is not already installed
# python = 'https://github.com/asdf-community/asdf-python'
#
# [alias.node]
# [alias.node.versions]
# # setup a custom alias so you can run `mise use -g node@work` for node-16.x
# work = '16'
"#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ snapshot_kind: text
# # note this will only be used if the plugin is not already installed
# python = 'https://github.com/asdf-community/asdf-python'
#
# [alias.node]
# [alias.node.versions]
# # setup a custom alias so you can run `mise use -g node@work` for node-16.x
# work = '16'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: src/cli/config/get.rs
expression: output
snapshot_kind: text
---
[alias.tiny]
[alias.tiny.versions]
"my/alias" = "3.0"

[env]
Expand Down
Loading

0 comments on commit dccbcde

Please sign in to comment.