Skip to content

Commit

Permalink
improve things
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Oct 9, 2024
1 parent bf0deab commit b36cbbc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 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 @@ -141,6 +141,7 @@ rattler_shell = { version = "0.22.4", default-features = false, features = ["sys
rattler_solve = { version = "1.1.0", default-features = false, features = ["resolvo", "serde"] }
rattler_virtual_packages = { version = "1.1.7", default-features = false }
rattler_package_streaming = { version = "0.22.10", default-features = false }
lazy_static = "1.5.0"

[dev-dependencies]
insta = { version = "1.40.0", features = ["yaml"] }
Expand Down
19 changes: 11 additions & 8 deletions src/recipe/jinja.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ fn parse_platform(platform: &str) -> Result<Platform, minijinja::Error> {
})
}

lazy_static::lazy_static! {
/// The syntax config for MiniJinja / rattler-build
pub static ref SYNTAX_CONFIG: SyntaxConfig = SyntaxConfig::builder()
.block_delimiters("{%", "%}")
.variable_delimiters("${{", "}}")
.comment_delimiters("#{{", "}}")
.build()
.unwrap();
}

fn set_jinja(config: &SelectorConfig) -> minijinja::Environment<'static> {
let SelectorConfig {
target_platform,
Expand All @@ -382,14 +392,7 @@ fn set_jinja(config: &SelectorConfig) -> minijinja::Environment<'static> {
default_filters(&mut env);

// Ok to unwrap here because we know that the syntax is valid
env.set_syntax(
SyntaxConfig::builder()
.block_delimiters("{%", "%}")
.variable_delimiters("${{", "}}")
.comment_delimiters("#{{", "}}")
.build()
.unwrap(),
);
env.set_syntax(SYNTAX_CONFIG.clone());

let variant = Arc::new(variant.clone());

Expand Down
36 changes: 15 additions & 21 deletions src/used_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
use std::collections::{HashSet, VecDeque};

use marked_yaml::Span;
use minijinja::{
machinery::{
ast::{self, Expr, Stmt},
WhitespaceConfig,
},
syntax::SyntaxConfig,
use minijinja::machinery::{
ast::{self, Expr, Stmt},
parse_expr, WhitespaceConfig,
};

use crate::recipe::{
custom_yaml::{self, HasSpan, Node, ScalarNode, SequenceNodeInternal},
jinja::SYNTAX_CONFIG,
parser::CollectErrors,
ParsingError,
};
Expand All @@ -45,16 +43,12 @@ fn parse<'source>(
expr: &'source str,
filename: &str,
) -> Result<ast::Stmt<'source>, minijinja::Error> {
let syntax_config = SyntaxConfig::builder()
.block_delimiters("{%", "%}")
.variable_delimiters("${{", "}}")
.comment_delimiters("#{{", "}}")
.build()
.unwrap();

let whitespace_config = WhitespaceConfig::default();

minijinja::machinery::parse(expr, filename, syntax_config, whitespace_config)
minijinja::machinery::parse(
expr,
filename,
SYNTAX_CONFIG.clone(),
WhitespaceConfig::default(),
)
}

/// Extract all variables from a jinja expression (called from [`extract_variables`])
Expand Down Expand Up @@ -149,6 +143,7 @@ fn find_jinja(
) -> Result<(), Vec<ParsingError>> {
let mut errs = Vec::<ParsingError>::new();
let mut queue = VecDeque::from([(node, src)]);

while let Some((node, src)) = queue.pop_front() {
match node {
Node::Mapping(map) => {
Expand All @@ -162,11 +157,10 @@ fn find_jinja(
match item {
SequenceNodeInternal::Simple(node) => queue.push_back((node, src)),
SequenceNodeInternal::Conditional(if_sel) => {
// we need to convert the if condition to a Jinja expression to parse it
let as_jinja_expr = format!("${{{{ {} }}}}", if_sel.cond().as_str());
match parse(&as_jinja_expr, "jinja.yaml") {
Ok(ast) => {
extract_variables(&ast, variables);
match parse_expr(if_sel.cond().as_str()) {
Ok(expr) => {
extract_variable_from_expression(&expr, variables);
// extract_variables(&ast, variables);
queue.push_back((if_sel.then(), src));
// find_jinja(if_sel.then(), src, variables)?;
if let Some(otherwise) = if_sel.otherwise() {
Expand Down

0 comments on commit b36cbbc

Please sign in to comment.