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

feat: fix BuildVersions during cargo build (#258) #260

Merged
merged 1 commit into from
Jan 10, 2025
Merged
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
56 changes: 34 additions & 22 deletions crates/cdk/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use regex::Regex;
use reqwest::blocking::get;
use std::env;
use std::fs::File;
use std::io::Write;
use std::io::{self, Write};
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use serde_json::Value;

fn main() {
let _ = build_versions();
Expand Down Expand Up @@ -55,45 +56,56 @@ fn main() {
}

// build_versions retrieves the versions from the Starlark file and embeds them in the binary.
fn build_versions() -> std::io::Result<()> {
// Retrieve the contents of the file from the URL
fn build_versions() -> io::Result<()> {
// URL of the Starlark file
let url = "https://raw.githubusercontent.com/0xPolygon/kurtosis-cdk/refs/heads/main/input_parser.star";

// Download the file content
let response = get(url).expect("Failed to send request");
let content = response.text().expect("Failed to read response text");

// Write the contents to a file
let out_dir = std::env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("input_parser.star");
let mut file = File::create(&dest_path)?;
file.write_all(content.as_bytes())?;

// Get the corresponding lines from the contents of the starlark file
let versions = content
// Extract the relevant lines (skip the first 30 lines, take the next 15)
let raw_versions = content
.lines()
.skip(30)
.take(15)
.collect::<Vec<&str>>()
.join("\n");

// Replace the string DEFAULT_IMAGES = from the versions string
let versions = versions.replace("DEFAULT_IMAGES = ", "");
// Remove the declaration `DEFAULT_IMAGES = `
let raw_versions = raw_versions.replace("DEFAULT_IMAGES = ", "");

// Clean up the content by removing comments and unnecessary spaces
let re_comments = Regex::new(r"#.*$").unwrap(); // Regex to remove comments
let re_trailing_commas = Regex::new(r",(\s*})").unwrap(); // Regex to fix trailing commas

let cleaned_versions = raw_versions
.lines()
.map(|line| re_comments.replace_all(line, "").trim().to_string()) // Remove comments and trim spaces
.filter(|line| !line.is_empty()) // Filter out empty lines
.collect::<Vec<_>>()
.join("\n");

// Remove all comments to the end of the line using a regexp
let re = Regex::new(r"\s#\s.*\n").unwrap();
let versions = re.replace_all(&versions, "");
// Replace the trailing comma on the last line
let versions = versions.replace(", }", " }");
// Fix improperly placed trailing commas
let cleaned_versions = re_trailing_commas.replace_all(&cleaned_versions, "$1");

// The versions string is a JSON object we can parse
let versions_json: serde_json::Value = serde_json::from_str(&versions).unwrap();
// Attempt to parse the cleaned content as JSON
let versions_json: Value = match serde_json::from_str(&cleaned_versions) {
Ok(json) => json,
Err(e) => {
eprintln!("Failed to parse JSON: {}", e); // Print the error
eprintln!("Input string was: {}", cleaned_versions); // Print the input causing the error
return Err(io::Error::new(io::ErrorKind::InvalidData, "JSON parsing failed"));
}
};

// Write the versions to a file
// Define the output file path for the JSON
let dest_path = Path::new(".").join("versions.json");
let mut file = File::create(&dest_path)?;
file.write_all(
format!(
"{}\n",
serde_json::to_string_pretty(&versions_json).unwrap()
serde_json::to_string_pretty(&versions_json).unwrap() // Pretty-print JSON to the file
)
.as_bytes(),
)?;
Expand Down
Loading