-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.rs
More file actions
88 lines (79 loc) · 3.02 KB
/
build.rs
File metadata and controls
88 lines (79 loc) · 3.02 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Build-time script to download generics
use std::collections::HashMap;
use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use anyhow::{Context, Result};
use serde_json::from_reader;
const OUTPUT_DIR: &str = "downloads";
async fn fetch_bytes(client: &reqwest::Client, url: &str) -> Result<Vec<u8>> {
if let Ok(path) = env::var("ACROPOLIS_OFFLINE_MIRROR") {
if let Ok(file) = File::open(&path) {
if let Ok(map) = from_reader::<_, HashMap<String, String>>(file) {
if let Some(path) = map.get(url.trim()) {
if let Ok(bytes) = fs::read(path) {
return Ok(bytes);
}
}
}
}
}
let req = client.get(url).build().with_context(|| format!("Failed to request {url}"))?;
let resp = client.execute(req).await.with_context(|| format!("Failed to fetch {url}"))?;
Ok(resp.bytes().await.context("Failed to read response")?.to_vec())
}
/// Download a URL to a file in OUTPUT_DIR
async fn download(client: &reqwest::Client, url: &str, filename: &str) -> Result<()> {
let data = fetch_bytes(client, url).await?;
let output_path = Path::new(OUTPUT_DIR);
if !output_path.exists() {
fs::create_dir_all(output_path)
.with_context(|| format!("Failed to create {OUTPUT_DIR} directory"))?;
}
let file_path = output_path.join(filename);
let mut file = fs::File::create(&file_path)
.with_context(|| format!("Failed to create file {}", file_path.display()))?;
file.write_all(data.as_ref())
.with_context(|| format!("Failed to write file {}", file_path.display()))?;
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
println!("cargo:rerun-if-changed=build.rs"); // Ensure the script runs if modified
let client = reqwest::Client::new();
tokio::try_join!(
download(
&client,
"https://book.world.dev.cardano.org/environments/mainnet/byron-genesis.json",
"mainnet-byron-genesis.json",
),
download(
&client,
"https://book.world.dev.cardano.org/environments/mainnet/shelley-genesis.json",
"mainnet-shelley-genesis.json",
),
download(
&client,
"https://book.world.dev.cardano.org/environments/preview/byron-genesis.json",
"preview-byron-genesis.json",
),
download(
&client,
"https://book.world.dev.cardano.org/environments/preview/shelley-genesis.json",
"preview-shelley-genesis.json",
),
download(
&client,
"https://raw.githubusercontent.com/Hornan7/SanchoNet-Tutorials/refs/heads/main/genesis/byron-genesis.json",
"sanchonet-byron-genesis.json",
),
download(
&client,
"https://raw.githubusercontent.com/Hornan7/SanchoNet-Tutorials/refs/heads/main/genesis/shelley-genesis.json",
"sanchonet-shelley-genesis.json",
)
)?;
Ok(())
}