diff --git a/src/analysis/installers/zip.rs b/src/analysis/installers/zip.rs index d45b1199b..96959087e 100644 --- a/src/analysis/installers/zip.rs +++ b/src/analysis/installers/zip.rs @@ -83,31 +83,11 @@ impl Zip { .count() == 1 { - let chosen_file_name = &possible_installer_files[0]; nested_installer_files = BTreeSet::from([NestedInstallerFiles { - relative_file_path: chosen_file_name.clone(), + relative_file_path: possible_installer_files[0].clone(), portable_command_alias: None, }]); - if let Ok(mut chosen_file) = zip.by_name(chosen_file_name.as_str()) { - let mut temp_file = tempfile::tempfile()?; - io::copy(&mut chosen_file, &mut temp_file)?; - temp_file.seek(SeekFrom::Start(0))?; - let file_analyzer = Analyzer::new(&mut temp_file, chosen_file_name.as_str())?; - installers = Some( - file_analyzer - .installers - .into_iter() - .map(|installer| Installer { - r#type: Some(InstallerType::Zip), - nested_installer_type: installer - .r#type - .and_then(|installer_type| installer_type.try_into().ok()), - nested_installer_files: nested_installer_files.clone(), - ..installer - }) - .collect::>(), - ); - } + installers = analyze_nested_files(&mut zip, &nested_installer_files)?; } Ok(Self { @@ -123,6 +103,13 @@ impl Zip { }) } + pub fn analyze_nested_files( + &mut self, + nested_installer_files: &BTreeSet, + ) -> Result>> { + analyze_nested_files(&mut self.archive, nested_installer_files) + } + pub fn prompt(&mut self) -> Result<()> { if !&self.possible_installer_files.is_empty() { let chosen = MultiSelect::new( @@ -178,3 +165,33 @@ impl Zip { Ok(()) } } + +fn analyze_nested_files( + archive: &mut ZipArchive, + nested_installer_files: &BTreeSet, +) -> Result>> { + let Some(first_file) = nested_installer_files.first() else { + return Ok(None); + }; + let Ok(mut nested_file) = archive.by_name(first_file.relative_file_path.as_str()) else { + return Ok(None); + }; + let mut temp_file = tempfile::tempfile()?; + io::copy(&mut nested_file, &mut temp_file)?; + temp_file.seek(SeekFrom::Start(0))?; + let file_analyzer = Analyzer::new(&mut temp_file, first_file.relative_file_path.as_str())?; + Ok(Some( + file_analyzer + .installers + .into_iter() + .map(|installer| Installer { + r#type: Some(InstallerType::Zip), + nested_installer_type: installer + .r#type + .and_then(|installer_type| installer_type.try_into().ok()), + nested_installer_files: nested_installer_files.clone(), + ..installer + }) + .collect(), + )) +} diff --git a/src/commands/new_version.rs b/src/commands/new_version.rs index ad05a8fc9..6e6cd95d6 100644 --- a/src/commands/new_version.rs +++ b/src/commands/new_version.rs @@ -222,7 +222,7 @@ impl NewVersion { let downloader = Downloader::new_with_concurrent(self.concurrent_downloads)?; let mut files = downloader.download(urls.iter().cloned()).await?; - let mut download_results = files.analyze().await?; + let mut download_results = files.analyze(|_, _| BTreeSet::new()).await?; let mut installers = Vec::new(); for analyzer in &mut download_results.values_mut() { diff --git a/src/commands/update_version.rs b/src/commands/update_version.rs index 4704f578a..f33aeed14 100644 --- a/src/commands/update_version.rs +++ b/src/commands/update_version.rs @@ -11,13 +11,14 @@ use clap::Parser; use color_eyre::eyre::{Error, Result, bail}; use futures_util::TryFutureExt; use indicatif::ProgressBar; +use itertools::Itertools; use secrecy::SecretString; use strsim::levenshtein; use tokio::try_join; use tracing::warn; use winget_types::{ InstallerManifest, PackageIdentifier, PackageVersion, - installer::{Installer, InstallerType, NestedInstallerFiles}, + installer::{Architecture, Installer, InstallerType, NestedInstallerFiles}, url::{DecodedUrl, ReleaseNotesUrl}, }; @@ -153,7 +154,19 @@ impl UpdateVersion { let manifests = package.manifests_mut().unwrap(); - let download_results = files.analyze().await?; + let download_results = files + .analyze(|architecture, possible_installer_files| { + previous_zip_installer(&manifests.installer, architecture) + .map(|previous_installer| { + previous_nested_files( + previous_installer, + &manifests.installer, + possible_installer_files, + ) + }) + .unwrap_or_default() + }) + .await?; let installers = download_results .into_values() @@ -386,7 +399,7 @@ fn suggest_release_notes_url( } /// Fills in the nested installer of a zip installer that couldn't be detected, such as when the zip -/// contains more than one executable, from the previous installer of the same architecture. +/// contains more than one executable, from the previous zip installer. fn carry_over_nested_installer( installer: &mut Installer, previous_manifest: &InstallerManifest, @@ -397,29 +410,53 @@ fn carry_over_nested_installer( return; } - let Some(previous_installer) = previous_manifest.installers.iter().find(|previous| { - previous.architecture == installer.architecture - && previous.r#type.or(previous_manifest.r#type) == Some(InstallerType::Zip) - }) else { + let Some(previous_installer) = + previous_zip_installer(previous_manifest, Some(installer.architecture)) + else { return; }; + installer.nested_installer_files = previous_nested_files( + previous_installer, + previous_manifest, + possible_installer_files, + ); + + if installer.nested_installer_type.is_none() { + installer.nested_installer_type = previous_installer + .nested_installer_type + .or(previous_manifest.nested_installer_type); + } +} + +fn previous_zip_installer( + previous_manifest: &InstallerManifest, + architecture: Option, +) -> Option<&Installer> { + let zip_installers = previous_manifest.installers.iter().filter(|previous| { + previous.r#type.or(previous_manifest.r#type) == Some(InstallerType::Zip) + }); + zip_installers + .clone() + .find(|previous| Some(previous.architecture) == architecture) + .or_else(|| zip_installers.exactly_one().ok()) +} + +fn previous_nested_files( + previous_installer: &Installer, + previous_manifest: &InstallerManifest, + possible_installer_files: &[Utf8PathBuf], +) -> BTreeSet { let previous_nested_files = if previous_installer.nested_installer_files.is_empty() { &previous_manifest.nested_installer_files } else { &previous_installer.nested_installer_files }; - installer.nested_installer_files = previous_nested_files + previous_nested_files .iter() .map(|nested_file| fix_relative_path(nested_file, possible_installer_files)) - .collect(); - - if installer.nested_installer_type.is_none() { - installer.nested_installer_type = previous_installer - .nested_installer_type - .or(previous_manifest.nested_installer_type); - } + .collect() } /// Points a nested file at the closest matching file in the new zip, as paths often contain the @@ -584,6 +621,44 @@ mod tests { ); } + #[test] + fn carries_over_only_previous_zip_installer_of_other_architecture() { + let previous = InstallerManifest { + nested_installer_type: InstallerType::Msi.try_into().ok(), + nested_installer_files: nested_files(&["Setup.msi"]), + installers: vec![zip_installer(Architecture::X86)], + ..previous_manifest() + }; + let mut installer = zip_installer(Architecture::Neutral); + + carry_over_nested_installer( + &mut installer, + &previous, + &[ + Utf8PathBuf::from("Setup.msi"), + Utf8PathBuf::from("setup.exe"), + ], + ); + + assert_eq!( + installer.nested_installer_files, + nested_files(&["Setup.msi"]) + ); + assert_eq!( + installer.nested_installer_type, + previous.nested_installer_type + ); + } + + #[test] + fn ignores_previous_zip_installers_of_other_architectures() { + let mut installer = zip_installer(Architecture::X86); + + carry_over_nested_installer(&mut installer, &previous_manifest(), &[]); + + assert!(installer.nested_installer_files.is_empty()); + } + #[test] fn keeps_detected_nested_files() { let mut installer = Installer { diff --git a/src/download/downloads.rs b/src/download/downloads.rs index 4d6b7b727..991fbdeac 100644 --- a/src/download/downloads.rs +++ b/src/download/downloads.rs @@ -1,15 +1,19 @@ use std::{ - collections::HashMap, + collections::{BTreeSet, HashMap}, io::{Read, Seek}, mem, num::NonZeroUsize, thread, }; +use camino::Utf8PathBuf; use color_eyre::Result; use futures_util::{StreamExt, TryStreamExt, stream}; use tracing::debug; -use winget_types::{installer::Architecture, url::DecodedUrl}; +use winget_types::{ + installer::{Architecture, NestedInstallerFiles}, + url::DecodedUrl, +}; use super::DownloadedFile; use crate::analysis::Analyzer; @@ -27,7 +31,14 @@ impl Downloads { Self(downloads.into_iter().collect()) } - pub async fn analyze(&mut self) -> Result>> { + pub async fn analyze( + &mut self, + previous_nested_files: F, + ) -> Result>> + where + F: Fn(Option, &[Utf8PathBuf]) -> BTreeSet, + { + let previous_nested_files = &previous_nested_files; stream::iter(self.0.iter_mut().map( |DownloadedFile { file, @@ -41,6 +52,18 @@ impl Downloads { .url() .override_architecture() .or_else(|| Architecture::from_url(download.url().as_str())); + if let Some(zip) = &mut file_analyzer.zip + && file_analyzer + .installers + .iter() + .all(|installer| installer.nested_installer_files.is_empty()) + && let Some(installers) = zip.analyze_nested_files(&previous_nested_files( + architecture, + &zip.possible_installer_files, + ))? + { + file_analyzer.installers = installers; + } for installer in &mut file_analyzer.installers { if let Some(architecture) = architecture { installer.architecture = architecture;