Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/tools/ci/src/prepare.rs
6596 views
1
use crate::args::Args;
2
3
/// Trait for preparing a subcommand to be run.
4
pub trait Prepare {
5
/// A method that returns a list of [`PreparedCommand`]s to be run for a given shell and flags.
6
///
7
/// # Example
8
///
9
/// ```
10
/// # use crate::{args::Args, Prepare, PreparedCommand};
11
/// # use argh::FromArgs;
12
/// # use xshell::Shell;
13
/// #
14
/// #[derive(FromArgs)]
15
/// #[argh(subcommand, name = "check")]
16
/// struct CheckCommand {}
17
///
18
/// impl Prepare for CheckCommand {
19
/// fn prepare<'a>(&self, sh: &'a Shell, args: Args) -> Vec<PreparedCommand<'a>> {
20
/// vec![PreparedCommand::new::<Self>(
21
/// cmd!(sh, "cargo check --workspace"),
22
/// "Please fix linter errors",
23
/// )]
24
/// }
25
/// }
26
/// ```
27
fn prepare<'a>(&self, sh: &'a xshell::Shell, args: Args) -> Vec<PreparedCommand<'a>>;
28
}
29
30
/// A command with associated metadata, created from a command that implements [`Prepare`].
31
#[derive(Debug)]
32
pub struct PreparedCommand<'a> {
33
/// The name of the command.
34
pub name: &'static str,
35
36
/// The command to execute
37
pub command: xshell::Cmd<'a>,
38
39
/// The message to display if the test command fails
40
pub failure_message: &'static str,
41
42
/// The subdirectory path to run the test command within
43
pub subdir: Option<&'static str>,
44
45
/// Environment variables that need to be set before the test runs
46
pub env_vars: Vec<(&'static str, &'static str)>,
47
}
48
49
impl<'a> PreparedCommand<'a> {
50
/// Creates a new [`PreparedCommand`] from a [`Cmd`] and a failure message.
51
///
52
/// The other fields of [`PreparedCommand`] are filled in with their default values.
53
///
54
/// For more information about creating a [`Cmd`], please see the [`cmd!`](xshell::cmd) macro.
55
///
56
/// [`Cmd`]: xshell::Cmd
57
pub fn new<T: argh::SubCommand>(
58
command: xshell::Cmd<'a>,
59
failure_message: &'static str,
60
) -> Self {
61
Self {
62
command,
63
name: T::COMMAND.name,
64
failure_message,
65
subdir: None,
66
env_vars: vec![],
67
}
68
}
69
70
/// A builder that overwrites the current sub-directory with a new value.
71
pub fn with_subdir(mut self, subdir: &'static str) -> Self {
72
self.subdir = Some(subdir);
73
self
74
}
75
76
/// A builder that adds a new environmental variable to the list.
77
pub fn with_env_var(mut self, key: &'static str, value: &'static str) -> Self {
78
self.env_vars.push((key, value));
79
self
80
}
81
}
82
83