feat(cli.rs): strip release binaries [TRI-031] (#22)

This commit is contained in:
Lucas Nogueira 2022-01-09 15:46:43 -03:00
parent 153a6a44b3
commit 2f3a582c69
No known key found for this signature in database
GPG Key ID: 2714B66BCFB01F7F
5 changed files with 71 additions and 24 deletions

5
.changes/strip.md Normal file
View File

@ -0,0 +1,5 @@
---
"cli.rs": "patch"
---
Automatically `strip` the built binary on Linux and macOS if `--debug` is not specified.

View File

@ -811,6 +811,12 @@ dependencies = [
"itoa 1.0.1",
]
[[package]]
name = "humansize"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02296996cb8796d7c6e3bc2d9211b7802812d36999a51bb754123ead7d37d026"
[[package]]
name = "icns"
version = "0.3.1"
@ -2281,6 +2287,7 @@ dependencies = [
"glob",
"handlebars",
"heck",
"humansize",
"include_dir",
"json-patch",
"lazy_static",

View File

@ -56,6 +56,9 @@ url = { version = "2.2", features = [ "serde" ] }
[target."cfg(windows)".dependencies]
encode_unicode = "0.3"
[target."cfg(not(windows))".dependencies]
humansize = "1.1"
[target."cfg(target_os = \"linux\")".build-dependencies]
heck = "0.4"

View File

@ -952,9 +952,6 @@
"FsAllowlistConfig": {
"description": "Allowlist for the file system APIs.",
"type": "object",
"required": [
"scope"
],
"properties": {
"all": {
"description": "Use this flag to enable all file system API features.",
@ -1003,6 +1000,9 @@
},
"scope": {
"description": "The access scope for the filesystem APIs.",
"default": [
"$APP/**"
],
"allOf": [
{
"$ref": "#/definitions/FsAllowlistScope"
@ -1203,9 +1203,6 @@
"ProtocolAllowlistConfig": {
"description": "Allowlist for the custom protocols.",
"type": "object",
"required": [
"assetScope"
],
"properties": {
"all": {
"description": "Use this flag to enable all custom protocols.",
@ -1219,6 +1216,9 @@
},
"assetScope": {
"description": "The access scope for the asset protocol.",
"default": [
"$APP/**"
],
"allOf": [
{
"$ref": "#/definitions/FsAllowlistScope"

View File

@ -140,28 +140,29 @@ pub fn command(options: Options) -> Result<()> {
let out_dir = app_settings
.get_out_dir(options.target.clone(), options.debug)
.with_context(|| "failed to get project out directory")?;
let bin_name = app_settings
.cargo_package_settings()
.name
.clone()
.expect("Cargo manifest must have the `package.name` field");
#[cfg(windows)]
let bin_path = out_dir.join(format!("{}.exe", bin_name));
#[cfg(not(windows))]
let bin_path = out_dir.join(&bin_name);
#[cfg(unix)]
if !options.debug {
strip(&bin_path, &logger)?;
}
if let Some(product_name) = config_.package.product_name.clone() {
let bin_name = app_settings
.cargo_package_settings()
.name
.clone()
.expect("Cargo manifest must have the `package.name` field");
#[cfg(windows)]
let (bin_path, product_path) = {
(
out_dir.join(format!("{}.exe", bin_name)),
out_dir.join(format!("{}.exe", product_name)),
)
};
let product_path = out_dir.join(format!("{}.exe", product_name));
#[cfg(target_os = "macos")]
let (bin_path, product_path) = { (out_dir.join(bin_name), out_dir.join(product_name)) };
let product_path = out_dir.join(product_name);
#[cfg(target_os = "linux")]
let (bin_path, product_path) = {
(
out_dir.join(bin_name),
out_dir.join(product_name.to_kebab_case()),
)
};
let product_path = out_dir.join(product_name.to_kebab_case());
rename(&bin_path, &product_path).with_context(|| {
format!(
"failed to rename `{}` to `{}`",
@ -308,3 +309,34 @@ fn print_signed_updater_archive(output_paths: &[PathBuf]) -> crate::Result<()> {
}
Ok(())
}
// TODO: drop this when https://github.com/rust-lang/rust/issues/72110 is stabilized
#[cfg(unix)]
fn strip(path: &std::path::Path, logger: &Logger) -> crate::Result<()> {
use humansize::{file_size_opts, FileSize};
let filesize_before = std::fs::metadata(&path)
.with_context(|| "failed to get executable file size")?
.len();
// Strip the binary
Command::new("strip")
.arg(&path)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.with_context(|| "failed to execute strip")?;
let filesize_after = std::fs::metadata(&path)
.with_context(|| "failed to get executable file size")?
.len();
logger.log(format!(
"Binary stripped, size reduced by {}",
(filesize_before - filesize_after)
.file_size(file_size_opts::CONVENTIONAL)
.unwrap(),
));
Ok(())
}