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
use crate::patterns::{merge_iter, merge_tasks_partials};
use crate::project::LanguageType;
use crate::project_config::{LayerType, StackType};
use crate::shapes::{FilePath, Input, OneOrMany};
use crate::task_config::{TaskConfig, TaskDependency, validate_deps};
use crate::task_options_config::{PartialTaskOptionsConfig, TaskOptionsConfig};
use crate::{config_enum, config_struct};
use moon_common::{Id, cacheable};
use rustc_hash::FxHashMap;
use schematic::schema::indexmap::IndexMap;
use schematic::{Config, merge, validate};
use std::collections::BTreeMap;
use std::path::Path;
#[derive(Default)]
pub struct InheritFor<'a> {
pub language: Option<&'a LanguageType>,
pub layer: Option<&'a LayerType>,
pub root: Option<&'a Path>,
pub stack: Option<&'a StackType>,
pub tags: Option<&'a [Id]>,
pub toolchains: Option<&'a [Id]>,
}
impl<'a> InheritFor<'a> {
pub fn language(mut self, language: &'a LanguageType) -> Self {
self.language = Some(language);
self
}
pub fn layer(mut self, layer: &'a LayerType) -> Self {
self.layer = Some(layer);
self
}
pub fn root(mut self, root: &'a Path) -> Self {
self.root = Some(root);
self
}
pub fn stack(mut self, stack: &'a StackType) -> Self {
self.stack = Some(stack);
self
}
pub fn tags(mut self, tags: &'a [Id]) -> Self {
self.tags = Some(tags);
self
}
pub fn toolchains(mut self, toolchains: &'a [Id]) -> Self {
self.toolchains = Some(toolchains);
self
}
}
config_struct!(
/// A condition that utilizes a combination of logical operators
/// to match against. When matching, all clauses must be satisfied.
#[derive(Config)]
pub struct InheritedClauseConfig {
/// Require all values to match, using an AND operator.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub and: Option<OneOrMany<Id>>,
/// Require any values to match, using an OR operator.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub or: Option<OneOrMany<Id>>,
/// Require no values to match, using a NOT operator.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub not: Option<OneOrMany<Id>>,
}
);
impl InheritedClauseConfig {
pub fn matches(&self, values: &[Id]) -> bool {
if self.not.is_none() && self.and.is_none() && self.or.is_none() {
return false;
}
if let Some(not) = &self.not
&& not.to_list().iter().any(|value| values.contains(value))
{
return false;
}
if let Some(and) = &self.and
&& !and.to_list().iter().all(|value| values.contains(value))
{
return false;
}
if let Some(or) = &self.or
&& !or.to_list().iter().any(|value| values.contains(value))
{
return false;
}
true
}
}
config_enum!(
/// Patterns in which a condition can be configured as.
#[derive(Config)]
#[serde(untagged)]
pub enum InheritedConditionConfig {
/// Condition applies to a single value.
One(Id),
/// Condition applies to multiple values,
/// and matches using an OR operator.
Many(Vec<Id>),
/// Condition applies using logical operator clauses.
#[setting(nested)]
Clause(InheritedClauseConfig),
}
);
impl InheritedConditionConfig {
pub fn matches(&self, values: &[Id]) -> bool {
match self {
Self::Clause(inner) => inner.matches(values),
Self::Many(inner) => values.iter().any(|value| inner.contains(value)),
Self::One(inner) => values.contains(inner),
}
}
}
config_struct!(
/// Configures conditions that must match against a project for tasks
/// to be inherited. If multiple conditions are defined, then all must match
/// for inheritance to occur. If no conditions are defined, then tasks will
/// be inherited by all projects.
#[derive(Config)]
#[serde(default)]
pub struct InheritedByConfig {
/// The order in which this configuration is inherited by a project.
/// Lower is inherited first, while higher is last.
#[serde(skip_serializing_if = "Option::is_none")]
pub order: Option<u16>,
/// Condition that matches against literal files within a project.
/// If multiple values are provided, at least 1 file needs to exist.
#[setting(alias = "file")]
#[serde(skip_serializing_if = "Option::is_none")]
pub files: Option<OneOrMany<FilePath>>,
/// Condition that matches against a project's `language`.
/// If multiple values are provided, it matches using an OR operator.
#[setting(alias = "language")]
#[serde(skip_serializing_if = "Option::is_none")]
pub languages: Option<OneOrMany<LanguageType>>,
/// Condition that matches against a project's `layer`.
/// If multiple values are provided, it matches using an OR operator.
#[setting(alias = "layer")]
#[serde(skip_serializing_if = "Option::is_none")]
pub layers: Option<OneOrMany<LayerType>>,
/// Condition that matches against a project's `stack`.
/// If multiple values are provided, it matches using an OR operator.
#[setting(alias = "stack")]
#[serde(skip_serializing_if = "Option::is_none")]
pub stacks: Option<OneOrMany<StackType>>,
/// Condition that matches against a tag within the project.
#[setting(alias = "tag", nested)]
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<InheritedConditionConfig>,
/// Condition that matches against a toolchain detected for a project.
#[setting(alias = "toolchain", nested)]
#[serde(skip_serializing_if = "Option::is_none")]
pub toolchains: Option<InheritedConditionConfig>,
}
);
impl InheritedByConfig {
pub fn default_toolchain(&self) -> Option<Id> {
self.toolchains.as_ref().and_then(|entry| match entry {
InheritedConditionConfig::One(id) => Some(id.to_owned()),
InheritedConditionConfig::Many(ids) => {
if ids.len() == 1 {
Some(ids[0].to_owned())
} else {
None
}
}
_ => None,
})
}
// 0 - (files)
// 50 - node
// 100 - frontend
// 150 - library
// 150 - node-frontend
// 200 - node-library
// 250 - frontend-library
// 300 - node-frontend-library
// 500 - (tags)
pub fn order(&self) -> u16 {
if let Some(order) = self.order {
return order;
}
let mut amount = 0;
// Toolchains/languages are the lowest level
if self.toolchains.is_some() || self.languages.is_some() {
amount += 50;
}
// Stacks are the middle level
if self.stacks.is_some() {
amount += 100;
}
// Layers are the highest level
if self.layers.is_some() {
amount += 150;
}
// Tags are their own level (typically)
if self.tags.is_some() {
amount += 500;
}
amount
}
pub fn matches(&self, input: &InheritFor) -> bool {
if let Some(condition) = &self.stacks
&& let Some(value) = &input.stack
&& !condition.matches(value)
{
return false;
}
if let Some(condition) = &self.languages
&& let Some(value) = &input.language
&& !condition.matches(value)
{
return false;
}
if let Some(condition) = &self.layers
&& let Some(value) = &input.layer
&& !condition.matches(value)
{
return false;
}
if let Some(condition) = &self.tags
&& let Some(value) = &input.tags
&& !condition.matches(value)
{
return false;
}
if let Some(condition) = &self.toolchains
&& let Some(value) = &input.toolchains
&& !condition.matches(value)
{
return false;
}
if let Some(files) = &self.files
&& let Some(value) = &input.root
&& !files.to_list().iter().any(|file| value.join(file).exists())
{
return false;
}
true
}
}
config_struct!(
/// Configures tasks and task related settings that'll be inherited by all
/// matching projects.
/// Docs: https://moonrepo.dev/docs/config/tasks
#[derive(Config)]
#[serde(default)]
pub struct InheritedTasksConfig {
#[setting(default = "../cache/schemas/tasks.json", rename = "$schema")]
pub schema: String,
/// Extends one or many tasks configuration files.
/// Supports a relative file path or a secure URL.
/// @since 1.12.0
#[setting(extend, validate = validate::extends_from)]
#[serde(skip_serializing_if = "Option::is_none")]
pub extends: Option<schematic::ExtendsFrom>,
/// A map of group identifiers to a list of file paths, globs, and
/// environment variables, that can be referenced from tasks.
#[setting(merge = merge_iter)]
#[serde(skip_serializing_if = "FxHashMap::is_empty")]
pub file_groups: FxHashMap<Id, Vec<Input>>,
/// Task dependencies (`deps`) that will be automatically injected into every
/// task that inherits this configuration.
#[setting(nested, merge = merge::append_vec, validate = validate_deps)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub implicit_deps: Vec<TaskDependency>,
/// Task inputs (`inputs`) that will be automatically injected into every
/// task that inherits this configuration.
#[setting(merge = merge::append_vec)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub implicit_inputs: Vec<Input>,
/// A map of conditions that define which projects will inherit these
/// tasks and configuration. If not defined, will be inherited by all projects.
/// @since 2.0.0
#[setting(nested)]
#[serde(skip_serializing_if = "Option::is_none")]
pub inherited_by: Option<InheritedByConfig>,
/// A map of identifiers to task objects. Tasks represent the work-unit
/// of a project, and can be ran in the action pipeline.
#[setting(nested, merge = merge_tasks_partials)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub tasks: BTreeMap<Id, TaskConfig>,
/// Default task options for all inherited tasks.
/// @since 1.20.0
#[setting(nested)]
#[serde(skip_serializing_if = "Option::is_none")]
pub task_options: Option<TaskOptionsConfig>,
}
);
cacheable!(
#[derive(Clone, Debug, Default)]
pub struct InheritedTasks {
// Inherited configs in order
pub configs: IndexMap<String, InheritedTasksConfig>,
// What was inherited for eash task
pub layers: FxHashMap<String, Vec<String>>,
}
);