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

Make libcnb_runtime_build and libcnb_runtime_detect public #313

Closed
wants to merge 2 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
21 changes: 16 additions & 5 deletions libcnb/src/layer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,15 @@ impl LayerEnvDelta {
// explicitly written in the spec, we read through the the reference implementation and
// determined that it also treats the file contents as raw bytes.
// See: https://github.com/buildpacks/lifecycle/blob/a7428a55c2a14d8a37e84285b95dc63192e3264e/env/env.go#L73-L106
use std::os::unix::ffi::OsStringExt;
let path = dir_entry?.path();
let file_contents = OsString::from_vec(fs::read(&path)?);

#[cfg(target_family = "unix")]
let file_contents = {
use std::os::unix::ffi::OsStringExt;
OsString::from_vec(fs::read(&path)?)
};
#[cfg(not(target_family = "unix"))]
let file_contents = OsString::from(&fs::read_to_string(&path)?);

// Rely on the Rust standard library for splitting stem and extension. Since paths
// are not necessarily UTF-8 encoded, this is not as trivial as it might look like.
Expand Down Expand Up @@ -549,8 +555,6 @@ impl LayerEnvDelta {
}

fn write_to_env_dir(&self, path: impl AsRef<Path>) -> Result<(), std::io::Error> {
use std::os::unix::ffi::OsStrExt;

if path.as_ref().exists() {
// This is a possible race condition if the path is deleted between the check and
// removal by this code. We accept this for now to keep it simple.
Expand All @@ -573,7 +577,14 @@ impl LayerEnvDelta {

let file_path = path.as_ref().join(file_name);

fs::write(file_path, &value.as_bytes())?;
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
fs::write(file_path, &value.as_bytes())?;
}

#[cfg(not(target_family = "unix"))]
fs::write(file_path, &value.to_string_lossy().as_bytes())?;
}

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion libcnb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub use platform::*;
pub use toml_file::*;

pub use buildpack::Buildpack;
pub use runtime::libcnb_runtime;
pub use runtime::{
libcnb_runtime, libcnb_runtime_build, libcnb_runtime_detect, BuildArgs, DetectArgs,
};

const LIBCNB_SUPPORTED_BUILDPACK_API: data::buildpack::BuildpackApi =
data::buildpack::BuildpackApi { major: 0, minor: 6 };
Expand Down
34 changes: 18 additions & 16 deletions libcnb/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ pub fn libcnb_runtime<B: Buildpack>(buildpack: &B) {
.and_then(Path::file_name)
.and_then(OsStr::to_str);

#[cfg(any(target_family = "unix"))]
let result = match current_exe_file_name {
Some("detect") => libcnb_runtime_detect(buildpack),
Some("build") => libcnb_runtime_build(buildpack),
Some("detect") => libcnb_runtime_detect(buildpack, parse_detect_args_or_exit()),
Some("build") => libcnb_runtime_build(buildpack, parse_build_args_or_exit()),
other => {
eprintln!(
"Error: Expected the name of this executable to be 'detect' or 'build', but it was '{}'",
Expand All @@ -76,14 +75,16 @@ pub fn libcnb_runtime<B: Buildpack>(buildpack: &B) {
}
};

if let Err(lib_cnb_error) = result {
exit(buildpack.on_error(lib_cnb_error));
match result {
Ok(code) => process::exit(code),
Err(lib_cnb_error) => exit(buildpack.on_error(lib_cnb_error)),
}
}

fn libcnb_runtime_detect<B: Buildpack>(buildpack: &B) -> crate::Result<(), B::Error> {
let args = parse_detect_args_or_exit();

pub fn libcnb_runtime_detect<B: Buildpack>(
buildpack: &B,
args: DetectArgs,
) -> crate::Result<i32, B::Error> {
let app_dir = env::current_dir().map_err(Error::CannotDetermineAppDirectory)?;

let stack_id: StackId = env::var("CNB_STACK_ID")
Expand All @@ -104,21 +105,22 @@ fn libcnb_runtime_detect<B: Buildpack>(buildpack: &B) -> crate::Result<(), B::Er
};

match buildpack.detect(detect_context)?.0 {
InnerDetectResult::Fail => process::exit(100),
InnerDetectResult::Fail => Ok(100),
InnerDetectResult::Pass { build_plan } => {
if let Some(build_plan) = build_plan {
write_toml_file(&build_plan, build_plan_path)
.map_err(Error::CannotWriteBuildPlan)?;
}

process::exit(0)
Ok(0)
}
}
}

fn libcnb_runtime_build<B: Buildpack>(buildpack: &B) -> crate::Result<(), B::Error> {
let args = parse_build_args_or_exit();

pub fn libcnb_runtime_build<B: Buildpack>(
buildpack: &B,
args: BuildArgs,
) -> crate::Result<i32, B::Error> {
let layers_dir = args.layers_dir_path;

let app_dir = env::current_dir().map_err(Error::CannotDetermineAppDirectory)?;
Expand Down Expand Up @@ -155,17 +157,17 @@ fn libcnb_runtime_build<B: Buildpack>(buildpack: &B) -> crate::Result<(), B::Err
.map_err(Error::CannotWriteStore)?;
};

process::exit(0)
Ok(0)
}
}
}

struct DetectArgs {
pub struct DetectArgs {
pub platform_dir_path: PathBuf,
pub build_plan_path: PathBuf,
}

struct BuildArgs {
pub struct BuildArgs {
pub layers_dir_path: PathBuf,
pub platform_dir_path: PathBuf,
pub buildpack_plan_path: PathBuf,
Expand Down