Skip to content

add toplevel recipe meta information #1513

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,35 @@ impl Output {
let mut env_vars = env_vars::vars(&self, "BUILD");
env_vars.extend(env_vars::os_vars(self.prefix(), &target_platform));

// We need to keep the PKG_NAME and PKG_VERSION for the build script consistent
// across different cache builds. To this end we use the top level metadata.
env_vars.insert(
"PKG_NAME".to_string(),
Some(
self.recipe
.top_level_recipe_metadata
.as_ref()
.map(|x| x.name.clone())
.unwrap_or("cache".to_string()),
),
);
env_vars.insert(
"PKG_VERSION".to_string(),
Some(
self.recipe
.top_level_recipe_metadata
.as_ref()
.map(|x| x.version.clone())
.flatten()
.map(|v| v.to_string())
.unwrap_or("0.0.0".to_string()),
),
);

env_vars.insert("PKG_HASH".to_string(), Some("".to_string()));
env_vars.insert("PKG_BUILDNUM".to_string(), Some("0".to_string()));
env_vars.insert("PKG_BUILD_STRING".to_string(), Some("".to_string()));

// Reindex the channels
let channels = build_reindexed_channels(&self.build_configuration, tool_configuration)
.await
Expand Down
9 changes: 9 additions & 0 deletions src/recipe/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! This phase parses YAML and [`SelectorConfig`] into a [`Recipe`], where
//! if-selectors are handled and any jinja string is processed, resulting in a rendered recipe.
use indexmap::IndexMap;
use package::TopLevelRecipe;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::Debug;
Expand Down Expand Up @@ -64,6 +65,9 @@ pub struct Recipe {
pub context: IndexMap<String, Variable>,
/// The package information
pub package: Package,
/// Contents of the top-level recipe key - only used in multi-output recipes
#[serde(default, skip_serializing_if = "Option::is_none")]
pub top_level_recipe_metadata: Option<TopLevelRecipe>,
/// The cache build that should be used for this package
/// This is the same for all outputs of a recipe
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -256,6 +260,7 @@ impl Recipe {
let mut about = About::default();
let mut cache = None;
let mut extra = IndexMap::default();
let mut top_level_recipe = Option::<TopLevelRecipe>::None;

rendered_node
.iter()
Expand All @@ -272,6 +277,9 @@ impl Recipe {
"The recipe field is only allowed in conjunction with multiple outputs"
)])
}
"__top_level_recipe" => {
top_level_recipe = value.try_convert(key_str)?;
}
"cache" => {
if experimental {
cache = Some(value.try_convert(key_str)?)
Expand Down Expand Up @@ -318,6 +326,7 @@ impl Recipe {

let recipe = Recipe {
schema_version,
top_level_recipe_metadata: top_level_recipe,
context,
package: package.ok_or_else(|| {
vec![_partialerror!(
Expand Down
3 changes: 3 additions & 0 deletions src/recipe/parser/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ pub struct Build {
/// Include files in the package
#[serde(default, skip_serializing_if = "GlobVec::is_empty")]
pub files: GlobVec,
/// Use the cache output for the build or not
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub use_cache: Option<UseCache>,
}

/// The build string can be either a user specified string, a resolved string or derived from the variant.
Expand Down
4 changes: 3 additions & 1 deletion src/recipe/parser/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ pub fn find_outputs_from_src<S: SourceCode>(src: S) -> Result<Vec<Node>, Parsing
}
}

output_map.remove("recipe");
if let Some(recipe_node) = output_map.remove("recipe") {
output_map.insert("__top_level_recipe".into(), recipe_node);
}

let recipe = match Node::try_from(output_node) {
Ok(node) => node,
Expand Down
54 changes: 54 additions & 0 deletions src/recipe/parser/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ use crate::{

use super::FlattenErrors;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopLevelRecipe {
/// The name of the recipe (used in the cache build)
pub name: String,

/// The version of the recipe (used as default in the outputs)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<VersionWithSource>,
}

/// A recipe package information.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Package {
Expand Down Expand Up @@ -89,6 +99,50 @@ impl TryConvertNode<Package> for RenderedMappingNode {
}
}

impl TryConvertNode<TopLevelRecipe> for RenderedNode {
fn try_convert(&self, name: &str) -> Result<TopLevelRecipe, Vec<PartialParsingError>> {
self.as_mapping()
.ok_or_else(|| vec![_partialerror!(*self.span(), ErrorKind::ExpectedMapping,)])
.and_then(|m| m.try_convert(name))
}
}

impl TryConvertNode<TopLevelRecipe> for RenderedMappingNode {
fn try_convert(&self, name: &str) -> Result<TopLevelRecipe, Vec<PartialParsingError>> {
let mut name_val = None;
let mut version = None;

self.iter()
.map(|(key, value)| {
let key_str = key.as_str();
match key_str {
"name" => name_val = value.try_convert(key_str)?,
"version" => version = value.try_convert(key_str)?,
invalid => {
return Err(vec![_partialerror!(
*key.span(),
ErrorKind::InvalidField(invalid.to_string().into()),
help = format!("valid fields for `{name}` are `name` and `version`")
)])
}
}
Ok(())
})
.flatten_errors()?;

let Some(name) = name_val else {
return Err(vec![_partialerror!(
*self.span(),
ErrorKind::MissingField("name".into()),
label = "add the field `name` in between here",
help = format!("the field `name` is required for `{name}`")
)]);
};

Ok(TopLevelRecipe { name, version })
}
}

/// A package information used for [`Output`]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OutputPackage {
Expand Down
Loading