Skip to content

Commit

Permalink
Merge pull request #27 from cat-in-136/feat-compresstype
Browse files Browse the repository at this point in the history
* Add `--payload-compress TYPE` to specify the payload compress type, none, gzip, or zstd.
* Set  the default payload compress type to zstd
  • Loading branch information
cat-in-136 authored Sep 4, 2021
2 parents 7ef8d67 + 60890e2 commit 5b6031f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
41 changes: 33 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -80,8 +101,7 @@ impl Config {

pub fn create_rpm_builder(
&self,
build_target: &BuildTarget,
auto_req_mode: AutoReqMode,
rpm_builder_config: RpmBuilderConfig,
) -> Result<RPMBuilder, Error> {
let metadata = self.metadata()?;

Expand Down Expand Up @@ -146,17 +166,18 @@ 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"))?;
let files = FileInfo::list_from_metadata(&metadata)?;
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)?;
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -316,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(_))
Expand Down
42 changes: 35 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<PathBuf>,
package: Option<String>,
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!(
Expand Down Expand Up @@ -59,7 +69,7 @@ fn process(
Ok(())
}

fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, AutoReqMode), Error> {
fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, CliSetting), Error> {
let program = env::args().nth(0).unwrap();
let mut build_target = BuildTarget::default();

Expand Down Expand Up @@ -93,6 +103,13 @@ fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, 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");

Expand Down Expand Up @@ -121,14 +138,25 @@ fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, 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| {
Expand Down

0 comments on commit 5b6031f

Please sign in to comment.