Move the build script of verso to a new crate

This commit is contained in:
Tony
2025-04-09 10:38:02 +08:00
parent 80383db17d
commit e2977a398e
7 changed files with 142 additions and 74 deletions

5
Cargo.lock generated
View File

@@ -7944,6 +7944,7 @@ dependencies = [
"serde",
"url",
"uuid",
"versoview_build",
"versoview_messages",
]
@@ -8018,6 +8019,10 @@ dependencies = [
"winit",
]
[[package]]
name = "versoview_build"
version = "0.0.1"
[[package]]
name = "versoview_messages"
version = "0.0.1"

View File

@@ -1,5 +1,5 @@
[workspace]
members = ["verso", "versoview_messages"]
members = ["verso", "versoview_messages", "versoview_build"]
[workspace.dependencies]
serde = { version = "1.0", features = ["derive"] }

View File

@@ -13,3 +13,6 @@ log = { workspace = true }
dpi = { workspace = true }
uuid = { workspace = true }
versoview_messages = { path = "../versoview_messages" }
[build-dependencies]
versoview_build = { path = "../versoview_build" }

View File

@@ -1,93 +1,32 @@
use std::{
env,
path::{Path, PathBuf},
process::Command,
time::Instant,
};
use std::{env, path::PathBuf};
const VERSO_VERSION: &str = env!("CARGO_PKG_VERSION");
use versoview_build::{
decompress_archive, default_archive_base_url, default_output_directory, download_archive,
};
fn main() {
println!("cargo:rerun-if-env-changed=PRE_BUILT_VERSOVIEW");
println!("cargo:rerun-if-env-changed=VERSO_ARCHIVE");
if let Ok(pre_built_versoview_env) = env::var("PRE_BUILT_VERSOVIEW") {
let output_directory = match pre_built_versoview_env.as_str() {
"true" => None,
_ => Some(PathBuf::from(pre_built_versoview_env)),
let output_directory = if pre_built_versoview_env == "true" {
default_output_directory()
} else {
PathBuf::from(pre_built_versoview_env)
};
download_and_extract_verso(output_directory).unwrap();
};
}
fn decompress_archive(
archive: &Path,
output_directory: Option<PathBuf>,
) -> Result<(), std::io::Error> {
let output_directory = output_directory.unwrap_or_else(|| {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
// Not ideal, but there's no good way of getting the target directory
out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
});
if Command::new("tar")
.current_dir(output_directory)
.arg("-xf")
.arg(archive)
.status()?
.success()
{
Ok(())
} else {
Err(std::io::Error::from(std::io::ErrorKind::NotFound))
}
}
fn download_archive(base_url: &str) -> Result<PathBuf, std::io::Error> {
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() {
let download_url =
format!("{base_url}/download/versoview-v{VERSO_VERSION}/verso-{target}.tar.gz");
let curl_start = Instant::now();
println!("Try downloading versoview from {download_url}");
if !Command::new("curl")
.arg("-L")
.arg("-f")
.arg("-s")
.arg("-o")
.arg(&archive_path)
.arg(download_url)
.status()?
.success()
{
return Err(std::io::Error::from(std::io::ErrorKind::NotFound));
}
println!(
"Successfully downloaded versoview archive in {} ms",
curl_start.elapsed().as_millis()
);
}
Ok(archive_path)
}
fn download_and_extract_verso(output_directory: Option<PathBuf>) -> Result<(), std::io::Error> {
pub fn download_and_extract_verso(output_directory: PathBuf) -> 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, output_directory)?;
decompress_archive(archive, output_directory)?;
} else {
let archive =
download_archive("https://github.com/versotile-org/versoview-release/releases")?;
decompress_archive(&archive, output_directory)?;
let archive_path = download_archive(default_archive_base_url())?;
decompress_archive(archive_path, output_directory)?;
};
Ok(())

View File

@@ -0,0 +1,6 @@
[package]
name = "versoview_build"
version = "0.0.1"
edition = "2024"
[dependencies]

13
versoview_build/README.md Normal file
View File

@@ -0,0 +1,13 @@
# VeroView Build
This is a crate to help with getting started with using verso as a webview without building it yourself
## Example
To use it, first add it to your build dependency, and in your build script:
```rust
fn main() {
versoview_build::download_and_extract_verso("output_directory").unwrap();
}
```

102
versoview_build/src/lib.rs Normal file
View File

@@ -0,0 +1,102 @@
//! # VeroView Build
//!
//! This is a crate to help with getting started with using verso as a webview without building it yourself
//!
//! ## Example
//!
//! To use it, first add it to your build dependency, and in your build script:
//!
//! ```no_run
//! fn main() {
//! versoview_build::download_and_extract_verso("output_directory").unwrap();
//! }
//! ```
use std::{
env, fs, io,
path::{Path, PathBuf},
process::Command,
time::Instant,
};
const VERSO_VERSION: &str = env!("CARGO_PKG_VERSION");
/// Decompress the archive to the output directory, this should resulting in a versoview(.exe) in that directory
pub fn decompress_archive<P1: AsRef<Path>, P2: AsRef<Path>>(
archive_path: P1,
output_directory: P2,
) -> Result<(), io::Error> {
fs::create_dir_all(&output_directory)?;
if Command::new("tar")
.current_dir(&output_directory)
.arg("-xf")
.arg(archive_path.as_ref())
.status()?
.success()
{
Ok(())
} else {
Err(io::Error::from(io::ErrorKind::NotFound))
}
}
/// Download the pre-built versoview archive to the `OUT_DIR` and returns that path
pub fn download_archive<S: AsRef<str>>(base_url: S) -> Result<PathBuf, io::Error> {
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() {
let base_url = base_url.as_ref();
let download_url =
format!("{base_url}/download/versoview-v{VERSO_VERSION}/verso-{target}.tar.gz");
let curl_start = Instant::now();
println!("Try downloading versoview from {download_url}");
if !Command::new("curl")
.arg("-L")
.arg("-f")
.arg("-s")
.arg("-o")
.arg(&archive_path)
.arg(download_url)
.status()?
.success()
{
return Err(io::Error::from(io::ErrorKind::NotFound));
}
println!(
"Successfully downloaded versoview archive in {} ms",
curl_start.elapsed().as_millis()
);
}
Ok(archive_path)
}
/// Download and extract the pre-built versoview executable to this directory
///
/// This function uses the base URL from [`default_archive_base_url`],
/// if you want more flexibility, use [`download_archive`] and then [`decompress_archive`] separately
pub fn download_and_extract_verso<P: AsRef<Path>>(output_directory: P) -> Result<(), io::Error> {
let archive_path = download_archive(default_archive_base_url())?;
decompress_archive(&archive_path, output_directory)?;
Ok(())
}
/// If you don't know where to put the versoview executable,
/// this function gives you the `target` directory
pub fn default_output_directory() -> PathBuf {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
// Not ideal, but there's no good way of getting the target directory
out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}
/// Default archive base URL: https://github.com/versotile-org/versoview-release/releases
pub const fn default_archive_base_url() -> &'static str {
"https://github.com/versotile-org/versoview-release/releases"
}