Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
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
35 changes: 0 additions & 35 deletions examples/execd/src/layer.rs

This file was deleted.

20 changes: 15 additions & 5 deletions examples/execd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
mod layer;

use crate::layer::ExecDLayer;
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::layer_name;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
use libcnb::generic::{GenericError, GenericMetadata, GenericPlatform};
use libcnb::{buildpack_main, Buildpack};
use libcnb::{additional_buildpack_binary_path, buildpack_main, Buildpack};

// Suppress warnings due to the `unused_crate_dependencies` lint not handling integration tests well.
use fastrand as _;
use libcnb::layer::UncachedLayerDefinition;
#[cfg(test)]
use libcnb_test as _;

Expand All @@ -24,7 +22,19 @@ impl Buildpack for ExecDBuildpack {
}

fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
context.handle_layer(layer_name!("layer_name"), ExecDLayer)?;
let layer = context.uncached_layer(
layer_name!("layer_name"),
UncachedLayerDefinition {
build: false,
launch: true,
},
)?;

layer.replace_exec_d_programs([(
"dice_roller",
additional_buildpack_binary_path!("dice_roller"),
)])?;

BuildResultBuilder::new().build()
}
}
Expand Down
74 changes: 65 additions & 9 deletions libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ use crate::data::store::Store;
use crate::data::{
buildpack::ComponentBuildpackDescriptor, buildpack_plan::BuildpackPlan, launch::Launch,
};
use crate::layer::{HandleLayerErrorOrBuildpackError, Layer, LayerData};
use crate::layer::handling::LayerErrorOrBuildpackError;
use crate::layer::{
CachedLayerDefinition, InspectExistingAction, IntoAction, InvalidMetadataAction, LayerRef,
UncachedLayerDefinition,
};
use crate::sbom::Sbom;
use crate::target::ContextTarget;
use libcnb_data::generic::GenericMetadata;
use libcnb_data::layer_content_metadata::LayerTypes;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::borrow::Borrow;
use std::path::PathBuf;

/// Context for the build phase execution.
Expand All @@ -24,13 +33,13 @@ pub struct BuildContext<B: Buildpack + ?Sized> {
}

impl<B: Buildpack + ?Sized> BuildContext<B> {
/// Handles the given [`Layer`] implementation in this context.
/// Handles the given [`crate::layer::Layer`] implementation in this context.
///
/// It will ensure that the layer with the given name is created and/or updated accordingly and
/// handles all errors that can occur during the process. After this method has executed, the
/// layer will exist on disk or an error has been returned by this method.
///
/// Use the returned [`LayerData`] to access the layers metadata and environment variables for
/// Use the returned [`crate::layer::LayerData`] to access the layers metadata and environment variables for
/// subsequent logic or layers.
///
/// # Example:
Expand Down Expand Up @@ -95,18 +104,65 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// }
/// }
/// ```
pub fn handle_layer<L: Layer<Buildpack = B>>(
#[deprecated = "The Layer trait API was replaced by LayerDefinitions. Use `cached_layer` and `uncached_layer`."]
#[allow(deprecated)]
pub fn handle_layer<L: crate::layer::Layer<Buildpack = B>>(
&self,
layer_name: LayerName,
layer: L,
) -> crate::Result<LayerData<L::Metadata>, B::Error> {
crate::layer::handle_layer(self, layer_name, layer).map_err(|error| match error {
HandleLayerErrorOrBuildpackError::HandleLayerError(e) => {
crate::Error::HandleLayerError(e)
) -> crate::Result<crate::layer::LayerData<L::Metadata>, B::Error> {
crate::layer::trait_api::handling::handle_layer(self, layer_name, layer).map_err(|error| {
match error {
LayerErrorOrBuildpackError::LayerError(e) => crate::Error::LayerError(e),
LayerErrorOrBuildpackError::BuildpackError(e) => crate::Error::BuildpackError(e),
}
HandleLayerErrorOrBuildpackError::BuildpackError(e) => crate::Error::BuildpackError(e),
})
}

pub fn uncached_layer(
&self,
layer_name: LayerName,
layer_definition: impl Borrow<UncachedLayerDefinition>,
) -> crate::Result<LayerRef<B, (), ()>, B::Error> {
let layer_definition = layer_definition.borrow();

crate::layer::execute(
LayerTypes {
launch: layer_definition.launch,
build: layer_definition.build,
cache: false,
},
&|_| InvalidMetadataAction::DeleteLayer,
&|_: &GenericMetadata, _| InspectExistingAction::Delete,
layer_name,
&self.layers_dir,
)
}

pub fn cached_layer<'a, M, X, Y, O, I>(
&self,
layer_name: LayerName,
layer_definition: impl Borrow<CachedLayerDefinition<'a, M, O, I>>,
) -> crate::Result<LayerRef<B, X, Y>, B::Error>
where
M: 'a + Serialize + DeserializeOwned,
O: 'a + IntoAction<InvalidMetadataAction<M>, X, B::Error>,
I: 'a + IntoAction<InspectExistingAction, Y, B::Error>,
{
let layer_definition = layer_definition.borrow();

crate::layer::execute(
LayerTypes {
launch: layer_definition.launch,
build: layer_definition.build,
cache: true,
},
layer_definition.invalid_metadata,
layer_definition.inspect_existing,
layer_name,
&self.layers_dir,
)
}
}

/// Describes the result of the build phase.
Expand Down
6 changes: 3 additions & 3 deletions libcnb/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::data::launch::ProcessTypeError;
use crate::layer::HandleLayerError;
use crate::layer::LayerError;
use libcnb_common::toml_file::TomlFileError;
use std::fmt::Debug;

Expand All @@ -11,8 +11,8 @@ pub type Result<T, E> = std::result::Result<T, Error<E>>;
/// An error that occurred during buildpack execution.
#[derive(thiserror::Error, Debug)]
pub enum Error<E> {
#[error("HandleLayer error: {0}")]
HandleLayerError(#[from] HandleLayerError),
#[error("Layer error: {0}")]
LayerError(#[from] LayerError),

#[error("Process type error: {0}")]
ProcessTypeError(#[from] ProcessTypeError),
Expand Down
13 changes: 6 additions & 7 deletions libcnb/src/layer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Provides types and helpers to work with layers.

mod handling;
mod public_interface;
pub(crate) mod shared;
pub(crate) mod struct_api;
pub(crate) mod trait_api;

#[cfg(test)]
mod tests;

pub(crate) use handling::*;
pub use public_interface::*;
pub use shared::LayerError;
pub use struct_api::*;
pub use trait_api::*;
Loading