2024-10-15 06:22:30 +03:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
|
|
|
const KIT_CACHE: &str = "/tmp/kinode-kit-cache";
|
2024-05-15 03:00:48 +03:00
|
|
|
const KINODE_WIT_0_7_0_URL: &str =
|
2024-04-04 04:43:15 +03:00
|
|
|
"https://raw.githubusercontent.com/kinode-dao/kinode-wit/aa2c8b11c9171b949d1991c32f58591c0e881f85/kinode.wit";
|
2024-05-15 03:00:48 +03:00
|
|
|
const KINODE_WIT_0_8_0_URL: &str =
|
|
|
|
"https://raw.githubusercontent.com/kinode-dao/kinode-wit/v0.8/kinode.wit";
|
|
|
|
|
2024-10-15 06:22:30 +03:00
|
|
|
/// copied from `kit`
|
|
|
|
async fn download_file(url: &str, path: &Path) -> anyhow::Result<()> {
|
|
|
|
fs::create_dir_all(&KIT_CACHE)?;
|
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
hasher.update(url.as_bytes());
|
|
|
|
let hashed_url = hasher.finalize();
|
|
|
|
let hashed_url_path = Path::new(KIT_CACHE).join(format!("{hashed_url:x}"));
|
|
|
|
|
|
|
|
let content = if hashed_url_path.exists() {
|
|
|
|
fs::read(hashed_url_path)?
|
|
|
|
} else {
|
|
|
|
let response = reqwest::get(url).await?;
|
|
|
|
|
|
|
|
// Check if response status is 200 (OK)
|
|
|
|
if response.status() != reqwest::StatusCode::OK {
|
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"Failed to download file: HTTP Status {}",
|
|
|
|
response.status()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = response.bytes().await?.to_vec();
|
|
|
|
fs::write(hashed_url_path, &content)?;
|
|
|
|
content
|
|
|
|
};
|
|
|
|
|
|
|
|
if path.exists() {
|
|
|
|
if path.is_dir() {
|
|
|
|
fs::remove_dir_all(path)?;
|
|
|
|
} else {
|
|
|
|
let existing_content = fs::read(path)?;
|
|
|
|
if content == existing_content {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs::create_dir_all(
|
|
|
|
path.parent()
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("path doesn't have parent"))?,
|
|
|
|
)?;
|
|
|
|
fs::write(path, &content)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-12 03:25:13 +03:00
|
|
|
fn main() {
|
2024-02-02 07:43:13 +03:00
|
|
|
if std::env::var("SKIP_BUILD_SCRIPT").is_ok() {
|
|
|
|
println!("Skipping build script");
|
2024-06-12 03:25:13 +03:00
|
|
|
return;
|
2024-02-02 07:43:13 +03:00
|
|
|
}
|
|
|
|
|
2024-06-12 03:25:13 +03:00
|
|
|
let pwd = std::env::current_dir().expect("Failed to get current directory");
|
2024-02-02 07:43:13 +03:00
|
|
|
|
2024-05-15 03:00:48 +03:00
|
|
|
let wit_file = pwd.join("wit-v0.7.0").join("kinode.wit");
|
|
|
|
|
|
|
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
rt.block_on(async {
|
2024-10-15 06:22:30 +03:00
|
|
|
download_file(KINODE_WIT_0_7_0_URL, &wit_file)
|
2024-05-15 03:00:48 +03:00
|
|
|
.await
|
2024-06-12 03:25:13 +03:00
|
|
|
.expect("Failed to download WIT 0.7");
|
|
|
|
});
|
2024-05-15 03:00:48 +03:00
|
|
|
|
|
|
|
let wit_file = pwd.join("wit-v0.8.0").join("kinode.wit");
|
2024-04-04 04:43:15 +03:00
|
|
|
|
|
|
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
rt.block_on(async {
|
2024-10-15 06:22:30 +03:00
|
|
|
download_file(KINODE_WIT_0_8_0_URL, &wit_file)
|
2024-04-04 04:43:15 +03:00
|
|
|
.await
|
2024-06-12 03:25:13 +03:00
|
|
|
.expect("Failed to download WIT 0.8");
|
2024-04-04 04:43:15 +03:00
|
|
|
})
|
2024-02-02 07:43:13 +03:00
|
|
|
}
|