Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions src/analysis/installers/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,11 @@ impl<R: Read + Seek> Zip<R> {
.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::<Vec<_>>(),
);
}
installers = analyze_nested_files(&mut zip, &nested_installer_files)?;
}

Ok(Self {
Expand All @@ -123,6 +103,13 @@ impl<R: Read + Seek> Zip<R> {
})
}

pub fn analyze_nested_files(
&mut self,
nested_installer_files: &BTreeSet<NestedInstallerFiles>,
) -> Result<Option<Vec<Installer>>> {
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(
Expand Down Expand Up @@ -178,3 +165,33 @@ impl<R: Read + Seek> Zip<R> {
Ok(())
}
}

fn analyze_nested_files<R: Read + Seek>(
archive: &mut ZipArchive<R>,
nested_installer_files: &BTreeSet<NestedInstallerFiles>,
) -> Result<Option<Vec<Installer>>> {
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(),
))
}
2 changes: 1 addition & 1 deletion src/commands/new_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
105 changes: 90 additions & 15 deletions src/commands/update_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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<Architecture>,
) -> 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<NestedInstallerFiles> {
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
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 26 additions & 3 deletions src/download/downloads.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,7 +31,14 @@ impl Downloads {
Self(downloads.into_iter().collect())
}

pub async fn analyze(&mut self) -> Result<HashMap<DecodedUrl, Analyzer<'_, impl Read + Seek>>> {
pub async fn analyze<F>(
&mut self,
previous_nested_files: F,
) -> Result<HashMap<DecodedUrl, Analyzer<'_, impl Read + Seek>>>
where
F: Fn(Option<Architecture>, &[Utf8PathBuf]) -> BTreeSet<NestedInstallerFiles>,
{
let previous_nested_files = &previous_nested_files;
stream::iter(self.0.iter_mut().map(
|DownloadedFile {
file,
Expand All @@ -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;
Expand Down
Loading