Skip to content

Commit

Permalink
archive: guess for {tool_name}-{tag}/ subdirectory in archive
Browse files Browse the repository at this point in the history
  • Loading branch information
hdhoang committed Oct 27, 2022
1 parent cbb26d9 commit 7cec858
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ available [on GitHub][2].
Supports casey/just, dalance/procs, derailed/k9s, and
sharkdp/hyperfine natively.
(by [@hdhoang][hdhoang])
* [#153](https://github.com/chshersh/tool-sync/issues/153)
Supports `tar.xz` assets, and koalaman/shellcheck
(by [@hdhoang][hdhoang])


### Fixed
Expand Down
32 changes: 28 additions & 4 deletions src/sync/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::model::asset_name::mk_exe_name;
pub struct Archive<'a> {
archive_path: &'a PathBuf,
tmp_dir: &'a Path,
tool_tag: &'a str,
exe_name: &'a str,
archive_type: ArchiveType<'a>,
}
Expand Down Expand Up @@ -44,20 +45,23 @@ impl<'a> Archive<'a> {
pub fn from(
archive_path: &'a PathBuf,
tmp_dir: &'a Path,
tool_tag: &'a str,
exe_name: &'a str,
asset_name: &'a str,
) -> Option<Archive<'a>> {
if let Some(exe_name) = asset_name.strip_suffix(".exe") {
return Some(Archive {
archive_path,
tmp_dir,
tool_tag,
exe_name,
archive_type: ArchiveType::Exe(asset_name),
});
};
if let Some(tar_gz_dir) = asset_name.strip_suffix(".tar.gz") {
return Some(Archive {
archive_path,
tool_tag,
tmp_dir,
exe_name,
archive_type: ArchiveType::TarBall(tar_gz_dir),
Expand All @@ -67,6 +71,7 @@ impl<'a> Archive<'a> {
return Some(Archive {
archive_path,
tmp_dir,
tool_tag,
exe_name,
archive_type: ArchiveType::TarBall(tar_xz_dir),
});
Expand All @@ -93,13 +98,25 @@ impl<'a> Archive<'a> {
// unpack .tar.gz archive
ArchiveType::TarBall(asset_name) => {
unpack_tar(self.archive_path, self.tmp_dir).map_err(UnpackError::IOError)?;
find_path_to_exe(self.archive_path, self.tmp_dir, self.exe_name, asset_name)
find_path_to_exe(
self.archive_path,
self.tmp_dir,
self.tool_tag,
self.exe_name,
asset_name,
)
}

// unpack .zip archive
ArchiveType::Zip(asset_name) => {
unpack_zip(self.archive_path, self.tmp_dir)?;
find_path_to_exe(self.archive_path, self.tmp_dir, self.exe_name, asset_name)
find_path_to_exe(
self.archive_path,
self.tmp_dir,
self.tool_tag,
self.exe_name,
asset_name,
)
}
}
}
Expand Down Expand Up @@ -133,10 +150,11 @@ fn unpack_zip(zip_path: &PathBuf, tmp_dir: &Path) -> Result<(), UnpackError> {
fn find_path_to_exe(
archive_path: &Path,
tmp_dir: &Path,
sub_directory: &str,
exe_name: &str,
asset_name: &str,
) -> Result<PathBuf, UnpackError> {
let path_candidates = exe_paths(exe_name, asset_name);
let path_candidates = exe_paths(sub_directory, exe_name, asset_name);

// find a path
for path in path_candidates {
Expand All @@ -158,13 +176,19 @@ fn find_path_to_exe(
}

// List of potential paths where an executable can be inside the archive
fn exe_paths(exe_name: &str, asset_name: &str) -> Vec<PathBuf> {
fn exe_paths(sub_directory: &str, exe_name: &str, asset_name: &str) -> Vec<PathBuf> {
let exe_name = mk_exe_name(exe_name);

vec![
[asset_name, &exe_name].iter().collect(),
[&exe_name].iter().collect(),
[sub_directory, &exe_name].iter().collect(),
[asset_name, sub_directory, &exe_name].iter().collect(),
["bin", &exe_name].iter().collect(),
[asset_name, "bin", &exe_name].iter().collect(),
[sub_directory, "bin", &exe_name].iter().collect(),
[asset_name, sub_directory, "bin", &exe_name]
.iter()
.collect(),
]
}
2 changes: 2 additions & 0 deletions src/sync/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ impl<'a> Installer<'a> {
};

let download_info = downloader.download(self.tmp_dir.path())?;
let tool_tag = format!("{}-{}", &tool_asset.tool_name, &tool_asset.tag);

let archive = Archive::from(
&download_info.archive_path,
self.tmp_dir.path(),
&tool_tag,
&tool_asset.exe_name,
&tool_asset.asset.name,
);
Expand Down

0 comments on commit 7cec858

Please sign in to comment.