Skip to content

Commit

Permalink
feat: Retrieve Nushell versions from modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Dec 30, 2024
1 parent b5e0481 commit 7b17c76
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
4 changes: 2 additions & 2 deletions process/drivers/docker_driver/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl TryFrom<(Metadata, Platform)> for ImageMetadata {
fn try_from((metadata, platform): (Metadata, Platform)) -> Result<Self, Self::Error> {
match metadata.image {
MetadataImage::Single(image) => Ok(Self {
labels: image.config.labels,
labels: Some(image.config.labels),
digest: metadata.manifest.digest,
}),
MetadataImage::Multi(mut platforms) => {
Expand All @@ -70,7 +70,7 @@ impl TryFrom<(Metadata, Platform)> for ImageMetadata {
bail!("Manifest does not exist for {platform}");
};
Ok(Self {
labels: image.config.labels,
labels: Some(image.config.labels),
digest: manifest.digest,
})
}
Expand Down
2 changes: 1 addition & 1 deletion process/drivers/podman_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl TryFrom<Vec<PodmanImageMetadata>> for ImageMetadata {
.to_string();

Ok(Self {
labels: value.labels,
labels: Some(value.labels),
digest,
})
}
Expand Down
5 changes: 3 additions & 2 deletions process/drivers/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ impl std::fmt::Display for Platform {
}
}

#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "PascalCase")]
pub struct ImageMetadata {
pub labels: HashMap<String, Value>,
pub labels: Option<HashMap<String, Value>>,
pub digest: String,
}

Expand All @@ -228,6 +228,7 @@ impl ImageMetadata {
pub fn get_version(&self) -> Option<u64> {
Some(
self.labels
.as_ref()?
.get(IMAGE_VERSION_LABEL)?
.as_str()
.and_then(|v| lenient_semver::parse(v).ok())?
Expand Down
12 changes: 12 additions & 0 deletions recipe/src/module/type_ver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Cow;

use blue_build_utils::constants::BLUE_BUILD_MODULE_IMAGE_REF;
use oci_distribution::Reference;
use serde::{Deserialize, Deserializer, Serialize};

#[derive(Debug, Clone)]
Expand All @@ -18,6 +20,16 @@ impl<'scope> ModuleTypeVersion<'scope> {
pub fn version(&self) -> &str {
&self.version
}

#[must_use]
pub fn as_reference(&self) -> Option<Reference> {
format!(
"{BLUE_BUILD_MODULE_IMAGE_REF}/{}:{}",
&self.typ, &self.version
)
.parse()
.ok()
}
}

impl std::fmt::Display for ModuleTypeVersion<'_> {
Expand Down
74 changes: 73 additions & 1 deletion src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{
borrow::Cow,
collections::HashSet,
env,
path::{Path, PathBuf},
};
Expand All @@ -9,7 +11,10 @@ use blue_build_process_management::drivers::{
use blue_build_recipe::Recipe;
use blue_build_template::{ContainerFileTemplate, Template};
use blue_build_utils::{
constants::{BUILD_SCRIPTS_IMAGE_REF, CONFIG_PATH, RECIPE_FILE, RECIPE_PATH},
constants::{
BUILD_SCRIPTS_IMAGE_REF, BUILTIN_MODULES, CONFIG_PATH, NUSHELL_VERSION_LABEL, RECIPE_FILE,
RECIPE_PATH,
},
syntax_highlighting::{self, DefaultThemes},
};
use bon::Builder;
Expand Down Expand Up @@ -159,6 +164,7 @@ impl GenerateCommand {
)?
.digest,
)
.nushell_versions(retrieve_nushell_versions(&recipe))
.build();

let output_str = template.render().into_diagnostic()?;
Expand Down Expand Up @@ -209,3 +215,69 @@ fn determine_scripts_tag(platform: Platform) -> Result<Reference> {
})
.inspect(|image| debug!("Using build scripts image: {image}"))
}

fn retrieve_nushell_versions<'a>(recipe: &'a Recipe) -> Vec<Cow<'a, str>> {
use rayon::prelude::*;
fn process_module<'a>(module: &'a blue_build_recipe::Module<'a>) -> Option<Cow<'a, str>> {
let required_fields = module.required_fields.as_ref()?;

if BUILTIN_MODULES.contains(&required_fields.module_type.typ()) {
return None;
}

required_fields
.nushell_version
.as_deref()
.map(Cow::<'a>::Borrowed)
.or_else(|| {
let image = &required_fields
.source
.as_deref()
.and_then(|source| source.parse().ok())
.or_else(|| required_fields.module_type.as_reference())?;

Driver::get_metadata(&GetMetadataOpts::builder().image(image).build())
.inspect_err(|e| warn!("Failed to inspect module image {image}:\n{e}"))
.ok()?
.labels
.as_ref()?
.get(NUSHELL_VERSION_LABEL)?
.as_str()
.map(ToOwned::to_owned)
.map(Cow::Owned)
})
}

[
recipe.stages_ext.as_ref().map_or_else(Vec::new, |stages| {
stages
.stages
.par_iter()
.filter_map(|stage| {
Some(
stage
.required_fields
.as_ref()?
.modules_ext
.modules
.par_iter()
.filter_map(process_module)
.collect::<Vec<_>>(),
)
})
.flatten()
.collect()
}),
recipe
.modules_ext
.modules
.par_iter()
.filter_map(process_module)
.collect(),
]
.into_iter()
.flatten()
.collect::<HashSet<Cow<'_, str>>>()
.into_iter()
.collect()
}
1 change: 1 addition & 0 deletions utils/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub const UNKNOWN_SHELL: &str = "<unknown shell>";
pub const UNKNOWN_VERSION: &str = "<unknown version>";
pub const UNKNOWN_TERMINAL: &str = "<unknown terminal>";
pub const GITHUB_CHAR_LIMIT: usize = 8100; // Magic number accepted by Github
pub const BUILTIN_MODULES: [&str; 2] = ["containerfile", "copy"];

// Messages
pub const BUG_REPORT_WARNING_MESSAGE: &str =
Expand Down

0 comments on commit 7b17c76

Please sign in to comment.