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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Schema of the build flow configuration file `zinoma.yml`.
///
/// In order to use Žinoma with your project, you need to create a file named `zinoma.yml`.
/// We recommend putting this file in the root directory of your project.
///
/// This struct describes the schema expected for this file. It assumes prior knowledge of the Yaml format.
///
/// __Example__
///
/// `zinoma.yml`:
///
/// ```yaml
/// targets:
///   download_dependencies:
///     input:
///       - paths: [package.json, package-lock.json]
///     output:
///       - paths: [node_modules]
///     build: npm install
///
///   test:
///     input:
///       - download_dependencies.output
///       - paths: [package.json, src, test]
///     build: npm test
///
///   lint:
///     input:
///       - download_dependencies.output
///       - paths: [package.json, src, test]
///     build: npm run lint
///
///   check:
///     dependencies: [test, lint]
///
///   start:
///     input:
///       - download_dependencies.output
///       - paths: [package.json, src]
///     service: exec npm run start
///
///   build:
///     dependencies: [check]
///     input:
///       - paths:
///         - Dockerfile
///         - package.json
///         - package-lock.json
///         - src
///     output:
///       - paths: [lambda.zip]
///     build: |
///       docker build -t build-my-project:latest .
///       docker create -ti --name build-my-project build-my-project:latest bash
///       docker cp build-my-project:/var/task/lambda.zip ./
///       docker rm -f build-my-project
/// ```
///
/// In this example:
///
/// - `zinoma check` will ensure the code complies to the test suites and the coding standards.
/// - `zinoma start --watch` will run the application and restart it whenever the sources are updated.
/// - `zinoma --clean build` will generate a clean artifact, ready to be deployed.
///
/// A fully functional and more advanced example project is available in [fbecart/zinoma-node-example](https://github.com/fbecart/zinoma-node-example).
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Project {
    /// Targets (aka tasks) of this project.
    ///
    /// [`Targets`] represent commands and scripts to execute in your build flow.
    ///
    /// [`Targets`]: struct.Target.html
    ///
    /// Targets run in parallel by default.
    /// To force targets to run sequentially, you can define [`dependencies`] on other targets.
    ///
    /// [`dependencies`]: enum.Target.html#variant.Build.field.dependencies
    ///
    /// Each target must have a unique name inside the project.
    /// The target name must be a string. It should start with an alphanumeric character or `_` and contain only alphanumeric characters, `-`, or `_`.
    ///
    /// __Example__
    ///
    /// ```yaml
    /// targets:
    ///   speak_cow:
    ///     build: echo 'Moo'
    ///   speak_dog:
    ///     build: echo 'Woof!'
    /// ```
    ///
    /// In this example:
    ///
    /// - `zinoma speak_cow` will print `Moo`
    /// - `zinoma speak_dog` will print `Woof!`
    /// - `zinoma speak_cow speak_dog` will print both `Moo` and `Woof!` (not necessarily in this order)
    #[serde(default)]
    pub targets: HashMap<String, Target>,

    /// Name of the project.
    ///
    /// A project name must be a string. It should start with an alphanumeric character or `_` and contain only alphanumeric characters, `-`, or `_`.
    ///
    /// Project names should be unique. Two projects cannot have the same name.
    ///
    /// __Example__
    ///
    /// ```yaml
    /// name: my_project
    /// ```
    #[serde(default)]
    pub name: Option<String>,

    /// Import definitions from other Žinoma projects.
    ///
    /// `imports` should be an object, the keys being the project names and the values their respective paths.
    ///
    /// Before importing a project, you should make sure this project has its name defined.
    /// You should use the same name as key in the `imports` object.
    ///
    /// Once a project is imported, targets from that project can be referenced by specifying their fully qualified name: `imported_project_name::target_name`.
    ///
    /// __Example__
    ///
    /// `packages/api/zinoma.yml`:
    ///
    /// ```yaml
    /// name: api
    ///
    /// targets:
    ///   test:
    ///     build: cargo test
    /// ```
    ///
    /// `packages/webapp/zinoma.yml`:
    ///
    /// ```yaml
    /// name: webapp
    ///
    /// targets:
    ///   test:
    ///     build: cargo test
    /// ```
    ///
    /// `./zinoma.yml`:
    ///
    /// ```yaml
    /// imports:
    ///   api: packages/api
    ///   webapp: packages/webapp
    ///
    /// targets:
    ///   test_all:
    ///     dependencies: [api::test, webapp::test]
    /// ```
    ///
    /// In this example, the target `test_all` depend from targets defined in different projects.
    #[serde(default)]
    pub imports: HashMap<String, String>,
}

/// A target is a command or a set of commands to run as part of your build flow.
///
/// Targets run in parallel by default.
/// To force targets to run sequentially, you can define [`dependencies`] on other targets.
///
/// [`dependencies`]: struct.Dependencies.html
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, untagged)]
pub enum Target {
    /// A build target represents a shell script to run as part of your build flow.
    ///
    /// This build script is expected to eventually complete,
    /// as opposed to the run script of a [`service`] target.
    ///
    /// [`service`]: #variant.Service.field.service
    Build {
        /// Dependencies of the target.
        #[serde(default)]
        dependencies: Dependencies,

        /// The shell script to run in order to build this target.
        ///
        /// It should be a string. This string can be multi-line, in case of scripts with multiple commands.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   create_file_deep:
        ///     build: |
        ///       mkdir -p deep/dir
        ///       touch deep/dir/file
        ///     output:
        ///       - paths: [deep/dir/file]
        /// ```
        ///
        /// In this example, running `zinoma create_file_deep` will execute the commands `mkdir -p deep/dir` and `touch deep/dir/my_file` sequentially.
        build: String,

        /// Input resources of the target.
        #[serde(default)]
        input: InputResources,

        /// Output resources of the target.
        #[serde(default)]
        output: OutputResources,
    },

    /// Service targets are useful to run scripts that do not complete.
    ///
    /// They enable the execution of long-lasting commands, such as servers.
    Service {
        /// Dependencies of the target.
        #[serde(default)]
        dependencies: Dependencies,

        /// Shell script starting a long-lasting service.
        ///
        /// It should be a string.
        ///
        /// If `zinoma` has no service target to run, it will automatically exit after all build targets ran to completion.
        /// On the contrary, if at least one service target is specified in the command line,
        /// `zinoma` will keep running even after all build targets completed, so that the services can remain alive.
        ///
        /// In watch mode (when the `--watch` flag is passed to `zinoma`), services are restarted when the relevant paths are modified.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   npm_server:
        ///     input:
        ///       - paths: [package.json, index.js]
        ///     service: npm start
        /// ```
        ///
        /// In this example, `zinoma npm_server --watch` will run `npm start`,
        /// and will restart this process every time `package.json` or `index.js` are updated.
        service: String,

        /// Input resources of the target.
        #[serde(default)]
        input: InputResources,
    },

    /// Aggregates other targets.
    ///
    /// __Example__
    ///
    /// ```yaml
    /// targets:
    ///   fmt:
    ///     build: cargo fmt -- --check
    ///   lint:
    ///     build: cargo clippy
    ///   test:
    ///     build: cargo test
    ///   check:
    ///     dependencies: [fmt, lint, test]
    /// ```
    ///
    /// In this example, the target named `check` aggregates the 3 other targets.
    /// `zinoma check` is equivalent to running `zinoma fmt lint test`.
    Aggregate {
        /// Dependencies of the target.
        dependencies: Dependencies,
    },
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, untagged)]
pub enum InputResource {
    /// Output resources of another target.
    ///
    /// It should be a string with the format `<project_name>::<target_name>.output`.
    /// If the other target is located in the same project, the project name can be skipped.
    /// The `input` would then have this format: `<target_name>.output`.
    ///
    /// When such an input is used:
    ///
    /// - all the output resources of the other target become input resources for this target;
    /// - the other target implicitly becomes a dependency to this target.
    ///
    /// __Example__
    ///
    /// ```yaml
    /// targets:
    ///   node_dependencies:
    ///     input:
    ///       - paths: [package.json, package-lock.json]
    ///     output:
    ///       - paths: [node_modules]
    ///     build: npm install
    ///
    ///   compile:
    ///     input:
    ///       - node_dependencies.output
    ///       - paths: [package.json, tsconfig.json, src]
    ///     output:
    ///       - paths: [dist]
    ///     build: tsc
    ///      
    ///   run:
    ///     input:
    ///       - node_dependencies.output
    ///       - paths: [package.json]
    ///       - compile.output
    ///     service: node dist/index.js
    /// ```
    DependencyOutput(String),
    Files {
        /// Paths to files or directories.
        ///
        /// It should be an array of strings.
        ///
        /// Each element of the array should be a path to a file or directory.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   npm_install:
        ///     input:
        ///       - paths: [package.json, package-lock.json]
        ///     output:
        ///       - paths: [node_modules]
        ///     build: npm install
        /// ```
        paths: Vec<String>,
        /// Filter files resource by file extensions.
        ///
        /// It should be an array of strings.
        ///
        /// If `extensions` are specified, only files matching at least one of the extensions will be included in the resource.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   fmt:
        ///     input:
        ///       - paths: [src, tests]
        ///         extensions: [rs]
        ///     build: exec cargo fmt --all -- --check
        extensions: Option<Vec<String>>,
    },
    CmdStdout {
        /// Shell script whose output identifies the state of a resource.
        ///
        /// It should be a string.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   build_docker_image:
        ///     input:
        ///       - paths: [Dockerfile, src]
        ///       - cmd_stdout: 'docker image ls base:latest --format "{{.ID}}"'
        ///     output:
        ///       - cmd_stdout: 'docker image ls webapp:latest --format "{{.ID}}"'
        ///     build: docker build -t webapp .
        /// ```
        cmd_stdout: String,
    },
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, untagged)]
pub enum OutputResource {
    Files {
        /// Paths to files or directories.
        ///
        /// It should be an array of strings.
        /// Each element of the array should be a path to a file or directory.
        ///
        /// If the `--clean` flag is provided to `zinoma`, the files or directories specified in `paths` will be deleted before running the build flow.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   npm_install:
        ///     input:
        ///       - paths: [package.json, package-lock.json]
        ///     output:
        ///       - paths: [node_modules]
        ///     build: npm install
        /// ```
        ///
        /// In this example, as the target specifies an `input`, `zinoma npm_install` is incremental.
        /// The script `npm install` will be skipped until `package.json`, `package-lock.json` or `node_modules` are modified.
        ///
        /// Additionally:
        ///
        /// - the command `zinoma --clean` will delete `node_modules`;
        /// - the command `zinoma --clean npm_install` will delete `node_modules`, then run `npm install`.
        paths: Vec<String>,
        /// Filter files resource by file extensions.
        ///
        /// It should be an array of strings.
        ///
        /// If `extensions` are specified, only files matching at least one of the extensions will be included in the resource.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   protoc:
        ///     input:
        ///       - paths: [protos]
        ///         extensions: [proto]
        ///     output:
        ///       - paths: [protos]
        ///         extensions: [go]
        ///     build: |
        ///       cd protos
        ///       docker run -v `pwd`:/defs namely/protoc-all -d . -o . -l go
        extensions: Option<Vec<String>>,
    },
    CmdStdout {
        /// Shell script whose output identifies the state of a resource.
        ///
        /// It should be a string.
        ///
        /// __Example__
        ///
        /// ```yaml
        /// targets:
        ///   build_docker_image:
        ///     input:
        ///       - paths: [Dockerfile, src]
        ///       - cmd_stdout: 'docker image ls base:latest --format "{{.ID}}"'
        ///     output:
        ///       - cmd_stdout: 'docker image ls webapp:latest --format "{{.ID}}"'
        ///     build: docker build -t webapp .
        /// ```
        cmd_stdout: String,
    },
}

/// List of [`targets`] that must complete successfully before this target can be built.
///
/// [`targets`]: enum.Target.html
///
/// It should be an array of strings.
///
/// If any of the dependencies fails to complete, this target will not be executed.
///
/// __Example__
///
/// ```yaml
/// targets:
///   target1:
///     dependencies: []
///   target2:
///     dependencies: [target1]
///   target3:
///     dependencies: [target2]
/// ```
///
/// In this example, `target1` must complete successfully before `target2` begins, while `target3` waits for `target2` to complete.
///
/// `zinoma target2` will run sequentially `target1` and `target2`.
///
/// `zinoma target3` will run sequentially `target1`, `target2` and `target3`.
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Dependencies(#[serde(default)] pub Vec<String>);

/// List of artifacts that this target depends on.
///
/// `input` should be an array of [`resources`].
///
/// [`resources`]: enum.InputResource.html
///
/// Specifying a target's `input` enables the incremental build for this target.
/// This means that, at the time of executing the target, Žinoma will skip its build if its input resources (and [`output`] resources, if any) have not changed since its last successful completion.
///
/// [`output`]: struct.OutputResources.html
///
/// __Example__
///
/// ```yaml
/// targets:
///   npm_install:
///     input:
///       - paths: [package.json, package-lock.json]
///     build: npm install
/// ```
///
/// In this example, running `zinoma npm_install` once will execute `npm install`.
/// Subsequent runs of `zinoma npm_install` will return immediately — until the content of `package.json` or `package-lock.json` is modified.
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct InputResources(#[serde(default)] pub Vec<InputResource>);

/// List of artifacts produced by this target.
///
/// It should be an array of [`resources`].
///
/// [`resources`]: enum.OutputResource.html
///
/// The incremental build takes in account the target `output`.
/// Just like with [`input`], if any of the target output resources were altered since its previous successful execution, the target state will be invalidated and its build will be run again.
///
/// [`input`]: struct.InputResources.html
///
/// __Example__
///
/// ```yaml
/// targets:
///   npm_install:
///     input:
///       - paths: [package.json, package-lock.json]
///     output:
///       - paths: [node_modules]
///     build: npm install
/// ```
///
/// In this example, running `zinoma npm_install` will return immediately in case `package.json`, `package-lock.json` and `node_modules` were not modified since the last completion of the target.
///
/// Running `zinoma --clean npm_install` will start by deleting `node_modules`, then will run `npm install`.
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct OutputResources(#[serde(default)] pub Vec<OutputResource>);