Skip to content

Commit

Permalink
installer: add support for 7Zip archives (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmatteini committed Oct 27, 2024
1 parent 18e5628 commit dd53499
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/installer/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ fn file_type_for(file: &FileInfo) -> Option<FileType> {
if file_name.ends_with(".zip") {
return Some(FileType::ZipArchive);
}
if file_name.ends_with(".7z") {
return Some(FileType::SevenZipArchive);
}
if is_elf_file(&file.path)
|| Path::new(&file_name).extension().is_none()
|| file_name.ends_with(".appimage")
Expand Down Expand Up @@ -142,6 +145,7 @@ mod tests {
#[test_case("file.exe", FileType::ExecutableFile)]
#[test_case("file", FileType::ExecutableFile)]
#[test_case("file.AppImage", FileType::ExecutableFile)]
#[test_case("file.7z", FileType::SevenZipArchive)]
fn supported_file(file_name: &str, expected_file_type: FileType) {
let file_info = any_file_info(file_name);
let result = validate_file(file_info);
Expand Down
3 changes: 2 additions & 1 deletion src/installer/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::installer::executable::Executable;
use crate::installer::executable_file_installer::ExecutableFileInstaller;
use crate::installer::file::{validate_file, Compression, FileInfo, FileType, SupportedFileInfo};
use crate::installer::result::InstallerResult;
use crate::installer::seven_zip_archive_installer::SevenZipArchiveInstaller;
use crate::installer::tar_archive_installer::TarArchiveInstaller;
use crate::installer::zip_archive_installer::ZipArchiveInstaller;
use std::path::Path;
Expand Down Expand Up @@ -38,7 +39,7 @@ fn find_installer_for(
FileType::TarArchive(Compression::Xz) => TarArchiveInstaller::xz,
FileType::TarArchive(Compression::Bz2) => TarArchiveInstaller::bz2,
FileType::ZipArchive => ZipArchiveInstaller::run,
FileType::SevenZipArchive => todo!(),
FileType::SevenZipArchive => SevenZipArchiveInstaller::run,
FileType::CompressedFile(Compression::Gz) => CompressedFileInstaller::gz,
FileType::CompressedFile(Compression::Xz) => CompressedFileInstaller::xz,
FileType::CompressedFile(Compression::Bz2) => CompressedFileInstaller::bz2,
Expand Down
1 change: 1 addition & 0 deletions src/installer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod executable_file_installer;
mod file;
mod install;
mod result;
mod seven_zip_archive_installer;
mod tar_archive_installer;
mod zip_archive_installer;

Expand Down
33 changes: 33 additions & 0 deletions src/installer/seven_zip_archive_installer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::installer::archive_installer::ArchiveInstaller;
use crate::installer::command::exec_command;
use crate::installer::destination::Destination;
use crate::installer::error::InstallError;
use crate::installer::executable::Executable;
use crate::installer::file::SupportedFileInfo;
use crate::installer::result::InstallerResult;
use std::path::Path;
use std::process::Command;

const _7Z: &str = "7z";

pub struct SevenZipArchiveInstaller;

impl SevenZipArchiveInstaller {
pub fn run(
file_info: SupportedFileInfo,
destination: Destination,
executable: &Executable,
) -> InstallerResult {
ArchiveInstaller::run(Self::extract_archive, file_info, destination, executable)
}

fn extract_archive(source: &Path, temp_dir: &Path) -> Result<(), InstallError> {
exec_command(
_7Z,
Command::new(_7Z)
.arg("x")
.arg(source)
.arg(format!("-o{}", temp_dir.display())),
)
}
}
2 changes: 2 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod install {
#[test_case("helloworld.tar.bz2", "helloworld"; "tar bzip2")]
#[test_case("helloworld.tar.xz", "helloworld"; "tar xz")]
#[test_case("helloworld.zip", "helloworld"; "zip")]
#[test_case("helloworld.7z", "helloworld"; "7zip")]
#[test_case("helloworld-compressed-unix.gz", "helloworld-compressed-unix"; "gzip")]
#[test_case("helloworld-compressed-unix.bz2", "helloworld-compressed-unix"; "bzip2")]
#[test_case("helloworld-compressed-unix.xz", "helloworld-compressed-unix"; "xz")]
Expand Down Expand Up @@ -45,6 +46,7 @@ mod install {
#[cfg(target_os = "windows")]
#[test_case("helloworld-windows.tar.gz", "helloworld.exe"; "tar gzip")]
#[test_case("helloworld-windows.zip", "helloworld.exe"; "zip")]
#[test_case("helloworld-windows.7z", "helloworld.exe"; "7zip")]
#[test_case("helloworld-compressed-windows.gz", "helloworld-compressed-windows"; "gzip")]
#[test_case("helloworld-compressed-windows.bz2", "helloworld-compressed-windows"; "bzip2")]
#[test_case("helloworld-compressed-windows.xz", "helloworld-compressed-windows"; "xz")]
Expand Down

0 comments on commit dd53499

Please sign in to comment.