-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlib.rs
More file actions
310 lines (276 loc) · 10.1 KB
/
lib.rs
File metadata and controls
310 lines (276 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2023-2023 CrabNebula Ltd.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
//! [](https://github.com/crabnebula-dev/cargo-packager)
//!
//! Executable packager, bundler and updater. A cli tool and library to generate installers or app bundles for your executables.
//! It also comes with useful addons:
//! - an [updater](https://docs.rs/cargo-packager-updater)
//! - a [resource resolver](https://docs.rs/cargo-packager-resource-resolver)
//!
//! ### Supported packages
//!
//! - macOS
//! - DMG (.dmg)
//! - Bundle (.app)
//! - Linux
//! - Debian package (.deb)
//! - AppImage (.AppImage)
//! - Pacman (.tar.gz and PKGBUILD)
//! - Windows
//! - NSIS (.exe)
//! - MSI using WiX Toolset (.msi)
//!
//! ## CLI
//!
//! This crate is a cargo subcommand so you can install using:
//!
//! ```sh
//! cargo install cargo-packager --locked
//! ```
//! You then need to configure your app so the cli can recognize it. Configuration can be done in `Packager.toml` or `packager.json` in your project or modify Cargo.toml and include this snippet:
//!
//! ```toml
//! [package.metadata.packager]
//! before-packaging-command = "cargo build --release"
//! ```
//!
//! Once, you are done configuring your app, run:
//!
//! ```sh
//! cargo packager --release
//! ```
//!
//! ### Configuration
//!
//! By default, the packager reads its configuration from `Packager.toml` or `packager.json` if it exists, and from `package.metadata.packager` table in `Cargo.toml`.
//! You can also specify a custom configuration using the `-c/--config` cli argument.
//!
//! For a full list of configuration options, see [Config].
//!
//! You could also use the [schema](./schema.json) file from GitHub to validate your configuration or have auto completions in your IDE.
//!
//! ### Building your application before packaging
//!
//! By default, the packager doesn't build your application, so if your app requires a compilation step, the packager has an option to specify a shell command to be executed before packaing your app, `beforePackagingCommand`.
//!
//! ### Cargo profiles
//!
//! By default, the packager looks for binaries built using the `debug` profile, if your `beforePackagingCommand` builds your app using `cargo build --release`, you will also need to
//! run the packager in release mode `cargo packager --release`, otherwise, if you have a custom cargo profile, you will need to specify it using `--profile` cli arg `cargo packager --profile custom-release-profile`.
//!
//! ### Library
//!
//! This crate is also published to crates.io as a library that you can integrate into your tooling, just make sure to disable the default-feature flags.
//!
//! ```sh
//! cargo add cargo-packager --no-default-features
//! ```
//!
//! #### Feature flags
//!
//! - **`cli`**: Enables the cli specifc features and dependencies. Enabled by default.
//! - **`tracing`**: Enables `tracing` crate integration.
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![deny(missing_docs)]
use std::{collections::HashMap, fs::File, io::Write, path::PathBuf};
mod codesign;
mod error;
mod package;
mod shell;
mod util;
#[cfg(feature = "cli")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "cli")))]
pub mod cli;
pub mod config;
pub mod sign;
pub use config::{Config, PackageFormat};
pub use error::{Error, Result};
use flate2::{write::GzEncoder, Compression};
use serde::Serialize;
pub use sign::SigningConfig;
pub use package::{package, PackageOutput};
use util::PathExt;
use crate::package::PackageOutputSummary;
#[cfg(feature = "cli")]
fn parse_log_level(verbose: u8) -> tracing::Level {
match verbose {
0 => tracing_subscriber::EnvFilter::builder()
.from_env_lossy()
.max_level_hint()
.and_then(|l| l.into_level())
.unwrap_or(tracing::Level::INFO),
1 => tracing::Level::DEBUG,
2.. => tracing::Level::TRACE,
}
}
/// Inits the tracing subscriber.
#[cfg(feature = "cli")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "cli")))]
pub fn init_tracing_subscriber(verbosity: u8) {
let level = parse_log_level(verbosity);
let debug = level == tracing::Level::DEBUG;
let tracing = level == tracing::Level::TRACE;
let subscriber = tracing_subscriber::fmt()
.with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
.with_target(debug)
.with_line_number(tracing)
.with_file(tracing)
.with_max_level(level);
let formatter = tracing_subscriber::fmt::format()
.compact()
.with_target(debug)
.with_line_number(tracing)
.with_file(tracing);
if tracing {
subscriber
.event_format(TracingFormatter::WithTime(formatter))
.init();
} else {
subscriber
.without_time()
.event_format(TracingFormatter::WithoutTime(formatter.without_time()))
.init();
}
}
#[cfg(feature = "cli")]
enum TracingFormatter {
WithoutTime(
tracing_subscriber::fmt::format::Format<tracing_subscriber::fmt::format::Compact, ()>,
),
WithTime(tracing_subscriber::fmt::format::Format<tracing_subscriber::fmt::format::Compact>),
}
#[cfg(feature = "cli")]
struct ShellFieldVisitor {
message: String,
}
#[cfg(feature = "cli")]
impl tracing::field::Visit for ShellFieldVisitor {
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
if field.name() == "message" {
self.message = value.to_string();
}
}
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
if field.name() == "message" {
self.message = format!("{value:?}");
}
}
}
#[cfg(feature = "cli")]
impl<S, N> tracing_subscriber::fmt::FormatEvent<S, N> for TracingFormatter
where
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
N: for<'a> tracing_subscriber::fmt::FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
mut writer: tracing_subscriber::fmt::format::Writer<'_>,
event: &tracing::Event<'_>,
) -> std::fmt::Result {
if event.fields().any(|f| f.name() == "shell") {
let mut visitor = ShellFieldVisitor { message: "".into() };
event.record(&mut visitor);
writeln!(writer, "{}", visitor.message)
} else {
match self {
TracingFormatter::WithoutTime(formatter) => {
formatter.format_event(ctx, writer, event)
}
TracingFormatter::WithTime(formatter) => formatter.format_event(ctx, writer, event),
}
}
}
}
/// Sign the specified packages and return the signatures paths.
///
/// If `packages` contain a directory in the case of [`PackageFormat::App`]
/// it will zip the directory before signing and appends it to `packages`.
#[tracing::instrument(level = "trace")]
pub fn sign_outputs(
config: &SigningConfig,
packages: &mut Vec<PackageOutput>,
) -> crate::Result<Vec<PathBuf>> {
let mut signatures = Vec::new();
for package in packages {
for path in &package.paths.clone() {
let path = if path.is_dir() {
let zip = path.with_additional_extension("tar.gz");
let dest_file = util::create_file(&zip)?;
let gzip_encoder = GzEncoder::new(dest_file, Compression::default());
let writer = util::create_tar_from_dir(path, gzip_encoder)?;
let mut dest_file = writer.finish()?;
dest_file.flush()?;
package.paths.push(zip);
package.paths.last().unwrap()
} else {
path
};
let (sig_file, sig) = sign::sign_file(config, path)?;
// Add signature to package summary
if let Some(summary) = &mut package.summary {
summary.signature = Some(sig);
}
signatures.push(sig_file);
}
}
Ok(signatures)
}
/// Create a `latest.json` output summarising the built packages
pub fn summarise_outputs(
config: &Config,
packages: &mut Vec<PackageOutput>,
) -> crate::Result<PathBuf> {
#[derive(Debug, Clone, Serialize)]
struct InnerRemoteRelease {
version: String,
notes: Option<String>,
pub_date: Option<String>,
platforms: Option<HashMap<String, PackageOutputSummary>>,
}
//Collect releases
let mut platforms = HashMap::with_capacity(packages.len());
for package in packages {
if let Some(summary) = package.summary.clone() {
// if summary.signature.is_some() {
platforms.insert(summary.platform.clone(), summary);
// } else {
// // The signer failed to update the signature field
// tracing::warn!("A package could not be summarized in latest.json because it could not be signed.")
// }
}
}
// Write latest.json
let release = InnerRemoteRelease {
version: config.version.clone(),
notes: None,
pub_date: Some(
time::OffsetDateTime::now_utc()
.format(&time::format_description::well_known::Rfc3339)
.unwrap(),
),
platforms: Some(platforms),
};
//Write it to the file
let summary_path = config.out_dir().join("latest.json");
let summary_file = File::create(summary_path.clone())?;
serde_json::to_writer_pretty(summary_file, &release)?;
tracing::info!("Finished summarising at:\n{}", summary_path.display());
Ok(summary_path)
}
/// Package an app using the specified config.
/// Then signs the generated packages.
///
/// This is similar to calling `sign_outputs(signing_config, package(config)?)`
///
/// Returns a tuple of list of packages and list of signatures.
#[tracing::instrument(level = "trace")]
pub fn package_and_sign(
config: &Config,
signing_config: &SigningConfig,
) -> crate::Result<(Vec<PackageOutput>, Vec<PathBuf>)> {
let mut packages = package(config)?;
let signatures = sign_outputs(signing_config, &mut packages)?;
Ok((packages, signatures))
}