Skip to content

Commit

Permalink
Merge pull request #161 from 0xPolygon/CDK-547-grab-the-contents-for-…
Browse files Browse the repository at this point in the history
…the-version-command-from-combinations-files

refactor: retrieve and parse versions at buildtime
  • Loading branch information
vcastellm authored Nov 7, 2024
2 parents 5bee873 + c0724a0 commit d00229d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 45 deletions.
58 changes: 38 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }
url = { workspace = true, features = ["serde"] }
colored = "2.0"


cdk-config = { path = "../cdk-config" }
serde.workspace = true
serde_json.workspace = true
tempfile = "3.12.0"
alloy-rpc-client = "0.4.2"
alloy-transport-http = "0.4.2"
alloy-rpc-client = "0.5.4"
alloy-transport-http = "0.5.4"
tokio = "1.40.0"
reqwest = "0.12.8"
alloy-json-rpc = "0.4.2"
alloy-json-rpc = "0.5.4"

[build-dependencies]
reqwest = {version = "0.12.8", features = ["blocking"]}
serde_json.workspace = true
regex = "1.11.1"
54 changes: 54 additions & 0 deletions crates/cdk/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use regex::Regex;
use reqwest::blocking::get;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;

fn main() {
let _ = build_versions();

let build_script_disabled = env::var("BUILD_SCRIPT_DISABLED")
.map(|v| v == "1")
.unwrap_or(false); // run by default
Expand Down Expand Up @@ -46,3 +53,50 @@ fn main() {
// only when a specific file changes:
// println!("cargo:rerun-if-changed=path/to/file");
}

// 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
let url = "https://raw.githubusercontent.com/0xPolygon/kurtosis-cdk/refs/heads/main/input_parser.star";
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
.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 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(", }", " }");

// The versions string is a JSON object we can parse
let versions_json: serde_json::Value = serde_json::from_str(&versions).unwrap();

// Write the versions to a file
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()
)
.as_bytes(),
)?;

Ok(())
}
30 changes: 11 additions & 19 deletions crates/cdk/src/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,26 @@ fn version() -> Result<Output, io::Error> {
}

pub(crate) fn versions() {
// Load the versions from the versions.json file in the crate directory
// and parse it using serde_json.
let versions = include_str!("../versions.json");
let versions_json: serde_json::Value = serde_json::from_str(versions).unwrap();

// Convert the JSON object to a HashMap.
let versions_map = versions_json.as_object().unwrap();

// Get the version of the cdk-node binary.
let output = version().unwrap();
let version = String::from_utf8(output.stdout).unwrap();

println!("{}", format!("{}", version.trim()).green());

let versions = vec![
(
"zkEVM Contracts",
"https://github.com/0xPolygonHermez/zkevm-contracts/releases/tag/v8.0.0-rc.4-fork.12",
),
("zkEVM Prover", "v8.0.0-RC12"),
("CDK Erigon", "hermeznetwork/cdk-erigon:0948e33"),
(
"zkEVM Pool Manager",
"hermeznetwork/zkevm-pool-manager:v0.1.1",
),
(
"CDK Data Availability Node",
"0xpolygon/cdk-data-availability:0.0.10",
),
];

// Multi-line string to print the versions with colors.
let formatted_versions: Vec<String> = versions
let formatted_versions: Vec<String> = versions_map
.iter()
.map(|(key, value)| format!("{}: {}", key.green(), value.blue()))
.map(|(key, value)| format!("{}: {}", key.green(), value.to_string().blue()))
.collect();

println!("{}", "Supported up to fork12".yellow());
println!("{}", formatted_versions.join("\n"));
}
15 changes: 15 additions & 0 deletions crates/cdk/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"agglayer_image": "ghcr.io/agglayer/agglayer:0.2.0-rc.5",
"cdk_erigon_node_image": "hermeznetwork/cdk-erigon:v2.1.2",
"cdk_node_image": "ghcr.io/0xpolygon/cdk:0.4.0-beta4",
"cdk_validium_node_image": "0xpolygon/cdk-validium-node:0.7.0-cdk",
"zkevm_bridge_proxy_image": "haproxy:3.0-bookworm",
"zkevm_bridge_service_image": "hermeznetwork/zkevm-bridge-service:v0.6.0-RC1",
"zkevm_bridge_ui_image": "leovct/zkevm-bridge-ui:multi-network-2",
"zkevm_contracts_image": "leovct/zkevm-contracts:v8.0.0-rc.4-fork.12",
"zkevm_da_image": "0xpolygon/cdk-data-availability:0.0.10",
"zkevm_node_image": "hermeznetwork/zkevm-node:v0.7.3",
"zkevm_pool_manager_image": "hermeznetwork/zkevm-pool-manager:v0.1.1",
"zkevm_prover_image": "hermeznetwork/zkevm-prover:v8.0.0-RC14-fork.12",
"zkevm_sequence_sender_image": "hermeznetwork/zkevm-sequence-sender:v0.2.4"
}

0 comments on commit d00229d

Please sign in to comment.