Skip to content

Commit

Permalink
simplify and clean up target-to-stack mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
dzuelke committed Jun 4, 2024
1 parent 2c83186 commit 1186ecd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 55 deletions.
4 changes: 2 additions & 2 deletions buildpacks/php/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) fn bootstrap(
let php_layer_data = context.handle_layer(
layer_name!("bootstrap_php"),
BootstrapLayer {
url: platform::get_platform_base_url_for_target(&context.target)
url: platform::platform_base_url_for_target(&context.target)
.join(&format!("php-min-{PHP_VERSION}.tar.gz"))
.expect("Internal error: failed to generate bootstrap download URL for PHP")
.to_string(),
Expand All @@ -39,7 +39,7 @@ pub(crate) fn bootstrap(
let composer_layer_data = context.handle_layer(
layer_name!("bootstrap_composer"),
BootstrapLayer {
url: platform::get_platform_base_url_for_target(&context.target)
url: platform::platform_base_url_for_target(&context.target)
.join(&format!("composer-{COMPOSER_VERSION}.tar.gz"))
.expect("Internal error: failed to generate bootstrap download URL for Composer")
.to_string(),
Expand Down
16 changes: 8 additions & 8 deletions buildpacks/php/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use crate::package_manager::composer::DependencyInstallationError;
use crate::php_project::{
PlatformJsonError, PlatformJsonNotice, ProjectLoadError, ProjectLoaderNotice,
};
use crate::platform::{get_stack_name_for_target, PlatformRepositoryUrlError, WebserversJsonError};
use crate::platform::{
heroku_stack_name_for_target, PlatformRepositoryUrlError, WebserversJsonError,
};
use indoc::formatdoc;
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::launch::{LaunchBuilder, ProcessBuilder};
Expand Down Expand Up @@ -59,6 +61,9 @@ impl Buildpack for PhpBuildpack {
}

fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
let stack_name = heroku_stack_name_for_target(&context.target)
.expect("Internal error: could not determine Heroku stack name for OS/distro");

let mut loader_notices = Vec::<ProjectLoaderNotice>::new();
let loader = php_project::ProjectLoader::from_env(context.platform.env())
.unwrap(&mut loader_notices);
Expand Down Expand Up @@ -90,12 +95,7 @@ impl Buildpack for PhpBuildpack {

let mut platform_json_notices = Vec::<PlatformJsonNotice>::new();
let platform_json = project
.platform_json(
&get_stack_name_for_target(&context.target),
&platform_installer_path,
&all_repos,
false,
)
.platform_json(&stack_name, &platform_installer_path, &all_repos, false)
.map_err(PhpBuildpackError::PlatformJson)?
.unwrap(&mut platform_json_notices); // Warned::unwrap() does not panic :)
platform_json_notices
Expand All @@ -116,7 +116,7 @@ impl Buildpack for PhpBuildpack {
log_header("Installing web servers");

let webservers_json = platform::webservers_json(
&get_stack_name_for_target(&context.target),
&stack_name,
&platform_installer_path,
&classic_buildpack_path,
&all_repos,
Expand Down
76 changes: 31 additions & 45 deletions buildpacks/php/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,54 +33,40 @@ pub(crate) enum PlatformRepositoryUrlError {
Parse(url::ParseError),
}

pub(crate) fn get_stack_name_for_target(target: &Target) -> String {
match target {
Target {
os,
distro_name,
distro_version,
..
} if os == "linux" && distro_name == "ubuntu" => format!(
"heroku-{}",
distro_version.strip_suffix(".04").unwrap_or(distro_version)
),
Target {
os,
distro_name,
distro_version,
..
} => format!("{os}-{distro_name}-{distro_version}"),
pub(crate) fn heroku_stack_name_for_target(target: &Target) -> Result<String, String> {
let Target {
os,
distro_name,
distro_version,
..
} = target;
match (os.as_str(), distro_name.as_str(), distro_version.as_str()) {
("linux", "ubuntu", v @ ("20.04" | "22.04")) => {
Ok(format!("heroku-{}", v.strip_suffix(".04").unwrap_or(v)))
}
_ => Err(format!("{os}-{distro_name}-{distro_version}")),
}
}

pub(crate) fn get_platform_base_url_for_target(target: &Target) -> Url {
let stack_identifier = match target {
Target {
os,
distro_name,
distro_version,
..
} if os == "linux"
&& distro_name == "ubuntu"
&& (distro_version == "20.04" || distro_version == "22.04") =>
{
get_stack_name_for_target(target)
}
Target {
os,
arch,
distro_name,
..
} if os == "linux" && distro_name == "ubuntu" => {
format!("{}-{}", get_stack_name_for_target(target), arch)
pub(crate) fn platform_base_url_for_target(target: &Target) -> Url {
let Target {
os,
arch,
distro_name,
distro_version,
..
} = target;
let stack_identifier = if let ("linux", "ubuntu", v) =
(os.as_str(), distro_name.as_str(), distro_version.as_str())
{
let stack_name = heroku_stack_name_for_target(target)
.expect("Internal error: could not determine Heroku stack name for OS/distro");
match v {
"20.04" | "22.04" => stack_name,
_ => format!("{stack_name}-{arch}"),
}
Target {
os,
arch,
distro_name,
distro_version,
..
} => format!("{os}-{arch}-{distro_name}-{distro_version}"),
} else {
format!("{os}-{arch}-{distro_name}-{distro_version}")
};

Url::parse(&format!(
Expand All @@ -98,7 +84,7 @@ pub(crate) fn platform_repository_urls_from_default_and_build_context(
context: &BuildContext<PhpBuildpack>,
) -> Result<Vec<Url>, PlatformRepositoryUrlError> {
// our default repo
let default_platform_repositories = vec![get_platform_base_url_for_target(&context.target)];
let default_platform_repositories = vec![platform_base_url_for_target(&context.target)];

// anything user-supplied
let user_repos = context
Expand Down

0 comments on commit 1186ecd

Please sign in to comment.