Skip to content

Commit

Permalink
feat: sync ruby --brew (#3577)
Browse files Browse the repository at this point in the history
Fixes #2129
  • Loading branch information
jdx authored Dec 15, 2024
1 parent 4be6d79 commit 2388dce
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/.vitepress/cli_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ export const commands: { [key: string]: Command } = {
python: {
hide: false,
},
ruby: {
hide: false,
},
},
},
tasks: {
Expand Down
1 change: 1 addition & 0 deletions docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Show extra output (use -vv for even more)
- [`mise sync <SUBCOMMAND>`](/cli/sync.md)
- [`mise sync node [FLAGS]`](/cli/sync/node.md)
- [`mise sync python [--pyenv] [--uv]`](/cli/sync/python.md)
- [`mise sync ruby [--brew]`](/cli/sync/ruby.md)
- [`mise tasks [FLAGS] [TASK] <SUBCOMMAND>`](/cli/tasks.md)
- [`mise tasks deps [--hidden] [--dot] [TASKS]...`](/cli/tasks/deps.md)
- [`mise tasks edit [-p --path] <TASK>`](/cli/tasks/edit.md)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Synchronize tools from other version managers with mise

- [`mise sync node [FLAGS]`](/cli/sync/node.md)
- [`mise sync python [--pyenv] [--uv]`](/cli/sync/python.md)
- [`mise sync ruby [--brew]`](/cli/sync/ruby.md)
20 changes: 20 additions & 0 deletions docs/cli/sync/ruby.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `mise sync ruby`

- **Usage**: `mise sync ruby [--brew]`
- **Source code**: [`src/cli/sync/ruby.rs`](https://github.com/jdx/mise/blob/main/src/cli/sync/ruby.rs)

Symlinks all ruby tool versions from an external tool into mise

## Flags

### `--brew`

Get tool versions from Homebrew

Examples:

```
brew install ruby
mise sync ruby --brew
mise use -g ruby - Use the latest version of Ruby installed by Homebrew
```
9 changes: 9 additions & 0 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,15 @@ This won't overwrite any existing installs but will overwrite any existing symli
flag "--pyenv" help="Get tool versions from pyenv"
flag "--uv" help="Sync tool versions with uv (2-way sync)"
}
cmd "ruby" help="Symlinks all ruby tool versions from an external tool into mise" {
after_long_help r"Examples:

$ brew install ruby
$ mise sync ruby --brew
$ mise use -g ruby - Use the latest version of Ruby installed by Homebrew
"
flag "--brew" help="Get tool versions from Homebrew"
}
}
cmd "tasks" help="Manage tasks" {
alias "t"
Expand Down
3 changes: 3 additions & 0 deletions src/cli/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use eyre::Result;

mod node;
mod python;
mod ruby;

#[derive(Debug, clap::Args)]
#[clap(about = "Synchronize tools from other version managers with mise")]
Expand All @@ -15,13 +16,15 @@ pub struct Sync {
enum Commands {
Node(node::SyncNode),
Python(python::SyncPython),
Ruby(ruby::SyncRuby),
}

impl Commands {
pub fn run(self) -> Result<()> {
match self {
Self::Node(cmd) => cmd.run(),
Self::Python(cmd) => cmd.run(),
Self::Ruby(cmd) => cmd.run(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/sync/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct SyncNode {
}

#[derive(Debug, clap::Args)]
#[group(required = true)]
#[group(required = true, multiple = true)]
pub struct SyncNodeType {
/// Get tool versions from Homebrew
#[clap(long)]
Expand Down
65 changes: 65 additions & 0 deletions src/cli/sync/ruby.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::path::PathBuf;

use eyre::Result;
use itertools::sorted;

use crate::{backend, cmd, config, dirs, file};

/// Symlinks all ruby tool versions from an external tool into mise
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct SyncRuby {
#[clap(flatten)]
_type: SyncRubyType,
}

#[derive(Debug, clap::Args)]
#[group(required = true, multiple = true)]
pub struct SyncRubyType {
/// Get tool versions from Homebrew
#[clap(long)]
brew: bool,
}

impl SyncRuby {
pub fn run(self) -> Result<()> {
if self._type.brew {
self.run_brew()?;
}
Ok(())
}

fn run_brew(&self) -> Result<()> {
let ruby = backend::get(&"ruby".into()).unwrap();

let brew_prefix = PathBuf::from(cmd!("brew", "--prefix").read()?).join("opt");
let installed_versions_path = dirs::INSTALLS.join("ruby");

file::remove_symlinks_with_target_prefix(&installed_versions_path, &brew_prefix)?;

let subdirs = file::dir_subdirs(&brew_prefix)?;
for entry in sorted(subdirs) {
if entry.starts_with(".") {
continue;
}
if !entry.starts_with("ruby@") {
continue;
}
let v = entry.trim_start_matches("ruby@");
if ruby.create_symlink(v, &brew_prefix.join(&entry))?.is_some() {
miseprintln!("Synced ruby@{} from Homebrew", v);
}
}

config::rebuild_shims_and_runtime_symlinks(&[])
}
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>brew install ruby</bold>
$ <bold>mise sync ruby --brew</bold>
$ <bold>mise use -g ruby</bold> - Use the latest version of Ruby installed by Homebrew
"#
);
2 changes: 1 addition & 1 deletion src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub fn dir_subdirs(dir: &Path) -> Result<BTreeSet<String>> {
for entry in dir.read_dir()? {
let entry = entry?;
let ft = entry.file_type()?;
if ft.is_dir() || (ft.is_symlink() && entry.path().read_link()?.is_dir()) {
if ft.is_dir() || (ft.is_symlink() && entry.path().is_dir()) {
output.insert(entry.file_name().into_string().unwrap());
}
}
Expand Down
15 changes: 15 additions & 0 deletions xtasks/fig/src/mise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2350,6 +2350,21 @@ const completionSpec: Fig.Spec = {
"isRepeatable": false
}
]
},
{
"name": [
"ruby"
],
"description": "Symlinks all ruby tool versions from an external tool into mise",
"options": [
{
"name": [
"--brew"
],
"description": "Get tool versions from Homebrew",
"isRepeatable": false
}
]
}
]
},
Expand Down

0 comments on commit 2388dce

Please sign in to comment.