Skip to content
Open
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
18 changes: 9 additions & 9 deletions src/analysis/installers/exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
pe::{PE, VSVersionInfo},
squirrel::SquirrelError,
},
traits::IntoWingetArchitecture,
traits::{AsciiExt, IntoWingetArchitecture},
};

const ORIGINAL_FILENAME: &str = "OriginalFilename";
Expand Down Expand Up @@ -127,14 +127,14 @@ impl Exe {
r#type: ExeType::Generic(Box::new(Installer {
architecture: pe.winget_architecture(),
r#type: if string_table.is_some_and(|mut table| {
let original_filename = table.swap_remove(ORIGINAL_FILENAME);
let file_description = table.swap_remove(FILE_DESCRIPTION);

BASIC_INSTALLER_KEYWORDS.iter().any(|keyword| {
original_filename.is_some_and(|filename| filename.contains(keyword))
|| file_description
.is_some_and(|description| description.contains(keyword))
})
[ORIGINAL_FILENAME, FILE_DESCRIPTION]
.into_iter()
.filter_map(|key| table.swap_remove(key))
.any(|value| {
BASIC_INSTALLER_KEYWORDS
.iter()
.any(|keyword| value.contains_ignore_ascii_case(keyword))
})
}) {
Some(InstallerType::Exe)
} else {
Expand Down
205 changes: 180 additions & 25 deletions src/commands/update_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl UpdateVersion {
&manifests.installer,
&possible_installer_files,
);
carry_over_installer_type(installer, &manifests.installer);
}
installers
})
Expand Down Expand Up @@ -399,14 +400,14 @@ 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 zip installer.
/// contains more than one executable, from the previous zip installer. A nested exe installer or
/// portable keeps the previous choice between the two, as [`carry_over_installer_type`] does.
fn carry_over_nested_installer(
installer: &mut Installer,
previous_manifest: &InstallerManifest,
possible_installer_files: &[Utf8PathBuf],
) {
if installer.r#type != Some(InstallerType::Zip) || !installer.nested_installer_files.is_empty()
{
if installer.r#type != Some(InstallerType::Zip) {
return;
}

Expand All @@ -416,30 +417,74 @@ fn carry_over_nested_installer(
return;
};

installer.nested_installer_files = previous_nested_files(
previous_installer,
previous_manifest,
possible_installer_files,
);
let previous_nested_type = previous_installer
.nested_installer_type
.or(previous_manifest.nested_installer_type);

if installer.nested_installer_type.is_none() {
installer.nested_installer_type = previous_installer
.nested_installer_type
.or(previous_manifest.nested_installer_type);
if installer.nested_installer_files.is_empty() {
installer.nested_installer_files = previous_nested_files(
previous_installer,
previous_manifest,
possible_installer_files,
);
installer.nested_installer_type = installer.nested_installer_type.or(previous_nested_type);
}

if is_exe_or_portable(installer.nested_installer_type.map(InstallerType::from))
&& is_exe_or_portable(previous_nested_type.map(InstallerType::from))
{
installer.nested_installer_type = previous_nested_type;
}
}

/// Keeps the previous choice between an exe installer and a portable, as a generic exe can only be
/// told apart from a portable by guessing from its version info.
fn carry_over_installer_type(installer: &mut Installer, previous_manifest: &InstallerManifest) {
if is_exe_or_portable(installer.r#type)
&& let Some(previous_installer) = previous_installer(
previous_manifest,
Some(installer.architecture),
is_exe_or_portable,
)
{
installer.r#type = previous_installer.r#type.or(previous_manifest.r#type);
}
}

const fn is_exe_or_portable(installer_type: Option<InstallerType>) -> bool {
matches!(
installer_type,
Some(InstallerType::Exe | InstallerType::Portable)
)
}

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
previous_installer(previous_manifest, architecture, |installer_type| {
installer_type == Some(InstallerType::Zip)
})
}

/// Finds the previous installer of the same architecture whose type matches, or the only one whose
/// type matches.
fn previous_installer<F>(
previous_manifest: &InstallerManifest,
architecture: Option<Architecture>,
matches_type: F,
) -> Option<&Installer>
where
F: Fn(Option<InstallerType>) -> bool,
{
let installers = previous_manifest
.installers
.iter()
.filter(|previous| matches_type(previous.r#type.or(previous_manifest.r#type)));
installers
.clone()
.find(|previous| Some(previous.architecture) == architecture)
.or_else(|| zip_installers.exactly_one().ok())
.or_else(|| installers.exactly_one().ok())
}

fn previous_nested_files(
Expand Down Expand Up @@ -493,7 +538,10 @@ mod tests {
installer::{Architecture, Installer, InstallerType, NestedInstallerFiles},
};

use super::{carry_over_nested_installer, fix_relative_path, suggest_release_notes_url};
use super::{
carry_over_installer_type, carry_over_nested_installer, fix_relative_path,
suggest_release_notes_url,
};

#[rstest]
#[case(
Expand Down Expand Up @@ -548,14 +596,18 @@ mod tests {
.collect()
}

fn zip_installer(architecture: Architecture) -> Installer {
fn typed_installer(architecture: Architecture, r#type: InstallerType) -> Installer {
Installer {
architecture,
r#type: Some(InstallerType::Zip),
r#type: Some(r#type),
..Installer::default()
}
}

fn zip_installer(architecture: Architecture) -> Installer {
typed_installer(architecture, InstallerType::Zip)
}

fn previous_manifest() -> InstallerManifest {
InstallerManifest {
r#type: Some(InstallerType::Zip),
Expand Down Expand Up @@ -676,18 +728,121 @@ mod tests {

#[test]
fn ignores_non_zip_installers() {
let mut installer = Installer {
architecture: Architecture::X64,
r#type: Some(InstallerType::Wix),
..Installer::default()
};
let mut installer = typed_installer(Architecture::X64, InstallerType::Wix);

carry_over_nested_installer(&mut installer, &previous_manifest(), &[]);

assert!(installer.nested_installer_files.is_empty());
assert_eq!(installer.nested_installer_type, None);
}

#[rstest]
#[case::root_exe(
Some(InstallerType::Exe),
None,
InstallerType::Portable,
InstallerType::Exe
)]
#[case::installer_portable(
None,
Some(InstallerType::Portable),
InstallerType::Exe,
InstallerType::Portable
)]
#[case::installer_overrides_root(
Some(InstallerType::Portable),
Some(InstallerType::Exe),
InstallerType::Portable,
InstallerType::Exe
)]
#[case::previous_inno(
Some(InstallerType::Inno),
None,
InstallerType::Portable,
InstallerType::Portable
)]
#[case::previous_msi(None, Some(InstallerType::Msi), InstallerType::Exe, InstallerType::Exe)]
#[case::detected_nullsoft(
Some(InstallerType::Exe),
None,
InstallerType::Nullsoft,
InstallerType::Nullsoft
)]
fn carries_over_exe_or_portable_type(
#[case] previous_root_type: Option<InstallerType>,
#[case] previous_installer_type: Option<InstallerType>,
#[case] analyzed_type: InstallerType,
#[case] expected: InstallerType,
) {
let previous = InstallerManifest {
r#type: previous_root_type,
installers: vec![Installer {
architecture: Architecture::X64,
r#type: previous_installer_type,
..Installer::default()
}],
..InstallerManifest::default()
};
let mut installer = typed_installer(Architecture::X64, analyzed_type);

carry_over_installer_type(&mut installer, &previous);

assert_eq!(installer.r#type, Some(expected));
}

#[test]
fn carries_over_exe_or_portable_type_of_same_architecture() {
let previous = InstallerManifest {
installers: vec![
typed_installer(Architecture::X64, InstallerType::Exe),
typed_installer(Architecture::X86, InstallerType::Portable),
],
..InstallerManifest::default()
};
let mut x64 = typed_installer(Architecture::X64, InstallerType::Portable);
let mut x86 = typed_installer(Architecture::X86, InstallerType::Exe);
let mut arm64 = typed_installer(Architecture::Arm64, InstallerType::Portable);

carry_over_installer_type(&mut x64, &previous);
carry_over_installer_type(&mut x86, &previous);
carry_over_installer_type(&mut arm64, &previous);

assert_eq!(x64.r#type, Some(InstallerType::Exe));
assert_eq!(x86.r#type, Some(InstallerType::Portable));
assert_eq!(arm64.r#type, Some(InstallerType::Portable));
}

#[rstest]
#[case::previous_exe(InstallerType::Exe, InstallerType::Portable, InstallerType::Exe)]
#[case::previous_msi(InstallerType::Msi, InstallerType::Portable, InstallerType::Portable)]
#[case::detected_inno(InstallerType::Exe, InstallerType::Inno, InstallerType::Inno)]
fn carries_over_exe_or_portable_nested_type(
#[case] previous_nested_type: InstallerType,
#[case] analyzed_nested_type: InstallerType,
#[case] expected: InstallerType,
) {
let previous = InstallerManifest {
nested_installer_type: previous_nested_type.try_into().ok(),
..previous_manifest()
};
let mut installer = Installer {
nested_installer_type: analyzed_nested_type.try_into().ok(),
nested_installer_files: nested_files(&["app-x64/app.exe"]),
..zip_installer(Architecture::X64)
};

carry_over_nested_installer(&mut installer, &previous, &[]);

assert_eq!(
installer.nested_installer_type.map(InstallerType::from),
Some(expected)
);
assert_eq!(
installer.nested_installer_files,
nested_files(&["app-x64/app.exe"])
);
}

#[test]
fn fixes_versioned_relative_path() {
let possible_files = [
Expand Down
Loading