Skip to content

Commit

Permalink
Add --list flag
Browse files Browse the repository at this point in the history
Related to #6
  • Loading branch information
Byron committed Jul 26, 2020
1 parent e1e642e commit 55b9b49
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ mod args {
/// if set, no change will actually be made to the Cargo.toml file, simulating what would be done instead.
pub dry_run: bool,

#[argh(option, short = 'l')]
/// provide any number N to limit the output to the N biggest files, ascending.
///
/// if to 0, list all entries that would be in the crate with the current configuration.
/// Otherwise list the given amount of entries.
pub list: Option<usize>,

#[argh(option, from_str_fn(parse_size))]
/// if set, and the estimated compressed size of the package would exceed the given size, i.e. 40KB, the command
/// will exit with a non-zero exit code.
Expand Down Expand Up @@ -75,6 +82,7 @@ fn main() -> anyhow::Result<()> {
version,
reset_manifest: reset,
dry_run,
list,
package_size_limit,
#[cfg(feature = "dev-support")]
save_package_for_unit_test,
Expand All @@ -88,6 +96,7 @@ fn main() -> anyhow::Result<()> {
reset,
dry_run,
colored_output: atty::is(atty::Stream::Stdout),
list,
package_size_limit,
#[cfg(feature = "dev-support")]
save_package_for_unit_test,
Expand Down
93 changes: 77 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn report_lean_crate(mut out: impl std::io::Write) -> std::io::Result<()> {
fn report_savings(
total_size_in_bytes: u64,
total_files: u64,
mut wasted_files: Vec<WastedFile>,
wasted_files: Vec<WastedFile>,
mut out: impl std::io::Write,
) -> std::io::Result<()> {
if wasted_files.is_empty() {
Expand All @@ -38,6 +38,30 @@ fn report_savings(
)?;
return Ok(());
}

let file_len = wasted_files.len();
let wasted_bytes = entries_to_table(wasted_files, &mut out, None)?;

writeln!(
out,
"Saved {:.0}% or {} in {} files (of {} and {} files in entire crate)",
(wasted_bytes as f32 / total_size_in_bytes as f32) * 100.0,
ByteSize(wasted_bytes),
file_len,
ByteSize(total_size_in_bytes),
total_files
)?;
Ok(())
}

fn entries_to_table(
mut entries: Vec<WastedFile>,
mut out: impl std::io::Write,
items: Option<usize>,
) -> std::io::Result<u64> {
if entries.is_empty() {
return Ok(0);
}
use ascii_table::{Align, AsciiTable, Column};
use std::fmt::Display;

Expand All @@ -62,23 +86,21 @@ fn report_savings(
},
);

wasted_files.sort_by(|x, y| x.1.cmp(&y.1));
let wasted_bytes: u64 = wasted_files.iter().map(|(_, size)| size).sum();
let data: Vec<Vec<&dyn Display>> = wasted_files
entries.sort_by(|x, y| y.1.cmp(&x.1));
let bytes: u64 = entries
.iter()
.take(items.unwrap_or(entries.len()))
.rev()
.map(|(_, size)| size)
.sum();
let data: Vec<Vec<&dyn Display>> = entries
.iter()
.take(items.unwrap_or(entries.len()))
.rev()
.map(|(path, size)| vec![path as &dyn Display, size as &dyn Display])
.collect();
ascii_table.print(data);
writeln!(
out,
"Saved {:.0}% or {} in {} files (of {} and {} files in entire crate)",
(wasted_bytes as f32 / total_size_in_bytes as f32) * 100.0,
ByteSize(wasted_bytes),
wasted_files.len(),
ByteSize(total_size_in_bytes),
total_files
)?;
Ok(())
out.write_all(ascii_table.format(data).as_bytes())?;
Ok(bytes)
}

fn edit(
Expand Down Expand Up @@ -290,6 +312,7 @@ pub struct Options {
pub reset: bool,
pub dry_run: bool,
pub colored_output: bool,
pub list: Option<usize>,
pub package_size_limit: Option<u64>,
#[cfg(feature = "dev-support")]
pub save_package_for_unit_test: Option<PathBuf>,
Expand Down Expand Up @@ -387,6 +410,9 @@ pub fn execute(options: Options, mut output: impl std::io::Write) -> Result<()>
if options.reset && options.dry_run {
std::fs::write(&manifest_path, &cargo_manifest_original_content)?;
}
let list = options
.list
.map(|list| (list, package.entries_meta_data.clone()));
let document = edit(document, package, &mut output)?;
write_manifest(
&manifest_path,
Expand All @@ -398,7 +424,42 @@ pub fn execute(options: Options, mut output: impl std::io::Write) -> Result<()>
)?;

if let Some((package, package_size_limit)) = package_size_limit {
check_package_size(package, package_size_limit, output, options.colored_output)?;
check_package_size(
package,
package_size_limit,
&mut output,
options.colored_output,
)?;
}
if let Some((list, entries)) = list {
list_entries(list, &mut output, entries)?;
}
Ok(())
}

fn list_entries(
num_entries: usize,
mut out: impl std::io::Write,
entries: Vec<TarHeader>,
) -> Result<()> {
let num_entries = if num_entries == 0 {
entries.len()
} else {
num_entries.min(entries.len())
};
let bytes = entries_to_table(
entries
.into_iter()
.map(|tf| (tar_path_to_utf8_str(&tf.path).into(), tf.size))
.collect(),
&mut out,
Some(num_entries),
)?;
writeln!(
out,
"Crate contains a total of {} files and {}",
num_entries,
ByteSize(bytes)
)?;
Ok(())
}
10 changes: 10 additions & 0 deletions tests/snapshots/success-list-no-limit-with-zero
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Your crate is perfectly lean!
There would be no change.
┌─────────────┬─────────────┐
│ File │ Size (Byte) │
├─────────────┼─────────────┤
│ README.md │ 0 │
│ src/main.rs │ 45 │
│ Cargo.toml │ 270 │
└─────────────┴─────────────┘
Crate contains a total of 3 files and <bytecount>
9 changes: 9 additions & 0 deletions tests/snapshots/success-list-with-limit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Your crate is perfectly lean!
There would be no change.
┌─────────────┬─────────────┐
│ File │ Size (Byte) │
├─────────────┼─────────────┤
│ src/main.rs │ 45 │
│ Cargo.toml │ 270 │
└─────────────┴─────────────┘
Crate contains a total of 2 files and <bytecount>
25 changes: 25 additions & 0 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,31 @@ function remove_bytecounts() {
}
)
)

(with "the --list flag"
(when "a small amount is specified"
it "runs successfully and lists the given amount of entries entries in the package" && {
SNAPSHOT_FILTER=remove_bytecounts \
WITH_SNAPSHOT="$snapshot/success-list-with-limit" \
expect_run ${SUCCESSFULLY} "$exe" diet -n --list 2
}

it "does not put a file in target/package" && {
expect_run ${WITH_FAILURE} find target/package
}
)
(when "a 0 is specified as amount"
it "runs successfully and lists all entries entries in the package" && {
SNAPSHOT_FILTER=remove_bytecounts \
WITH_SNAPSHOT="$snapshot/success-list-no-limit-with-zero" \
expect_run ${SUCCESSFULLY} "$exe" diet -n --list 0
}

it "does not put a file in target/package" && {
expect_run ${WITH_FAILURE} find target/package
}
)
)
)
)
)
Expand Down

0 comments on commit 55b9b49

Please sign in to comment.