From 1186ecd38dd3fdb2db252a5d84cc3c3c2187b79a Mon Sep 17 00:00:00 2001 From: David Zuelke Date: Wed, 5 Jun 2024 00:56:24 +0200 Subject: [PATCH] simplify and clean up target-to-stack mapping --- buildpacks/php/src/bootstrap.rs | 4 +- buildpacks/php/src/main.rs | 16 +++---- buildpacks/php/src/platform.rs | 76 ++++++++++++++------------------- 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/buildpacks/php/src/bootstrap.rs b/buildpacks/php/src/bootstrap.rs index e4cca04..554ac96 100644 --- a/buildpacks/php/src/bootstrap.rs +++ b/buildpacks/php/src/bootstrap.rs @@ -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(), @@ -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(), diff --git a/buildpacks/php/src/main.rs b/buildpacks/php/src/main.rs index a549696..c0b7445 100644 --- a/buildpacks/php/src/main.rs +++ b/buildpacks/php/src/main.rs @@ -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}; @@ -59,6 +61,9 @@ impl Buildpack for PhpBuildpack { } fn build(&self, context: BuildContext) -> libcnb::Result { + 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::::new(); let loader = php_project::ProjectLoader::from_env(context.platform.env()) .unwrap(&mut loader_notices); @@ -90,12 +95,7 @@ impl Buildpack for PhpBuildpack { let mut platform_json_notices = Vec::::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 @@ -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, diff --git a/buildpacks/php/src/platform.rs b/buildpacks/php/src/platform.rs index a69a378..accda31 100644 --- a/buildpacks/php/src/platform.rs +++ b/buildpacks/php/src/platform.rs @@ -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 { + 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!( @@ -98,7 +84,7 @@ pub(crate) fn platform_repository_urls_from_default_and_build_context( context: &BuildContext, ) -> Result, 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