Skip to content

Commit

Permalink
fix: build
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaxos committed Mar 4, 2024
1 parent 8f447a5 commit a645d9d
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,42 @@ use std::{
};

fn main() -> Result<(), Box<dyn Error>> {
// Skip building from docs.rs.
// Skip building online from docs.rs
if std::env::var_os("DOCS_RS").is_some() {
return Ok(());
}
// Retrieve a live version of the reports.proto file
let proto_url = "https://usage-reporting.api.apollographql.com/proto/reports.proto";
let response = reqwest::blocking::get(proto_url)?;
let mut content = response.text()?;
} else {
// Retrieve a live version of the reports.proto file
let proto_url = "https://usage-reporting.api.apollographql.com/proto/reports.proto";
let response = reqwest::blocking::get(proto_url)?;
let mut content = response.text()?;

// Process the retrieved content to:
// - Insert a package Report; line after the import lines (currently only one) and before the first message definition
// - Remove the Apollo TS extensions [(js_use_toArray)=true] and [(js_preEncoded)=true] from the file
// Note: Only two in use at the moment. This may fail in future if new extensions are
// added to the source, so be aware future self. It will manifest as a protobuf compile
// error.
let message = "\nmessage";
let msg_index = content.find(message).ok_or("cannot find message string")?;
content.insert_str(msg_index, "\npackage Report;\n");
// Process the retrieved content to:
// - Insert a package Report; line after the import lines (currently only one) and before the first message definition
// - Remove the Apollo TS extensions [(js_use_toArray)=true] and [(js_preEncoded)=true] from the file
// Note: Only two in use at the moment. This may fail in future if new extensions are
// added to the source, so be aware future self. It will manifest as a protobuf compile
// error.
let message = "\nmessage";
let msg_index = content.find(message).ok_or("cannot find message string")?;
content.insert_str(msg_index, "\npackage Report;\n");

content = content.replace("[(js_use_toArray) = true]", "");
content = content.replace("[(js_preEncoded) = true]", "");
content = content.replace("[(js_use_toArray) = true]", "");
content = content.replace("[(js_preEncoded) = true]", "");

// Try to avoid writing out the same content since it will trigger unnecessary re-builds, which wastes time
let write_content = match File::open("proto/reports.proto") {
Ok(mut existing) => {
let mut existing_content = String::new();
existing.read_to_string(&mut existing_content)?;
content != existing_content
}
Err(_) => true,
};
// Try to avoid writing out the same content since it will trigger unnecessary re-builds, which wastes time
let write_content = match File::open("proto/reports.proto") {
Ok(mut existing) => {
let mut existing_content = String::new();
existing.read_to_string(&mut existing_content)?;
content != existing_content
}
Err(_) => true,
};

// Write the content out if they differ or an error occured trying to open proto file
if write_content {
let mut dest = File::create("proto/reports.proto")?;
copy(&mut content.as_bytes(), &mut dest)?;
// Write the content out if they differ or an error occured trying to open proto file
if write_content {
let mut dest = File::create("proto/reports.proto")?;
copy(&mut content.as_bytes(), &mut dest)?;
}
}

// Process the proto files
Expand Down

0 comments on commit a645d9d

Please sign in to comment.