use std::path::{Path, PathBuf};
use std::time::SystemTime;
use crate::{Config, LauncherBuild};
pub const NEWEST_PROFILES: [&str; 2] = ["dist", "release"];
pub fn checkout_root(exe: &Path) -> Option<PathBuf> {
let target = exe.parent()?.parent()?;
if target.file_name()? != "target" {
return None;
}
let root = target.parent()?;
(root.join("Cargo.toml").is_file() && root.join("crates/gitpoulpi-tui").is_dir())
.then(|| root.to_path_buf())
}
pub fn is_dev_mode(explicit: Option<bool>, exe: &Path) -> bool {
explicit.unwrap_or_else(|| checkout_root(exe).is_some())
}
pub fn dev_mode(config: &Config, exe: &Path) -> bool {
is_dev_mode(config.general.dev_mode, exe)
}
pub fn launcher_newest(config: &Config, exe: &Path) -> bool {
config.ui.launcher_build == LauncherBuild::Newest && dev_mode(config, exe)
}
pub fn launcher_newest_on_disk(exe: &Path) -> bool {
crate::ConfigPaths::discover()
.and_then(|p| Config::load(&p.config_file()).ok())
.is_some_and(|c| launcher_newest(&c, exe))
}
fn modified(path: &Path) -> Option<SystemTime> {
std::fs::metadata(path)
.ok()
.filter(|m| m.is_file())
.and_then(|m| m.modified().ok())
}
pub fn newest_build(exe: &Path) -> PathBuf {
let (Some(root), Some(name)) = (checkout_root(exe), exe.file_name()) else {
return exe.to_path_buf();
};
let mut best: Option<(SystemTime, PathBuf)> = None;
for profile in NEWEST_PROFILES {
let path = root.join("target").join(profile).join(name);
if let Some(time) = modified(&path) {
if best.as_ref().is_none_or(|(t, _)| time > *t) {
best = Some((time, path));
}
}
}
best.map_or_else(|| exe.to_path_buf(), |(_, p)| p)
}
pub fn build_variants(exe: &Path) -> Vec<PathBuf> {
let (Some(root), Some(name)) = (checkout_root(exe), exe.file_name()) else {
return vec![exe.to_path_buf()];
};
NEWEST_PROFILES
.iter()
.map(|p| root.join("target").join(p).join(name))
.collect()
}
fn same_path(a: &Path, b: &Path) -> bool {
if cfg!(windows) {
a.to_string_lossy().to_lowercase() == b.to_string_lossy().to_lowercase()
} else {
a == b
}
}
pub fn same_build_family(a: &Path, b: &Path) -> bool {
same_path(a, b)
|| build_variants(a).len() == 2 && build_variants(a).iter().any(|v| same_path(v, b))
}
pub fn resolve_command(command: &[String]) -> Vec<String> {
command
.iter()
.map(|arg| {
let path = Path::new(arg);
let in_profile = path
.parent()
.and_then(Path::file_name)
.is_some_and(|p| NEWEST_PROFILES.iter().any(|n| p == *n));
let ours = path
.file_name()
.is_some_and(|n| n.to_string_lossy().starts_with("gitpoulpi"));
if !(in_profile && ours) {
return arg.clone();
}
newest_build(path).to_string_lossy().into_owned()
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
fn checkout(root: &Path) {
std::fs::write(root.join("Cargo.toml"), "[workspace]\n").unwrap();
std::fs::create_dir_all(root.join("crates/gitpoulpi-tui")).unwrap();
}
fn build(root: &Path, profile: &str, name: &str, age_secs: u64) -> PathBuf {
let dir = root.join("target").join(profile);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join(name);
let file = std::fs::File::create(&path).unwrap();
let when = SystemTime::now() - Duration::from_secs(age_secs);
file.set_modified(when).unwrap();
path
}
#[test]
fn explicit_dev_mode_wins_either_way() {
let tmp = tempfile::tempdir().unwrap();
let exe = tmp.path().join("bin/gitpoulpi-tui");
assert!(is_dev_mode(Some(true), &exe));
checkout(tmp.path());
let exe = build(tmp.path(), "release", "gitpoulpi-tui", 0);
assert!(!is_dev_mode(Some(false), &exe));
}
#[test]
fn auto_dev_mode_needs_a_checkout_around_target_profile() {
let tmp = tempfile::tempdir().unwrap();
let loose = build(tmp.path(), "release", "gitpoulpi-tui", 0);
assert!(!is_dev_mode(None, &loose), "no Cargo.toml / crates");
checkout(tmp.path());
assert!(is_dev_mode(None, &loose));
let elsewhere = tmp.path().join("opt/gitpoulpi-tui");
assert!(!is_dev_mode(None, &elsewhere));
let other = tempfile::tempdir().unwrap();
let notarget = other.path().join("bin/gitpoulpi-tui");
std::fs::create_dir_all(other.path().join("bin")).unwrap();
checkout(other.path());
assert!(!is_dev_mode(None, ¬arget), "not under target/");
}
#[test]
fn newest_picks_the_later_mtime_of_dist_and_release() {
let tmp = tempfile::tempdir().unwrap();
checkout(tmp.path());
let dist = build(tmp.path(), "dist", "gitpoulpi-tui", 100);
let release = build(tmp.path(), "release", "gitpoulpi-tui", 10);
assert_eq!(newest_build(&dist), release);
assert_eq!(newest_build(&release), release);
build(tmp.path(), "dist", "gitpoulpi-tui", 1);
assert_eq!(newest_build(&release), dist);
}
#[test]
fn newest_with_one_build_missing_or_none_or_debug_ignored() {
let tmp = tempfile::tempdir().unwrap();
checkout(tmp.path());
let debug = build(tmp.path(), "debug", "gitpoulpi-tui", 0);
assert_eq!(newest_build(&debug), debug, "nothing to choose from");
let release = build(tmp.path(), "release", "gitpoulpi-tui", 500);
assert_eq!(newest_build(&debug), release, "debug never wins");
build(tmp.path(), "custom", "gitpoulpi-tui", 0);
assert_eq!(newest_build(&release), release, "custom never wins");
let outside = tmp.path().join("elsewhere/gitpoulpi-tui");
assert_eq!(newest_build(&outside), outside);
}
#[test]
fn the_command_is_resolved_per_gitpoulpi_program_only() {
let tmp = tempfile::tempdir().unwrap();
checkout(tmp.path());
let old = build(tmp.path(), "dist", "gitpoulpi-tui", 100);
let new = build(tmp.path(), "release", "gitpoulpi-tui", 1);
let cmd: Vec<String> = ["wt.exe", old.to_str().unwrap(), "--launcher"]
.map(String::from)
.to_vec();
let out = resolve_command(&cmd);
assert_eq!(out[0], "wt.exe");
assert_eq!(out[1], new.to_string_lossy());
assert_eq!(out[2], "--launcher");
}
#[test]
fn dist_and_release_of_one_file_are_one_family() {
let tmp = tempfile::tempdir().unwrap();
checkout(tmp.path());
let a = build(tmp.path(), "dist", "gitpoulpi-tui", 0);
let b = build(tmp.path(), "release", "gitpoulpi-tui", 0);
let c = build(tmp.path(), "release", "gitpoulpi", 0);
assert!(same_build_family(&a, &b));
assert!(!same_build_family(&a, &c));
assert!(!same_build_family(
&a,
Path::new("/elsewhere/gitpoulpi-tui")
));
}
#[test]
fn launcher_newest_needs_both_switches() {
let tmp = tempfile::tempdir().unwrap();
checkout(tmp.path());
let exe = build(tmp.path(), "release", "gitpoulpi-tui", 0);
let mut config = Config::default();
assert!(!launcher_newest(&config, &exe));
config.ui.launcher_build = LauncherBuild::Newest;
assert!(launcher_newest(&config, &exe));
config.general.dev_mode = Some(false);
assert!(!launcher_newest(&config, &exe));
config.general.dev_mode = Some(true);
assert!(launcher_newest(&config, Path::new("/opt/gp/gitpoulpi-tui")));
}
}