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
418
419
420
421
use crate::shapes::{FilePath, GlobPath, OneOrMany};
use crate::{config_enum, config_struct, config_unit_enum, config_untagged_enum, generate_switch};
use schematic::schema::{StringType, UnionType};
use schematic::{Config, ConfigEnum, Schema, SchemaBuilder, Schematic, ValidateError};
use std::env::consts;
fn validate_interactive<C>(
enabled: &bool,
options: &PartialTaskOptionsConfig,
_ctx: &C,
_finalize: bool,
) -> Result<(), ValidateError> {
if *enabled && options.persistent.is_some_and(|v| v) {
return Err(ValidateError::new(
"an interactive task cannot be persistent",
));
}
Ok(())
}
config_enum!(
/// The pattern in which affected files will be passed to the affected task.
#[serde(expecting = "expected `args`, `env`, or a boolean")]
pub enum TaskOptionAffectedFilesPattern {
/// Passed as command line arguments.
Args,
/// Passed as environment variables.
Env,
/// Passed as command line arguments and environment variables.
#[serde(untagged)]
Enabled(bool),
}
);
generate_switch!(TaskOptionAffectedFilesPattern, ["args", "env"]);
impl Default for TaskOptionAffectedFilesPattern {
fn default() -> Self {
Self::Enabled(true)
}
}
config_struct!(
/// Expanded information about affected files handling.
#[derive(Config)]
pub struct TaskOptionAffectedFilesConfig {
/// A list of glob patterns to filter the affected files list before
/// passing to the task as arguments or environment variables. Globs
/// must start with `**` to match against absolute paths.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub filter: Vec<GlobPath>,
/// When matching affected files, ignore the project boundary and include
/// workspace relative files. Otherwise, only files within the project are matched.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ignore_project_boundary: Option<bool>,
/// The pattern in which affected files will be passed to the affected task.
pub pass: TaskOptionAffectedFilesPattern,
/// When there are no affected files after matching, use the
/// task's inputs instead.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pass_inputs_when_no_match: Option<bool>,
/// When there are no affected files after matching and filtering,
/// use `.` instead of an empty value.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[setting(default = true)]
pub pass_dot_when_no_results: Option<bool>,
}
);
config_untagged_enum!(
#[derive(Config)]
pub enum TaskOptionAffectedFilesEntry {
Pattern(TaskOptionAffectedFilesPattern),
#[setting(nested)]
Object(TaskOptionAffectedFilesConfig),
}
);
config_enum!(
/// The environment in which to apply task caching.
#[serde(expecting = "expected `local`, `remote`, or a boolean")]
pub enum TaskOptionCache {
/// Only cache locally.
Local,
/// Only cache remotely.
Remote,
/// Cache both locally and remotely.
#[serde(untagged)]
Enabled(bool),
}
);
generate_switch!(TaskOptionCache, ["local", "remote"]);
impl TaskOptionCache {
pub fn is_local_enabled(&self) -> bool {
matches!(self, Self::Enabled(true) | Self::Local)
}
pub fn is_remote_enabled(&self) -> bool {
matches!(self, Self::Enabled(true) | Self::Remote)
}
}
config_untagged_enum!(
/// The pattern in which a task is dependent on a `.env` file.
pub enum TaskOptionEnvFile {
/// Uses an `.env` file in the project root.
Enabled(bool),
/// An explicit path to an `.env` file.
File(FilePath),
/// A list of explicit `.env` file paths.
Files(Vec<FilePath>),
}
);
impl Schematic for TaskOptionEnvFile {
fn schema_name() -> Option<String> {
Some("TaskOptionEnvFile".into())
}
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.union(UnionType::new_any([
schema.infer::<bool>(),
schema.infer::<String>(),
schema.infer::<Vec<String>>(),
]))
}
}
config_enum!(
/// The pattern in which to run the task automatically in CI.
#[serde(expecting = "expected `always`, `affected`, `only`, `skip`, or a boolean")]
pub enum TaskOptionRunInCI {
/// Always run, regardless of affected status.
Always,
/// Only run if affected by changed files.
Affected,
/// Only run in CI and not locally if affected by changed files.
Only,
/// Skip running in CI but run locally and allow task relationships to be valid.
Skip,
/// Either affected, or don't run at all.
#[serde(untagged)]
Enabled(bool),
}
);
generate_switch!(TaskOptionRunInCI, ["always", "affected", "only", "skip"]);
config_unit_enum!(
/// The strategy in which to merge a specific task option.
#[derive(ConfigEnum)]
pub enum TaskMergeStrategy {
#[default]
Append,
Prepend,
Preserve,
Replace,
}
);
config_unit_enum!(
/// The style in which task output will be printed to the console.
#[derive(ConfigEnum)]
pub enum TaskOutputStyle {
#[default]
Buffer,
BufferOnlyFailure,
Hash,
None,
Stream,
}
);
config_enum!(
/// The operating system in which to only run this task on.
#[derive(ConfigEnum, Copy)]
pub enum TaskOperatingSystem {
Linux,
#[serde(alias = "mac")]
Macos,
#[serde(alias = "win")]
Windows,
}
);
impl TaskOperatingSystem {
pub fn is_current_system(&self) -> bool {
let os = consts::OS;
match self {
Self::Linux => os == "linux" || os.ends_with("bsd"),
Self::Macos => os == "macos",
Self::Windows => os == "windows",
}
}
}
config_unit_enum!(
/// The priority levels a task can be bucketed into when running
/// in the action pipeline.
#[derive(ConfigEnum)]
pub enum TaskPriority {
Critical = 0,
High = 1,
#[default]
Normal = 2,
Low = 3,
}
);
impl TaskPriority {
pub fn get_level(&self) -> u8 {
*self as u8
}
}
config_unit_enum!(
/// A list of available shells on Unix.
#[derive(ConfigEnum)]
pub enum TaskUnixShell {
#[default]
Bash,
Elvish,
Fish,
Ion,
Murex,
#[serde(alias = "nushell")]
Nu,
#[serde(alias = "powershell")]
Pwsh,
Sh,
Xonsh,
Zsh,
}
);
config_unit_enum!(
/// A list of available shells on Windows.
#[derive(ConfigEnum)]
pub enum TaskWindowsShell {
Bash,
Elvish,
Fish,
Murex,
#[serde(alias = "nushell")]
Nu,
#[default]
Pwsh,
#[serde(rename = "powershell")]
PowerShell,
Xonsh,
}
);
config_struct!(
/// Options to control task inheritance, execution, and more.
#[derive(Config)]
#[serde(default)]
pub struct TaskOptionsConfig {
/// The pattern in which affected files will be passed to the task.
#[serde(skip_serializing_if = "Option::is_none")]
pub affected_files: Option<TaskOptionAffectedFilesEntry>,
/// Allow the task to fail without failing the entire action pipeline.
/// @since 1.13.0
#[serde(skip_serializing_if = "Option::is_none")]
pub allow_failure: Option<bool>,
/// Cache the `outputs` of the task for incremental builds.
/// Defaults to `true` if outputs are configured for the task.
#[serde(skip_serializing_if = "Option::is_none")]
pub cache: Option<TaskOptionCache>,
/// A custom key to include in the cache hashing process. Can be
/// used to invalidate local and remote caches.
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_key: Option<String>,
/// Lifetime to cache the task itself, in the format of "1h", "30m", etc.
/// If not defined, caches live forever, or until inputs change.
/// @since 1.29.0
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_lifetime: Option<String>,
/// Loads and sets environment variables from the `.env` file(s) when
/// running the task.
#[serde(skip_serializing_if = "Option::is_none")]
pub env_file: Option<TaskOptionEnvFile>,
/// Automatically infer inputs from file groups or environment variables
/// that were utilized within `command`, `script`, `args`, and `env`.
/// @since 1.31.0
#[serde(skip_serializing_if = "Option::is_none")]
pub infer_inputs: Option<bool>,
/// Marks the task as interactive, so that it will run in isolation,
/// and have direct access to stdin.
/// @since 1.12.0
#[setting(validate = validate_interactive)]
#[serde(skip_serializing_if = "Option::is_none")]
pub interactive: Option<bool>,
/// Marks the task as internal, which disables it from being ran
/// from the command line, but can still be depended on by other tasks.
/// @since 1.23.0
#[serde(skip_serializing_if = "Option::is_none")]
pub internal: Option<bool>,
/// The default strategy to use when merging `args`, `deps`, `env`,
/// `inputs`, or `outputs` with an inherited task. Can be overridden
/// with the other field-specific merge options.
#[serde(skip_serializing_if = "Option::is_none")]
pub merge: Option<TaskMergeStrategy>,
/// The strategy to use when merging `args` with an inherited task.
#[serde(skip_serializing_if = "Option::is_none")]
pub merge_args: Option<TaskMergeStrategy>,
/// The strategy to use when merging `deps` with an inherited task.
#[serde(skip_serializing_if = "Option::is_none")]
pub merge_deps: Option<TaskMergeStrategy>,
/// The strategy to use when merging `env` with an inherited task.
#[serde(skip_serializing_if = "Option::is_none")]
pub merge_env: Option<TaskMergeStrategy>,
/// The strategy to use when merging `inputs` with an inherited task.
#[serde(skip_serializing_if = "Option::is_none")]
pub merge_inputs: Option<TaskMergeStrategy>,
/// The strategy to use when merging `outputs` with an inherited task.
#[serde(skip_serializing_if = "Option::is_none")]
pub merge_outputs: Option<TaskMergeStrategy>,
/// The strategy to use when merging `tags` with an inherited task.
/// @since 2.3.0
#[serde(skip_serializing_if = "Option::is_none")]
pub merge_tags: Option<TaskMergeStrategy>,
/// The strategy to use when merging `toolchains` with an inherited task.
/// @since 2.0.0
#[serde(skip_serializing_if = "Option::is_none")]
pub merge_toolchains: Option<TaskMergeStrategy>,
/// Creates an exclusive lock on a virtual resource, preventing other
/// tasks using the same resource from running concurrently.
/// @since 1.24.0
#[serde(skip_serializing_if = "Option::is_none")]
pub mutex: Option<String>,
/// The operating system in which to only run this task on.
#[serde(skip_serializing_if = "Option::is_none")]
pub os: Option<OneOrMany<TaskOperatingSystem>>,
/// The style in which task output will be printed to the console.
#[setting(env = "MOON_OUTPUT_STYLE")]
#[serde(skip_serializing_if = "Option::is_none")]
pub output_style: Option<TaskOutputStyle>,
/// Marks the task as persistent (continuously running). This is ideal
/// for watchers, servers, or never-ending processes.
/// @since 1.6.0
#[serde(skip_serializing_if = "Option::is_none")]
pub persistent: Option<bool>,
/// Marks the task with a certain priority, which determines the order
/// in which it is ran within the action pipeline.
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<TaskPriority>,
/// The number of times a failing task will be retried to succeed.
#[setting(env = "MOON_RETRY_COUNT")]
#[serde(skip_serializing_if = "Option::is_none")]
pub retry_count: Option<u8>,
/// Runs direct task dependencies (via `deps`) in sequential order.
/// This _does not_ apply to indirect or transient dependencies.
#[serde(skip_serializing_if = "Option::is_none")]
pub run_deps_in_parallel: Option<bool>,
/// Whether to run the task in CI or not, when executing `moon ci`,
/// `moon check`, or `moon run`.
#[setting(rename = "runInCI")]
#[serde(skip_serializing_if = "Option::is_none")]
pub run_in_ci: Option<TaskOptionRunInCI>,
/// Runs the task automatically when executing `moon sync`.
pub run_in_sync_phase: Option<bool>,
/// Runs the task from the workspace root, instead of the project root.
#[serde(skip_serializing_if = "Option::is_none")]
pub run_from_workspace_root: Option<bool>,
/// Runs the task within a shell. When not defined, runs the task
/// directly while relying on native `PATH` resolution.
#[serde(skip_serializing_if = "Option::is_none")]
pub shell: Option<bool>,
/// The maximum time in seconds that a task can run before being cancelled.
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u64>,
/// The shell to run the task in when on a Unix-based machine.
/// @since 1.21.0
#[serde(skip_serializing_if = "Option::is_none")]
pub unix_shell: Option<TaskUnixShell>,
/// The shell to run the task in when on a Windows machine.
/// @since 1.21.0
#[serde(skip_serializing_if = "Option::is_none")]
pub windows_shell: Option<TaskWindowsShell>,
}
);