+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_cli): termination as diagnostic #4058

Merged
merged 10 commits into from
Dec 21, 2022
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
23 changes: 2 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/rome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ tracing-appender = "0.2"
lazy_static = "1.4.0"
hdrhistogram = { version = "7.5.0", default-features = false }
crossbeam = "0.8.1"
thiserror = "1.0.30"
rayon = "1.5.1"
serde = { version = "1.0.133", features = ["derive"] }
serde_json = { version = "1.0.74" }
tokio = { workspace = true, features = ["io-std", "io-util", "net", "time", "rt","sync", "rt-multi-thread", "macros"] }
anyhow = "1.0.52"
dashmap = { workspace = true }
rome_text_size = { path = "../rome_text_size" }

[target.'cfg(unix)'.dependencies]
libc = "0.2.127"
Expand Down
6 changes: 3 additions & 3 deletions crates/rome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::commands::format::apply_format_settings_from_cli;
use crate::configuration::load_configuration;
use crate::{execute_mode, CliSession, Execution, Termination, TraversalMode};
use crate::{execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode};
use rome_service::workspace::{FixFileMode, UpdateSettingsParams};

/// Handler for the "check" command of the Rome CLI
pub(crate) fn check(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn check(mut session: CliSession) -> Result<(), CliDiagnostic> {
let mut configuration = load_configuration(&mut session)?;

apply_format_settings_from_cli(&mut session, &mut configuration)?;
Expand All @@ -18,7 +18,7 @@ pub(crate) fn check(mut session: CliSession) -> Result<(), Termination> {
let apply_suggested = session.args.contains("--apply-suggested");

let fix_file_mode = if apply && apply_suggested {
return Err(Termination::IncompatibleArguments(
return Err(CliDiagnostic::incompatible_arguments(
"--apply",
"--apply-suggested",
));
Expand Down
16 changes: 5 additions & 11 deletions crates/rome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
configuration::load_configuration, execute_mode, CliSession, Execution, Termination,
configuration::load_configuration, execute_mode, CliDiagnostic, CliSession, Execution,
TraversalMode,
};
use rome_service::configuration::{FormatterConfiguration, LinterConfiguration};
Expand All @@ -8,24 +8,18 @@ use rome_service::workspace::UpdateSettingsParams;
use super::format::apply_format_settings_from_cli;

/// Handler for the "ci" command of the Rome CLI
pub(crate) fn ci(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn ci(mut session: CliSession) -> Result<(), CliDiagnostic> {
let mut configuration = load_configuration(&mut session)?;

let formatter_enabled = session
.args
.opt_value_from_str("--formatter-enabled")
.map_err(|source| Termination::ParseError {
argument: "--formatter-enabled",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--formatter-enabled", source))?;

let linter_enabled = session
.args
.opt_value_from_str("--linter-enabled")
.map_err(|source| Termination::ParseError {
argument: "--linter-enabled",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--linter-enabled", source))?;

let formatter = configuration
.formatter
Expand All @@ -45,7 +39,7 @@ pub(crate) fn ci(mut session: CliSession) -> Result<(), Termination> {

// no point in doing the traversal if all the checks have been disabled
if configuration.is_formatter_disabled() && configuration.is_linter_disabled() {
return Err(Termination::IncompatibleEndConfiguration("Formatter and Linter are both disabled, can't perform the command. This probably and error."));
return Err(CliDiagnostic::incompatible_end_configuration("Formatter and Linter are both disabled, can't perform the command. This is probably and error."));
}

if !configuration.is_formatter_disabled() {
Expand Down
20 changes: 10 additions & 10 deletions crates/rome_cli/src/commands/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rome_console::{markup, ConsoleExt};
use rome_lsp::ServerFactory;
use rome_service::{workspace::WorkspaceClient, RomeError, TransportError};
use rome_service::{workspace::WorkspaceClient, TransportError, WorkspaceError};
use std::{env, fs, path::PathBuf};
use tokio::io;
use tokio::runtime::Runtime;
Expand All @@ -16,10 +16,10 @@ use tracing_tree::HierarchicalLayer;
use crate::{
open_transport,
service::{self, ensure_daemon, open_socket, run_daemon},
CliSession, Termination,
CliDiagnostic, CliSession,
};

pub(crate) fn start(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn start(session: CliSession) -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;
let did_spawn = rt.block_on(ensure_daemon(false))?;

Expand All @@ -36,16 +36,16 @@ pub(crate) fn start(mut session: CliSession) -> Result<(), Termination> {
Ok(())
}

pub(crate) fn stop(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn stop(session: CliSession) -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;

if let Some(transport) = open_transport(rt)? {
let client = WorkspaceClient::new(transport)?;
match client.shutdown() {
// The `ChannelClosed` error is expected since the server can
// shutdown before sending a response
Ok(()) | Err(RomeError::TransportError(TransportError::ChannelClosed)) => {}
Err(err) => return Err(Termination::from(err)),
Ok(()) | Err(WorkspaceError::TransportError(TransportError::ChannelClosed)) => {}
Err(err) => return Err(CliDiagnostic::from(err)),
};

session.app.console.log(markup! {
Expand All @@ -60,7 +60,7 @@ pub(crate) fn stop(mut session: CliSession) -> Result<(), Termination> {
Ok(())
}

pub(crate) fn run_server(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn run_server(mut session: CliSession) -> Result<(), CliDiagnostic> {
setup_tracing_subscriber();

let stop_on_disconnect = session.args.contains("--stop-on-disconnect");
Expand All @@ -86,13 +86,13 @@ pub(crate) fn run_server(mut session: CliSession) -> Result<(), Termination> {
})
}

pub(crate) fn print_socket() -> Result<(), Termination> {
pub(crate) fn print_socket() -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;
rt.block_on(service::print_socket())?;
Ok(())
}

pub(crate) fn lsp_proxy() -> Result<(), Termination> {
pub(crate) fn lsp_proxy() -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;
rt.block_on(start_lsp_proxy(&rt))?;

Expand All @@ -102,7 +102,7 @@ pub(crate) fn lsp_proxy() -> Result<(), Termination> {
/// Start a proxy process.
/// Receives a process via `stdin` and then copy the content to the LSP socket.
/// Copy to the process on `stdout` when the LSP responds to a message
async fn start_lsp_proxy(rt: &Runtime) -> Result<(), Termination> {
async fn start_lsp_proxy(rt: &Runtime) -> Result<(), CliDiagnostic> {
ensure_daemon(true).await?;

match open_socket().await? {
Expand Down
51 changes: 12 additions & 39 deletions crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::path::PathBuf;

use crate::configuration::load_configuration;
use crate::execute::ReportMode;
use crate::{execute_mode, CliSession, Execution, Termination, TraversalMode};
use crate::{execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode};

/// Handler for the "format" command of the Rome CLI
pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn format(mut session: CliSession) -> Result<(), CliDiagnostic> {
let mut configuration = load_configuration(&mut session)?;
apply_format_settings_from_cli(&mut session, &mut configuration)?;

Expand All @@ -24,10 +24,7 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
let stdin_file_path: Option<String> = session
.args
.opt_value_from_str("--stdin-file-path")
.map_err(|source| Termination::ParseError {
argument: "--stdin-file-path",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--stdin-file-path", source))?;

let stdin = if let Some(stdin_file_path) = stdin_file_path {
let console = &mut session.app.console;
Expand All @@ -37,10 +34,7 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
Some((path, input_code))
} else {
// we provided the argument without a piped stdin, we bail
return Err(Termination::MissingArgument {
subcommand: "format",
argument: "stdin",
});
return Err(CliDiagnostic::missing_argument("stdin", "format"));
}
} else {
None
Expand Down Expand Up @@ -71,34 +65,25 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn apply_format_settings_from_cli(
session: &mut CliSession,
configuration: &mut Configuration,
) -> Result<(), Termination> {
) -> Result<(), CliDiagnostic> {
let formatter = configuration
.formatter
.get_or_insert_with(FormatterConfiguration::default);

let size = session
.args
.opt_value_from_str("--indent-size")
.map_err(|source| Termination::ParseError {
argument: "--indent-size",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--indent-size", source))?;

let indent_style = session
.args
.opt_value_from_str("--indent-style")
.map_err(|source| Termination::ParseError {
argument: "--indent-style",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--indent-style", source))?;

let line_width = session
.args
.opt_value_from_str("--line-width")
.map_err(|source| Termination::ParseError {
argument: "--line-width",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--line-width", source))?;

match indent_style {
Some(IndentStyle::Tab) => {
Expand All @@ -118,34 +103,22 @@ pub(crate) fn apply_format_settings_from_cli(
let quote_properties = session
.args
.opt_value_from_str("--quote-properties")
.map_err(|source| Termination::ParseError {
argument: "--quote-properties",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--quote-properties", source))?;

let quote_style = session
.args
.opt_value_from_str("--quote-style")
.map_err(|source| Termination::ParseError {
argument: "--quote-style",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--quote-style", source))?;

let trailing_comma = session
.args
.opt_value_from_str("--trailing-comma")
.map_err(|source| Termination::ParseError {
argument: "--trailing-comma",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--trailing-comma", source))?;

let semicolons = session
.args
.opt_value_from_str("--semicolons")
.map_err(|source| Termination::ParseError {
argument: "--semicolons",
source,
})?;
.map_err(|source| CliDiagnostic::parse_error("--semicolons", source))?;

let javascript = configuration
.javascript
Expand Down
10 changes: 3 additions & 7 deletions crates/rome_cli/src/commands/help.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rome_console::{markup, ConsoleExt, Markup};

use crate::{CliSession, Termination, VERSION};
use crate::{CliDiagnostic, CliSession, VERSION};

const MAIN: Markup = markup! {
"Rome CLI v"{VERSION}"
Expand Down Expand Up @@ -127,7 +127,7 @@ const VERSION_HELP_TEXT: Markup = markup! {
rome version"
};

pub(crate) fn help(mut session: CliSession, command: Option<&str>) -> Result<(), Termination> {
pub(crate) fn help(session: CliSession, command: Option<&str>) -> Result<(), CliDiagnostic> {
let help_text = match command {
Some("help") | None => MAIN,
Some("check") => CHECK,
Expand All @@ -140,11 +140,7 @@ pub(crate) fn help(mut session: CliSession, command: Option<&str>) -> Result<(),
Some("version") => VERSION_HELP_TEXT,
Some("rage") => RAGE,

Some(cmd) => {
return Err(Termination::UnknownCommandHelp {
command: cmd.into(),
})
}
Some(cmd) => return Err(CliDiagnostic::new_unknown_help(cmd)),
};

session.app.console.log(help_text);
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_cli/src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{CliSession, Termination};
use crate::{CliDiagnostic, CliSession};
use rome_console::{markup, ConsoleExt, HorizontalLine};
use rome_service::configuration::Configuration;
use rome_service::create_config;

pub(crate) fn init(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn init(mut session: CliSession) -> Result<(), CliDiagnostic> {
let fs = &mut session.app.fs;
create_config(fs, Configuration::default())?;

Expand Down
4 changes: 2 additions & 2 deletions crates/rome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use tokio::runtime::Runtime;

use crate::commands::daemon::read_most_recent_log_file;
use crate::service::enumerate_pipes;
use crate::{service, CliSession, Termination, VERSION};
use crate::{service, CliDiagnostic, CliSession, VERSION};

/// Handler for the `rage` command
pub(crate) fn rage(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn rage(session: CliSession) -> Result<(), CliDiagnostic> {
let terminal_supports_colors = termcolor::BufferWriter::stdout(ColorChoice::Auto)
.buffer()
.supports_color();
Expand Down
6 changes: 3 additions & 3 deletions crates/rome_cli/src/commands/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use rome_console::fmt::Formatter;
use rome_console::{fmt, markup, ConsoleExt};
use rome_service::workspace::ServerInfo;

use crate::{CliSession, Termination, VERSION};
use crate::{CliDiagnostic, CliSession, VERSION};

/// Handler for the `--version` argument. Prints a brief rome version.
pub(crate) fn brief_version(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn brief_version(session: CliSession) -> Result<(), CliDiagnostic> {
session
.app
.console
Expand All @@ -15,7 +15,7 @@ pub(crate) fn brief_version(mut session: CliSession) -> Result<(), Termination>
}

/// Handle of the `version` command. Prints a more in detail version of rome.
pub(crate) fn full_version(mut session: CliSession) -> Result<(), Termination> {
pub(crate) fn full_version(session: CliSession) -> Result<(), CliDiagnostic> {
session.app.console.log(markup! {
"CLI: "{VERSION}
});
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载