beet_core 0.0.8

Core utilities and types for other beet crates
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::prelude::*;
use std::path::PathBuf;
use std::process::Command;

/// Verbatim clone of cargo build/run args
#[derive(Debug, Clone, Component)]
pub struct CargoBuildCmd {
	/// The top level command to run: `build`, `run`, `test`, etc.
	pub cmd: String,
	/// Package with the target to run
	pub package: Option<String>,
	/// Name of the bin target to run
	pub bin: Option<String>,
	/// Name of the example target to run
	pub example: Option<String>,
	/// Specify the integration test to
	pub test: Option<String>,
	/// Build artifacts in release mode, with optimizations
	pub release: bool,
	/// Only test lib
	pub lib: bool,
	/// Only test docs
	pub doc: bool,
	/// Build for the target triple
	pub target: Option<String>,
	pub message_format: Option<String>,
	/// Use verbose output (-vv very verbose/build.rs output)
	pub verbose: u8,
	/// Do not print cargo log messages
	pub quiet: bool,
	/// Coloring: auto, always, never
	pub color: Option<String>,
	/// Override a configuration value
	pub config: Option<String>,
	/// Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
	pub z: Option<String>,
	/// Space or comma separated list of features to activate
	pub features: Option<String>,
	/// Activate all available features
	pub all_features: bool,
	/// Do not activate the `default` feature
	pub no_default_features: bool,
	/// Number of parallel jobs, defaults to # of CPUs.
	pub jobs: Option<String>,
	/// Do not abort the build as soon as there is an error
	pub keep_going: bool,
	/// Build artifacts with the specified profile
	pub profile: Option<String>,
	/// Directory for all generated artifacts
	pub target_dir: Option<String>,
	/// Output build graph in JSON (unstable)
	pub unit_graph: bool,
	/// Timing output formats (unstable) (comma separated): html, json
	pub timings: Option<String>,
	/// Path to Cargo.toml
	pub manifest_path: Option<String>,
	/// Path to Cargo.lock (unstable)
	pub lockfile_path: Option<String>,
	/// Ignore `rust-version` specification in packages
	pub ignore_rust_version: bool,
	/// Assert that `Cargo.lock` will remain unchanged
	pub locked: bool,
	/// Run without accessing the network
	pub offline: bool,
	/// Equivalent to specifying both --locked and --offline
	pub frozen: bool,
	/// Any additional arguments passed to cargo
	pub trailing_args: Vec<String>,
}

impl Default for CargoBuildCmd {
	fn default() -> Self {
		Self {
			cmd: "build".to_string(),
			package: None,
			bin: None,
			example: None,
			test: None,
			release: false,
			lib: false,
			doc: false,
			target: None,
			message_format: None,
			verbose: 0,
			quiet: false,
			color: None,
			config: None,
			z: None,
			features: None,
			all_features: false,
			no_default_features: false,
			jobs: None,
			keep_going: false,
			profile: None,
			target_dir: None,
			unit_graph: false,
			timings: None,
			manifest_path: None,
			lockfile_path: None,
			ignore_rust_version: false,
			locked: false,
			offline: false,
			frozen: false,
			trailing_args: Vec::new(),
		}
	}
}

impl CargoBuildCmd {
	pub fn new(cmd: impl Into<String>) -> Self {
		Self {
			cmd: cmd.into(),
			..default()
		}
	}

	pub fn cmd(mut self, cmd: impl Into<String>) -> Self {
		self.cmd = cmd.into();
		self
	}

	pub fn release(mut self) -> Self {
		self.release = true;
		self
	}

	pub fn package(mut self, package: impl Into<String>) -> Self {
		self.package = Some(package.into());
		self
	}

	pub fn no_default_features(mut self) -> Self {
		self.no_default_features = true;
		self
	}
	pub fn target(mut self, target: impl Into<String>) -> Self {
		self.target = Some(target.into());
		self
	}
	pub fn trailing_arg(mut self, arg: impl Into<String>) -> Self {
		self.trailing_args.push(arg.into());
		self
	}

	pub fn feature(mut self, feature: impl Into<String>) -> Self {
		self.push_feature(feature);
		self
	}

	pub fn push_feature(&mut self, feature: impl Into<String>) -> &mut Self {
		let feature = feature.into();
		if let Some(features) = &mut self.features {
			features.push_str(&format!(",{}", feature));
		} else {
			self.features = Some(feature);
		}
		self
	}
	/// ## Panics
	///
	/// Panics if no crate name provided and no package, bin or example is set.
	pub fn binary_name(&self, pkg_name: Option<&str>) -> String {
		if let Some(bin) = &self.bin {
			bin.clone()
		} else if let Some(example) = &self.example {
			example.clone()
		} else if let Some(pkg) = &self.package {
			pkg.clone()
		} else if let Some(pkg_name) = pkg_name {
			// otherwise it must be the default crate name
			pkg_name.to_string()
		} else {
			panic!("No binary or package name provided.");
		}
	}

	/// Best effort attempt to retrieve the path to the executable.
	/// For packages, binaries and examples that name is used to resolve the
	/// executable name, otherwise the crate name is used.
	/// In the case of a wasm target, the path will have a `.wasm` extension.
	/// - The `pkg_name` must be passed in, this is different from [`Self::package`]
	/// 	in that we need this name even for a non-workspace Cargo.toml
	///
	/// ## Panics
	///
	/// Panics if no crate name provided and no package, bin or example is set.
	pub fn exe_path(&self, pkg_name: Option<&str>) -> PathBuf {
		let target_dir = env_ext::var("CARGO_TARGET_DIR")
			.unwrap_or_else(|_| "target".to_string());
		let mut path = PathBuf::from(target_dir);

		if let Some(target) = &self.target {
			path.push(target);
		}

		if self.release {
			path.push("release");
		} else {
			path.push("debug");
		}

		if let Some(example) = &self.example {
			path.push("examples");
			path.push(example);
		// package examples are not nested under package name
		} else if let Some(pkg) = &self.package {
			path.push(pkg);
		} else if let Some(bin) = &self.bin {
			path.push(bin);
		} else if let Some(pkg_name) = pkg_name {
			path.push(pkg_name);
		} else {
			panic!(
				"No crate name provided and no package, bin or example is set."
			);
		}

		if let Some("wasm32-unknown-unknown") = self.target.as_deref() {
			path.set_extension("wasm");
		}

		path
	}


	/// Returns the arguments to be passed to the cargo command,
	/// excluding `cargo` itself.
	pub fn get_args(&self) -> Vec<&str> {
		let CargoBuildCmd {
			cmd,
			package,
			bin,
			example,
			test,
			lib,
			doc,
			release,
			target,
			trailing_args,
			message_format,
			verbose,
			quiet,
			color,
			config,
			z,
			features,
			all_features,
			no_default_features,
			jobs,
			keep_going,
			profile,
			target_dir,
			unit_graph,
			timings,
			manifest_path,
			lockfile_path,
			ignore_rust_version,
			locked,
			offline,
			frozen,
		} = self;


		// Collect args in a vector for printing
		let mut args = Vec::new();
		args.push(cmd.as_str());

		if let Some(pkg) = package {
			args.push("--package");
			args.push(pkg.as_str());
		}
		if let Some(bin) = bin {
			args.push("--bin");
			args.push(bin.as_str());
		}
		if let Some(ex) = example {
			args.push("--example");
			args.push(ex.as_str());
		}
		if let Some(test) = test {
			args.push("--test");
			args.push(test.as_str());
		}
		if *lib {
			args.push("--lib");
		}
		if *doc {
			args.push("--doc");
		}
		if *release {
			args.push("--release");
		}
		if let Some(target) = target {
			args.push("--target");
			args.push(target.as_str());
		}

		if let Some(format) = message_format {
			args.push("--message-format");
			args.push(format.as_str());
		}
		match verbose {
			1 => {
				args.push("-v");
			}
			2 => {
				args.push("-vv");
			}
			n if *n > 2 => {
				args.push("-vvv");
			}
			_ => {}
		}
		if *quiet {
			args.push("--quiet");
		}
		if let Some(color_opt) = color {
			args.push("--color");
			args.push(color_opt.as_str());
		}
		if let Some(config_value) = config {
			args.push("--config");
			args.push(config_value.as_str());
		}
		if let Some(z_flag) = z {
			args.push("-Z");
			args.push(z_flag.as_str());
		}
		if *no_default_features {
			args.push("--no-default-features");
		}
		if *all_features {
			args.push("--all-features");
		}
		if let Some(features_list) = features {
			args.push("--features");
			args.push(features_list.as_str());
		}
		if let Some(jobs_count) = jobs {
			args.push("--jobs");
			args.push(jobs_count.as_str());
		}
		if *keep_going {
			args.push("--keep-going");
		}
		if let Some(profile_name) = profile {
			args.push("--profile");
			args.push(profile_name.as_str());
		}
		if let Some(dir) = target_dir {
			args.push("--target-dir");
			args.push(dir.as_str());
		}
		if *unit_graph {
			args.push("--unit-graph");
		}
		if let Some(timings_format) = timings {
			args.push("--timings");
			args.push(timings_format.as_str());
		}
		if let Some(manifest) = manifest_path {
			args.push("--manifest-path");
			args.push(manifest.as_str());
		}
		if let Some(lockfile) = lockfile_path {
			args.push("--lockfile-path");
			args.push(lockfile.as_str());
		}
		if *ignore_rust_version {
			args.push("--ignore-rust-version");
		}
		if *locked {
			args.push("--locked");
		}
		if *offline {
			args.push("--offline");
		}
		if *frozen {
			args.push("--frozen");
		}

		if !trailing_args.is_empty() {
			args.push("--");
			for arg in trailing_args {
				args.push(arg);
			}
		}
		args
	}

	/// Blocking spawn of the cargo build command
	pub fn spawn(&self) -> Result<&Self> {
		let args = self.get_args();

		// Print the command
		debug!("Running: cargo {}", args.join(" "));

		// Build and execute command
		let mut command = Command::new("cargo");
		command.args(&args);


		command.status()?.exit_ok()?;
		Ok(self)
	}
}

//cargo build -p beet_site --message-format=json | jq -r 'select(.reason == "compiler-artifact" and .target.kind == ["bin"]) | .filenames[]'


#[cfg(test)]
mod test {
	use crate::prelude::*;

	#[test]
	fn works() { CargoBuildCmd::default().cmd.xpect_eq("build"); }
}