Skip to content

Commit

Permalink
Use github api
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiekat committed Aug 31, 2021
1 parent 5f816f2 commit 4711172
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Laboratory/Rust/functionality-testing/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ fn main() {
unsafe {
bebop::COMPILER_PATH = Some(PathBuf::from(BEBOP_BIN));
}
// bebop::download_bebopc(
// PathBuf::from("../target").join("bebopc"),
// );
bebop::build_schema_dir("schemas", "src/generated");
}
9 changes: 8 additions & 1 deletion Tools/cargo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Tools/cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "bebop-tools"
version = "2.3.0-alpha"
version = "0.1.0"
edition = "2018"
description = "Cargo buildscript tools for Bebop, a schema-based binary serialization format."
readme = "../../README.md"
Expand All @@ -17,7 +17,8 @@ homepage = "https://github.com/RainwayApp/bebop/wiki"
[dependencies]
reqwest = { version = "0.11", features = ["blocking"], optional = true }
zip = { version = "0.5", optional = true }
json = { version = "0.12", optional = true }

[features]
default = ["downloader"]
downloader = ["reqwest", "zip"]
downloader = ["reqwest", "zip", "json"]
36 changes: 32 additions & 4 deletions Tools/cargo/src/downloader.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use json::JsonValue;
use std::fs::{self, File};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -42,10 +43,18 @@ fn download_bebopc_internal(dest: impl AsRef<Path>) -> PathBuf {
}
mkdir_p(&root_path);
let zip_name = format!("bebopc-{}64.zip", OS_NAME);
let url = format!(
"https://github.com/RainwayApp/bebop/releases/download/v{}/{}",
BEBOPC_VERSION, zip_name
);
let release_info = get_json(format!(
"https://api.github.com/repos/rainwayapp/bebop/releases/tags/v{}",
BEBOPC_VERSION
));
let url = release_info["assets"]
.members()
.find(|asset| asset["name"].as_str().unwrap() == zip_name)
.expect("Could not find expected asset")["browser_download_url"]
.as_str()
.unwrap()
.to_owned();

let zip_path = root_path.join(&zip_name);
download(url, &zip_path);
let tmp_path = &root_path.join("tmp");
Expand Down Expand Up @@ -95,3 +104,22 @@ fn download(uri: impl reqwest::IntoUrl, path: impl AsRef<Path>) {
let mut file = File::create(path).expect("Failed to create file");
res.copy_to(&mut file).expect("Failed to write to file");
}

fn get_json(uri: impl reqwest::IntoUrl) -> JsonValue {
let uri = uri.into_url().unwrap();
let res = reqwest::blocking::Client::builder()
.build()
.unwrap()
.get(uri.clone())
.header("User-Agent", "hyper/0.5")
.send()
.expect("Failed to GET");
if res.status() != 200 {
panic!(
"Invalid status code from GET {}: {}",
uri.as_str(),
res.status()
)
}
json::parse(&res.text().unwrap()).expect("Failed to parse JSON response")
}

0 comments on commit 4711172

Please sign in to comment.