use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Arc, Mutex};
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::install::{self, Layout};
use crate::out::{self, Kind, Line};
use crate::status::UpdateNote;
use crate::theme::theme;
static BUSY: Mutex<()> = Mutex::new(());
#[derive(Debug, Clone, Deserialize)]
pub struct Manifest {
pub version: String,
pub platforms: BTreeMap<String, Asset>,
#[serde(default)]
pub notes: Vec<String>,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Asset {
pub file: String,
pub sha256: String,
}
impl Manifest {
pub fn asset(&self, platform: &str) -> Result<&Asset> {
self.platforms
.get(platform)
.or_else(|| install::fallback_platform(platform).and_then(|p| self.platforms.get(p)))
.with_context(|| format!("release {} has no {platform} build", self.version))
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WhatsNew {
pub version: String,
pub notes: Vec<String>,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
impl WhatsNew {
pub fn path(data_dir: &Path) -> PathBuf {
data_dir.join("whats-new.json")
}
pub fn save(&self, data_dir: &Path) {
if let Ok(json) = serde_json::to_string_pretty(self) {
let _ = std::fs::write(Self::path(data_dir), json);
}
}
pub fn take(data_dir: &Path, version: &str) -> Option<Self> {
let path = Self::path(data_dir);
let w: Self = serde_json::from_str(&std::fs::read_to_string(&path).ok()?).ok()?;
if !install::is_newer(&w.version, version) {
let _ = std::fs::remove_file(&path);
}
(w.version == version).then_some(w)
}
}
pub fn news(title: &str, notes: &[String], link: &str) -> Vec<Line> {
let t = theme();
let mut heading = vec![(Kind::Paint(t.good, true), "✓ ".into()), (Kind::Bold, title.to_owned())];
if !notes.is_empty() {
heading.push((Kind::Dim, " · what's new".into()));
}
let mut lines = vec![heading];
for n in notes {
lines.push(vec![
(Kind::Paint(t.accent, false), " • ".into()),
(Kind::Out, n.clone()),
]);
}
lines.push(vec![(Kind::Dim, format!(" {link} for details"))]);
lines.push(vec![(Kind::Out, String::new())]);
lines
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Updated {
UpToDate,
Installed(String),
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct State {
pub checked_at: u64,
pub result: String,
}
impl State {
pub fn path(data_dir: &Path) -> PathBuf {
data_dir.join("update.json")
}
pub fn load(data_dir: &Path) -> Option<Self> {
let text = std::fs::read_to_string(Self::path(data_dir)).ok()?;
serde_json::from_str(&text).ok()
}
pub fn record(data_dir: &Path, result: impl Into<String>) {
let state = State {
checked_at: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs()),
result: result.into(),
};
if std::fs::create_dir_all(data_dir).is_ok()
&& let Ok(json) = serde_json::to_string_pretty(&state)
{
let _ = std::fs::write(Self::path(data_dir), json);
}
}
}
type Keep = Arc<dyn Fn(&Manifest) -> Map<String, Value> + Send + Sync>;
#[derive(Clone)]
pub struct Updater {
pub name: String,
pub repo: String,
pub platform: String,
pub version: String,
pub env_prefix: String,
pub layout: Layout,
pub data_dir: PathBuf,
pub install_args: Vec<String>,
pub data_dir_flag: Option<String>,
keep: Option<Keep>,
}
impl Updater {
pub fn new(name: &str, app: &str, repo: &str, version: &str, data_dir: impl Into<PathBuf>) -> Result<Self> {
Ok(Updater {
name: name.to_owned(),
repo: repo.to_owned(),
platform: install::platform(),
version: version.to_owned(),
env_prefix: install::env_prefix(app),
layout: Layout::new(app)?,
data_dir: data_dir.into(),
install_args: vec!["install".into(), "--update".into()],
data_dir_flag: None,
keep: None,
})
}
pub fn platform(mut self, platform: &str) -> Self {
self.platform = platform.to_owned();
self
}
pub fn layout(mut self, layout: Layout) -> Self {
self.layout = layout;
self
}
pub fn data_dir_flag(mut self, flag: &str) -> Self {
self.data_dir_flag = Some(flag.to_owned());
self
}
pub fn keep_extra(mut self, keep: impl Fn(&Manifest) -> Map<String, Value> + Send + Sync + 'static) -> Self {
self.keep = Some(Arc::new(keep));
self
}
pub fn disable_env(&self) -> String {
format!("{}_DISABLE_AUTOUPDATER", self.env_prefix)
}
pub fn url_env(&self) -> String {
format!("{}_UPDATE_URL", self.env_prefix)
}
pub fn agent(&self) -> String {
format!("{}/{}", self.layout.app, self.version)
}
pub fn auto_disabled(&self, setting: bool) -> Option<String> {
let env = self.disable_env();
if std::env::var_os(&env).is_some_and(|v| !v.is_empty() && v != "0") {
return Some(format!("disabled by {env}"));
}
(!setting).then(|| "turned off in the settings".to_owned())
}
pub fn override_base(&self) -> Option<String> {
std::env::var(self.url_env())
.ok()
.filter(|u| !u.is_empty())
.map(|u| u.trim_end_matches('/').to_owned())
}
pub fn releases(&self) -> String {
format!("github.com/{}/releases/tag", self.repo)
}
pub fn main_file(&self, path: &str) -> String {
match self.override_base() {
Some(base) => format!("{base}/{path}"),
None => format!("https://raw.githubusercontent.com/{}/main/{path}", self.repo),
}
}
pub fn get_text(&self, url: &str) -> Result<String> {
let mut resp = install::http_get(url, &self.agent())?;
resp.body_mut()
.read_to_string()
.with_context(|| format!("reading {url}"))
}
pub fn latest(&self) -> Result<Manifest> {
let url = match self.override_base() {
Some(base) => format!("{base}/manifest.json"),
None => format!(
"https://github.com/{}/releases/latest/download/manifest.json",
self.repo
),
};
let text = self.get_text(&url)?;
let manifest: Manifest = serde_json::from_str(&text).with_context(|| format!("parsing {url}"))?;
if install::version_key(&manifest.version).iter().all(|&n| n == 0) {
bail!("{url} names no usable version ({:?})", manifest.version);
}
Ok(manifest)
}
pub fn file_url(&self, manifest: &Manifest, file: &str) -> String {
match self.override_base() {
Some(base) => format!("{base}/{file}"),
None => format!(
"https://github.com/{}/releases/download/{}/{file}",
self.repo, manifest.version
),
}
}
pub fn apply(&self, manifest: &Manifest) -> Result<PathBuf> {
let _busy = BUSY.lock().unwrap_or_else(|e| e.into_inner());
let asset = manifest.asset(&self.platform)?;
let exe = self.layout.version_exe(&manifest.version);
let present = exe.is_file() && install::check_sha256(&exe, &asset.sha256, "").is_ok();
if !present {
let dir = exe.parent().expect("version_exe has a parent");
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
let url = self.file_url(manifest, &asset.file);
install::download_verified(&url, &exe, &self.agent(), &asset.sha256, &asset.file)?;
install::make_executable(&exe)?;
}
let mut cmd = Command::new(&exe);
cmd.args(&self.install_args)
.env(install::home_env(&self.layout.app), &self.layout.root);
if let Some(flag) = &self.data_dir_flag {
cmd.arg(flag).arg(&self.data_dir);
}
let output = cmd.output().with_context(|| format!("running {}", exe.display()))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let last = stderr
.lines()
.rev()
.find(|l| !l.trim().is_empty())
.unwrap_or("no error output");
bail!("{} {} failed to install itself: {last}", self.name, manifest.version);
}
WhatsNew {
version: manifest.version.clone(),
notes: manifest.notes.clone(),
extra: self.keep.as_ref().map(|k| k(manifest)).unwrap_or_default(),
}
.save(&self.data_dir);
Ok(exe)
}
pub fn installed(&self) -> Option<&Layout> {
self.layout.runs_this().then_some(&self.layout)
}
pub fn check(&self, install_it: bool) -> Option<UpdateNote> {
let layout = self.installed();
if let Some(l) = layout {
install::cleanup(l, &self.version);
}
let manifest = match self.latest() {
Ok(m) => m,
Err(e) => {
State::record(&self.data_dir, format!("check failed: {e:#}"));
return None;
}
};
let v = manifest.version.clone();
if !install::is_newer(&v, &self.version) {
State::record(&self.data_dir, format!("up to date ({})", self.version));
return None;
}
if layout.is_none() || !install_it {
State::record(&self.data_dir, format!("{v} available"));
return Some(UpdateNote::Available);
}
Some(match self.apply(&manifest) {
Ok(_) => {
State::record(&self.data_dir, format!("installed {v}"));
UpdateNote::Installed
}
Err(e) => {
State::record(&self.data_dir, format!("failed: {e:#}"));
UpdateNote::Failed
}
})
}
pub fn whats_new(&self) -> Option<Vec<Line>> {
let w = WhatsNew::take(&self.data_dir, &self.version)?;
Some(self.news(&w))
}
pub fn news(&self, w: &WhatsNew) -> Vec<Line> {
news(
&format!("{} updated to v{}", self.name, w.version),
&w.notes,
&format!("{}/{}", self.releases(), w.version),
)
}
pub fn update_command(&self) -> Result<Updated> {
if let Some(l) = self.installed() {
install::cleanup(l, &self.version);
}
let manifest = self.latest().context("checking for updates failed")?;
if !install::is_newer(&manifest.version, &self.version) {
State::record(&self.data_dir, format!("up to date ({})", self.version));
crate::outln!("{} is up to date (v{})", self.name, self.version);
return Ok(Updated::UpToDate);
}
match self.apply(&manifest) {
Ok(_) => {
State::record(&self.data_dir, format!("installed {}", manifest.version));
out::row(vec![
(Kind::Paint(theme().good, true), "✓ ".into()),
(Kind::Bold, "New version installed".into()),
]);
out::emit(
Kind::Dim,
format!(
" Restart {} to use it: /exit, then run {} again. What's new shows when it opens.",
self.name, self.layout.app
),
);
Ok(Updated::Installed(manifest.version))
}
Err(e) => {
State::record(&self.data_dir, format!("failed: {e:#}"));
Err(e)
}
}
}
pub fn background(
self,
auto: impl Fn() -> bool + Send + Sync + 'static,
) -> impl Fn(Option<UpdateNote>) -> Option<UpdateNote> + Send + Sync + 'static {
move |note| {
if note == Some(UpdateNote::Installed) {
return note;
}
let install_it = self.auto_disabled(auto()).is_none();
self.check(install_it)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_a_manifest_and_keeps_other_keys() {
let m: Manifest = serde_json::from_str(
r#"{
"version": "0.2.0",
"platforms": {"windows-x64": {"file": "demo-windows-x64.exe", "sha256": "ab"}},
"notes": ["One.", "Two."],
"helper": {"tag": "v0.3.0"}
}"#,
)
.unwrap();
assert_eq!(m.asset("windows-x64").unwrap().file, "demo-windows-x64.exe");
assert!(m.asset("linux-x64").is_err());
assert_eq!(
m.asset("windows-arm64").unwrap().file,
"demo-windows-x64.exe",
"Arm Windows falls back to x64"
);
assert_eq!(m.notes, ["One.", "Two."]);
assert_eq!(m.extra["helper"]["tag"], "v0.3.0");
}
#[test]
fn whats_new_shows_once() {
let dir = std::env::temp_dir().join(format!("abalone-whats-new-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let mut extra = Map::new();
extra.insert("helper".into(), serde_json::json!({"tag": "v1"}));
WhatsNew {
version: "0.2.0".into(),
notes: vec!["New.".into()],
extra,
}
.save(&dir);
assert!(WhatsNew::take(&dir, "0.1.0").is_none(), "an older build leaves them");
let w = WhatsNew::take(&dir, "0.2.0").unwrap();
assert_eq!(w.notes, ["New."]);
assert_eq!(w.extra["helper"]["tag"], "v1");
assert!(WhatsNew::take(&dir, "0.2.0").is_none());
WhatsNew {
version: "0.2.0".into(),
..WhatsNew::default()
}
.save(&dir);
assert!(WhatsNew::take(&dir, "0.3.0").is_none());
assert!(!WhatsNew::path(&dir).exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn names_its_urls_and_environment() {
let u = Updater::new("Demo", "demo", "me/demo", "0.1.0", "C:/data")
.unwrap()
.layout(Layout::at("C:/nowhere", "demo"));
assert_eq!(u.disable_env(), "DEMO_DISABLE_AUTOUPDATER");
assert_eq!(u.agent(), "demo/0.1.0");
if u.override_base().is_none() {
assert_eq!(
u.main_file("x.json"),
"https://raw.githubusercontent.com/me/demo/main/x.json"
);
}
assert!(u.installed().is_none());
}
}