From daca7cc85f3d0fb33f6e6857557837bb0a39e8d8 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 30 Oct 2024 14:49:35 +0800 Subject: [PATCH 1/2] Download and use pre-built versoview --- verso/Cargo.toml | 3 +++ verso/build.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 verso/build.rs diff --git a/verso/Cargo.toml b/verso/Cargo.toml index 807eaadc..663d88c1 100644 --- a/verso/Cargo.toml +++ b/verso/Cargo.toml @@ -8,3 +8,6 @@ ipc-channel = { workspace = true } serde = { workspace = true } url = { workspace = true } versoview_messages = { path = "../versoview_messages" } + +[features] +pre-built-versoview = [] diff --git a/verso/build.rs b/verso/build.rs new file mode 100644 index 00000000..1cbc05df --- /dev/null +++ b/verso/build.rs @@ -0,0 +1,64 @@ +use std::{ + env, + path::{Path, PathBuf}, + process::Command, +}; + +const VERSO_VERSION: &str = "0.0.1"; + +fn main() { + if env::var_os("CARGO_FEATURE_PRE_BUILT_VERSOVIEW").is_some() { + download_and_extract_verso().unwrap(); + } +} + +fn decompress_archive(archive: &Path) -> Result<(), std::io::Error> { + if Command::new("tar") + .current_dir(archive.parent().unwrap()) + .arg("-xf") + .arg(archive) + .status()? + .success() + { + Ok(()) + } else { + Err(std::io::Error::from(std::io::ErrorKind::NotFound)) + } +} + +fn download_archive(base_url: &str) -> Result { + let target = env::var("TARGET").unwrap(); + let archive_path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("verso.tar.gz"); + if !archive_path.exists() { + if !Command::new("curl") + .arg("-L") + .arg("-f") + .arg("-s") + .arg("-o") + .arg(&archive_path) + .arg(format!( + "{base_url}/download/versoview-v{VERSO_VERSION}/verso-{target}.tar.gz" + )) + .status()? + .success() + { + return Err(std::io::Error::from(std::io::ErrorKind::NotFound)); + } + } + + Ok(archive_path) +} + +fn download_and_extract_verso() -> Result<(), std::io::Error> { + if let Ok(archive) = env::var("VERSO_ARCHIVE") { + // If the archive variable is present, assume it's a URL base to download from. + let archive = download_archive(&archive).unwrap_or(PathBuf::from(archive)); + // Panic directly since the archive is specified manually. + decompress_archive(&archive).unwrap(); + } else { + let archive = download_archive("https://github.com/Legend-Master/verso/releases")?; + decompress_archive(&archive)?; + }; + + Ok(()) +} From 85b8189b7fc505261c44ec9df22727db6092d734 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 30 Oct 2024 15:22:19 +0800 Subject: [PATCH 2/2] Use enviroment var instead of feature also put versoview in target dir instead --- verso/Cargo.toml | 3 --- verso/build.rs | 13 +++++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/verso/Cargo.toml b/verso/Cargo.toml index 663d88c1..807eaadc 100644 --- a/verso/Cargo.toml +++ b/verso/Cargo.toml @@ -8,6 +8,3 @@ ipc-channel = { workspace = true } serde = { workspace = true } url = { workspace = true } versoview_messages = { path = "../versoview_messages" } - -[features] -pre-built-versoview = [] diff --git a/verso/build.rs b/verso/build.rs index 1cbc05df..d2c830db 100644 --- a/verso/build.rs +++ b/verso/build.rs @@ -7,14 +7,23 @@ use std::{ const VERSO_VERSION: &str = "0.0.1"; fn main() { - if env::var_os("CARGO_FEATURE_PRE_BUILT_VERSOVIEW").is_some() { + if env::var_os("PRE_BUILT_VERSOVIEW").is_some() { download_and_extract_verso().unwrap(); } } fn decompress_archive(archive: &Path) -> Result<(), std::io::Error> { + let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + // Not ideal, but there's no good way of getting the target directory + let target_dir = out_dir + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap(); if Command::new("tar") - .current_dir(archive.parent().unwrap()) + .current_dir(target_dir) .arg("-xf") .arg(archive) .status()?