Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): some cleanup #1526

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/biome_cli/src/cli_options.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 6 additions & 23 deletions crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"));
Expand Down
13 changes: 5 additions & 8 deletions crates/biome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
Expand All @@ -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,
Expand Down
27 changes: 5 additions & 22 deletions crates/biome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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<JavascriptFormatter>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
32 changes: 6 additions & 26 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<String>,
console: &mut dyn Console,
command_name: &str,
) -> Result<Option<Stdin>, 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)
}
41 changes: 33 additions & 8 deletions crates/biome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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`
Expand All @@ -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<Stdin>,
},
/// This mode is enabled when running the command `biome lint`
Lint {
Expand All @@ -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<Stdin>,
},
/// This mode is enabled when running the command `biome ci`
CI {
Expand All @@ -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<Stdin>,
},
/// This mode is enabled when running the command `biome migrate`
Migrate {
Expand Down Expand Up @@ -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, .. }
Expand All @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions crates/biome_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down