Skip to content

Commit 8302e69

Browse files
committed
Update templates to also get env and spk data
Signed-off-by: Ryan Bottriell <[email protected]>
1 parent 92e30ed commit 8302e69

File tree

9 files changed

+56
-10
lines changed

9 files changed

+56
-10
lines changed

crates/spk-cli/cmd-make-recipe/src/cmd_make_recipe.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ impl Run for MakeRecipe {
5656

5757
tracing::info!("rendering template for {}", template.name());
5858
tracing::info!("using options {}", options.format_option_map());
59-
let rendered = spk_schema_liquid::render_template(template.source(), &options)
59+
let data = spk_schema::TemplateData::new(&options);
60+
tracing::debug!("full template data: {data:#?}");
61+
let rendered = spk_schema_liquid::render_template(template.source(), &data)
6062
.context("Failed to render template")?;
6163
print!("{rendered}");
6264

crates/spk-schema/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub use spk_schema_foundation::{
5252
FromYaml,
5353
};
5454
pub use spk_schema_ident::{self as ident, Ident};
55-
pub use template::{Template, TemplateExt};
55+
pub use template::{Template, TemplateExt, TemplateData};
5656
pub use test_spec::TestStage;
5757
pub use validation::{default_validators, ValidationSpec, Validator};
5858
pub use {serde_json, spk_schema_validators as validators};

crates/spk-schema/src/spec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ impl Template for SpecTemplate {
123123
}
124124

125125
fn render(&self, options: &OptionMap) -> Result<Self::Output> {
126-
let option_data = options.to_yaml_value_expanded();
127-
let rendered = spk_schema_liquid::render_template(&self.template, &option_data)
126+
let data = super::TemplateData::new(options);
127+
let rendered = spk_schema_liquid::render_template(&self.template, &data)
128128
.map_err(Error::InvalidTemplate)?;
129129
Ok(SpecRecipe::from_yaml(&rendered)?)
130130
}

crates/spk-schema/src/template.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// https://github.com/imageworks/spk
44

5+
use std::collections::HashMap;
56
use std::path::Path;
67

78
use crate::foundation::option_map::OptionMap;
@@ -24,3 +25,45 @@ pub trait TemplateExt: Template {
2425
/// Load this template from a file on disk
2526
fn from_file(path: &Path) -> Result<Self>;
2627
}
28+
29+
30+
/// The structured data that should be made available
31+
/// when rendering spk templates into recipes
32+
#[derive(serde::Serialize, Debug, Clone)]
33+
pub struct TemplateData {
34+
/// Information about the release of spk being used
35+
spk: SpkInfo,
36+
/// The option values for this template, expanded
37+
/// from an option map so that namespaced options
38+
/// like `python.abi` actual live under the `python`
39+
/// field rather than as a field with a '.' in the name
40+
opt: serde_yaml::Mapping,
41+
/// Environment variable data for the current process
42+
env: HashMap<String, String>,
43+
}
44+
45+
/// The structured data that should be made available
46+
/// when rendering spk templates into recipes
47+
#[derive(serde::Serialize, Debug, Clone)]
48+
struct SpkInfo {
49+
version: &'static str,
50+
}
51+
52+
impl Default for SpkInfo {
53+
fn default() -> Self {
54+
Self {
55+
version: env!("CARGO_PKG_VERSION")
56+
}
57+
}
58+
}
59+
60+
impl TemplateData {
61+
/// Create the set of templating data for the current process and options
62+
pub fn new(options: &OptionMap) -> Self {
63+
TemplateData {
64+
spk: SpkInfo::default(),
65+
opt: options.to_yaml_value_expanded(),
66+
env: std::env::vars().into_iter().collect()
67+
}
68+
}
69+
}

packages/cmake/cmake.spk.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# {% default version = "3.16.4" %}
1+
# {% assign version = opt.version | default: "3.16.4" %}
22
pkg: cmake/{{ version }}
33
api: v0/package
44

packages/gnu/gcc/gcc48.spk.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# {{ default version = "4.8.5" }}
1+
# {{ assign version = opt.version | default: "4.8.5" }}
22
pkg: gcc/{{ version }}
33
api: v0/package
44

packages/gnu/gcc/gcc63.spk.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# {{ default version = "{{ version }}" }}
1+
# {% assign version = opt.version | default: "6.3.3" %}
22
pkg: gcc/{{ version }}
33
api: v0/package
44

packages/python/python2.spk.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# {% default version = "2.7.5" %}
2-
# {% assign cpXX = version | replace_re: "(\d+)\.(\d+).*", "cp$1$2" %}
1+
# {% assign version = env.VV | default: "2.7.5" %}
2+
# {% assign version = opt.version | default: "2.7.5" %}
3+
# {% assign cpXX = opt.version | replace_re: "(\d+)\.(\d+).*", "cp$1$2" %}
34
pkg: python/{{ version }}
45
api: v0/package
56
sources:

packages/python/python3.spk.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# {% default version = "3.7.3" %}
1+
# {% assign version = opt.version | default: "3.7.3" %}
22
# {% assign cpXX = version | replace_re: "(\d+)\.(\d+).*", "cp$1$2" %}
33
pkg: python/{{ version }}
44
api: v0/package

0 commit comments

Comments
 (0)