use std::path::PathBuf;
use crate::time::day_start_before;
pub const NPM_BEFORE: &str = "npm_config_before";
pub const UV_EXCLUDE_NEWER: &str = "UV_EXCLUDE_NEWER";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UserConfig {
Npm,
Uv,
}
pub struct Found<'a> {
pub inherited: &'a dyn Fn(&str) -> Option<String>,
pub project_has: &'a dyn Fn(&str) -> bool,
pub project_file: &'a dyn Fn(&str) -> Option<String>,
pub user_file: &'a dyn Fn(UserConfig) -> Option<String>,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ReleaseAge {
pub vars: Vec<(&'static str, String)>,
pub notices: Vec<String>,
}
pub fn env(days: u16, now_ms: i64, found: &Found<'_>) -> ReleaseAge {
let Found {
inherited,
project_has,
project_file,
user_file,
} = found;
let mut out = ReleaseAge::default();
if days == 0 {
return out;
}
let cutoff = day_start_before(now_ms, days);
let npm_person = inherited(NPM_BEFORE).is_some()
|| inherited("NPM_CONFIG_BEFORE").is_some()
|| user_file(UserConfig::Npm).is_some_and(|text| npmrc_value(&text, "before").is_some());
let npm_stricter = project_file(".npmrc")
.and_then(|text| npmrc_value(&text, "before"))
.is_some_and(|value| stricter(&value, &cutoff));
if !npm_person && !npm_stricter {
out.vars.push((NPM_BEFORE, cutoff.clone()));
}
let uv_person = inherited(UV_EXCLUDE_NEWER).is_some()
|| user_file(UserConfig::Uv).is_some_and(|text| uv_value(&text, false).is_some());
let uv_stricter = [("pyproject.toml", true), ("uv.toml", false)]
.iter()
.filter_map(|(file, pyproject)| uv_value(&project_file(file)?, *pyproject))
.any(|value| stricter(&value, &cutoff));
if uv_person || uv_stricter {
return out;
}
if project_has("uv.lock") {
out.notices.push(format!(
"moat run: uv gets no release-age cut-off ({days} days): the project has a uv.lock, \
which records the cut-off, so one that moves every day would make uv resolve it \
again. Set `exclude-newer` under `[tool.uv]` in pyproject.toml, or \
`{UV_EXCLUDE_NEWER}` before `openmoat run`, to keep one"
));
} else {
out.vars.push((UV_EXCLUDE_NEWER, cutoff));
}
out
}
pub fn user_config_path(
tool: UserConfig,
var: &dyn Fn(&str) -> Option<String>,
home: Option<PathBuf>,
) -> Option<PathBuf> {
let set = |name: &str| var(name).filter(|v| !v.is_empty());
match tool {
UserConfig::Npm => set("npm_config_userconfig")
.or_else(|| set("NPM_CONFIG_USERCONFIG"))
.map(PathBuf::from)
.or_else(|| Some(home?.join(".npmrc"))),
UserConfig::Uv => set("XDG_CONFIG_HOME")
.map(PathBuf::from)
.or_else(|| Some(home?.join(".config")))
.map(|config| config.join("uv").join("uv.toml")),
}
}
fn npmrc_value(npmrc: &str, key: &str) -> Option<String> {
npmrc
.lines()
.filter_map(|line| {
let (name, value) = line.trim().split_once('=')?;
(name.trim() == key).then(|| value.trim().trim_matches('"').to_owned())
})
.next_back()
}
fn uv_value(text: &str, pyproject: bool) -> Option<String> {
let doc: toml_edit::DocumentMut = text.parse().ok()?;
let table = doc.as_table();
let table = if pyproject {
table
.get("tool")?
.as_table_like()?
.get("uv")?
.as_table_like()?
} else {
table
};
let value = table.get("exclude-newer")?.as_str()?;
Some(value.to_owned())
}
fn stricter(value: &str, cutoff: &str) -> bool {
let date = |text: &str| -> Option<String> {
let day = text.trim().get(..10)?;
let shape = day.char_indices().all(|(i, c)| match i {
4 | 7 => c == '-',
_ => c.is_ascii_digit(),
});
shape.then(|| day.to_owned())
};
matches!((date(value), date(cutoff)), (Some(v), Some(c)) if v < c)
}
#[cfg(test)]
mod tests;