Skip to content

Commit

Permalink
Drop current project in workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Jan 4, 2025
1 parent 78c8910 commit 85b9700
Show file tree
Hide file tree
Showing 58 changed files with 1,479 additions and 999 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

22 changes: 17 additions & 5 deletions crates/biome_cli/examples/text_reporter.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
use biome_cli::{
DiagnosticsPayload, Execution, Reporter, ReporterVisitor, TraversalSummary, VcsTargeted,
};
use biome_service::projects::ProjectKey;

/// This will be the visitor, which where we **write** the data
struct BufferVisitor(String);

/// This is the reporter, which will be a type that will hold the information needed to the reporter
struct TextReport {
project_key: ProjectKey,
summary: TraversalSummary,
}

impl Reporter for TextReport {
fn write(self, visitor: &mut dyn ReporterVisitor) -> std::io::Result<()> {
let execution = Execution::new_format(VcsTargeted {
staged: false,
changed: false,
});
let execution = Execution::new_format(
self.project_key,
VcsTargeted {
staged: false,
changed: false,
},
);
visitor.report_summary(&execution, self.summary)?;
Ok(())
}
Expand All @@ -42,13 +47,20 @@ impl ReporterVisitor for BufferVisitor {
}

pub fn main() {
// In a real scenario, the project key is obtained from the
// `Workspace::open_project()` call.
let project_key = ProjectKey::new();

let summary = TraversalSummary {
changed: 32,
unchanged: 28,
..TraversalSummary::default()
};
let mut visitor = BufferVisitor(String::new());
let reporter = TextReport { summary };
let reporter = TextReport {
project_key,
summary,
};
reporter.write(&mut visitor).unwrap();

assert_eq!(visitor.0.as_str(), "Total is 64")
Expand Down
3 changes: 3 additions & 0 deletions crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use biome_configuration::{
use biome_console::Console;
use biome_deserialize::Merge;
use biome_fs::FileSystem;
use biome_service::projects::ProjectKey;
use biome_service::{configuration::LoadedConfiguration, Workspace, WorkspaceError};
use std::ffi::OsString;

Expand Down Expand Up @@ -123,6 +124,7 @@ impl CommandRunner for CheckCommandPayload {
cli_options: &CliOptions,
console: &mut dyn Console,
_workspace: &dyn Workspace,
project_key: ProjectKey,
) -> Result<Execution, CliDiagnostic> {
let fix_file_mode = determine_fix_file_mode(FixFileModeOptions {
write: self.write,
Expand All @@ -133,6 +135,7 @@ impl CommandRunner for CheckCommandPayload {
})?;

Ok(Execution::new(TraversalMode::Check {
project_key,
fix_file_mode,
stdin: self.get_stdin(console)?,
vcs_targeted: (self.staged, self.changed).into(),
Expand Down
4 changes: 3 additions & 1 deletion crates/biome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use biome_console::Console;
use biome_deserialize::Merge;
use biome_fs::FileSystem;
use biome_service::configuration::LoadedConfiguration;
use biome_service::projects::ProjectKey;
use biome_service::{Workspace, WorkspaceError};
use std::ffi::OsString;

Expand Down Expand Up @@ -114,8 +115,9 @@ impl CommandRunner for CiCommandPayload {
cli_options: &CliOptions,
_console: &mut dyn Console,
_workspace: &dyn Workspace,
project_key: ProjectKey,
) -> Result<Execution, CliDiagnostic> {
Ok(Execution::new_ci((false, self.changed).into()).set_report(cli_options))
Ok(Execution::new_ci(project_key, (false, self.changed).into()).set_report(cli_options))
}

fn check_incompatible_arguments(&self) -> Result<(), CliDiagnostic> {
Expand Down
3 changes: 3 additions & 0 deletions crates/biome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use biome_console::Console;
use biome_deserialize::Merge;
use biome_fs::FileSystem;
use biome_service::configuration::LoadedConfiguration;
use biome_service::projects::ProjectKey;
use biome_service::{Workspace, WorkspaceError};
use std::ffi::OsString;

Expand Down Expand Up @@ -134,8 +135,10 @@ impl CommandRunner for FormatCommandPayload {
cli_options: &CliOptions,
console: &mut dyn Console,
_workspace: &dyn Workspace,
project_key: ProjectKey,
) -> Result<Execution, CliDiagnostic> {
Ok(Execution::new(TraversalMode::Format {
project_key,
ignore_errors: cli_options.skip_errors,
write: self.should_write(),
stdin: self.get_stdin(console)?,
Expand Down
3 changes: 3 additions & 0 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use biome_console::Console;
use biome_deserialize::Merge;
use biome_fs::FileSystem;
use biome_service::configuration::LoadedConfiguration;
use biome_service::projects::ProjectKey;
use biome_service::{Workspace, WorkspaceError};
use std::ffi::OsString;

Expand Down Expand Up @@ -128,6 +129,7 @@ impl CommandRunner for LintCommandPayload {
cli_options: &CliOptions,
console: &mut dyn Console,
_workspace: &dyn Workspace,
project_key: ProjectKey,
) -> Result<Execution, CliDiagnostic> {
let fix_file_mode = determine_fix_file_mode(FixFileModeOptions {
write: self.write,
Expand All @@ -137,6 +139,7 @@ impl CommandRunner for LintCommandPayload {
suppression_reason: self.suppression_reason.clone(),
})?;
Ok(Execution::new(TraversalMode::Lint {
project_key,
fix_file_mode,
stdin: self.get_stdin(console)?,
only: self.only.clone(),
Expand Down
3 changes: 3 additions & 0 deletions crates/biome_cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use biome_configuration::PartialConfiguration;
use biome_console::{markup, Console, ConsoleExt};
use biome_fs::FileSystem;
use biome_service::configuration::LoadedConfiguration;
use biome_service::projects::ProjectKey;
use biome_service::{Workspace, WorkspaceError};
use camino::Utf8PathBuf;
use std::ffi::OsString;
Expand Down Expand Up @@ -56,12 +57,14 @@ impl CommandRunner for MigrateCommandPayload {
_cli_options: &CliOptions,
console: &mut dyn Console,
_workspace: &dyn Workspace,
project_key: ProjectKey,
) -> Result<Execution, CliDiagnostic> {
if let (Some(path), Some(directory_path)) = (
self.configuration_file_path.clone(),
self.configuration_directory_path.clone(),
) {
Ok(Execution::new(TraversalMode::Migrate {
project_key,
write: self.should_write(),
configuration_file_path: path,
configuration_directory_path: directory_path,
Expand Down
27 changes: 19 additions & 8 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use biome_service::configuration::{
load_configuration, load_editorconfig, LoadedConfiguration, PartialConfigurationExt,
};
use biome_service::documentation::Doc;
use biome_service::workspace::{FixFileMode, RegisterProjectFolderParams, UpdateSettingsParams};
use biome_service::projects::ProjectKey;
use biome_service::workspace::{
FixFileMode, OpenProjectParams, ScanProjectFolderParams, UpdateSettingsParams,
};
use biome_service::{Workspace, WorkspaceError};
use bpaf::Bpaf;
use camino::Utf8PathBuf;
Expand Down Expand Up @@ -795,27 +798,34 @@ pub(crate) trait CommandRunner: Sized {
let (vcs_base_path, gitignore_matches) =
configuration.retrieve_gitignore_matches(fs, vcs_base_path.as_deref())?;
let paths = self.get_files_to_process(fs, &configuration)?;
workspace.register_project_folder(RegisterProjectFolderParams {
path: fs.working_directory().map(BiomePath::from),
set_as_current_workspace: true,
let project_path = fs
.working_directory()
.map(BiomePath::from)
.unwrap_or_default();
let project_key = workspace.open_project(OpenProjectParams {
path: project_path.clone(),
})?;

let manifest_data = resolve_manifest(fs)?;

if let Some(manifest_data) = manifest_data {
workspace.set_manifest_for_project(manifest_data.into())?;
if let Some((path, content)) = manifest_data {
workspace.set_manifest_for_project((project_key, path, content).into())?;
}
workspace.update_settings(UpdateSettingsParams {
project_key,
workspace_directory: configuration_path.map(BiomePath::from),
configuration,
vcs_base_path: vcs_base_path.map(BiomePath::from),
gitignore_matches,
})?;

let execution = self.get_execution(cli_options, console, workspace)?;
let execution = self.get_execution(cli_options, console, workspace, project_key)?;

if execution.traversal_mode().should_scan_project() {
let result = workspace.scan_current_project_folder(())?;
let result = workspace.scan_project_folder(ScanProjectFolderParams {
project_key,
path: Some(project_path),
})?;
if cli_options.verbose && matches!(execution.report_mode(), ReportMode::Terminal { .. })
{
console.log(markup! {
Expand Down Expand Up @@ -879,6 +889,7 @@ pub(crate) trait CommandRunner: Sized {
cli_options: &CliOptions,
console: &mut dyn Console,
workspace: &dyn Workspace,
project_key: ProjectKey,
) -> Result<Execution, CliDiagnostic>;

// Below, methods that consumers can implement
Expand Down
3 changes: 3 additions & 0 deletions crates/biome_cli/src/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use biome_deserialize::Merge;
use biome_fs::FileSystem;
use biome_grit_patterns::GritTargetLanguage;
use biome_service::configuration::LoadedConfiguration;
use biome_service::projects::ProjectKey;
use biome_service::workspace::ParsePatternParams;
use biome_service::{Workspace, WorkspaceError};
use std::ffi::OsString;
Expand Down Expand Up @@ -63,6 +64,7 @@ impl CommandRunner for SearchCommandPayload {
cli_options: &CliOptions,
console: &mut dyn Console,
workspace: &dyn Workspace,
project_key: ProjectKey,
) -> Result<Execution, CliDiagnostic> {
let pattern = workspace
.parse_pattern(ParsePatternParams {
Expand All @@ -71,6 +73,7 @@ impl CommandRunner for SearchCommandPayload {
})?
.pattern_id;
Ok(Execution::new(TraversalMode::Search {
project_key,
pattern,
language: self.language.clone(),
stdin: self.get_stdin(console)?,
Expand Down
16 changes: 14 additions & 2 deletions crates/biome_cli/src/execute/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use biome_json_parser::{parse_json_with_cache, JsonParserOptions};
use biome_json_syntax::{JsonFileSource, JsonRoot};
use biome_migrate::{migrate_configuration, ControlFlow};
use biome_rowan::{AstNode, NodeCache};
use biome_service::projects::ProjectKey;
use biome_service::workspace::{
ChangeFileParams, FileContent, FixAction, FormatFileParams, OpenFileParams,
};
Expand All @@ -34,6 +35,7 @@ mod prettier;

pub(crate) struct MigratePayload<'a> {
pub(crate) session: CliSession<'a>,
pub(crate) project_key: ProjectKey,
pub(crate) write: bool,
pub(crate) configuration_file_path: Utf8PathBuf,
pub(crate) configuration_directory_path: Utf8PathBuf,
Expand All @@ -44,6 +46,7 @@ pub(crate) struct MigratePayload<'a> {
pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic> {
let MigratePayload {
session,
project_key,
write,
configuration_file_path,
configuration_directory_path,
Expand All @@ -67,6 +70,7 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>

let biome_path = BiomePath::new(configuration_file_path.as_path());
workspace.open_file(OpenFileParams {
project_key,
path: biome_path.clone(),
content: FileContent::FromClient(biome_config_content.to_string()),
version: 0,
Expand Down Expand Up @@ -131,11 +135,15 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
})
})?;
workspace.change_file(ChangeFileParams {
project_key,
path: biome_path.clone(),
content: new_content,
version: 1,
})?;
let printed = workspace.format_file(FormatFileParams { path: biome_path })?;
let printed = workspace.format_file(FormatFileParams {
project_key,
path: biome_path,
})?;
if write {
biome_config_file.set_content(printed.as_code().as_bytes())?;
console.log(markup!{
Expand Down Expand Up @@ -208,11 +216,15 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
})
})?;
workspace.change_file(ChangeFileParams {
project_key,
path: biome_path.clone(),
content: new_content,
version: 1,
})?;
let printed = workspace.format_file(FormatFileParams { path: biome_path })?;
let printed = workspace.format_file(FormatFileParams {
project_key,
path: biome_path,
})?;
if write {
biome_config_file.set_content(printed.as_code().as_bytes())?;
console.log(markup!{
Expand Down
Loading

0 comments on commit 85b9700

Please sign in to comment.