use std::{
env, fs, io,
path::{Path, PathBuf},
process::Command,
sync::atomic::{AtomicU64, Ordering},
};
pub fn sources(root: &Path) -> io::Result<Vec<PathBuf>> {
fn visit(root: &Path, dir: &Path, files: &mut Vec<PathBuf>) -> io::Result<()> {
for entry in fs::read_dir(root.join(dir))? {
let entry = entry?;
let name = entry.file_name();
if dir.as_os_str().is_empty()
&& ["Cargo.toml", ".git", "target", "gen"]
.iter()
.any(|s| name == *s)
{
continue;
}
let path = dir.join(name);
let kind = entry.file_type()?;
if kind.is_dir() {
visit(root, &path, files)?;
} else if kind.is_file() {
files.push(path);
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("unexpected desktop source: {}", path.display()),
));
}
}
Ok(())
}
let mut files = Vec::new();
visit(root, Path::new(""), &mut files)?;
files.sort();
for input in ["Cargo.toml.in", "Cargo.lock"] {
if !files.iter().any(|f| f == Path::new(input)) {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("missing desktop {input}"),
));
}
}
Ok(files)
}
pub fn stage(source: &Path, destination: &Path) -> io::Result<()> {
let files = sources(source)?;
fs::create_dir(destination)?;
let copy = || {
for file in files {
let output = if file == Path::new("Cargo.toml.in") {
destination.join("Cargo.toml")
} else {
destination.join(&file)
};
fs::create_dir_all(output.parent().expect("staged source parent"))?;
fs::copy(source.join(file), output)?;
}
Ok(())
};
let result = copy();
if result.is_err() {
let _ = fs::remove_dir_all(destination);
}
result
}
pub fn build(source: &Path, work: &Path, target: &str, revision: &str) -> io::Result<PathBuf> {
if target.is_empty()
|| !target
.bytes()
.all(|c| c.is_ascii_alphanumeric() || b"-_".contains(&c))
{
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid Rust target triple",
));
}
fs::create_dir_all(work)?;
let work = work.canonicalize()?;
if work.starts_with(source.canonicalize()?) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"place the build directory outside the desktop sources",
));
}
static NEXT: AtomicU64 = AtomicU64::new(0);
let staged = loop {
let id = NEXT.fetch_add(1, Ordering::Relaxed);
let path = work.join(format!("source-{}-{id}", std::process::id()));
match stage(source, &path) {
Ok(()) => break path,
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e),
}
};
let target_dir = work.join("target");
let cargo = env::var_os("CARGO").map(PathBuf::from).unwrap_or_else(|| {
let cargo_home = env::var_os("CARGO_HOME").map(PathBuf::from);
let default_home = env::var_os("HOME").map(|home| PathBuf::from(home).join(".cargo"));
[cargo_home, default_home]
.into_iter()
.flatten()
.map(|home| home.join("bin/cargo"))
.find(|path| path.is_file())
.unwrap_or_else(|| PathBuf::from("cargo"))
});
let status = Command::new(cargo)
.current_dir(&staged)
.env("GENT_DESKTOP_BUILD_REVISION", revision)
.args([
"build",
"--release",
"--locked",
"--bin",
"gent-desktop",
"--target",
target,
"--target-dir",
])
.arg(&target_dir)
.status();
fs::remove_dir_all(staged)?;
if !status?.success() {
return Err(io::Error::other(
"could not build the Gent Desktop companion",
));
}
Ok(target_dir.join(target).join("release/gent-desktop"))
}