Skip to content

Commit 98de95c

Browse files
committed
Add a section to the documentation on templating
Signed-off-by: Ryan Bottriell <[email protected]>
1 parent 8302e69 commit 98de95c

File tree

2 files changed

+83
-100
lines changed

2 files changed

+83
-100
lines changed

docs/usage.md

Lines changed: 0 additions & 100 deletions
This file was deleted.

docs/use/spec.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,86 @@ tests:
415415
script:
416416
- pytest
417417
```
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
445+
spk build my-package.spk.yaml -o version=2.4.0 # builds 2.4.0
446+
```
447+
448+
#### Template Extensions
449+
450+
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.
468+
469+
```liquid
470+
{% assign is_py3 = python.version | compare_version: ">=3" %}
471+
{% assign is_py3 = python.version | compare_version: ">=", 3 %}
472+
{% assign three = 3 %}
473+
{% 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" %}
499+
{{ major_minor }} # 2.3
500+
```

0 commit comments

Comments
 (0)