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

Commit

Permalink
feat(rome_cli): rome migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 14, 2023
1 parent 0133f40 commit 59996fd
Show file tree
Hide file tree
Showing 35 changed files with 828 additions and 212 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
## [Unreleased]

### CLI

#### Other changes
- Add new command `rome migrate` the transform the configuration file `rome.json`
when there are breaking changes.

### Configuration
### Editors
### Formatter
Expand Down
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ countme = "3.0.1"
tokio = { version = "~1.18.5" }
insta = "1.21.2"
quote = { version = "1.0.21" }
lazy_static = "1.4.0"
7 changes: 6 additions & 1 deletion crates/rome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tracing = { workspace = true }
tracing-tree = "0.2.2"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tracing-appender = "0.2"
lazy_static = "1.4.0"
lazy_static = { workspace = true }
hdrhistogram = { version = "7.5.0", default-features = false }
crossbeam = "0.8.1"
rayon = "1.5.1"
Expand All @@ -36,6 +36,11 @@ tokio = { workspace = true, features = ["io-std", "io-util", "net", "time", "rt"
anyhow = "1.0.52"
dashmap = { workspace = true }
rome_text_size = { path = "../rome_text_size" }
rome_json_parser = { path = "../rome_json_parser" }
rome_json_formatter = { path = "../rome_json_formatter" }
rome_json_syntax = { path = "../rome_json_syntax" }
rome_migrate = { path = "../rome_migrate" }
rome_rowan = { path = "../rome_rowan" }

[target.'cfg(unix)'.dependencies]
libc = "0.2.127"
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rome_service::workspace::{FixFileMode, UpdateSettingsParams};

/// Handler for the "check" command of the Rome CLI
pub(crate) fn check(mut session: CliSession) -> Result<(), CliDiagnostic> {
let (mut configuration, diagnostics) = load_configuration(&mut session)?.consume();
let (mut configuration, diagnostics, _) = load_configuration(&mut session)?.consume();
if !diagnostics.is_empty() {
let console = &mut session.app.console;
console.log(markup!{
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rome_service::workspace::UpdateSettingsParams;

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

if !diagnostics.is_empty() {
let console = &mut session.app.console;
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::PathBuf;

/// Handler for the "format" command of the Rome CLI
pub(crate) fn format(mut session: CliSession) -> Result<(), CliDiagnostic> {
let (mut configuration, diagnostics) = load_configuration(&mut session)?.consume();
let (mut configuration, diagnostics, _) = load_configuration(&mut session)?.consume();
if !diagnostics.is_empty() {
let console = &mut session.app.console;
console.log(markup!{
Expand Down
20 changes: 17 additions & 3 deletions crates/rome_cli/src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const MAIN: Markup = markup! {
- "<Emphasis>"ci"</Emphasis>" Run the linter and check the formatting of a set of files
- "<Emphasis>"format"</Emphasis>" Run the formatter on a set of files
- "<Emphasis>"init"</Emphasis>" Bootstraps a new rome project
- "<Emphasis>"start"</Emphasis>" Start the Rome daemon server process
- "<Emphasis>"stop"</Emphasis>" Stop the Rome daemon server process
- "<Emphasis>"help"</Emphasis>" Prints this help message
- "<Emphasis>"lsp-proxy"</Emphasis>" Acts as a server for the Language Server Protocol over stdin/stdout
- "<Emphasis>"migrate"</Emphasis>" It updates the configuration when there are breaking changes
- "<Emphasis>"rage"</Emphasis>" Prints information for debugging
- "<Emphasis>"start"</Emphasis>" Start the Rome daemon server process
- "<Emphasis>"stop"</Emphasis>" Stop the Rome daemon server process
- "<Emphasis>"version"</Emphasis>" Shows the Rome version information and quit
- "<Emphasis>"help"</Emphasis>" Prints this help message
"<Emphasis>"OPTIONS:"</Emphasis>"
"<Dim>"--colors=<off|force>"</Dim>" Set the formatting mode for markup: \"off\" prints everything as plain text, \"force\" forces the formatting of markup using ANSI even if the console output is determined to be incompatible
Expand Down Expand Up @@ -146,6 +147,18 @@ const VERSION_HELP_TEXT: Markup = markup! {
rome version"
};

const MIGRATE: Markup = markup! {
"Rome migrate: updates the configuration file to a newer version
"<Emphasis>"EXAMPLES:"</Emphasis>"
rome migrate
rome migrate --write
"<Emphasis>"OPTIONS:"</Emphasis>"
"<Dim>"--write"</Dim>" It writes the contents to disk
"
};

pub(crate) fn help(session: CliSession, command: Option<&str>) -> Result<(), CliDiagnostic> {
let help_text = match command {
Some("help") | None => MAIN,
Expand All @@ -158,6 +171,7 @@ pub(crate) fn help(session: CliSession, command: Option<&str>) -> Result<(), Cli
Some("lsp-proxy") => START_LSP_PROXY,
Some("version") => VERSION_HELP_TEXT,
Some("rage") => RAGE,
Some("migrate") => MIGRATE,

Some(cmd) => return Err(CliDiagnostic::new_unknown_help(cmd)),
};
Expand Down
22 changes: 22 additions & 0 deletions crates/rome_cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::configuration::load_configuration;
use crate::diagnostics::MigrationDiagnostic;
use crate::execute::{execute_mode, Execution, TraversalMode};
use crate::{CliDiagnostic, CliSession};

/// Handler for the "check" command of the Rome CLI
pub(crate) fn migrate(mut session: CliSession) -> Result<(), CliDiagnostic> {
let (_, _, path) = load_configuration(&mut session)?.consume();
if let Some(path) = path {
execute_mode(
Execution::new(TraversalMode::Migrate {
write: session.args.contains("--dry-run"),
configuration_path: path,
}),
session,
)
} else {
Err(CliDiagnostic::MigrateError(MigrationDiagnostic {
reason: "Rome couldn't find the configuration file".to_string(),
}))
}
}
1 change: 1 addition & 0 deletions crates/rome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub(crate) mod daemon;
pub(crate) mod format;
pub(crate) mod help;
pub(crate) mod init;
pub(crate) mod migrate;
pub(crate) mod rage;
pub(crate) mod version;
1 change: 1 addition & 0 deletions crates/rome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl Display for RageConfiguration<'_, '_> {
match load_config(self.0, ConfigurationBasePath::default()) {
Ok(None) => KeyValuePair("Status", markup!(<Dim>"unset"</Dim>)).fmt(fmt)?,
Ok(Some(deserialized)) => {
let (deserialized, _) = deserialized;
let (configuration, diagnostics) = deserialized.consume();
let status = if !diagnostics.is_empty() {
for diagnostic in diagnostics {
Expand Down
31 changes: 29 additions & 2 deletions crates/rome_cli/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,38 @@ use crate::{CliDiagnostic, CliSession};
use rome_deserialize::Deserialized;
use rome_service::{load_config, Configuration, ConfigurationBasePath};

#[derive(Default)]
pub struct LoadedConfiguration {
deserialized: Deserialized<Configuration>,
path: Option<PathBuf>,
}

impl LoadedConfiguration {
pub fn consume(self) -> (Configuration, Vec<rome_diagnostics::Error>, Option<PathBuf>) {
let path = self.path;
let (configuration, diagnostics) = self.deserialized.consume();
(configuration, diagnostics, path)
}
}

impl From<Option<(Deserialized<Configuration>, PathBuf)>> for LoadedConfiguration {
fn from(value: Option<(Deserialized<Configuration>, PathBuf)>) -> Self {
if let Some((deserialized, path)) = value {
LoadedConfiguration {
deserialized,
path: Some(path),
}
} else {
LoadedConfiguration::default()
}
}
}

/// Load the configuration for this session of the CLI, merging the content of
/// the `rome.json` file if it exists on disk with common command line options
pub(crate) fn load_configuration(
session: &mut CliSession,
) -> Result<Deserialized<Configuration>, CliDiagnostic> {
) -> Result<LoadedConfiguration, CliDiagnostic> {
let config_path: Option<PathBuf> = session
.args
.opt_value_from_str("--config-path")
Expand All @@ -21,5 +48,5 @@ pub(crate) fn load_configuration(

let config = load_config(&session.app.fs, base_path)?;

Ok(config.unwrap_or_default())
Ok(config.into())
}
24 changes: 24 additions & 0 deletions crates/rome_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum CliDiagnostic {
IncompatibleEndConfiguration(IncompatibleEndConfiguration),
/// No files processed during the file system traversal
NoFilesWereProcessed(NoFilesWereProcessed),
/// Errors thrown when running the `rome migrate` command
MigrateError(MigrationDiagnostic),
}

#[derive(Debug, Diagnostic)]
Expand Down Expand Up @@ -258,6 +260,19 @@ pub struct IncompatibleEndConfiguration {
)]
pub struct NoFilesWereProcessed;

#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "migrate",
severity = Error,
message(
message("Migration has encountered an error: "{{&self.reason}}),
description = "Migration has encountered an error: {reason}"
)
)]
pub struct MigrationDiagnostic {
pub reason: String,
}

/// Advices for the [CliDiagnostic]
#[derive(Debug, Default)]
struct CliAdvice {
Expand Down Expand Up @@ -422,6 +437,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.category(),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.category(),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.category(),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.category(),
}
}

Expand All @@ -442,6 +458,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.tags(),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.tags(),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.tags(),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.tags(),
}
}

Expand All @@ -462,6 +479,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.severity(),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.severity(),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.severity(),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.severity(),
}
}

Expand All @@ -482,6 +500,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.location(),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.location(),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.location(),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.location(),
}
}

Expand All @@ -502,6 +521,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.message(fmt),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.message(fmt),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.message(fmt),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.message(fmt),
}
}

Expand All @@ -522,6 +542,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.description(fmt),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.description(fmt),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.description(fmt),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.description(fmt),
}
}

Expand All @@ -542,6 +563,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.advices(visitor),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.advices(visitor),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.advices(visitor),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.advices(visitor),
}
}

Expand All @@ -566,6 +588,7 @@ impl Diagnostic for CliDiagnostic {
}
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.verbose_advices(visitor),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.verbose_advices(visitor),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.verbose_advices(visitor),
}
}

Expand All @@ -586,6 +609,7 @@ impl Diagnostic for CliDiagnostic {
CliDiagnostic::IncompatibleEndConfiguration(diagnostic) => diagnostic.source(),
CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.source(),
CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.source(),
CliDiagnostic::MigrateError(diagnostic) => diagnostic.source(),
}
}
}
Expand Down
Loading

0 comments on commit 59996fd

Please sign in to comment.