From a1d5c5f1b014360d5e8e3e759eb6ec68d452aa64 Mon Sep 17 00:00:00 2001 From: mio Date: Mon, 18 Oct 2021 21:39:59 +0200 Subject: [PATCH] Don't reply on git command --- .github/workflows/Crate-publishing.yml | 2 +- bindings/rust/Cargo.toml | 4 ++ bindings/rust/build.rs | 73 ++++++++++++++++++-------- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/.github/workflows/Crate-publishing.yml b/.github/workflows/Crate-publishing.yml index fcd5552b..bcbd2cb1 100644 --- a/.github/workflows/Crate-publishing.yml +++ b/.github/workflows/Crate-publishing.yml @@ -15,7 +15,7 @@ on: pull_request: env: - CI: true + UNICORN_VERSION: dev jobs: build: diff --git a/bindings/rust/Cargo.toml b/bindings/rust/Cargo.toml index 45d10f16..9c020538 100644 --- a/bindings/rust/Cargo.toml +++ b/bindings/rust/Cargo.toml @@ -25,3 +25,7 @@ libc = "0.2" [build-dependencies] build-helper = "0.1" +reqwest = { version = "0.11", features = ["blocking"] } +flate2 = "1.0.22" +tar = "0.4.37" +bytes = "1.1.0" \ No newline at end of file diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs index c128ef14..8a64ae9e 100644 --- a/bindings/rust/build.rs +++ b/bindings/rust/build.rs @@ -1,31 +1,60 @@ +use std::fmt::format; use std::result::Result; use std::{env, process::Command}; +use std::path::{Path, PathBuf}; +use bytes::Buf; +use flate2::read::GzDecoder; +use reqwest::header::USER_AGENT; +use tar::Archive; + + +fn find_unicorn(unicorn_dir: &PathBuf) -> Option { + for entry in std::fs::read_dir(unicorn_dir).ok()? { + let entry = entry.unwrap(); + let path = entry.path(); + + if path.is_dir() && path.file_name()?.to_str()?.contains("unicorn") { + return Some(path); + } + } + + None +} + +fn download_unicorn() -> Option { + // https://docs.github.com/en/rest/reference/repos#download-a-repository-archive-tar + let pkg_version; + if let Ok(unicorn_version) = env::var("UNICORN_VERSION") { + pkg_version = unicorn_version; + } else { + pkg_version = env::var("CARGO_PKG_VERSION").unwrap(); + } + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let client = reqwest::blocking::Client::new(); + let resp = client + .get(format!( + "https://api.github.com/repos/unicorn-engine/unicorn/tarball/{}", + pkg_version + )) + .header(USER_AGENT, "unicorn-engine-rust-bindings") + .send().unwrap() + .bytes().unwrap(); + let tar = GzDecoder::new(resp.reader()); + + let mut archive = Archive::new(tar); + archive.unpack(&out_dir).unwrap(); + + match find_unicorn(&out_dir) { + Some(dir) => Some(String::from(out_dir.join(dir).to_str()?)), + None => None + } +} fn main() { - let out_dir = env::var("OUT_DIR").unwrap(); + let profile = env::var("PROFILE").unwrap(); - let mut version = String::from("dev"); - if let Result::Ok(version_env) = env::var("UNICORN_BRANCH") { - version = version_env; - } - let unicorn_dir; - if let Result::Ok(_) = env::var("CI") { - unicorn_dir = format!("../.."); - } else { - unicorn_dir = format!("{}/unicorn_git", out_dir); - - Command::new("rm").arg("-rf").arg(&unicorn_dir); - - Command::new("git") - .arg("clone") - .arg("git@github.com:unicorn-engine/unicorn.git") - .arg("-b") - .arg(version) - .arg(&unicorn_dir) - .output() - .expect("Fail to clone Unicorn repository."); - } + let unicorn_dir = download_unicorn().unwrap(); println!("cargo:rerun-if-changed={}", &unicorn_dir);