#[cfg(feature = "cargo-flw")]
use std::io::Write;
#[cfg(unix)]
#[allow(unused_imports)]
use std::os::unix::fs::PermissionsExt;
#[cfg(feature = "cargo-flw")]
fn main() {
println!("cargo:rerun-if-changed=build.rs");
if std::env::var("CARGO_CFG_TARGET_ARCH") != Ok(String::from("wasm32")) {
let version = env!("CARGO_PKG_VERSION");
let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
let (arch_os, binary) = match (arch.as_str(), os.as_str()) {
("x86_64", "windows") => ("x64-windows", "flawless.exe"),
("x86_64", "linux") => ("x64-linux", "flawless"),
("x86_64", "macos") => ("x64-macOS", "flawless"),
("aarch64", "macos") => ("aarch64-macOS", "flawless"),
_ => panic!("Unsupported architecture {arch}-{os}"),
};
let url = format!("https://downloads.flawless.dev/{version}/{arch_os}/{binary}");
if let Ok(response) = ureq::get(&url).call() {
let status = response.status();
if status != 200 {
panic!("Unable to download binary from '{url}'. Status code: {status}")
}
let mut body = Vec::new();
response.into_reader().read_to_end(&mut body).expect("url returns binary");
let mut flawless_path = dirs::home_dir().expect("$HOME directory exists");
flawless_path.push(".cargo");
flawless_path.push("bin");
flawless_path.push(binary);
if let Ok(mut file) = std::fs::File::create(&flawless_path) {
let mut permissions = file.metadata().expect("metadata works").permissions();
#[cfg(unix)] permissions.set_mode(0o755);
file.set_permissions(permissions).expect("permissions failed");
file.write_all(&body).expect("write failed");
} else {
panic!("Can't create file '{flawless_path:?}'")
}
} else {
panic!("Unable to download binary from '{url}'. Check your internet connection");
}
}
}
#[cfg(not(feature = "cargo-flw"))]
fn main() {
}