Skip to content

Commit

Permalink
wip state
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Dec 1, 2024
1 parent b2b2738 commit facbedc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
11 changes: 1 addition & 10 deletions src/env_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,7 @@ pub fn vars(output: &Output, build_state: &str) -> HashMap<String, Option<String
);

let hash = output.build_configuration.hash.clone();
insert!(
vars,
"PKG_BUILD_STRING",
output
.recipe
.build()
.string()
.resolve(&hash, output.recipe.build().number)
.into_owned()
);
insert!(vars, "PKG_BUILD_STRING", output.build_string().to_string());
insert!(vars, "PKG_HASH", hash);

if output.build_configuration.cross_compilation() {
Expand Down
41 changes: 26 additions & 15 deletions src/recipe/parser/build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::str::FromStr;

use rattler_conda_types::{package::EntryPoint, NoArchType};
use serde::{Deserialize, Serialize};

use super::glob_vec::{AllOrGlobVec, GlobVec};
use super::{Dependency, FlattenErrors, SerializableRegex};
use crate::normalized_key::NormalizedKey;
use crate::recipe::custom_yaml::RenderedSequenceNode;
use crate::recipe::parser::script::Script;
use crate::recipe::parser::skip::Skip;

use crate::hash::HashInfo;
use crate::recipe::Jinja;
use crate::validate_keys;
use crate::{
_partialerror,
Expand Down Expand Up @@ -118,32 +121,37 @@ pub struct Build {
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(from = "Option<String>", into = "Option<String>")]
pub enum BuildString {
/// The build string is explicitly set by the user.
/// The build string is explicitly set by the user. This is the
/// source template with unresolved Jinja variables.
UserSpecified(String),

/// The build string is resolved and should be used as is.
Resolved(String),

/// The build string should be derived from the variants
#[default]
Derived,
}

impl From<Option<String>> for BuildString {
fn from(value: Option<String>) -> Self {
value.map_or_else(|| BuildString::Derived, BuildString::UserSpecified)
value.map_or_else(|| BuildString::Derived, BuildString::Resolved)
}
}

impl From<BuildString> for Option<String> {
fn from(value: BuildString) -> Self {
match value {
BuildString::UserSpecified(s) => Some(s),
BuildString::UserSpecified(_) => None,
BuildString::Resolved(s) => Some(s),
BuildString::Derived => None,
}
}
}

impl From<String> for BuildString {
fn from(value: String) -> Self {
BuildString::UserSpecified(value)
BuildString::Resolved(value)
}
}

Expand All @@ -153,18 +161,21 @@ impl BuildString {
matches!(self, BuildString::Derived)
}

/// Returns the user specified build string.
pub fn as_deref(&self) -> Option<&str> {
match self {
BuildString::UserSpecified(s) => Some(s),
BuildString::Derived => None,
}
}

/// Returns the final build string, either based on the user defined value or by computing the derived value.
pub fn resolve(&self, hash: &HashInfo, build_number: u64) -> Cow<'_, str> {
pub fn resolve(
&self,
hash: &HashInfo,
build_number: u64,
variant: BTreeMap<NormalizedKey, String>,
) -> Cow<'_, str> {
match self {
BuildString::UserSpecified(s) => s.as_str().into(),
// TODO
BuildString::UserSpecified(template) => {
let jinja = Jinja::new();

Check failure on line 174 in src/recipe/parser/build.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

this function takes 1 argument but 0 arguments were supplied
let context = jinja.create_context(variant);

Check failure on line 175 in src/recipe/parser/build.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

no method named `create_context` found for struct `Jinja` in the current scope
Cow::Owned(jinja.render(template, &context).unwrap())

Check failure on line 176 in src/recipe/parser/build.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

no method named `render` found for struct `Jinja` in the current scope
}
BuildString::Resolved(s) => s.as_str().into(),
BuildString::Derived => Self::compute(hash, build_number).into(),
}
}
Expand All @@ -185,7 +196,7 @@ impl TryConvertNode<BuildString> for RenderedNode {

impl TryConvertNode<BuildString> for RenderedScalarNode {
fn try_convert(&self, _name: &str) -> Result<BuildString, Vec<PartialParsingError>> {
Ok(BuildString::UserSpecified(self.as_str().to_owned()))
Ok(BuildString::UserSpecified(self.source().to_string()))
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/variant_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
env_vars,
hash::HashInfo,
normalized_key::NormalizedKey,
recipe::{custom_yaml::Node, parser::Dependency, ParsingError, Recipe},
recipe::{custom_yaml::Node, parser::Dependency, Jinja, ParsingError, Recipe},

Check failure on line 13 in src/variant_render.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

unused import: `Jinja`
selectors::SelectorConfig,
used_variables::used_vars_from_expressions,
variant_config::{ParseErrors, VariantConfig, VariantError},
Expand Down Expand Up @@ -201,12 +201,23 @@ impl Stage1Render {
let variant = self.variant_for_output(idx);
let recipe = &self.stage_0_render.rendered_outputs[self.order[idx]];
let hash = HashInfo::from_variant(&variant, recipe.build().noarch());

let build_string = recipe
.build()
.string()
.resolve(&hash, recipe.build().number)
.resolve(&hash, recipe.build().number, &variant)

Check failure on line 208 in src/variant_render.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

mismatched types
.into_owned();

// original build string
let original_recipe = &self.stage_0_render.raw_outputs.vec[self.order[idx]];
let original_build_string = original_recipe
.as_mapping()
.unwrap()
.get("build")
.map(|x| x.as_mapping().unwrap().get("string"));

println!("original build string: {:?}", original_build_string);

build_string
}

Expand Down

0 comments on commit facbedc

Please sign in to comment.