Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package Templating (va Liquid) #513

Merged
merged 12 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"crates/spk-config",
"crates/spk-launcher",
"crates/spk-schema",
"crates/spk-schema/crates/*",
"crates/spk-solve",
"crates/spk-solve/crates/*",
"crates/spk-storage",
Expand Down
14 changes: 14 additions & 0 deletions crates/spk-cli/cmd-make-recipe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
authors = ["Ryan Bottriell <[email protected]>"]
edition = "2021"
name = "spk-cmd-make-recipe"
version = "0.36.0"

[dependencies]
anyhow = "1.0"
async-trait = "0.1"
clap = { version = "3.2", features = ["derive", "env"] }
spk-cli-common = { path = '../common' }
spk-schema = { path = '../../spk-schema' }
spk-schema-liquid = { path = '../../spk-schema/crates/liquid' }
tracing = "0.1.35"
76 changes: 76 additions & 0 deletions crates/spk-cli/cmd-make-recipe/src/cmd_make_recipe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Sony Pictures Imageworks, et al.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/imageworks/spk

use std::sync::Arc;

use anyhow::{Context, Result};
use clap::Args;
use spk_cli_common::{flags, CommandArgs, Run};
use spk_schema::foundation::format::FormatOptionMap;
use spk_schema::foundation::spec_ops::Named;
use spk_schema::{SpecTemplate, Template, TemplateExt};

/// Render a package spec template into a recipe
///
/// This is done automatically when building packages, but can
/// be a useful debugging tool when writing package spec files.
#[derive(Args)]
#[clap(visible_aliases = &["mkrecipe", "mkr"])]
pub struct MakeRecipe {
#[clap(flatten)]
pub options: flags::Options,

#[clap(short, long, global = true, parse(from_occurrences))]
pub verbose: u32,

/// The package spec file to render
#[clap(name = "SPEC_FILE")]
pub package: Option<String>,
}

impl CommandArgs for MakeRecipe {
// The important positional arg for a make-recipe is the package
fn get_positional_args(&self) -> Vec<String> {
match self.package.clone() {
Some(p) => vec![p],
None => vec![],
}
}
}

#[async_trait::async_trait]
impl Run for MakeRecipe {
async fn run(&mut self) -> Result<i32> {
let options = self.options.get_options()?;

let template = match flags::find_package_template(&self.package)? {
flags::FindPackageTemplateResult::NotFound(name) => {
Arc::new(SpecTemplate::from_file(name.as_ref())?)
}
res => {
let (_, template) = res.must_be_found();
template
}
};

tracing::info!("rendering template for {}", template.name());
tracing::info!("using options {}", options.format_option_map());
let data = spk_schema::TemplateData::new(&options);
tracing::debug!("full template data: {data:#?}");
let rendered = spk_schema_liquid::render_template(template.source(), &data)
.context("Failed to render template")?;
print!("{rendered}");

match template.render(&options) {
Err(err) => {
tracing::error!("This template did not render into a valid spec {err}");
Ok(1)
}
Ok(_) => {
tracing::info!("Successfully rendered a valid spec");
Ok(0)
}
}
}
}
7 changes: 7 additions & 0 deletions crates/spk-cli/cmd-make-recipe/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Sony Pictures Imageworks, et al.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/imageworks/spk

#![deny(unsafe_op_in_unsafe_fn)]

pub mod cmd_make_recipe;
Loading