From 44789e033be8043cfd1f448b69b26cb999a0e676 Mon Sep 17 00:00:00 2001 From: cat_in_136 Date: Sat, 4 Sep 2021 20:55:07 +0900 Subject: [PATCH 1/2] introduce --payload-compress option and use zstd if not specified --- src/config.rs | 35 ++++++++++++++++++++++++++++------- src/main.rs | 42 +++++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0b1391f..63f8924 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,6 +11,27 @@ use crate::build_target::BuildTarget; use crate::error::{ConfigError, Error}; use crate::file_info::FileInfo; +#[derive(Debug)] +pub struct RpmBuilderConfig<'a, 'b> { + build_target: &'a BuildTarget, + auto_req_mode: AutoReqMode, + payload_compress: &'b str, +} + +impl<'a, 'b> RpmBuilderConfig<'a, 'b> { + pub fn new( + build_target: &'a BuildTarget, + auto_req_mode: AutoReqMode, + payload_compress: &'b str, + ) -> RpmBuilderConfig<'a, 'b> { + RpmBuilderConfig { + build_target, + auto_req_mode, + payload_compress, + } + } +} + #[derive(Debug)] pub struct Config { manifest: Manifest, @@ -80,8 +101,7 @@ impl Config { pub fn create_rpm_builder( &self, - build_target: &BuildTarget, - auto_req_mode: AutoReqMode, + rpm_builder_config: RpmBuilderConfig, ) -> Result { let metadata = self.metadata()?; @@ -146,7 +166,7 @@ impl Config { let license = get_str_from_metadata!("license") .or_else(|| pkg.license.as_ref().map(|v| v.as_ref())) .ok_or(ConfigError::Missing("package.license"))?; - let arch = build_target.binary_arch(); + let arch = rpm_builder_config.build_target.binary_arch(); let desc = get_str_from_metadata!("summary") .or_else(|| pkg.description.as_ref().map(|v| v.as_ref())) .ok_or(ConfigError::Missing("package.description"))?; @@ -154,9 +174,10 @@ impl Config { let parent = self.path.parent().unwrap(); let mut builder = RPMBuilder::new(name, version, license, arch.as_str(), desc) - .compression(Compressor::from_str("gzip").unwrap()); + .compression(Compressor::from_str(rpm_builder_config.payload_compress)?); for file in &files { - let file_source = file.generate_rpm_file_path(build_target, parent)?; + let file_source = + file.generate_rpm_file_path(rpm_builder_config.build_target, parent)?; let options = file.generate_rpm_file_options(); builder = builder.with_file(file_source, options)?; } @@ -186,14 +207,14 @@ impl Config { builder = builder.requires(dependency); } } - let auto_req = if auto_req_mode == AutoReqMode::Auto + let auto_req = if rpm_builder_config.auto_req_mode == AutoReqMode::Auto && matches!( get_str_from_metadata!("auto-req"), Some("no") | Some("disabled") ) { AutoReqMode::Disabled } else { - auto_req_mode + rpm_builder_config.auto_req_mode }; for requires in find_requires(files.iter().map(|v| Path::new(v.source)), auto_req)? { builder = builder.requires(Dependency::any(requires)); diff --git a/src/main.rs b/src/main.rs index df3f274..b40be33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use crate::auto_req::AutoReqMode; use crate::build_target::BuildTarget; -use crate::config::Config; +use crate::config::{Config, RpmBuilderConfig}; use crate::error::Error; use getopts::Options; use std::convert::TryFrom; @@ -14,18 +14,28 @@ mod config; mod error; mod file_info; +#[derive(Debug)] +struct CliSetting { + auto_req_mode: AutoReqMode, + payload_compress: String, +} + fn process( build_target: &BuildTarget, target_file: Option, package: Option, - auto_req_mode: AutoReqMode, + setting: CliSetting, ) -> Result<(), Error> { let manifest_file_dir = package.map_or(env::current_dir()?, PathBuf::from); let manifest_file_path = manifest_file_dir.join("Cargo.toml"); let config = Config::new(manifest_file_path)?; let rpm_pkg = config - .create_rpm_builder(build_target, auto_req_mode)? + .create_rpm_builder(RpmBuilderConfig::new( + build_target, + setting.auto_req_mode, + setting.payload_compress.as_str(), + ))? .build()?; let default_file_name = build_target.target_path("generate-rpm").join(format!( @@ -59,7 +69,7 @@ fn process( Ok(()) } -fn parse_arg() -> Result<(BuildTarget, Option, Option, AutoReqMode), Error> { +fn parse_arg() -> Result<(BuildTarget, Option, Option, CliSetting), Error> { let program = env::args().nth(0).unwrap(); let mut build_target = BuildTarget::default(); @@ -93,6 +103,13 @@ fn parse_arg() -> Result<(BuildTarget, Option, Option, AutoReqM May be specified with CARGO_BUILD_TARGET_DIR or CARGO_TARGET_DIR environment variables.", "DIRECTORY", ); + opts.optopt( + "", + "payload-compress", + "Compression type of package payloads.\ + none, gzip or zstd(Default).", + "TYPE", + ); opts.optflag("h", "help", "print this help menu"); @@ -121,14 +138,25 @@ fn parse_arg() -> Result<(BuildTarget, Option, Option, AutoReqM if let Some(target_dir) = opt_matches.opt_str("target-dir") { build_target.target_dir = Some(target_dir); } + let payload_compress = opt_matches + .opt_str("payload-compress") + .unwrap_or("zstd".to_string()); - Ok((build_target, target_file, package, auto_req_mode)) + Ok(( + build_target, + target_file, + package, + CliSetting { + auto_req_mode, + payload_compress, + }, + )) } fn main() { (|| -> Result<(), Error> { - let (build_target, target_file, package, auto_req_mode) = parse_arg()?; - process(&build_target, target_file, package, auto_req_mode)?; + let (build_target, target_file, package, setting) = parse_arg()?; + process(&build_target, target_file, package, setting)?; Ok(()) })() .unwrap_or_else(|err| { From 60890e29ebc2438dea9c220d905520df4c5b9478 Mon Sep 17 00:00:00 2001 From: cat_in_136 Date: Sat, 4 Sep 2021 21:02:19 +0900 Subject: [PATCH 2/2] fix test_config_create_rpm_builder() for payload compress --- src/config.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 63f8924..530c8ea 100644 --- a/src/config.rs +++ b/src/config.rs @@ -337,7 +337,11 @@ mod test { #[test] fn test_config_create_rpm_builder() { let config = Config::new("Cargo.toml").unwrap(); - let builder = config.create_rpm_builder(&BuildTarget::default(), AutoReqMode::Disabled); + let builder = config.create_rpm_builder(RpmBuilderConfig::new( + &BuildTarget::default(), + AutoReqMode::Disabled, + "zstd", + )); assert!(if Path::new("target/release/cargo-generate-rpm").exists() { matches!(builder, Ok(_))