Upgrade packer

Summary:
This had collected a bit of dust and we will need to extend this a little for flipper-server.

- Update deps.
- Migrate away from deprecated clap2 model to derive (it's much shorter now and less imperative).
- ~~Some COW changes on how the progress bar crate handles strings.~~ (Reverted as this causes havvoc in `fbsource//third-party/rust`.)
- Upgraded to 2021 Edition.

Reviewed By: nikoant

Differential Revision: D35433571

fbshipit-source-id: ae0a91558610ae46069a5fc5162b524cde759454
This commit is contained in:
Pascal Hartig
2022-04-07 07:40:57 -07:00
committed by Facebook GitHub Bot
parent 9cf630b27b
commit b80766323f
5 changed files with 252 additions and 175 deletions

View File

@@ -14,9 +14,9 @@ mod tarsum;
mod types;
use anyhow::{bail, Context, Result};
use clap::value_t_or_exit;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::collections::BTreeMap;
use std::ffi;
use std::fs::File;
use std::io::{self, BufReader, BufWriter, Write};
use std::path;
@@ -26,6 +26,46 @@ const DEFAULT_PACKLIST: &str = include_str!("packlist.yaml");
// This is to ensure that all progress bar prefixes are aligned.
const PROGRESS_PREFIX_LEN: usize = 24;
fn expand_tilde_path(path: &ffi::OsStr) -> Result<path::PathBuf> {
Ok(path::PathBuf::from(
shellexpand::tilde(
&path
.to_str()
.ok_or_else(|| anyhow::anyhow!("Not a valid path: {:?}", path))?,
)
.to_string(),
))
}
#[derive(clap::Parser, Debug)]
#[clap(author, version, about)]
struct Args {
/// Directory to write output files to.
#[clap(short, long, parse(from_os_str), default_value = ".")]
output: path::PathBuf,
/// Flipper dist directory to read from.
#[clap(
short,
long,
parse(try_from_os_str = expand_tilde_path),
default_value = "~/fbsource/xplat/sonar/dist"
)]
dist: path::PathBuf,
/// Custom list of files to pack.
#[clap(short, long, parse(from_os_str))]
packlist: Option<path::PathBuf>,
/// Skip compressing the archives (for debugging)
#[clap(long)]
no_compression: bool,
/// Platform to build for
#[clap(value_name = "PLATFORM", arg_enum)]
platform: Platform,
}
type PackListPlatform = BTreeMap<PackType, Vec<String>>;
#[derive(Debug, serde::Deserialize)]
@@ -194,62 +234,26 @@ fn sha256_digest<R: io::Read>(mut reader: &mut R) -> Result<HashSum> {
}
fn main() -> Result<(), anyhow::Error> {
// Ensure to define all env vars used here in the BUCK env, too.
let args = clap::App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about("Split the Flipper distribution into smaller, cacheable artifacts")
.arg(
clap::Arg::from_usage("-o, --output [DIRECTORY] 'Directory to write output files to.'")
.default_value("."),
)
.arg(
clap::Arg::from_usage("-d, --dist [DIRECTORY] 'Flipper dist directory to read from.'")
.default_value("~/fbsource/xplat/sonar/dist"),
)
.arg(clap::Arg::from_usage(
"-p, --packlist=packlist.yaml 'Custom list of files to pack.'",
))
.arg(clap::Arg::from_usage(
"--no-compression 'Skip compressing the archives (for debugging)'",
))
.arg(
clap::Arg::from_usage("[PLATFORM] 'Platform to build for'")
.case_insensitive(true)
.required(true)
.possible_values(&Platform::variants()),
)
.get_matches();
let platform = value_t_or_exit!(args.value_of("PLATFORM"), Platform);
let dist_dir = path::PathBuf::from(
shellexpand::tilde(args.value_of("dist").expect("argument has default")).to_string(),
);
let compress = !args.is_present("no-compression");
let pack_list_str = args.value_of("packlist").map_or_else(
use clap::Parser;
let args = Args::parse();
let pack_list_str = args.packlist.as_ref().map_or_else(
|| DEFAULT_PACKLIST.to_string(),
|f| {
std::fs::read_to_string(f)
.unwrap_or_else(|e| panic!("Failed to open packfile {}: {}", f, e))
std::fs::read_to_string(&f)
.unwrap_or_else(|e| panic!("Failed to open packfile {:?}: {}", &f, e))
},
);
let pack_list: PackList =
serde_yaml::from_str(&pack_list_str).expect("Failed to deserialize YAML packlist.");
let output_directory =
&path::PathBuf::from(args.value_of("output").expect("argument has default"));
std::fs::create_dir_all(output_directory).with_context(|| {
format!(
"Failed to create output directory '{}'.",
output_directory.to_string_lossy()
)
})?;
let archive_paths = pack(platform, &dist_dir, &pack_list, output_directory)?;
let compressed_archive_paths = if compress {
Some(compress_paths(&archive_paths)?)
} else {
std::fs::create_dir_all(&args.output)
.with_context(|| format!("Failed to create output directory '{:?}'.", &args.output))?;
let archive_paths = pack(args.platform, &args.dist, &pack_list, &args.output)?;
let compressed_archive_paths = if args.no_compression {
None
} else {
Some(compress_paths(&archive_paths)?)
};
manifest(&archive_paths, &compressed_archive_paths, output_directory)?;
manifest(&archive_paths, &compressed_archive_paths, &args.output)?;
Ok(())
}