From 70d07feb70ceab804f7296be899304b8300756be Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Sun, 20 Oct 2024 18:13:08 +0200 Subject: [PATCH] fix: sync scripts repo in background --- CHANGELOG.md | 1 + src/cli/commands/install.rs | 13 +++++++++++-- src/cli/commands/mod.rs | 18 ++++++++++++++---- src/cli/commands/run.rs | 16 +++++++++++++--- src/cli/commands/update.rs | 9 +++++++-- src/main.rs | 34 +++++++++++++++++++++++----------- 6 files changed, 69 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f95641e..ded06bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Use updated aliases when reusing lockfile dependencies by @daimond113 - Listen for device flow completion without requiring pressing enter by @daimond113 +- Sync scripts repo in background by @daimond113 ### Performance - Clone dependency repos shallowly by @daimond113 diff --git a/src/cli/commands/install.rs b/src/cli/commands/install.rs index 5861793..f22141f 100644 --- a/src/cli/commands/install.rs +++ b/src/cli/commands/install.rs @@ -10,7 +10,10 @@ use pesde::{ manifest::{target::TargetKind, DependencyType}, Project, MANIFEST_FILE_NAME, }; -use std::collections::{BTreeSet, HashSet}; +use std::{ + collections::{BTreeSet, HashSet}, + thread::JoinHandle, +}; #[derive(Debug, Args, Copy, Clone)] pub struct InstallCommand { @@ -93,6 +96,7 @@ impl InstallCommand { project: Project, multi: MultiProgress, reqwest: reqwest::blocking::Client, + update_task: &mut Option>, ) -> anyhow::Result<()> { let mut refreshed_sources = HashSet::new(); @@ -182,6 +186,11 @@ impl InstallCommand { .dependency_graph(old_graph.as_ref(), &mut refreshed_sources) .context("failed to build dependency graph")?; + if let Some(task) = update_task.take() { + log::debug!("waiting for update task to finish"); + task.join().expect("failed to join update task"); + } + let downloaded_graph = download_graph( &project, &mut refreshed_sources, @@ -274,7 +283,7 @@ impl InstallCommand { graph: downloaded_graph, workspace: run_on_workspace_members(&project, |project| { - self.run(project, multi.clone(), reqwest.clone()) + self.run(project, multi.clone(), reqwest.clone(), &mut None) })?, }) .context("failed to write lockfile")?; diff --git a/src/cli/commands/mod.rs b/src/cli/commands/mod.rs index e650ace..feacfb8 100644 --- a/src/cli/commands/mod.rs +++ b/src/cli/commands/mod.rs @@ -1,5 +1,6 @@ use indicatif::MultiProgress; use pesde::Project; +use std::thread::JoinHandle; mod add; mod auth; @@ -77,13 +78,16 @@ impl Subcommand { project: Project, multi: MultiProgress, reqwest: reqwest::blocking::Client, + update_task: JoinHandle<()>, ) -> anyhow::Result<()> { - match self { + let mut update_task = Some(update_task); + + let res = match self { Subcommand::Auth(auth) => auth.run(project, reqwest), Subcommand::Config(config) => config.run(), Subcommand::Init(init) => init.run(project), - Subcommand::Run(run) => run.run(project), - Subcommand::Install(install) => install.run(project, multi, reqwest), + Subcommand::Run(run) => run.run(project, &mut update_task), + Subcommand::Install(install) => install.run(project, multi, reqwest, &mut update_task), Subcommand::Publish(publish) => publish.run(project, reqwest), #[cfg(feature = "version-management")] Subcommand::SelfInstall(self_install) => self_install.run(), @@ -94,9 +98,15 @@ impl Subcommand { #[cfg(feature = "version-management")] Subcommand::SelfUpgrade(self_upgrade) => self_upgrade.run(reqwest), Subcommand::Add(add) => add.run(project), - Subcommand::Update(update) => update.run(project, multi, reqwest), + Subcommand::Update(update) => update.run(project, multi, reqwest, &mut update_task), Subcommand::Outdated(outdated) => outdated.run(project), Subcommand::Execute(execute) => execute.run(project, reqwest), + }; + + if let Some(handle) = update_task.take() { + handle.join().expect("failed to join update task"); } + + res } } diff --git a/src/cli/commands/run.rs b/src/cli/commands/run.rs index fd2c3ea..c07370d 100644 --- a/src/cli/commands/run.rs +++ b/src/cli/commands/run.rs @@ -8,7 +8,9 @@ use pesde::{ Project, PACKAGES_CONTAINER_NAME, }; use relative_path::RelativePathBuf; -use std::{env::current_dir, ffi::OsString, io::Write, path::PathBuf, process::Command}; +use std::{ + env::current_dir, ffi::OsString, io::Write, path::PathBuf, process::Command, thread::JoinHandle, +}; #[derive(Debug, Args)] pub struct RunCommand { @@ -22,8 +24,16 @@ pub struct RunCommand { } impl RunCommand { - pub fn run(self, project: Project) -> anyhow::Result<()> { - let run = |path: PathBuf| { + pub fn run( + self, + project: Project, + update_task: &mut Option>, + ) -> anyhow::Result<()> { + let mut run = |path: PathBuf| { + if let Some(handle) = update_task.take() { + handle.join().expect("failed to join update task"); + } + let mut caller = tempfile::NamedTempFile::new().expect("failed to create tempfile"); caller .write_all( diff --git a/src/cli/commands/update.rs b/src/cli/commands/update.rs index fd139b9..5ac1b42 100644 --- a/src/cli/commands/update.rs +++ b/src/cli/commands/update.rs @@ -4,7 +4,7 @@ use clap::Args; use colored::Colorize; use indicatif::MultiProgress; use pesde::{lockfile::Lockfile, Project}; -use std::collections::HashSet; +use std::{collections::HashSet, thread::JoinHandle}; #[derive(Debug, Args, Copy, Clone)] pub struct UpdateCommand { @@ -19,6 +19,7 @@ impl UpdateCommand { project: Project, multi: MultiProgress, reqwest: reqwest::blocking::Client, + update_task: &mut Option>, ) -> anyhow::Result<()> { let mut refreshed_sources = HashSet::new(); @@ -37,6 +38,10 @@ impl UpdateCommand { .dependency_graph(None, &mut refreshed_sources) .context("failed to build dependency graph")?; + if let Some(handle) = update_task.take() { + handle.join().expect("failed to join update task"); + } + project .write_lockfile(Lockfile { name: manifest.name, @@ -58,7 +63,7 @@ impl UpdateCommand { )?, workspace: run_on_workspace_members(&project, |project| { - self.run(project, multi.clone(), reqwest.clone()) + self.run(project, multi.clone(), reqwest.clone(), &mut None) })?, }) .context("failed to write lockfile")?; diff --git a/src/main.rs b/src/main.rs index bc93952..1aafa15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +#[cfg(feature = "version-management")] +use crate::cli::version::{ + check_for_updates, current_version, get_or_download_version, max_installed_version, +}; +use crate::cli::{auth::get_tokens, home_dir, repos::update_repo_dependencies, HOME_DIR}; use anyhow::Context; use clap::Parser; use colored::Colorize; @@ -8,14 +13,9 @@ use std::{ collections::HashSet, fs::create_dir_all, path::{Path, PathBuf}, + thread::spawn, }; -#[cfg(feature = "version-management")] -use crate::cli::version::{ - check_for_updates, current_version, get_or_download_version, max_installed_version, -}; -use crate::cli::{auth::get_tokens, home_dir, repos::update_repo_dependencies, HOME_DIR}; - mod cli; pub mod util; @@ -257,12 +257,24 @@ fn run() -> anyhow::Result<()> { display_err(check_for_updates(&reqwest), " while checking for updates"); } - display_err( - update_repo_dependencies(&project), - " while updating repository dependencies", - ); + let project_2 = project.clone(); + let update_task = spawn(move || { + display_err( + update_repo_dependencies(&project_2), + " while updating repository dependencies", + ); + }); - Cli::parse().subcommand.run(project, multi, reqwest) + let cli = match Cli::try_parse() { + Ok(cli) => cli, + Err(err) => { + let _ = err.print(); + update_task.join().expect("failed to join update task"); + std::process::exit(err.exit_code()); + } + }; + + cli.subcommand.run(project, multi, reqwest, update_task) } fn display_err(result: anyhow::Result<()>, prefix: &str) {