-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathbuild.rs
72 lines (60 loc) · 3.11 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Build script that generates a `configs.json` file from the configs.
use kona_genesis::{ChainConfig, Superchain, SuperchainConfig, Superchains};
fn main() {
// If the `KONA_BIND` environment variable is _not_ set, then return early.
let kona_bind: bool =
std::env::var("KONA_BIND").unwrap_or_else(|_| "false".to_string()) == "true";
if !kona_bind {
return;
}
// Get the directory of this file from the environment
let src_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
// Check if the `superchain-registry` directory exists
let superchain_registry = format!("{}/superchain-registry", src_dir);
if !std::path::Path::new(&superchain_registry).exists() {
panic!("Git Submodule missing. Please run `just source` to initialize the submodule.");
}
// Copy the `superchain-registry/chainList.json` file to `etc/chainList.json`
let chain_list = format!("{}/superchain-registry/chainList.json", src_dir);
std::fs::copy(chain_list, "etc/chainList.json").unwrap();
// Get the `superchain-registry/superchain/configs` directory`
let configs_dir = format!("{}/superchain-registry/superchain/configs", src_dir);
let configs = std::fs::read_dir(configs_dir).unwrap();
// Get all the directories in the `configs` directory
let mut superchains = Superchains::default();
for config in configs {
let config = config.unwrap();
let config_path = config.path();
let superchain_name = config.file_name().into_string().unwrap();
let mut superchain =
Superchain { name: superchain_name, chains: Vec::new(), ..Default::default() };
if config_path.is_dir() {
let config_files = std::fs::read_dir(&config_path).unwrap();
for config_file in config_files {
let config_file = config_file.unwrap();
let config_file_path = config_file.path();
// Read the `superchain.toml` as the `SuperchainConfig`
let config_file_name = config_file.file_name().into_string().unwrap();
if config_file_name == "superchain.toml" {
let config = std::fs::read_to_string(config_file_path).unwrap();
let config: SuperchainConfig = toml::from_str(&config).unwrap();
superchain.config = config;
continue;
}
// Read the config file as a `ChainConfig`
let config = std::fs::read_to_string(config_file_path).unwrap();
let config: ChainConfig = toml::from_str(&config).unwrap();
superchain.chains.push(config);
}
superchains.superchains.push(superchain);
}
}
// Sort the superchains by name.
superchains.superchains.sort_by(|a, b| a.name.cmp(&b.name));
// For each superchain, sort the list of chains by chain id.
for superchain in superchains.superchains.iter_mut() {
superchain.chains.sort_by(|a, b| a.chain_id.cmp(&b.chain_id));
}
std::fs::write("etc/configs.json", serde_json::to_string_pretty(&superchains).unwrap())
.unwrap();
}