2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2021-04-02 17:21:48 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::path::Path;
|
2024-10-17 14:03:22 +00:00
|
|
|
use std::sync::LazyLock;
|
2020-05-21 16:43:13 +00:00
|
|
|
|
|
|
|
use crate::configs::ocaml::OCamlConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2021-04-29 21:22:20 +00:00
|
|
|
use crate::formatter::VersionFormatter;
|
2020-05-21 16:43:13 +00:00
|
|
|
|
2021-04-02 17:21:48 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum SwitchType {
|
|
|
|
Global,
|
|
|
|
Local,
|
|
|
|
}
|
|
|
|
type OpamSwitch = (SwitchType, String);
|
|
|
|
|
2020-05-21 16:43:13 +00:00
|
|
|
/// Creates a module with the current OCaml version
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2021-02-21 18:50:40 +00:00
|
|
|
let mut module = context.new_module("ocaml");
|
|
|
|
let config: OCamlConfig = OCamlConfig::try_load(module.config);
|
2020-05-21 16:43:13 +00:00
|
|
|
let is_ocaml_project = context
|
|
|
|
.try_begin_scan()?
|
2021-02-21 18:50:40 +00:00
|
|
|
.set_files(&config.detect_files)
|
|
|
|
.set_folders(&config.detect_folders)
|
|
|
|
.set_extensions(&config.detect_extensions)
|
2020-05-21 16:43:13 +00:00
|
|
|
.is_match();
|
|
|
|
|
|
|
|
if !is_ocaml_project {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-10-17 14:03:22 +00:00
|
|
|
let opam_switch: LazyLock<Option<OpamSwitch>, _> = LazyLock::new(|| get_opam_switch(context));
|
2021-04-02 17:21:48 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
2021-04-02 17:21:48 +00:00
|
|
|
"switch_indicator" => {
|
|
|
|
let (switch_type, _) = &opam_switch.deref().as_ref()?;
|
|
|
|
match switch_type {
|
|
|
|
SwitchType::Global => Some(config.global_switch_indicator),
|
|
|
|
SwitchType::Local => Some(config.local_switch_indicator),
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2021-04-02 17:21:48 +00:00
|
|
|
"switch_name" => {
|
|
|
|
let (_, name) = opam_switch.deref().as_ref()?;
|
|
|
|
Some(Ok(name.to_string()))
|
|
|
|
}
|
2021-01-22 17:08:04 +00:00
|
|
|
"version" => {
|
|
|
|
let is_esy_project = context
|
|
|
|
.try_begin_scan()?
|
|
|
|
.set_folders(&["esy.lock"])
|
|
|
|
.is_match();
|
|
|
|
|
|
|
|
let ocaml_version = if is_esy_project {
|
2021-02-11 20:34:47 +00:00
|
|
|
context.exec_cmd("esy", &["ocaml", "-vnum"])?.stdout
|
2021-01-22 17:08:04 +00:00
|
|
|
} else {
|
2021-02-11 20:34:47 +00:00
|
|
|
context.exec_cmd("ocaml", &["-vnum"])?.stdout
|
2021-01-22 17:08:04 +00:00
|
|
|
};
|
2021-04-29 21:22:20 +00:00
|
|
|
VersionFormatter::format_module_version(
|
|
|
|
module.get_name(),
|
2021-07-29 18:27:46 +00:00
|
|
|
ocaml_version.trim(),
|
2021-04-29 21:22:20 +00:00
|
|
|
config.version_format,
|
|
|
|
)
|
|
|
|
.map(Ok)
|
2021-01-22 17:08:04 +00:00
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2020-05-21 16:43:13 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `ocaml`: \n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2020-05-21 16:43:13 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:21:48 +00:00
|
|
|
fn get_opam_switch(context: &Context) -> Option<OpamSwitch> {
|
|
|
|
let opam_switch = context
|
|
|
|
.exec_cmd("opam", &["switch", "show", "--safe"])?
|
|
|
|
.stdout;
|
|
|
|
|
2021-07-29 18:27:46 +00:00
|
|
|
parse_opam_switch(opam_switch.trim())
|
2021-04-02 17:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_opam_switch(opam_switch: &str) -> Option<OpamSwitch> {
|
|
|
|
if opam_switch.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = Path::new(opam_switch);
|
|
|
|
if !path.has_root() {
|
|
|
|
Some((SwitchType::Global, opam_switch.to_string()))
|
|
|
|
} else {
|
|
|
|
Some((SwitchType::Local, path.file_name()?.to_str()?.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 16:43:13 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-04-02 17:21:48 +00:00
|
|
|
use super::{parse_opam_switch, SwitchType};
|
|
|
|
use crate::{test::ModuleRenderer, utils::CommandOutput};
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2020-05-21 16:43:13 +00:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io;
|
|
|
|
|
2021-04-02 17:21:48 +00:00
|
|
|
#[test]
|
|
|
|
fn test_parse_opam_switch() {
|
|
|
|
let global_switch = "ocaml-base-compiler.4.10.0";
|
|
|
|
let local_switch = "/path/to/my-project";
|
|
|
|
assert_eq!(
|
|
|
|
parse_opam_switch(global_switch),
|
|
|
|
Some((SwitchType::Global, "ocaml-base-compiler.4.10.0".to_string()))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
parse_opam_switch(local_switch),
|
|
|
|
Some((SwitchType::Local, "my-project".to_string()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-21 16:43:13 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_without_ocaml_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2020-05-21 16:43:13 +00:00
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_opam_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.opam"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_opam_directory() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
fs::create_dir_all(dir.path().join("_opam"))?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_esy_lock_directory() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
fs::create_dir_all(dir.path().join("esy.lock"))?;
|
2020-07-05 17:20:11 +00:00
|
|
|
File::create(dir.path().join("package.json"))?.sync_all()?;
|
|
|
|
fs::write(
|
|
|
|
dir.path().join("package.lock"),
|
2021-05-03 19:48:58 +00:00
|
|
|
r#"{"dependencies": {"ocaml": "4.8.1000"}}"#,
|
2020-07-05 17:20:11 +00:00
|
|
|
)?;
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.08.1 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_dune() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("dune"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_dune_project() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("dune-project"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_jbuild() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("jbuild"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_jbuild_ignore() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("jbuild-ignore"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_merlin_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join(".merlin"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_ml_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.ml"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_mli_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.mli"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_re_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.re"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_rei_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.rei"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect();
|
2021-04-02 17:21:48 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (default) ")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn without_opam_switch() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.ml"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("ocaml")
|
|
|
|
.cmd(
|
|
|
|
"opam switch show --safe",
|
|
|
|
Some(CommandOutput {
|
|
|
|
stdout: String::default(),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.path(dir.path())
|
|
|
|
.collect();
|
2021-01-22 17:08:04 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 ")));
|
2020-05-21 16:43:13 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
2021-04-02 17:21:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_global_opam_switch() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.ml"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("ocaml")
|
|
|
|
.cmd(
|
|
|
|
"opam switch show --safe",
|
|
|
|
Some(CommandOutput {
|
|
|
|
stdout: String::from("ocaml-base-compiler.4.10.0\n"),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.path(dir.path())
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
|
|
|
.paint("🐫 v4.10.0 (ocaml-base-compiler.4.10.0) ")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:58 +00:00
|
|
|
#[test]
|
|
|
|
fn with_global_opam_switch_custom_indicator() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.ml"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("ocaml")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[ocaml]
|
|
|
|
global_switch_indicator = "g/"
|
|
|
|
})
|
|
|
|
.cmd(
|
|
|
|
"opam switch show --safe",
|
|
|
|
Some(CommandOutput {
|
|
|
|
stdout: String::from("ocaml-base-compiler.4.10.0\n"),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.path(dir.path())
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
|
|
|
.paint("🐫 v4.10.0 (g/ocaml-base-compiler.4.10.0) ")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:21:48 +00:00
|
|
|
#[test]
|
|
|
|
fn with_local_opam_switch() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.ml"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("ocaml")
|
|
|
|
.cmd(
|
|
|
|
"opam switch show --safe",
|
|
|
|
Some(CommandOutput {
|
|
|
|
stdout: String::from("/path/to/my-project\n"),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.path(dir.path())
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (*my-project) ")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
2021-05-03 19:48:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_local_opam_switch_custom_indicator() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.ml"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("ocaml")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[ocaml]
|
|
|
|
local_switch_indicator = "^"
|
|
|
|
})
|
|
|
|
.cmd(
|
|
|
|
"opam switch show --safe",
|
|
|
|
Some(CommandOutput {
|
|
|
|
stdout: String::from("/path/to/my-project\n"),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.path(dir.path())
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"via {}",
|
|
|
|
Color::Yellow.bold().paint("🐫 v4.10.0 (^my-project) ")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
2020-05-21 16:43:13 +00:00
|
|
|
}
|