Skip to content

Commit 73fc97c

Browse files
committed
generate: allow choosing the compression formats to output
1 parent 56d0165 commit 73fc97c

File tree

6 files changed

+99
-8
lines changed

6 files changed

+99
-8
lines changed

src/compression.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use anyhow::{Context, Error};
22
use flate2::{read::GzDecoder, write::GzEncoder};
33
use rayon::prelude::*;
4-
use std::{io::Read, io::Write, path::Path};
4+
use std::{convert::TryFrom, io::Read, io::Write, path::Path};
55
use xz2::{read::XzDecoder, write::XzEncoder};
66

7-
pub(crate) enum CompressionFormat {
7+
#[derive(Debug, Copy, Clone)]
8+
pub enum CompressionFormat {
89
Gz,
910
Xz,
1011
}
@@ -59,6 +60,38 @@ impl CompressionFormat {
5960
}
6061
}
6162

63+
/// This struct wraps Vec<CompressionFormat> in order to parse the value from the command line.
64+
#[derive(Debug, Clone)]
65+
pub struct CompressionFormats(Vec<CompressionFormat>);
66+
67+
impl TryFrom<&'_ str> for CompressionFormats {
68+
type Error = Error;
69+
70+
fn try_from(value: &str) -> Result<Self, Self::Error> {
71+
let mut parsed = Vec::new();
72+
for format in value.split(',') {
73+
match format.trim() {
74+
"gz" => parsed.push(CompressionFormat::Gz),
75+
"xz" => parsed.push(CompressionFormat::Xz),
76+
other => anyhow::bail!("unknown compression format: {}", other),
77+
}
78+
}
79+
Ok(CompressionFormats(parsed))
80+
}
81+
}
82+
83+
impl Default for CompressionFormats {
84+
fn default() -> Self {
85+
Self(vec![CompressionFormat::Gz, CompressionFormat::Xz])
86+
}
87+
}
88+
89+
impl CompressionFormats {
90+
pub(crate) fn iter(&self) -> impl Iterator<Item = CompressionFormat> + '_ {
91+
self.0.iter().map(|i| *i)
92+
}
93+
}
94+
6295
pub(crate) trait Encoder: Send + Write {
6396
fn finish(self: Box<Self>) -> Result<(), Error>;
6497
}

src/generator.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::Scripter;
22
use super::Tarballer;
3+
use crate::compression::CompressionFormats;
34
use crate::util::*;
45
use anyhow::{bail, format_err, Context, Result};
56
use std::io::Write;
@@ -40,6 +41,9 @@ actor! {
4041

4142
/// The location to put the final image and tarball
4243
output_dir: String = "./dist",
44+
45+
/// The formats used to compress the tarball
46+
compression_formats: CompressionFormats = CompressionFormats::default(),
4347
}
4448
}
4549

@@ -95,7 +99,8 @@ impl Generator {
9599
tarballer
96100
.work_dir(self.work_dir)
97101
.input(self.package_name)
98-
.output(path_to_str(&output)?.into());
102+
.output(path_to_str(&output)?.into())
103+
.compression_formats(self.compression_formats.clone());
99104
tarballer.run()?;
100105

101106
Ok(())

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn generate(matches: &ArgMatches<'_>) -> Result<()> {
6060
"image-dir" => image_dir,
6161
"work-dir" => work_dir,
6262
"output-dir" => output_dir,
63+
"compression-formats" => compression_formats,
6364
});
6465

6566
generator.run().context("failed to generate installer")?;

src/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ subcommands:
6060
long: output-dir
6161
takes_value: true
6262
value_name: DIR
63+
- compression-formats:
64+
help: Comma-separated list of compression formats to use
65+
long: compression-formats
66+
takes_value: true
67+
value_name: FORMAT
6368
- combine:
6469
about: Combine installer tarballs
6570
args:

src/tarballer.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use std::path::Path;
55
use tar::{Builder, Header};
66
use walkdir::WalkDir;
77

8-
use crate::{compression::CombinedEncoder, compression::CompressionFormat, util::*};
8+
use crate::{
9+
compression::{CombinedEncoder, CompressionFormats},
10+
util::*,
11+
};
912

1013
actor! {
1114
#[derive(Debug)]
@@ -18,17 +21,22 @@ actor! {
1821

1922
/// The folder in which the input is to be found.
2023
work_dir: String = "./workdir",
24+
25+
/// The formats used to compress the tarball.
26+
compression_formats: CompressionFormats = CompressionFormats::default(),
2127
}
2228
}
2329

2430
impl Tarballer {
2531
/// Generates the actual tarballs
2632
pub fn run(self) -> Result<()> {
2733
let tarball_name = self.output.clone() + ".tar";
28-
let encoder = CombinedEncoder::new(vec![
29-
CompressionFormat::Gz.encode(&tarball_name)?,
30-
CompressionFormat::Xz.encode(&tarball_name)?,
31-
]);
34+
let encoder = CombinedEncoder::new(
35+
self.compression_formats
36+
.iter()
37+
.map(|f| f.encode(&tarball_name))
38+
.collect::<Result<Vec<_>>>()?,
39+
);
3240

3341
// Sort files by their suffix, to group files with the same name from
3442
// different locations (likely identical) and files with the same

test.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,45 @@ combine_installers_different_input_compression_formats() {
11901190
}
11911191
runtest combine_installers_different_input_compression_formats
11921192

1193+
generate_compression_formats_one() {
1194+
try sh "$S/gen-installer.sh" \
1195+
--image-dir="$TEST_DIR/image1" \
1196+
--work-dir="$WORK_DIR" \
1197+
--output-dir="$OUT_DIR" \
1198+
--package-name="rustc" \
1199+
--component-name="rustc" \
1200+
--compression-formats="xz"
1201+
1202+
try test ! -e "${OUT_DIR}/rustc.tar.gz"
1203+
try test -e "${OUT_DIR}/rustc.tar.xz"
1204+
}
1205+
runtest generate_compression_formats_one
1206+
1207+
generate_compression_formats_multiple() {
1208+
try sh "$S/gen-installer.sh" \
1209+
--image-dir="$TEST_DIR/image1" \
1210+
--work-dir="$WORK_DIR" \
1211+
--output-dir="$OUT_DIR" \
1212+
--package-name="rustc" \
1213+
--component-name="rustc" \
1214+
--compression-formats="gz,xz"
1215+
1216+
try test -e "${OUT_DIR}/rustc.tar.gz"
1217+
try test -e "${OUT_DIR}/rustc.tar.xz"
1218+
}
1219+
runtest generate_compression_formats_multiple
1220+
1221+
generate_compression_formats_error() {
1222+
expect_fail sh "$S/gen-installer.sh" \
1223+
--image-dir="$TEST_DIR/image1" \
1224+
--work-dir="$WORK_DIR" \
1225+
--output-dir="$OUT_DIR" \
1226+
--package-name="rustc" \
1227+
--component-name="rustc" \
1228+
--compression-formats="xz,foobar"
1229+
}
1230+
runtest generate_compression_formats_error
1231+
11931232
echo
11941233
echo "TOTAL SUCCESS!"
11951234
echo

0 commit comments

Comments
 (0)