From 139a6b02afa330841d62983da655b5f1af2c5c8c Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 14 Apr 2023 16:51:52 +0100 Subject: [PATCH] chore: use folder from configuration file --- crates/rome_cli/src/commands/check.rs | 23 +++++++--- crates/rome_cli/src/diagnostics.rs | 8 ++++ crates/rome_cli/src/parse_arguments.rs | 45 +++++++++++++++++++ .../malformedOptions.js.snap | 2 +- crates/rome_service/src/configuration/vcs.rs | 19 +++++++- rome.json | 3 +- 6 files changed, 90 insertions(+), 10 deletions(-) diff --git a/crates/rome_cli/src/commands/check.rs b/crates/rome_cli/src/commands/check.rs index 7f5bd9bb162..5e2c4ce2891 100644 --- a/crates/rome_cli/src/commands/check.rs +++ b/crates/rome_cli/src/commands/check.rs @@ -1,5 +1,8 @@ use crate::configuration::load_configuration; -use crate::parse_arguments::{apply_files_settings_from_cli, apply_format_settings_from_cli}; +use crate::diagnostics::DisabledVcs; +use crate::parse_arguments::{ + apply_files_settings_from_cli, apply_format_settings_from_cli, apply_vcs_settings_from_cli, +}; use crate::vcs::read_vcs_ignore_file; use crate::{execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode}; use indexmap::IndexSet; @@ -26,15 +29,25 @@ pub(crate) fn check(mut session: CliSession) -> Result<(), CliDiagnostic> { } apply_files_settings_from_cli(&mut session, &mut configuration)?; apply_format_settings_from_cli(&mut session, &mut configuration)?; + apply_vcs_settings_from_cli(&mut session, &mut configuration)?; // check if support of git ignore files is enabled - let files_to_ignore = - if let (Some(vcs), Some(configuration_path)) = (&configuration.vcs, configuration_path) { - read_vcs_ignore_file(&mut session, configuration_path, vcs)? + let vcs_base_path = configuration_path.or(session.app.fs.working_directory()); + let files_to_ignore = if let Some(vcs) = &configuration.vcs { + if let Some(vcs_base_path) = vcs_base_path { + read_vcs_ignore_file(&mut session, vcs_base_path, vcs)? } else { + let console = &mut session.app.console; + let diagnostic = DisabledVcs {}; + console.error(markup! { + {PrintDiagnostic::verbose(&diagnostic)} + }); vec![] - }; + } + } else { + vec![] + }; if files_to_ignore.len() > 0 { let files = configuration .files diff --git a/crates/rome_cli/src/diagnostics.rs b/crates/rome_cli/src/diagnostics.rs index d71b9111e85..7c327679acd 100644 --- a/crates/rome_cli/src/diagnostics.rs +++ b/crates/rome_cli/src/diagnostics.rs @@ -289,6 +289,14 @@ pub struct NoVcsFolderFound { pub path: String, } +#[derive(Debug, Diagnostic)] +#[diagnostic( + category = "internalError/fs", + severity = Warning, + message = "Rome couldn't determine a directory for the VCS integration. VCS integration will be disabled." +)] +pub struct DisabledVcs {} + /// Advices for the [CliDiagnostic] #[derive(Debug, Default)] struct CliAdvice { diff --git a/crates/rome_cli/src/parse_arguments.rs b/crates/rome_cli/src/parse_arguments.rs index 1e4b8ab9538..f01484d15ac 100644 --- a/crates/rome_cli/src/parse_arguments.rs +++ b/crates/rome_cli/src/parse_arguments.rs @@ -1,5 +1,6 @@ use crate::{CliDiagnostic, CliSession}; use rome_formatter::IndentStyle; +use rome_service::configuration::vcs::{VcsClientKind, VcsConfiguration}; use rome_service::configuration::{ FormatterConfiguration, JavascriptConfiguration, JavascriptFormatter, PlainIndentStyle, }; @@ -107,3 +108,47 @@ pub(crate) fn apply_files_settings_from_cli( Ok(()) } + +pub(crate) fn apply_vcs_settings_from_cli( + session: &mut CliSession, + configuration: &mut Configuration, +) -> Result<(), CliDiagnostic> { + let vcs = configuration + .vcs + .get_or_insert_with(VcsConfiguration::default); + + let enabled = session + .args + .opt_value_from_str("--vcs-enabled") + .map_err(|source| CliDiagnostic::parse_error("--vcs-enabled", source))?; + let client_kind = session + .args + .opt_value_from_str("--vcs-client-kind") + .map_err(|source| CliDiagnostic::parse_error("--vcs-client-kind", source))?; + + let use_ignore_file = session.args.contains("--vcs-use-ignore-file"); + let root = session + .args + .opt_value_from_str("--vcs-root") + .map_err(|source| CliDiagnostic::parse_error("--vcs-root", source))?; + + if let Some(enabled) = enabled { + vcs.enabled = enabled; + } + + match client_kind { + None => {} + Some(VcsClientKind::Git) => { + vcs.client_kind = Some(VcsClientKind::Git); + } + } + + if use_ignore_file { + vcs.use_ignore_file = Some(true); + } + if let Some(root) = root { + vcs.enabled = root; + } + + Ok(()) +} diff --git a/crates/rome_js_analyze/tests/specs/nursery/useExhaustiveDependencies/malformedOptions.js.snap b/crates/rome_js_analyze/tests/specs/nursery/useExhaustiveDependencies/malformedOptions.js.snap index a27c7d8e865..8e96baed153 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/useExhaustiveDependencies/malformedOptions.js.snap +++ b/crates/rome_js_analyze/tests/specs/nursery/useExhaustiveDependencies/malformedOptions.js.snap @@ -11,7 +11,7 @@ expression: malformedOptions.js ``` malformedOptions.js:2:5 deserialize ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - × Found an unknown key `hook` + × Found an unknown key `hook`. 1 │ { > 2 │ "hook": [ diff --git a/crates/rome_service/src/configuration/vcs.rs b/crates/rome_service/src/configuration/vcs.rs index 704e36b97f1..1de6a0921e0 100644 --- a/crates/rome_service/src/configuration/vcs.rs +++ b/crates/rome_service/src/configuration/vcs.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use std::str::FromStr; const GIT_IGNORE_FILE_NAME: &str = ".gitignore"; @@ -19,8 +20,11 @@ pub struct VcsConfiguration { #[serde(skip_serializing_if = "Option::is_none")] pub use_ignore_file: Option, - /// The folder where Rome should check for VCS files. By default, Rome will use the - /// the working directory. + /// The folder where Rome should check for VCS files. By default, Rome will use the same + /// folder where `rome.json` was found. + /// + /// If Rome can't fine the configuration, it will attempt to use the current working directory. + /// If no current working directory can't be found, Rome won't use the VCS integration. #[serde(skip_serializing_if = "Option::is_none")] pub root: Option, } @@ -44,6 +48,17 @@ impl VcsClientKind { } } +impl FromStr for VcsClientKind { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match s { + "git" => Ok(Self::Git), + _ => Err("Value not supported for VcsClientKind"), + } + } +} + impl VcsConfiguration { pub const KNOWN_KEYS: &'static [&'static str] = &["clientKind", "enabled", "useIgnoreFile", "root"]; diff --git a/rome.json b/rome.json index a4724ff788e..99bea037ae6 100644 --- a/rome.json +++ b/rome.json @@ -3,8 +3,7 @@ "vcs": { "enabled": true, "clientKind": "git", - "useIgnoreFile": true, - "root": "/" + "useIgnoreFile": true }, "files": { "ignore": [