Skip to content

Commit

Permalink
fix: fix packaging of external binaries (#124)
Browse files Browse the repository at this point in the history
* fix: fix packaging of external binaries

* clippy
  • Loading branch information
amr-crabnebula authored Jan 8, 2024
1 parent 21b40f3 commit 2a50c8e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .changes/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"rust": {
"version": true,
"getPublishedVersion": "cargo search ${ pkg.pkg } --limit 1 | sed -nE 's/^[^\"]*\"//; s/\".*//1p' -",
"prepublish": [
{
"command": "cargo generate-lockfile",
"dryRunCommand": true,
"runFromRoot": true,
"pipe": true
}
],
"publish": [
{
"command": "cargo package --no-verify",
Expand Down
6 changes: 6 additions & 0 deletions .changes/external-binaries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cargo-packager": "patch"
"@crabnebula/packager": "patch"
---

Fix packaing of external binaries.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions crates/packager/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,14 +1786,21 @@ impl Config {
pub(crate) fn copy_external_binaries(&self, path: &Path) -> crate::Result<Vec<PathBuf>> {
let mut paths = Vec::new();
if let Some(external_binaries) = &self.external_binaries {
let cwd = std::env::current_dir()?;
let target_triple = self.target_triple();
for src in external_binaries {
let src = dunce::canonicalize(src)?;
let file_name_no_triple = src
let file_name = src
.file_name()
.ok_or_else(|| crate::Error::FailedToExtractFilename(src.clone()))?
.to_string_lossy()
.replace(&format!("-{}", self.target_triple()), "");
let dest = path.join(file_name_no_triple);
.to_string_lossy();
#[cfg(windows)]
let src = src.with_file_name(format!("{file_name}-{target_triple}.exe"));
#[cfg(not(windows))]
let src = src.with_file_name(format!("{file_name}-{target_triple}"));
#[cfg(windows)]
let dest = path.join(format!("{file_name}.exe"));
#[cfg(not(windows))]
let dest = path.join(&*file_name);
std::fs::copy(src, &dest)?;
paths.push(dest);
}
Expand Down
17 changes: 9 additions & 8 deletions crates/packager/src/package/nsis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,19 @@ type BinariesMap = BTreeMap<PathBuf, String>;
#[tracing::instrument(level = "trace")]
fn generate_binaries_data(config: &Config) -> crate::Result<BinariesMap> {
let mut binaries = BinariesMap::new();
let cwd = std::env::current_dir()?;

if let Some(external_binaries) = &config.external_binaries {
let cwd = std::env::current_dir()?;
let target_triple = config.target_triple();
for src in external_binaries {
let src = src.with_extension("exe");
let bin_path = dunce::canonicalize(cwd.join(src))?;
let dest_filename = bin_path
let file_name = src
.file_name()
.ok_or_else(|| crate::Error::FailedToExtractFilename(bin_path.clone()))?
.to_string_lossy()
.replace(&format!("-{}", config.target_triple()), "");
binaries.insert(bin_path, dest_filename);
.ok_or_else(|| crate::Error::FailedToExtractFilename(src.clone()))?
.to_string_lossy();
let src = src.with_file_name(format!("{file_name}-{target_triple}.exe"));
let bin_path = dunce::canonicalize(cwd.join(src))?;
let dest_file_name = format!("{file_name}.exe");
binaries.insert(bin_path, dest_file_name);
}
}

Expand Down
24 changes: 13 additions & 11 deletions crates/packager/src/package/wix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,29 @@ struct Binary {
#[tracing::instrument(level = "trace")]
fn generate_binaries_data(config: &Config) -> crate::Result<Vec<Binary>> {
let mut binaries = Vec::new();
let cwd = std::env::current_dir()?;
let tmp_dir = std::env::temp_dir();
let regex = Regex::new(r"[^\w\d\.]")?;

if let Some(external_binaries) = &config.external_binaries {
let cwd = std::env::current_dir()?;
let target_triple = config.target_triple();
for src in external_binaries {
let src = src.with_extension("exe");
let bin_path = dunce::canonicalize(cwd.join(src))?;
let dest_filename = bin_path
let file_name = src
.file_name()
.ok_or_else(|| crate::Error::FailedToExtractFilename(bin_path.clone()))?
.to_string_lossy()
.replace(&format!("-{}", config.target_triple()), "");
let dest = tmp_dir.join(&dest_filename);
.ok_or_else(|| crate::Error::FailedToExtractFilename(src.clone()))?
.to_string_lossy();
let src = src.with_file_name(format!("{file_name}-{target_triple}.exe"));
let bin_path = dunce::canonicalize(cwd.join(src))?;
let dest_file_name = format!("{file_name}.exe");
let dest = tmp_dir.join(&*dest_file_name);

std::fs::copy(bin_path, &dest)?;

binaries.push(Binary {
guid: Uuid::new_v4().to_string(),
path: dest.into_os_string().into_string().unwrap_or_default(),
id: regex
.replace_all(&dest_filename.replace('-', "_"), "")
.replace_all(&dest_file_name.replace('-', "_"), "")
.to_string(),
});
}
Expand All @@ -167,8 +169,8 @@ fn generate_binaries_data(config: &Config) -> crate::Result<Vec<Binary>> {
.binary_path(bin)
.with_extension("exe")
.into_os_string()
.into_string()
.unwrap_or_default(),
.to_string_lossy()
.to_string(),
id: regex
.replace_all(
&bin.path
Expand Down

0 comments on commit 2a50c8e

Please sign in to comment.