Skip to content

Commit

Permalink
fix(ruby): fixed MISE_RUBY_BUILD_OPTS
Browse files Browse the repository at this point in the history
Fixes #2608
  • Loading branch information
jdx committed Sep 18, 2024
1 parent a92e7bc commit 27eb0bd
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 51 deletions.
73 changes: 65 additions & 8 deletions docs/lang/ruby.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,71 @@ for additional settings and some troubleshooting.

`ruby-build` already has a
[handful of settings](https://github.com/rbenv/ruby-build?tab=readme-ov-file#custom-build-configuration),
in additional to that mise has a few extra configuration variables:

- `MISE_RUBY_INSTALL` [bool]: Build with ruby-install instead of ruby-build
- `MISE_RUBY_APPLY_PATCHES` [string]: A list of patches (files or URLs) to apply to the ruby source code
- `MISE_RUBY_VERBOSE_INSTALL` [bool]: Show verbose output during installation (passes --verbose to ruby-build)
- `MISE_RUBY_BUILD_OPTS` [string]: Command line options to pass to ruby-build when installing
- `MISE_RUBY_INSTALL_OPTS` [string]: Command line options to pass to ruby-install when installing (if MISE_RUBY_INSTALL=1)
- `MISE_RUBY_DEFAULT_PACKAGES_FILE` [string]: location of default gems file, defaults to `$HOME/.default-gems`
in additional to that mise has a few extra settings:

### `ruby.apply_patches`

* Type: `Option<String>`
* Env: `MISE_RUBY_APPLY_PATCHES`
* Default: `None`

A list of patches (files or URLs) to apply to the ruby source code.

### `ruby.verbose_install`

* Type: `Option<bool>`
* Env: `MISE_RUBY_VERBOSE_INSTALL`
* Default: `None` (behaves like `false` unless --verbose is passed to mise)

Show verbose output during installation (passes --verbose to ruby-build)

### `ruby.ruby_build_repo`

* Type: `String`
* Env: `MISE_RUBY_BUILD_REPO`
* Default: `https://github.com/rbenv/ruby-build.git`

The git repository to use for getting ruby-build.

### `ruby.ruby_build_opts`

* Type: `Option<String>`
* Env: `MISE_RUBY_BUILD_OPTS`
* Default: `None`

Command line options to pass to ruby-build when installing

### `ruby.ruby_install`

* Type: `bool`
* Env: `MISE_RUBY_INSTALL`
* Default: `false`

Build with [ruby-install](https://github.com/postmodern/ruby-install) instead of [ruby-build](https://github.com/rbenv/ruby-build).

### `ruby.ruby_install_repo`

* Type: `String`
* Env: `MISE_RUBY_INSTALL_REPO`
* Default: `https://github.com/postmodern/ruby-install.git`

The git repository to use for getting ruby-install.

### `ruby.ruby_install_opts`

* Type: `Option<String>`
* Env: `MISE_RUBY_INSTALL_OPTS`
* Default: `None`

Command line options to pass to ruby-install when installing (if MISE_RUBY_INSTALL=1)

### `ruby.default_packages_file`

* Type: `String`
* Env: `MISE_RUBY_DEFAULT_PACKAGES_FILE`
* Default: `$HOME/.default-gems`

Location of file containing a list of gems to install after installing a new ruby version.

## Default gems

Expand Down
39 changes: 39 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,45 @@
"description": "path to file containing shorthand mappings",
"type": "string"
},
"ruby": {
"description": "settings specific to ruby",
"type": "object",
"additionalProperties": false,
"properties": {
"apply_patches": {
"description": "A list of patches (files or URLs) to apply to Ruby source code before building",
"type": "string"
},
"verbose_install": {
"description": "Show verbose output during installation (passes --verbose to ruby-build)",
"type": "boolean"
},
"ruby_build_repo": {
"description": "The git repository to use for getting ruby-build",
"type": "string"
},
"ruby_build_opts": {
"description": "Command line options to pass to ruby-build when installing",
"type": "string"
},
"ruby_install": {
"description": "Build with ruby-install instead of ruby-build",
"type": "boolean"
},
"ruby_install_repo": {
"description": "The git repository to use for getting ruby-install",
"type": "string"
},
"ruby_install_opts": {
"description": "Command line options to pass to ruby-install when installing (if MISE_RUBY_INSTALL=1)",
"type": "string"
},
"default_packages_file": {
"description": "Location of file containing a list of gems to install after installing a new ruby version.",
"type": "string"
}
}
},
"status": {
"description": "configure messages displayed when changing directories or executing tools",
"type": "object",
Expand Down
32 changes: 28 additions & 4 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::iter::once;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock};

use crate::config::{system_config_files, DEFAULT_CONFIG_FILENAMES};
use crate::file::FindUp;
use crate::{config, dirs, env, file};
#[allow(unused_imports)]
use confique::env::parse::{list_by_colon, list_by_comma};
use confique::{Config, Partial};
Expand All @@ -12,10 +15,6 @@ use once_cell::sync::Lazy;
use serde::ser::Error;
use serde_derive::{Deserialize, Serialize};

use crate::config::{system_config_files, DEFAULT_CONFIG_FILENAMES};
use crate::file::FindUp;
use crate::{config, dirs, env, file};

#[rustfmt::skip]
#[derive(Config, Default, Debug, Clone, Serialize)]
#[config(partial_attr(derive(Clone, Serialize, Default)))]
Expand Down Expand Up @@ -118,6 +117,8 @@ pub struct Settings {
pub python_pyenv_repo: String,
#[config(env = "MISE_RAW", default = false)]
pub raw: bool,
#[config(nested)]
pub ruby: SettingsRuby,
#[config(env = "MISE_SHORTHANDS_FILE")]
pub shorthands_file: Option<PathBuf>,
/// what level of status messages to display when entering directories
Expand Down Expand Up @@ -159,6 +160,29 @@ pub struct Settings {
pub python_venv_auto_create: bool,
}

#[derive(Config, Default, Debug, Clone, Serialize)]
#[config(partial_attr(derive(Clone, Serialize, Default)))]
#[config(partial_attr(serde(deny_unknown_fields)))]
#[rustfmt::skip]
pub struct SettingsRuby {
#[config(env = "MISE_RUBY_APPLY_PATCHES")]
pub apply_patches: Option<String>,
#[config(env = "MISE_RUBY_DEFAULT_PACKAGES_FILE", default = "~/.default-gems")]
pub default_packages_file: String,
#[config(env = "MISE_RUBY_BUILD_REPO", default = "https://github.com/rbenv/ruby-build.git")]
pub ruby_build_repo: String,
#[config(env = "MISE_RUBY_BUILD_OPTS")]
pub ruby_build_opts: Option<String>,
#[config(env = "MISE_RUBY_INSTALL", default = false)]
pub ruby_install: bool,
#[config(env = "MISE_RUBY_INSTALL_REPO", default = "https://github.com/postmodern/ruby-install.git")]
pub ruby_install_repo: String,
#[config(env = "MISE_RUBY_INSTALL_OPTS")]
pub ruby_install_opts: Option<String>,
#[config(env = "MISE_RUBY_VERBOSE_INSTALL")]
pub verbose_install: Option<bool>,
}

#[derive(Config, Default, Debug, Clone, Serialize)]
#[config(partial_attr(derive(Clone, Serialize, Default)))]
#[config(partial_attr(serde(deny_unknown_fields)))]
Expand Down
28 changes: 0 additions & 28 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,34 +232,6 @@ pub static NVM_DIR: Lazy<PathBuf> =
pub static NODENV_ROOT: Lazy<PathBuf> =
Lazy::new(|| var_path("NODENV_ROOT").unwrap_or_else(|| HOME.join(".nodenv")));

// ruby
#[cfg(unix)]
pub static MISE_RUBY_BUILD_REPO: Lazy<String> = Lazy::new(|| {
var("MISE_RUBY_BUILD_REPO").unwrap_or_else(|_| "https://github.com/rbenv/ruby-build.git".into())
});
#[cfg(unix)]
pub static MISE_RUBY_INSTALL_REPO: Lazy<String> = Lazy::new(|| {
var("MISE_RUBY_INSTALL_REPO")
.unwrap_or_else(|_| "https://github.com/postmodern/ruby-install.git".into())
});
#[cfg(unix)]
pub static MISE_RUBY_INSTALL: Lazy<bool> = Lazy::new(|| var_is_true("MISE_RUBY_INSTALL"));
#[cfg(unix)]
pub static MISE_RUBY_APPLY_PATCHES: Lazy<Option<String>> =
Lazy::new(|| var("MISE_RUBY_APPLY_PATCHES").ok());
#[cfg(unix)]
pub static MISE_RUBY_VERBOSE_INSTALL: Lazy<Option<bool>> =
Lazy::new(|| var_option_bool("MISE_RUBY_VERBOSE_INSTALL"));
#[cfg(unix)]
pub static MISE_RUBY_INSTALL_OPTS: Lazy<Result<Vec<String>, shell_words::ParseError>> =
Lazy::new(|| shell_words::split(&var("MISE_RUBY_INSTALL_OPTS").unwrap_or_default()));
#[cfg(unix)]
pub static MISE_RUBY_BUILD_OPTS: Lazy<Result<Vec<String>, shell_words::ParseError>> =
Lazy::new(|| shell_words::split(&var("MISE_RUBY_BUILD_OPTS").unwrap_or_default()));
pub static MISE_RUBY_DEFAULT_PACKAGES_FILE: Lazy<PathBuf> = Lazy::new(|| {
var_path("MISE_RUBY_DEFAULT_PACKAGES_FILE").unwrap_or_else(|| HOME.join(".default-gems"))
});

fn get_env_diff() -> EnvDiff {
let env = vars().collect::<HashMap<_, _>>();
match env.get("__MISE_DIFF") {
Expand Down
39 changes: 28 additions & 11 deletions src/plugins/core/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ impl RubyPlugin {
}

fn lock_build_tool(&self) -> Result<fslock::LockFile> {
let build_tool_path = if *env::MISE_RUBY_INSTALL {
let settings = Settings::get();
let build_tool_path = if settings.ruby.ruby_install {
self.ruby_build_bin()
} else {
self.ruby_install_bin()
Expand All @@ -62,7 +63,8 @@ impl RubyPlugin {
}

fn update_build_tool(&self) -> Result<()> {
if *env::MISE_RUBY_INSTALL {
let settings = Settings::get();
if settings.ruby.ruby_install {
self.update_ruby_install()
.wrap_err("failed to update ruby-install")?;
}
Expand All @@ -71,6 +73,7 @@ impl RubyPlugin {
}

fn install_ruby_build(&self) -> Result<()> {
let settings = Settings::get();
debug!(
"Installing ruby-build to {}",
self.ruby_build_path().display()
Expand All @@ -79,7 +82,7 @@ impl RubyPlugin {
file::remove_all(&tmp)?;
file::create_dir_all(tmp.parent().unwrap())?;
let git = Git::new(tmp.clone());
git.clone(&env::MISE_RUBY_BUILD_REPO)?;
git.clone(&settings.ruby.ruby_build_repo)?;

cmd!("sh", "install.sh")
.env("PREFIX", self.ruby_build_path())
Expand Down Expand Up @@ -112,6 +115,7 @@ impl RubyPlugin {
}

fn install_ruby_install(&self) -> Result<()> {
let settings = Settings::get();
debug!(
"Installing ruby-install to {}",
self.ruby_install_path().display()
Expand All @@ -120,7 +124,7 @@ impl RubyPlugin {
file::remove_all(&tmp)?;
file::create_dir_all(tmp.parent().unwrap())?;
let git = Git::new(tmp.clone());
git.clone(&env::MISE_RUBY_INSTALL_REPO)?;
git.clone(&settings.ruby.ruby_install_repo)?;

cmd!("make", "install")
.env("PREFIX", self.ruby_install_path())
Expand Down Expand Up @@ -184,7 +188,9 @@ impl RubyPlugin {
tv: &ToolVersion,
pr: &dyn SingleReport,
) -> Result<()> {
let body = file::read_to_string(&*env::MISE_RUBY_DEFAULT_PACKAGES_FILE).unwrap_or_default();
let settings = Settings::get();
let default_gems_file = file::replace_path(&settings.ruby.default_packages_file);
let body = file::read_to_string(&default_gems_file).unwrap_or_default();
for package in body.lines() {
let package = package.split('#').next().unwrap_or_default().trim();
if package.is_empty() {
Expand Down Expand Up @@ -257,7 +263,8 @@ impl RubyPlugin {
tv: &ToolVersion,
pr: &'a dyn SingleReport,
) -> Result<CmdLineRunner<'a>> {
let cmd = if *env::MISE_RUBY_INSTALL {
let settings = Settings::get();
let cmd = if settings.ruby.ruby_install {
CmdLineRunner::new(self.ruby_install_bin()).args(self.install_args_ruby_install(tv)?)
} else {
CmdLineRunner::new(self.ruby_build_bin())
Expand All @@ -267,18 +274,24 @@ impl RubyPlugin {
Ok(cmd.with_pr(pr).envs(config.env()?))
}
fn install_args_ruby_build(&self, tv: &ToolVersion) -> Result<Vec<String>> {
let mut args = env::MISE_RUBY_BUILD_OPTS.clone()?;
let settings = Settings::get();
let mut args = vec![];
if self.verbose_install() {
args.push("--verbose".into());
}
if env::MISE_RUBY_APPLY_PATCHES.is_some() {
if settings.ruby.apply_patches.is_some() {
args.push("--patch".into());
}
args.push(tv.version.clone());
args.push(tv.install_path().to_string_lossy().to_string());
if let Some(opts) = &settings.ruby.ruby_build_opts {
args.push("--".into());
args.extend(shell_words::split(opts)?);
}
Ok(args)
}
fn install_args_ruby_install(&self, tv: &ToolVersion) -> Result<Vec<String>> {
let settings = Settings::get();
let mut args = vec![];
for patch in self.fetch_patch_sources() {
args.push("--patch".into());
Expand All @@ -292,18 +305,22 @@ impl RubyPlugin {
args.push(version.into());
args.push("--install-dir".into());
args.push(tv.install_path().to_string_lossy().to_string());
args.extend(env::MISE_RUBY_INSTALL_OPTS.clone()?);
if let Some(opts) = &settings.ruby.ruby_install_opts {
args.push("--".into());
args.extend(shell_words::split(opts)?);
}
Ok(args)
}

fn verbose_install(&self) -> bool {
let settings = Settings::get();
let verbose_env = *env::MISE_RUBY_VERBOSE_INSTALL;
let verbose_env = settings.ruby.verbose_install;
verbose_env == Some(true) || (settings.verbose && verbose_env != Some(false))
}

fn fetch_patch_sources(&self) -> Vec<String> {
let patch_sources = env::MISE_RUBY_APPLY_PATCHES.clone().unwrap_or_default();
let settings = Settings::get();
let patch_sources = settings.ruby.apply_patches.clone().unwrap_or_default();
patch_sources
.split('\n')
.map(|s| s.to_string())
Expand Down

0 comments on commit 27eb0bd

Please sign in to comment.