From f576a528840a38ab88626eedbca586dce97eeabe Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 11 Jan 2024 10:53:04 +0000 Subject: [PATCH 1/2] refactor(cli): some cleanup --- crates/biome_cli/src/cli_options.rs | 12 ++++++++ crates/biome_cli/src/commands/check.rs | 29 ++++------------- crates/biome_cli/src/commands/ci.rs | 13 +++----- crates/biome_cli/src/commands/format.rs | 27 +++------------- crates/biome_cli/src/commands/lint.rs | 32 ++++--------------- crates/biome_cli/src/commands/mod.rs | 26 ++++++++++++++++ crates/biome_cli/src/execute/mod.rs | 41 ++++++++++++++++++++----- 7 files changed, 93 insertions(+), 87 deletions(-) diff --git a/crates/biome_cli/src/cli_options.rs b/crates/biome_cli/src/cli_options.rs index 404d057ac671..a736adaab9f7 100644 --- a/crates/biome_cli/src/cli_options.rs +++ b/crates/biome_cli/src/cli_options.rs @@ -1,7 +1,9 @@ use crate::logging::LoggingKind; use crate::LoggingLevel; use biome_diagnostics::Severity; +use biome_service::ConfigurationBasePath; use bpaf::Bpaf; +use std::path::PathBuf; use std::str::FromStr; /// Global options applied to all commands @@ -78,6 +80,16 @@ pub struct CliOptions { pub diagnostic_level: Severity, } +impl CliOptions { + /// Computes the [ConfigurationBasePath] based on the options passed by the user + pub(crate) fn as_configuration_base_path(&self) -> ConfigurationBasePath { + match self.config_path.as_ref() { + None => ConfigurationBasePath::default(), + Some(path) => ConfigurationBasePath::FromUser(PathBuf::from(path)), + } + } +} + #[derive(Debug, Clone)] pub enum ColorsArg { Off, diff --git a/crates/biome_cli/src/commands/check.rs b/crates/biome_cli/src/commands/check.rs index 49c9ef90d0b3..ac99ef336b6b 100644 --- a/crates/biome_cli/src/commands/check.rs +++ b/crates/biome_cli/src/commands/check.rs @@ -1,6 +1,6 @@ use crate::changed::get_changed_files; use crate::cli_options::CliOptions; -use crate::commands::validate_configuration_diagnostics; +use crate::commands::{get_stdin, validate_configuration_diagnostics}; use crate::{ execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode, }; @@ -9,9 +9,8 @@ use biome_service::configuration::{ load_configuration, FormatterConfiguration, LinterConfiguration, LoadedConfiguration, }; use biome_service::workspace::{FixFileMode, UpdateSettingsParams}; -use biome_service::{Configuration, ConfigurationBasePath, MergeWith}; +use biome_service::{Configuration, MergeWith}; use std::ffi::OsString; -use std::path::PathBuf; pub(crate) struct CheckCommandPayload { pub(crate) apply: bool, @@ -29,7 +28,7 @@ pub(crate) struct CheckCommandPayload { /// Handler for the "check" command of the Biome CLI pub(crate) fn check( - mut session: CliSession, + session: CliSession, payload: CheckCommandPayload, ) -> Result<(), CliDiagnostic> { let CheckCommandPayload { @@ -60,12 +59,8 @@ pub(crate) fn check( Some(FixFileMode::SafeAndUnsafeFixes) }; - let base_path = match cli_options.config_path.as_ref() { - None => ConfigurationBasePath::default(), - Some(path) => ConfigurationBasePath::FromUser(PathBuf::from(path)), - }; - - let loaded_configuration = load_configuration(&session.app.fs, base_path)?; + let loaded_configuration = + load_configuration(&session.app.fs, cli_options.as_configuration_base_path())?; validate_configuration_diagnostics( &loaded_configuration, session.app.console, @@ -109,19 +104,7 @@ pub(crate) fn check( let (vcs_base_path, gitignore_matches) = fs_configuration.retrieve_gitignore_matches(&session.app.fs, vcs_base_path.as_deref())?; - let stdin = if let Some(stdin_file_path) = stdin_file_path { - let console = &mut session.app.console; - let input_code = console.read(); - if let Some(input_code) = input_code { - let path = PathBuf::from(stdin_file_path); - Some((path, input_code)) - } else { - // we provided the argument without a piped stdin, we bail - return Err(CliDiagnostic::missing_argument("stdin", "check")); - } - } else { - None - }; + let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "check")?; if since.is_some() && !changed { return Err(CliDiagnostic::incompatible_arguments("since", "changed")); diff --git a/crates/biome_cli/src/commands/ci.rs b/crates/biome_cli/src/commands/ci.rs index 3d8100e05d8d..37b9bc440163 100644 --- a/crates/biome_cli/src/commands/ci.rs +++ b/crates/biome_cli/src/commands/ci.rs @@ -7,9 +7,8 @@ use biome_service::configuration::{ load_configuration, FormatterConfiguration, LinterConfiguration, LoadedConfiguration, }; use biome_service::workspace::UpdateSettingsParams; -use biome_service::{Configuration, ConfigurationBasePath, MergeWith}; +use biome_service::{Configuration, MergeWith}; use std::ffi::OsString; -use std::path::PathBuf; pub(crate) struct CiCommandPayload { pub(crate) formatter_enabled: Option, @@ -29,12 +28,10 @@ pub(crate) fn ci(session: CliSession, mut payload: CiCommandPayload) -> Result<( payload.cli_options.log_kind.clone(), ); - let base_path = match payload.cli_options.config_path.as_ref() { - None => ConfigurationBasePath::default(), - Some(path) => ConfigurationBasePath::FromUser(PathBuf::from(path)), - }; - - let loaded_configuration = load_configuration(&session.app.fs, base_path)?; + let loaded_configuration = load_configuration( + &session.app.fs, + payload.cli_options.as_configuration_base_path(), + )?; validate_configuration_diagnostics( &loaded_configuration, diff --git a/crates/biome_cli/src/commands/format.rs b/crates/biome_cli/src/commands/format.rs index 0d79ffa63b4c..06a20d27e5ba 100644 --- a/crates/biome_cli/src/commands/format.rs +++ b/crates/biome_cli/src/commands/format.rs @@ -1,6 +1,6 @@ use crate::changed::get_changed_files; use crate::cli_options::CliOptions; -use crate::commands::validate_configuration_diagnostics; +use crate::commands::{get_stdin, validate_configuration_diagnostics}; use crate::diagnostics::DeprecatedArgument; use crate::execute::ReportMode; use crate::{ @@ -15,9 +15,8 @@ use biome_service::configuration::{ load_configuration, FilesConfiguration, FormatterConfiguration, LoadedConfiguration, }; use biome_service::workspace::UpdateSettingsParams; -use biome_service::{ConfigurationBasePath, JavascriptFormatter, MergeWith}; +use biome_service::{JavascriptFormatter, MergeWith}; use std::ffi::OsString; -use std::path::PathBuf; pub(crate) struct FormatCommandPayload { pub(crate) javascript_formatter: Option, @@ -55,12 +54,8 @@ pub(crate) fn format( } = payload; setup_cli_subscriber(cli_options.log_level.clone(), cli_options.log_kind.clone()); - let base_path = match cli_options.config_path.as_ref() { - None => ConfigurationBasePath::default(), - Some(path) => ConfigurationBasePath::FromUser(PathBuf::from(path)), - }; - - let loaded_configuration = load_configuration(&session.app.fs, base_path)?; + let loaded_configuration = + load_configuration(&session.app.fs, cli_options.as_configuration_base_path())?; validate_configuration_diagnostics( &loaded_configuration, session.app.console, @@ -154,19 +149,7 @@ pub(crate) fn format( gitignore_matches, })?; - let stdin = if let Some(stdin_file_path) = stdin_file_path { - let console = &mut session.app.console; - let input_code = console.read(); - if let Some(input_code) = input_code { - let path = PathBuf::from(stdin_file_path); - Some((path, input_code)) - } else { - // we provided the argument without a piped stdin, we bail - return Err(CliDiagnostic::missing_argument("stdin", "format")); - } - } else { - None - }; + let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "format")?; let execution = if cli_options.json { Execution::with_report( diff --git a/crates/biome_cli/src/commands/lint.rs b/crates/biome_cli/src/commands/lint.rs index 2a9a1b8b60ca..2bc7124f715b 100644 --- a/crates/biome_cli/src/commands/lint.rs +++ b/crates/biome_cli/src/commands/lint.rs @@ -1,6 +1,6 @@ use crate::changed::get_changed_files; use crate::cli_options::CliOptions; -use crate::commands::validate_configuration_diagnostics; +use crate::commands::{get_stdin, validate_configuration_diagnostics}; use crate::{ execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode, }; @@ -9,9 +9,8 @@ use biome_service::configuration::{ load_configuration, FilesConfiguration, LinterConfiguration, LoadedConfiguration, }; use biome_service::workspace::{FixFileMode, UpdateSettingsParams}; -use biome_service::{ConfigurationBasePath, MergeWith}; +use biome_service::MergeWith; use std::ffi::OsString; -use std::path::PathBuf; pub(crate) struct LintCommandPayload { pub(crate) apply: bool, @@ -27,10 +26,7 @@ pub(crate) struct LintCommandPayload { } /// Handler for the "lint" command of the Biome CLI -pub(crate) fn lint( - mut session: CliSession, - payload: LintCommandPayload, -) -> Result<(), CliDiagnostic> { +pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(), CliDiagnostic> { let LintCommandPayload { apply, apply_unsafe, @@ -58,12 +54,8 @@ pub(crate) fn lint( Some(FixFileMode::SafeAndUnsafeFixes) }; - let base_path = match cli_options.config_path.as_ref() { - None => ConfigurationBasePath::default(), - Some(path) => ConfigurationBasePath::FromUser(PathBuf::from(path)), - }; - - let loaded_configuration = load_configuration(&session.app.fs, base_path)?; + let loaded_configuration = + load_configuration(&session.app.fs, cli_options.as_configuration_base_path())?; validate_configuration_diagnostics( &loaded_configuration, session.app.console, @@ -92,19 +84,7 @@ pub(crate) fn lint( paths = get_changed_files(&session.app.fs, &fs_configuration, since)?; } - let stdin = if let Some(stdin_file_path) = stdin_file_path { - let console = &mut session.app.console; - let input_code = console.read(); - if let Some(input_code) = input_code { - let path = PathBuf::from(stdin_file_path); - Some((path, input_code)) - } else { - // we provided the argument without a piped stdin, we bail - return Err(CliDiagnostic::missing_argument("stdin", "lint")); - } - } else { - None - }; + let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "lint")?; session .app diff --git a/crates/biome_cli/src/commands/mod.rs b/crates/biome_cli/src/commands/mod.rs index e5613987ed53..bb5b66b98cb3 100644 --- a/crates/biome_cli/src/commands/mod.rs +++ b/crates/biome_cli/src/commands/mod.rs @@ -1,5 +1,6 @@ use crate::cli_options::{cli_options, CliOptions, ColorsArg}; use crate::diagnostics::DeprecatedConfigurationFile; +use crate::execute::Stdin; use crate::logging::LoggingKind; use crate::{CliDiagnostic, LoggingLevel, VERSION}; use biome_console::{markup, Console, ConsoleExt}; @@ -426,3 +427,28 @@ pub(crate) fn validate_configuration_diagnostics( Ok(()) } + +/// Computes [Stdin] if the CLI has the necessary information. +/// +/// ## Errors +/// - If the user didn't provide anything via `stdin` but the option `--stdin-file-path` is passed. +pub(crate) fn get_stdin( + stdin_file_path: Option, + console: &mut dyn Console, + command_name: &str, +) -> Result, CliDiagnostic> { + let stdin = if let Some(stdin_file_path) = stdin_file_path { + let input_code = console.read(); + if let Some(input_code) = input_code { + let path = PathBuf::from(stdin_file_path); + Some((path, input_code).into()) + } else { + // we provided the argument without a piped stdin, we bail + return Err(CliDiagnostic::missing_argument("stdin", command_name)); + } + } else { + None + }; + + Ok(stdin) +} diff --git a/crates/biome_cli/src/execute/mod.rs b/crates/biome_cli/src/execute/mod.rs index d7cf94936cfa..c3d26fcd5e5a 100644 --- a/crates/biome_cli/src/execute/mod.rs +++ b/crates/biome_cli/src/execute/mod.rs @@ -12,7 +12,7 @@ use biome_fs::RomePath; use biome_service::workspace::{FeatureName, FixFileMode}; use std::ffi::OsString; use std::fmt::{Display, Formatter}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; /// Useful information during the traversal of files and virtual content pub(crate) struct Execution { @@ -40,6 +40,31 @@ pub(crate) enum ExecutionEnvironment { GitHub, } +/// A type that holds the information to execute the CLI via `stdin +#[derive(Debug)] +pub(crate) struct Stdin( + /// The virtual path to the file + PathBuf, + /// The content of the file + String, +); + +impl Stdin { + fn as_path(&self) -> &Path { + self.0.as_path() + } + + fn as_content(&self) -> &str { + self.1.as_str() + } +} + +impl From<(PathBuf, String)> for Stdin { + fn from((path, content): (PathBuf, String)) -> Self { + Self(path, content) + } +} + #[derive(Debug)] pub(crate) enum TraversalMode { /// This mode is enabled when running the command `biome check` @@ -52,7 +77,7 @@ pub(crate) enum TraversalMode { /// An optional tuple. /// 1. The virtual path to the file /// 2. The content of the file - stdin: Option<(PathBuf, String)>, + stdin: Option, }, /// This mode is enabled when running the command `biome lint` Lint { @@ -64,7 +89,7 @@ pub(crate) enum TraversalMode { /// An optional tuple. /// 1. The virtual path to the file /// 2. The content of the file - stdin: Option<(PathBuf, String)>, + stdin: Option, }, /// This mode is enabled when running the command `biome ci` CI { @@ -80,7 +105,7 @@ pub(crate) enum TraversalMode { /// An optional tuple. /// 1. The virtual path to the file /// 2. The content of the file - stdin: Option<(PathBuf, String)>, + stdin: Option, }, /// This mode is enabled when running the command `biome migrate` Migrate { @@ -239,7 +264,7 @@ impl Execution { } } - pub(crate) fn as_stdin_file(&self) -> Option<&(PathBuf, String)> { + pub(crate) fn as_stdin_file(&self) -> Option<&Stdin> { match &self.traversal_mode { TraversalMode::Format { stdin, .. } | TraversalMode::Lint { stdin, .. } @@ -260,13 +285,13 @@ pub(crate) fn execute_mode( mode.max_diagnostics = cli_options.max_diagnostics; // don't do any traversal if there's some content coming from stdin - if let Some((path, content)) = mode.as_stdin_file() { - let rome_path = RomePath::new(path); + if let Some(stdin) = mode.as_stdin_file() { + let rome_path = RomePath::new(stdin.as_path()); std_in::run( session, &mode, rome_path, - content.as_str(), + stdin.as_content(), cli_options.verbose, ) } else if let TraversalMode::Migrate { From 448e1bf59a8d744f925aa8e0031ccf07c5bdbbba Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 11 Jan 2024 11:11:09 +0000 Subject: [PATCH 2/2] really wasm pack??? --- crates/biome_wasm/Cargo.toml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/biome_wasm/Cargo.toml b/crates/biome_wasm/Cargo.toml index c0585c65041e..726f44694bb6 100644 --- a/crates/biome_wasm/Cargo.toml +++ b/crates/biome_wasm/Cargo.toml @@ -1,15 +1,15 @@ [package] -authors.workspace = true -categories.workspace = true -description = "WebAssembly bindings to the Biome workspace API" -edition.workspace = true -homepage.workspace = true -keywords.workspace = true -license.workspace = true -name = "biome_wasm" -publish = false -repository.workspace = true -version = "0.0.0" +authors = ["Biome Developers and Contributors"] +categories = ["development-tools", "web-programming"] +description = "WebAssembly bindings to the Biome workspace API" +edition = "2021" +homepage = "https://biomejs.dev/" +keywords = ["parser", "linter", "formatter"] +license = "MIT OR Apache-2.0" +name = "biome_wasm" +publish = false +repository = "https://github.com/biomejs/biome" +version = "0.0.0" [lib]