Skip to content

Commit

Permalink
fix: properly append additional extention to path
Browse files Browse the repository at this point in the history
  • Loading branch information
amr-crabnebula committed Dec 11, 2023
1 parent de32ea0 commit 821ced8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
9 changes: 2 additions & 7 deletions crates/packager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub use error::{Error, Result};
pub use sign::SigningConfig;

pub use package::{package, PackageOuput};
use util::PathExt;

fn parse_log_level(verbose: u8) -> tracing::Level {
match verbose {
Expand Down Expand Up @@ -145,13 +146,7 @@ pub fn sign_outputs(
for package in packages {
for path in &package.paths.clone() {
let path = if path.is_dir() {
let extension = path.extension().unwrap_or_default().to_string_lossy();
let extension = format!(
"{}{}tar.gz",
extension,
if extension.is_empty() { "" } else { "." }
);
let zip = path.with_extension(extension);
let zip = path.with_additional_extension("tar.gz");
let dest_file = util::create_file(&zip)?;
let gzip_encoder = libflate::gzip::Encoder::new(dest_file)?;
let writer = util::create_tar_from_dir(path, gzip_encoder)?;
Expand Down
7 changes: 5 additions & 2 deletions crates/packager/src/package/deb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use serde::Serialize;
use walkdir::WalkDir;

use super::Context;
use crate::{config::Config, util};
use crate::{
config::Config,
util::{self, PathExt},
};

#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct DebIcon {
Expand Down Expand Up @@ -318,7 +321,7 @@ fn generate_md5sums(control_dir: &Path, data_dir: &Path) -> crate::Result<()> {
/// directory and returns the path to the new file.
fn tar_and_gzip_dir<P: AsRef<Path>>(src_dir: P) -> crate::Result<PathBuf> {
let src_dir = src_dir.as_ref();
let dest_path = src_dir.with_extension("tar.gz");
let dest_path = src_dir.with_additional_extension("tar.gz");
let dest_file = util::create_file(&dest_path)?;
let gzip_encoder = libflate::gzip::Encoder::new(dest_file)?;
let gzip_encoder = create_tar_from_dir(src_dir, gzip_encoder)?;
Expand Down
5 changes: 2 additions & 3 deletions crates/packager/src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use base64::{engine::general_purpose::STANDARD, Engine};
use minisign::{sign, KeyPair as KP, SecretKey, SecretKeyBox};
use serde::{Deserialize, Serialize};

use crate::util;
use crate::util::{self, PathExt};

/// A public and secret key pair.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -148,8 +148,7 @@ pub fn sign_file_with_secret_key<P: AsRef<Path> + Debug>(
path: P,
) -> crate::Result<PathBuf> {
let path = path.as_ref();
let extension = path.extension().unwrap_or_default().to_string_lossy();
let signature_path = path.with_extension(format!("{}.sig", extension));
let signature_path = path.with_additional_extension("sig");
let signature_path = dunce::simplified(&signature_path);

let mut signature_box_writer = util::create_file(signature_path)?;
Expand Down
59 changes: 49 additions & 10 deletions crates/packager/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use sha2::Digest;
use std::{
ffi::OsStr,
fs::File,
io::{Cursor, Read, Write},
path::{Path, PathBuf},
Expand Down Expand Up @@ -48,6 +49,16 @@ pub fn create_clean_dir<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
std::fs::create_dir_all(path)
}

/// Creates a new file at the given path, creating any parent directories as needed.
#[inline]
pub(crate) fn create_file(path: &Path) -> crate::Result<std::io::BufWriter<File>> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let file = File::create(path)?;
Ok(std::io::BufWriter::new(file))
}

#[derive(Debug, PartialEq, Eq)]
struct RustCfg {
target_arch: Option<String>,
Expand Down Expand Up @@ -261,16 +272,6 @@ pub(crate) fn is_retina<P: AsRef<Path>>(path: P) -> bool {
.unwrap_or(false)
}

/// Creates a new file at the given path, creating any parent directories as needed.
#[inline]
pub(crate) fn create_file(path: &Path) -> crate::Result<std::io::BufWriter<File>> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let file = File::create(path)?;
Ok(std::io::BufWriter::new(file))
}

// Given a list of icon files, try to produce an ICNS file in the out_dir
// and return the path to it. Returns `Ok(None)` if no usable icons
// were provided.
Expand Down Expand Up @@ -409,3 +410,41 @@ pub fn create_tar_from_dir<P: AsRef<Path>, W: Write>(src_dir: P, dest_file: W) -
builder.append_dir_all(filename, src_dir)?;
builder.into_inner().map_err(Into::into)
}

pub trait PathExt {
fn with_additional_extension(&self, extension: impl AsRef<OsStr>) -> PathBuf;
}

impl PathExt for Path {
fn with_additional_extension(&self, extension: impl AsRef<OsStr>) -> PathBuf {
match self.extension() {
Some(ext) => {
let mut e = ext.to_os_string();
e.push(".");
e.push(extension);
self.with_extension(e)
}
None => self.with_extension(extension),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_appends_ext() {
// Something that has an extention getting another suffix.
assert_eq!(
PathBuf::from("./asset.zip").with_additional_extension("sig"),
PathBuf::from("./asset.zip.sig")
);

// Something that doesn't have an extention, setting its extension.
assert_eq!(
PathBuf::from("./executable").with_additional_extension("sig"),
PathBuf::from("./executable.sig")
)
}
}

0 comments on commit 821ced8

Please sign in to comment.