You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/use/spec.md
+83Lines changed: 83 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -415,3 +415,86 @@ tests:
415
415
script:
416
416
- pytest
417
417
```
418
+
419
+
### Spec File Templating
420
+
421
+
SPK package spec files also support the [liquid](https://shopify.github.io/liquid/) templating language, so long as the spec file remains valid yaml.
422
+
423
+
The templating is rendered when the yaml file is read from disk, and before it's processed any farther (to start a build, run tests, etc.). This means that it cannot, for example, be involved in rendering different specs for different variants of the package (unless you define and orchestrate those variants through a separate build system).
424
+
425
+
The data that's made available to the template takes the form:
426
+
427
+
```yaml
428
+
spk:
429
+
version: "0.23.0" # the version of spk being used
430
+
opt: {} # a map of all build options specified (either host options or at the command line)
431
+
env: {} # a map of the current environment variables from the caller
432
+
```
433
+
434
+
One common templating use case is to allow your package spec to be reused to build many different versions, for example:
435
+
436
+
```yaml
437
+
# {% default opt.version = "2.3.4" %}
438
+
pkg: my-package/{{ version }}
439
+
```
440
+
441
+
Which could then be invoked for different versions at the command line:
442
+
443
+
```sh
444
+
spk build my-package.spk.yaml # builds the default 2.3.4
In addition to the default tags and filters within the liquid language, spk provides a few additional ones to help package maintainers:
451
+
452
+
##### Tags
453
+
454
+
**default**
455
+
456
+
The `default` tag can be used to more easily declare the default value for a variable. The following two statements are equivalent:
457
+
458
+
```liquid
459
+
{% assign version = version | default: "2.3.4" %}
460
+
{% default version = "2.3.4" %}
461
+
```
462
+
463
+
##### Filters
464
+
465
+
**compare_version**
466
+
467
+
The `compare_version` allows for comparing spk versions using any of the [version comparison operators](/use/versioning). It takes one or two parameters, depending on the data that you have to give. In all cases, the parameters are concatenated together and parsed as a version range. For example, the following assignments to py_3 all end up checking the same statement.
{% assign is_py3 = python.version | compare_version: ">=", three %}
474
+
```
475
+
476
+
**parse_version**
477
+
478
+
The `parse_version` filter breaks down an spk version into it's components, either returning an object or a single field from it, for example:
479
+
480
+
```liquid
481
+
{% assign v = "1.2.3.4-alpha.0+r.4" | parse_version %}
482
+
{{ v.base }} # 1.2.3.4
483
+
{{ v.major }} # 1
484
+
{{ v.minor }} # 2
485
+
{{ v.patch }} # 3
486
+
{{ v.parts[3] }} # 4
487
+
{{ v.post.r }} # 4
488
+
{{ v.pre.alpha }} # 0
489
+
{{ "1.2.3.4-alpha.0+r.4" | parse_version: minor }} # 2
490
+
```
491
+
492
+
**replace_re**
493
+
494
+
The `replace_re` filter works like the built-in `replace` filter, except that it matches using a perl-style regular expression and allows group replacement in the output. These regular expressions do not support look-arounds or back-references. For example:
495
+
496
+
```liquid
497
+
{% default version = "2.3.4" %}
498
+
{% assign major_minor = version | replace_re: "(\d+)\.(\d+).*", "$1.$2" %}
0 commit comments