Skip to content

Commit

Permalink
Update resolve_version to support both inventory formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mlarraz committed Nov 12, 2024
1 parent 2e00edc commit 913582e
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions common/nodejs-utils/src/bin/resolve_version.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +18,7 @@ const INVENTORY_EXIT_CODE: i32 = 3;
fn main() {
let args: Vec<String> = 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);
Expand All @@ -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::<Inventory<Version, Sha256, Option<()>>>().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::<Os>(), consts::ARCH.parse::<Arch>()) {
(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);
}

0 comments on commit 913582e

Please sign in to comment.