Skip to content
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

Env helper layers #598

Closed
wants to merge 3 commits into from
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `libherokubuildpack`: Add `env::DefaultEnvLayer` and `env::ConfigureEnvLayer` structs for setting environment variables ([#598](https://github.com/heroku/libcnb.rs/pull/598))

## [0.17.0] - 2023-12-06

Expand Down
3 changes: 2 additions & 1 deletion libherokubuildpack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ all-features = true
workspace = true

[features]
default = ["command", "download", "digest", "error", "log", "tar", "toml", "fs", "write"]
default = ["command", "download", "digest", "error", "env_layer", "log", "tar", "toml", "fs", "write"]
download = ["dep:ureq", "dep:thiserror"]
digest = ["dep:sha2"]
env_layer = ["dep:libcnb"]
error = ["log", "dep:libcnb"]
log = ["dep:termcolor"]
tar = ["dep:tar", "dep:flate2"]
Expand Down
125 changes: 125 additions & 0 deletions libherokubuildpack/src/env_layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use libcnb::build::BuildContext;
use libcnb::data::layer_content_metadata::LayerTypes;
use libcnb::generic::GenericMetadata;
use libcnb::layer::{Layer, LayerResult, LayerResultBuilder};
use libcnb::layer_env::LayerEnv;
use std::marker::PhantomData;
use std::path::Path;

/// Convenience layer for setting environment variables
///
/// If you do not need to modify files on disk or cache metadata, you can use this layer along with
/// [`BuildContext::handle_layer`] to apply results of [`LayerEnv::chainable_insert`] to build and
/// launch (runtime) environments.
///
/// Example:
///
/// ```no_run
///# use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
///# use libcnb::data::launch::{LaunchBuilder, ProcessBuilder};
///# use libcnb::data::process_type;
///# use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
///# use libcnb::generic::{GenericError, GenericMetadata, GenericPlatform};
///# use libcnb::{buildpack_main, Buildpack};
///# use libcnb::data::layer::LayerName;
///
///# pub(crate) struct HelloWorldBuildpack;
///
/// use libcnb::Env;
/// use libcnb::data::layer_name;
/// use libcnb::layer_env::{LayerEnv, ModificationBehavior, Scope};
/// use libherokubuildpack::env_layer;
///
///# impl Buildpack for HelloWorldBuildpack {
///# type Platform = GenericPlatform;
///# type Metadata = GenericMetadata;
///# type Error = GenericError;
///
///# fn detect(&self, _context: DetectContext<Self>) -> libcnb::Result<DetectResult, Self::Error> {
///# todo!()
///# }
///
///# fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
/// let env = Env::from_current();
///
/// let env = {
/// let layer = context.handle_layer(
/// layer_name!("configure_env"),
/// env_layer::ConfigureEnvLayer::new(
/// LayerEnv::new()
/// .chainable_insert(
/// Scope::All,
/// ModificationBehavior::Override,
/// "BUNDLE_GEMFILE", // Tells bundler where to find the `Gemfile`
/// context.app_dir.join("Gemfile"),
/// )
/// .chainable_insert(
/// Scope::All,
/// ModificationBehavior::Override,
/// "BUNDLE_CLEAN", // After successful `bundle install` bundler will automatically run `bundle clean`
/// "1",
/// )
/// .chainable_insert(
/// Scope::All,
/// ModificationBehavior::Override,
/// "BUNDLE_DEPLOYMENT", // Requires the `Gemfile.lock` to be in sync with the current `Gemfile`.
/// "1",
/// )
/// .chainable_insert(
/// Scope::All,
/// ModificationBehavior::Default,
/// "MY_ENV_VAR",
/// "Whatever I want",
/// ),
/// ),
/// )?;
/// layer.env.apply(Scope::Build, &env)
/// };
///
///# todo!()
///# }
///# }
/// ```
pub struct ConfigureEnvLayer<B: libcnb::Buildpack> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we end up keeping this (versus a read/write layer concept), we need to align on naming. We already have a LayerEnv which this takes, so I didn't want to call it EnvLayer. We could use Set instead of Configure. The module name is meh as well, I'm open to suggestions.

pub(crate) data: LayerEnv,
pub(crate) _buildpack: std::marker::PhantomData<B>,
}

impl<B> ConfigureEnvLayer<B>
where
B: libcnb::Buildpack,
{
#[must_use]
pub fn new(env: LayerEnv) -> Self {
ConfigureEnvLayer {
data: env,
_buildpack: PhantomData,
}
}
}

impl<B> Layer for ConfigureEnvLayer<B>
where
B: libcnb::Buildpack,
{
type Buildpack = B;
type Metadata = GenericMetadata;

fn types(&self) -> LayerTypes {
LayerTypes {
build: true,
launch: true,
cache: false,
}
}

fn create(
&self,
_context: &BuildContext<Self::Buildpack>,
_layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, B::Error> {
LayerResultBuilder::new(GenericMetadata::default())
.env(self.data.clone())
.build()
}
}
2 changes: 2 additions & 0 deletions libherokubuildpack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod command;
pub mod digest;
#[cfg(feature = "download")]
pub mod download;
#[cfg(feature = "env_layer")]
pub mod env_layer;
#[cfg(feature = "error")]
pub mod error;
#[cfg(feature = "fs")]
Expand Down