flawless 1.0.0-alpha.16+croatian-pine-marten

Toolkit for writing durable execution workflows in Rust.
Documentation
//! This build script is used to download the `flawless` pre-built binary for the right platform.

#[cfg(feature = "cargo-flw")]
use std::io::Write;
#[cfg(unix)]
#[allow(unused_imports)]
use std::os::unix::fs::PermissionsExt;

// Only download `flawless` in case we are building/installing the binary.
// We don't want to re-download it each time someone is using the library.
#[cfg(feature = "cargo-flw")]
fn main() {
    // Only run this script once.
    println!("cargo:rerun-if-changed=build.rs");

    // The library is exclusively compiled to `wasm32`. This is just another safety measure to avoid running
    // this script when compiling to `wasm32`.
    if std::env::var("CARGO_CFG_TARGET_ARCH") != Ok(String::from("wasm32")) {
        // Determine the right binary to download.
        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)] // Set execute permission on unix systems.
                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() {
    // In case we are not building the binary, do nothing.
}