From 913582e80feda356e9f706abd6d224a565464bea Mon Sep 17 00:00:00 2001 From: Matt Larraz Date: Thu, 17 Oct 2024 16:16:22 -0400 Subject: [PATCH] Update resolve_version to support both inventory formats --- .../nodejs-utils/src/bin/resolve_version.rs | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/common/nodejs-utils/src/bin/resolve_version.rs b/common/nodejs-utils/src/bin/resolve_version.rs index a26e1f28..3d7b9e8e 100644 --- a/common/nodejs-utils/src/bin/resolve_version.rs +++ b/common/nodejs-utils/src/bin/resolve_version.rs @@ -1,7 +1,14 @@ // Required due to: https://github.com/rust-lang/rust/issues/95513 #![allow(unused_crate_dependencies)] -use heroku_nodejs_utils::{inv::Inventory, vrs::Requirement}; +use std::fs; +use std::env::consts; +use sha2::Sha256; + +use libherokubuildpack::inventory::artifact::{Arch, Os}; +use libherokubuildpack::inventory::Inventory; +use heroku_nodejs_utils::vrs::{Requirement, Version}; +use heroku_nodejs_utils::inv::Inventory as NodeJSInventory; const SUCCESS_EXIT_CODE: i32 = 0; const ARGS_EXIT_CODE: i32 = 1; @@ -11,7 +18,7 @@ const INVENTORY_EXIT_CODE: i32 = 3; fn main() { let args: Vec = std::env::args().collect(); - if &args[1] == "-v" || &args[1] == "--version" { + if args.len() > 0 && (&args[1] == "-v" || &args[1] == "--version") { const VERSION: &str = env!("CARGO_PKG_VERSION"); println!("v{VERSION}"); std::process::exit(SUCCESS_EXIT_CODE); @@ -29,15 +36,36 @@ fn main() { std::process::exit(VERSION_REQS_EXIT_CODE); }); - let inv = Inventory::read(filename).unwrap_or_else(|e| { - eprintln!("Error reading '{filename}': {e}"); + // Try to parse the new inventory file format first + let inv = fs::read_to_string(&filename).unwrap_or_else(|e| { + eprintln!("Error reading '{filename}'': {e}"); std::process::exit(INVENTORY_EXIT_CODE); - }); + }).parse::>>().ok(); - let version = inv.resolve(&version_requirements); - if let Some(version) = version { - println!("{} {}", version.version, version.url); - } else { - println!("No result"); + // Fall back to the old format. Remove this after cutting over to the new format. + if inv.is_none() { + let inv = NodeJSInventory::read(filename).unwrap_or_else(|e| { + eprintln!("Error reading '{filename}': {e}"); + std::process::exit(INVENTORY_EXIT_CODE); + }); + + let version = inv.resolve(&version_requirements); + if let Some(version) = version { + println!("{} {}", version.version, version.url); + } else { + eprintln!("No result"); + } + std::process::exit(SUCCESS_EXIT_CODE); } + + let inv = inv.unwrap(); + let artifact = match (consts::OS.parse::(), consts::ARCH.parse::()) { + (Ok(os), Ok(arch)) => inv.resolve(os, arch, &version_requirements), + (_, _) => None, + }.unwrap_or_else(|| { + eprintln!("Could not find version to satisfy requirements \"{}\" for OS {} on arch {}", version_requirements.to_string(), consts::OS.to_string(), consts::ARCH.to_string()); + std::process::exit(VERSION_REQS_EXIT_CODE); + }); + + println!("{} {}", artifact.version, artifact.url); }