-
Notifications
You must be signed in to change notification settings - Fork 106
refactor(cli): simplify abstractions #574
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
880812f
chore: prepare plan for refactoring
psteinroe 60edbaf
Merge branch 'main' into feat/dblint
psteinroe bd5ff58
progress
psteinroe 888356d
progress
psteinroe 3f04408
Merge branch 'main' into feat/dblint
psteinroe 99c2264
progress
psteinroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,141 @@ | ||
| use crate::cli_options::CliOptions; | ||
| use crate::{CliDiagnostic, Execution, TraversalMode}; | ||
| use biome_deserialize::Merge; | ||
| use crate::commands::get_files_to_process_with_cli_options; | ||
| use crate::execute::{StdinPayload, run_files, run_stdin}; | ||
| use crate::reporter::Report; | ||
| use crate::{CliDiagnostic, CliSession, VcsIntegration}; | ||
| use crate::{ExecutionConfig, ExecutionMode, VcsTargeting}; | ||
| use pgt_configuration::PartialConfiguration; | ||
| use pgt_console::Console; | ||
| use pgt_diagnostics::category; | ||
| use pgt_fs::FileSystem; | ||
| use pgt_workspace::{DynRef, Workspace, WorkspaceError, configuration::LoadedConfiguration}; | ||
| use pgt_workspace::DynRef; | ||
| use std::ffi::OsString; | ||
|
|
||
| use super::{CommandRunner, get_files_to_process_with_cli_options}; | ||
| #[allow(dead_code)] | ||
| pub struct CheckArgs { | ||
| pub configuration: Option<PartialConfiguration>, | ||
| pub paths: Vec<OsString>, | ||
| pub stdin_file_path: Option<String>, | ||
| pub staged: bool, | ||
| pub changed: bool, | ||
| pub since: Option<String>, | ||
| pub apply: bool, | ||
| pub apply_unsafe: bool, | ||
| } | ||
|
|
||
| pub fn check( | ||
| mut session: CliSession, | ||
| cli_options: &CliOptions, | ||
| args: CheckArgs, | ||
| ) -> Result<(), CliDiagnostic> { | ||
| validate_args(&args)?; | ||
|
|
||
| let configuration = session.prepare_with_config(cli_options, args.configuration.clone())?; | ||
| session.setup_workspace(configuration.clone(), VcsIntegration::Enabled)?; | ||
|
|
||
| let paths = resolve_paths(session.fs(), &configuration, &args)?; | ||
|
|
||
| let vcs = VcsTargeting { | ||
| staged: args.staged, | ||
| changed: args.changed, | ||
| }; | ||
|
|
||
| let max_diagnostics = if cli_options.reporter.is_default() { | ||
| cli_options.max_diagnostics.into() | ||
| } else { | ||
| u32::MAX | ||
| }; | ||
|
|
||
| let mode = ExecutionMode::Check { vcs }; | ||
| let execution = ExecutionConfig::new(mode, max_diagnostics); | ||
|
|
||
| pub(crate) struct CheckCommandPayload { | ||
| pub(crate) configuration: Option<PartialConfiguration>, | ||
| pub(crate) paths: Vec<OsString>, | ||
| pub(crate) stdin_file_path: Option<String>, | ||
| pub(crate) staged: bool, | ||
| pub(crate) changed: bool, | ||
| pub(crate) since: Option<String>, | ||
| if let Some(stdin_path) = args.stdin_file_path.as_deref() { | ||
| let payload = read_stdin_payload(stdin_path, session.console())?; | ||
| run_stdin(&mut session, &execution, payload) | ||
| } else { | ||
| let report: Report = run_files(&mut session, &execution, paths)?; | ||
|
|
||
| let exit_result = enforce_exit_codes(cli_options, &report); | ||
| session.report("check", cli_options, &report)?; | ||
| exit_result | ||
| } | ||
| } | ||
|
|
||
| impl CommandRunner for CheckCommandPayload { | ||
| const COMMAND_NAME: &'static str = "check"; | ||
|
|
||
| fn merge_configuration( | ||
| &mut self, | ||
| loaded_configuration: LoadedConfiguration, | ||
| _fs: &DynRef<'_, dyn FileSystem>, | ||
| _console: &mut dyn Console, | ||
| ) -> Result<PartialConfiguration, WorkspaceError> { | ||
| let LoadedConfiguration { | ||
| configuration: mut fs_configuration, | ||
| .. | ||
| } = loaded_configuration; | ||
|
|
||
| if let Some(configuration) = self.configuration.clone() { | ||
| // overwrite fs config with cli args | ||
| fs_configuration.merge_with(configuration); | ||
| fn resolve_paths( | ||
| fs: &DynRef<'_, dyn FileSystem>, | ||
| configuration: &PartialConfiguration, | ||
| args: &CheckArgs, | ||
| ) -> Result<Vec<OsString>, CliDiagnostic> { | ||
| let mut paths = get_files_to_process_with_cli_options( | ||
| args.since.as_deref(), | ||
| args.changed, | ||
| args.staged, | ||
| fs, | ||
| configuration, | ||
| )? | ||
| .unwrap_or_else(|| args.paths.clone()); | ||
|
|
||
| if paths.is_empty() && args.stdin_file_path.is_none() { | ||
| if let Some(current_dir) = fs.working_directory() { | ||
| paths.push(current_dir.into_os_string()); | ||
| } | ||
| } | ||
|
|
||
| Ok(fs_configuration) | ||
| Ok(paths) | ||
| } | ||
|
|
||
| fn read_stdin_payload( | ||
| path: &str, | ||
| console: &mut dyn Console, | ||
| ) -> Result<StdinPayload, CliDiagnostic> { | ||
| let input_code = console.read(); | ||
| if let Some(input_code) = input_code { | ||
| Ok(StdinPayload { | ||
| path: path.into(), | ||
| content: input_code, | ||
| }) | ||
| } else { | ||
| Err(CliDiagnostic::missing_argument("stdin", "check")) | ||
| } | ||
| } | ||
|
|
||
| fn enforce_exit_codes(cli_options: &CliOptions, payload: &Report) -> Result<(), CliDiagnostic> { | ||
| let traversal = payload.traversal.as_ref(); | ||
| let processed = traversal.map_or(0, |t| t.changed + t.unchanged); | ||
| let skipped = traversal.map_or(0, |t| t.skipped); | ||
|
|
||
| fn get_files_to_process( | ||
| &self, | ||
| fs: &DynRef<'_, dyn FileSystem>, | ||
| configuration: &PartialConfiguration, | ||
| ) -> Result<Vec<OsString>, CliDiagnostic> { | ||
| let paths = get_files_to_process_with_cli_options( | ||
| self.since.as_deref(), | ||
| self.changed, | ||
| self.staged, | ||
| fs, | ||
| configuration, | ||
| )? | ||
| .unwrap_or(self.paths.clone()); | ||
|
|
||
| Ok(paths) | ||
| if processed.saturating_sub(skipped) == 0 && !cli_options.no_errors_on_unmatched { | ||
| return Err(CliDiagnostic::no_files_processed()); | ||
| } | ||
|
|
||
| fn get_stdin_file_path(&self) -> Option<&str> { | ||
| self.stdin_file_path.as_deref() | ||
| let warnings = payload.warnings; | ||
| let errors = payload.errors; | ||
| let category = category!("check"); | ||
|
|
||
| if errors > 0 { | ||
| return Err(CliDiagnostic::check_error(category)); | ||
| } | ||
|
|
||
| fn get_execution( | ||
| &self, | ||
| cli_options: &CliOptions, | ||
| console: &mut dyn Console, | ||
| _workspace: &dyn Workspace, | ||
| ) -> Result<Execution, CliDiagnostic> { | ||
| Ok(Execution::new(TraversalMode::Check { | ||
| stdin: self.get_stdin(console)?, | ||
| vcs_targeted: (self.staged, self.changed).into(), | ||
| }) | ||
| .set_report(cli_options)) | ||
| if warnings > 0 && cli_options.error_on_warnings { | ||
| return Err(CliDiagnostic::check_warnings(category)); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn validate_args(args: &CheckArgs) -> Result<(), CliDiagnostic> { | ||
| if args.since.is_some() { | ||
| if !args.changed { | ||
| return Err(CliDiagnostic::incompatible_arguments("since", "changed")); | ||
| } | ||
| if args.staged { | ||
| return Err(CliDiagnostic::incompatible_arguments("since", "staged")); | ||
| } | ||
| } | ||
|
|
||
| if args.changed && args.staged { | ||
| return Err(CliDiagnostic::incompatible_arguments("changed", "staged")); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use crate::cli_options::CliOptions; | ||
| use crate::reporter::Report; | ||
| use crate::{CliDiagnostic, CliSession, VcsIntegration}; | ||
| use pgt_configuration::PartialConfiguration; | ||
|
|
||
| pub fn dblint( | ||
| mut session: CliSession, | ||
| cli_options: &CliOptions, | ||
| cli_configuration: Option<PartialConfiguration>, | ||
| ) -> Result<(), CliDiagnostic> { | ||
| let configuration = session.prepare_with_config(cli_options, cli_configuration)?; | ||
| session.setup_workspace(configuration, VcsIntegration::Disabled)?; | ||
|
|
||
| // TODO: Implement actual dblint logic here | ||
| let report = Report::new(vec![], std::time::Duration::new(0, 0), 0, None); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is clearly a peak MVP |
||
|
|
||
| session.report("dblint", cli_options, &report) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these seem to be dead, do we need them?
if not, let's remove the unnecessary complexity 👍🏻