From ae424ee9763f0b71ede86e996b4a7019b10ebc12 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 12 Apr 2023 14:54:28 -0400 Subject: [PATCH] refactor: `ProcState::build` -> `ProcState::from_flags` (#18672) --- cli/lsp/language_server.rs | 2 +- cli/lsp/testing/execution.rs | 2 +- cli/main.rs | 4 ++-- cli/proc_state.rs | 12 ++++++------ cli/standalone.rs | 2 +- cli/tools/bench.rs | 4 ++-- cli/tools/bundle.rs | 2 +- cli/tools/coverage/mod.rs | 2 +- cli/tools/doc.rs | 2 +- cli/tools/info.rs | 2 +- cli/tools/installer.rs | 2 +- cli/tools/repl/mod.rs | 2 +- cli/tools/run.rs | 13 ++++++------- cli/tools/standalone.rs | 2 +- cli/tools/task.rs | 2 +- cli/tools/test.rs | 4 ++-- cli/tools/upgrade.rs | 2 +- cli/tools/vendor/mod.rs | 2 +- 18 files changed, 31 insertions(+), 32 deletions(-) diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 85bff7fbff..a1e23894b9 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -182,7 +182,7 @@ impl LanguageServer { .into_iter() .map(|d| (d.specifier().clone(), d)) .collect::>(); - let ps = ProcState::from_options(Arc::new(cli_options)).await?; + let ps = ProcState::from_cli_options(Arc::new(cli_options)).await?; let mut inner_loader = ps.create_graph_loader(); let mut loader = crate::lsp::documents::OpenDocumentsGraphLoader { inner_loader: &mut inner_loader, diff --git a/cli/lsp/testing/execution.rs b/cli/lsp/testing/execution.rs index 44a0729de2..466c0d9423 100644 --- a/cli/lsp/testing/execution.rs +++ b/cli/lsp/testing/execution.rs @@ -259,7 +259,7 @@ impl TestRun { let args = self.get_args(); lsp_log!("Executing test run with arguments: {}", args.join(" ")); let flags = flags_from_vec(args.into_iter().map(String::from).collect())?; - let ps = proc_state::ProcState::build(flags).await?; + let ps = proc_state::ProcState::from_flags(flags).await?; // Various test files should not share the same permissions in terms of // `PermissionsContainer` - otherwise granting/revoking permissions in one // file would have impact on other files, which is undesirable. diff --git a/cli/main.rs b/cli/main.rs index 6d6199bdac..849e0979ca 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -69,13 +69,13 @@ async fn run_subcommand(flags: Flags) -> Result { tools::run::eval_command(flags, eval_flags).await } DenoSubcommand::Cache(cache_flags) => { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; ps.load_and_type_check_files(&cache_flags.files).await?; ps.cache_module_emits()?; Ok(0) } DenoSubcommand::Check(check_flags) => { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; ps.load_and_type_check_files(&check_flags.files).await?; Ok(0) } diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 659e2d1db5..2d4f181fab 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -112,17 +112,17 @@ impl Deref for ProcState { } impl ProcState { - pub async fn build(flags: Flags) -> Result { - Self::from_options(Arc::new(CliOptions::from_flags(flags)?)).await - } - - pub async fn from_options( + pub async fn from_cli_options( options: Arc, ) -> Result { Self::build_with_sender(options, None).await } - pub async fn build_for_file_watcher( + pub async fn from_flags(flags: Flags) -> Result { + Self::from_cli_options(Arc::new(CliOptions::from_flags(flags)?)).await + } + + pub async fn from_flags_for_file_watcher( flags: Flags, files_to_watch_sender: tokio::sync::mpsc::UnboundedSender>, ) -> Result { diff --git a/cli/standalone.rs b/cli/standalone.rs index cef8e5afd9..3ef0ad1320 100644 --- a/cli/standalone.rs +++ b/cli/standalone.rs @@ -307,7 +307,7 @@ pub async fn run( ) -> Result<(), AnyError> { let flags = metadata_to_flags(&metadata); let main_module = &metadata.entrypoint; - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let permissions = PermissionsContainer::new(Permissions::from_options( &metadata.permissions, )?); diff --git a/cli/tools/bench.rs b/cli/tools/bench.rs index 56ddb99fb0..c864c0221f 100644 --- a/cli/tools/bench.rs +++ b/cli/tools/bench.rs @@ -574,7 +574,7 @@ pub async fn run_benchmarks( cli_options: CliOptions, bench_options: BenchOptions, ) -> Result<(), AnyError> { - let ps = ProcState::from_options(Arc::new(cli_options)).await?; + let ps = ProcState::from_cli_options(Arc::new(cli_options)).await?; // Various bench files should not share the same permissions in terms of // `PermissionsContainer` - otherwise granting/revoking permissions in one // file would have impact on other files, which is undesirable. @@ -609,7 +609,7 @@ pub async fn run_benchmarks_with_watch( cli_options: CliOptions, bench_options: BenchOptions, ) -> Result<(), AnyError> { - let ps = ProcState::from_options(Arc::new(cli_options)).await?; + let ps = ProcState::from_cli_options(Arc::new(cli_options)).await?; // Various bench files should not share the same permissions in terms of // `PermissionsContainer` - otherwise granting/revoking permissions in one // file would have impact on other files, which is undesirable. diff --git a/cli/tools/bundle.rs b/cli/tools/bundle.rs index e5531d7e1a..77b4f8a461 100644 --- a/cli/tools/bundle.rs +++ b/cli/tools/bundle.rs @@ -41,7 +41,7 @@ pub async fn bundle( let module_specifier = &module_specifier; async move { log::debug!(">>>>> bundle START"); - let ps = ProcState::from_options(cli_options).await?; + let ps = ProcState::from_cli_options(cli_options).await?; let graph = create_graph_and_maybe_check(vec![module_specifier.clone()], &ps) .await?; diff --git a/cli/tools/coverage/mod.rs b/cli/tools/coverage/mod.rs index 7f25988114..87c9836006 100644 --- a/cli/tools/coverage/mod.rs +++ b/cli/tools/coverage/mod.rs @@ -621,7 +621,7 @@ pub async fn cover_files( return Err(generic_error("No matching coverage profiles found")); } - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let root_dir_url = ps.npm_resolver.root_dir_url(); let script_coverages = collect_coverages(coverage_flags.files)?; diff --git a/cli/tools/doc.rs b/cli/tools/doc.rs index 38553e9ffa..6b06918276 100644 --- a/cli/tools/doc.rs +++ b/cli/tools/doc.rs @@ -23,7 +23,7 @@ pub async fn print_docs( flags: Flags, doc_flags: DocFlags, ) -> Result<(), AnyError> { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let mut doc_nodes = match doc_flags.source_file { DocSourceFileFlag::Builtin => { diff --git a/cli/tools/info.rs b/cli/tools/info.rs index 3f11903ad8..a204c72457 100644 --- a/cli/tools/info.rs +++ b/cli/tools/info.rs @@ -33,7 +33,7 @@ use crate::proc_state::ProcState; use crate::util::checksum; pub async fn info(flags: Flags, info_flags: InfoFlags) -> Result<(), AnyError> { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; if let Some(specifier) = info_flags.file { let specifier = resolve_url_or_path(&specifier, ps.options.initial_cwd())?; let mut loader = ps.create_graph_loader(); diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs index 2578c38455..3bffbacada 100644 --- a/cli/tools/installer.rs +++ b/cli/tools/installer.rs @@ -233,7 +233,7 @@ pub async fn install_command( install_flags: InstallFlags, ) -> Result<(), AnyError> { // ensure the module is cached - ProcState::build(flags.clone()) + ProcState::from_flags(flags.clone()) .await? .load_and_type_check_files(&[install_flags.module_url.clone()]) .await?; diff --git a/cli/tools/repl/mod.rs b/cli/tools/repl/mod.rs index fcc31a764d..a6cc716373 100644 --- a/cli/tools/repl/mod.rs +++ b/cli/tools/repl/mod.rs @@ -80,7 +80,7 @@ async fn read_eval_file( } pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let main_module = ps.options.resolve_main_module()?; let mut worker = create_main_worker( &ps, diff --git a/cli/tools/run.rs b/cli/tools/run.rs index 007e0fb2a4..7f4b5c8f74 100644 --- a/cli/tools/run.rs +++ b/cli/tools/run.rs @@ -1,7 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use std::io::Read; -use std::sync::Arc; use deno_ast::MediaType; use deno_ast::ModuleSpecifier; @@ -35,7 +34,7 @@ To grant permissions, set them before the script argument. For example: // TODO(bartlomieju): actually I think it will also fail if there's an import // map specified and bare specifier is used on the command line - this should // probably call `ProcState::resolve` instead - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; // Run a background task that checks for available upgrades. If an earlier // run of this background task found a new version of Deno. @@ -56,7 +55,7 @@ To grant permissions, set them before the script argument. For example: } pub async fn run_from_stdin(flags: Flags) -> Result { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let main_module = ps.options.resolve_main_module()?; let mut worker = create_main_worker( @@ -90,10 +89,10 @@ pub async fn run_from_stdin(flags: Flags) -> Result { // TODO(bartlomieju): this function is not handling `exit_code` set by the runtime // code properly. async fn run_with_watch(flags: Flags) -> Result { - let flags = Arc::new(flags); let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); let mut ps = - ProcState::build_for_file_watcher((*flags).clone(), sender.clone()).await?; + ProcState::from_flags_for_file_watcher(flags, sender.clone()).await?; + let clear_screen = !ps.options.no_clear_screen(); let main_module = ps.options.resolve_main_module()?; let operation = |main_module: ModuleSpecifier| { @@ -116,7 +115,7 @@ async fn run_with_watch(flags: Flags) -> Result { main_module, util::file_watcher::PrintConfig { job_name: "Process".to_string(), - clear_screen: !flags.no_clear_screen, + clear_screen, }, ) .await?; @@ -128,7 +127,7 @@ pub async fn eval_command( flags: Flags, eval_flags: EvalFlags, ) -> Result { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let main_module = ps.options.resolve_main_module()?; let permissions = PermissionsContainer::new(Permissions::from_options( &ps.options.permissions_options(), diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index 31f472b789..29899f0d82 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -38,7 +38,7 @@ pub async fn compile( flags: Flags, compile_flags: CompileFlags, ) -> Result<(), AnyError> { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let module_specifier = ps.options.resolve_main_module()?; let module_roots = { let mut vec = Vec::with_capacity(compile_flags.include.len() + 1); diff --git a/cli/tools/task.rs b/cli/tools/task.rs index 33595ad2eb..9e82f14d9d 100644 --- a/cli/tools/task.rs +++ b/cli/tools/task.rs @@ -23,7 +23,7 @@ pub async fn execute_script( flags: Flags, task_flags: TaskFlags, ) -> Result { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let tasks_config = ps.options.resolve_tasks_config()?; let maybe_package_json = ps.options.maybe_package_json(); let package_json_scripts = maybe_package_json diff --git a/cli/tools/test.rs b/cli/tools/test.rs index fa9798779c..87cb3c5e3b 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -1531,7 +1531,7 @@ pub async fn run_tests( cli_options: CliOptions, test_options: TestOptions, ) -> Result<(), AnyError> { - let ps = ProcState::from_options(Arc::new(cli_options)).await?; + let ps = ProcState::from_cli_options(Arc::new(cli_options)).await?; // Various test files should not share the same permissions in terms of // `PermissionsContainer` - otherwise granting/revoking permissions in one // file would have impact on other files, which is undesirable. @@ -1575,7 +1575,7 @@ pub async fn run_tests_with_watch( cli_options: CliOptions, test_options: TestOptions, ) -> Result<(), AnyError> { - let ps = ProcState::from_options(Arc::new(cli_options)).await?; + let ps = ProcState::from_cli_options(Arc::new(cli_options)).await?; // Various test files should not share the same permissions in terms of // `PermissionsContainer` - otherwise granting/revoking permissions in one // file would have impact on other files, which is undesirable. diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs index e1f86dccfb..310d60d994 100644 --- a/cli/tools/upgrade.rs +++ b/cli/tools/upgrade.rs @@ -263,7 +263,7 @@ pub async fn upgrade( flags: Flags, upgrade_flags: UpgradeFlags, ) -> Result<(), AnyError> { - let ps = ProcState::build(flags).await?; + let ps = ProcState::from_flags(flags).await?; let current_exe_path = std::env::current_exe()?; let metadata = fs::metadata(¤t_exe_path)?; let permissions = metadata.permissions(); diff --git a/cli/tools/vendor/mod.rs b/cli/tools/vendor/mod.rs index 3198348e37..86af7d4150 100644 --- a/cli/tools/vendor/mod.rs +++ b/cli/tools/vendor/mod.rs @@ -42,7 +42,7 @@ pub async fn vendor( let output_dir = resolve_from_cwd(&raw_output_dir)?; validate_output_dir(&output_dir, &vendor_flags)?; validate_options(&mut cli_options, &output_dir)?; - let ps = ProcState::from_options(Arc::new(cli_options)).await?; + let ps = ProcState::from_cli_options(Arc::new(cli_options)).await?; let graph = create_graph(&ps, &vendor_flags).await?; let vendored_count = build::build( graph,