This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
557af4b
commit 90f446e
Showing
14 changed files
with
788 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::fs; | ||
use std::io::{self, BufRead}; | ||
use std::path::Path; | ||
|
||
fn write_json_schemas() -> Result<(), Box<dyn std::error::Error>> { | ||
let commit_hash = "96a1a7164e8e0a608befa31b6cf0c9a4e5cc0f07"; | ||
let schemas = vec![ | ||
("DEFINITIONS_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/definitions.json", commit_hash)), | ||
("RESOURCE_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/resource.schema.json", commit_hash)), | ||
("BALANCE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/balance.schema.json", commit_hash)), | ||
("OFFERING_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/offering.schema.json", commit_hash)), | ||
("MESSAGE_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/message.schema.json", commit_hash)), | ||
("RFQ_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/rfq.schema.json", commit_hash)), | ||
("RFQ_PRIVATE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/rfq-private.schema.json", commit_hash)), | ||
("QUOTE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/quote.schema.json", commit_hash)), | ||
("ORDER_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/order.schema.json", commit_hash)), | ||
("ORDER_STATUS_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/orderstatus.schema.json", commit_hash)), | ||
("CLOSE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/close.schema.json", commit_hash)), | ||
]; | ||
|
||
let dest_path = Path::new("src/json_schemas").join("generated.rs"); | ||
|
||
// Check if the existing file contains the same commit hash | ||
if let Ok(file) = fs::File::open(&dest_path) { | ||
let reader = io::BufReader::new(file); | ||
let mut lines = reader.lines(); | ||
let third_line = lines.nth(3); // Get the third line (index 2) | ||
|
||
if let Some(Ok(line)) = third_line { | ||
if line == format!("pub const GIT_COMMIT_HASH: &str = \"{}\";", commit_hash) { | ||
// The commit hash matches, no need to re-fetch the schemas | ||
println!("Schemas are up-to-date, skipping download."); | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
|
||
let mut file_content = String::new(); | ||
|
||
// Add the commit hash to the top of the file | ||
file_content.push_str("// THIS FILE IS AUTO-GENERATED BY build.rs\n"); | ||
file_content.push_str("#[warn(unused_imports)]\n"); | ||
file_content.push_str("#[allow(dead_code)]\n"); | ||
file_content.push_str(&format!( | ||
"pub const GIT_COMMIT_HASH: &str = \"{}\";\n", | ||
commit_hash | ||
)); | ||
|
||
for (name, url) in schemas { | ||
let response = reqwest::blocking::get(&url)?; | ||
let mut schema = response.text()?; | ||
schema = schema.replace('#', "\\#"); | ||
file_content.push_str(&format!("pub const {}: &str = r#\"{}\"#;\n", name, schema)); | ||
} | ||
|
||
fs::write(dest_path, file_content)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
write_json_schemas()?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.