|
| 1 | +use std::process::Command; |
| 2 | + |
| 3 | +use anyhow::Context; |
| 4 | +use anyhow::Result; |
| 5 | +use anyhow::bail; |
| 6 | + |
| 7 | +#[cfg(not(windows))] |
| 8 | +const INSTALL_SH_URL: &str = "https://raw.githubusercontent.com/7df-lab/devo/main/install.sh"; |
| 9 | +#[cfg(windows)] |
| 10 | +const INSTALL_PS1_URL: &str = "https://raw.githubusercontent.com/7df-lab/devo/main/install.ps1"; |
| 11 | + |
| 12 | +pub fn run_upgrade() -> Result<()> { |
| 13 | + run_platform_upgrade() |
| 14 | +} |
| 15 | + |
| 16 | +#[cfg(not(windows))] |
| 17 | +fn run_platform_upgrade() -> Result<()> { |
| 18 | + let status = Command::new("sh") |
| 19 | + .arg("-c") |
| 20 | + .arg(unix_upgrade_script()) |
| 21 | + .status() |
| 22 | + .context("run install.sh for devo upgrade")?; |
| 23 | + |
| 24 | + if !status.success() { |
| 25 | + bail!("devo upgrade failed with status {status}"); |
| 26 | + } |
| 27 | + |
| 28 | + Ok(()) |
| 29 | +} |
| 30 | + |
| 31 | +#[cfg(not(windows))] |
| 32 | +fn unix_upgrade_script() -> String { |
| 33 | + format!( |
| 34 | + r#"set -eu |
| 35 | +command -v curl >/dev/null 2>&1 || {{ |
| 36 | + printf '%s\n' "Error: 'curl' is required but not installed." >&2 |
| 37 | + exit 1 |
| 38 | +}} |
| 39 | +tmp_dir="$(mktemp -d "${{TMPDIR:-/tmp}}/devo-upgrade.XXXXXX")" |
| 40 | +trap 'rm -rf "$tmp_dir"' EXIT INT TERM |
| 41 | +curl -fsSL '{INSTALL_SH_URL}' -o "$tmp_dir/install.sh" |
| 42 | +sh "$tmp_dir/install.sh" |
| 43 | +"# |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(windows)] |
| 48 | +fn run_platform_upgrade() -> Result<()> { |
| 49 | + use std::process::Stdio; |
| 50 | + |
| 51 | + let parent_pid = std::process::id(); |
| 52 | + Command::new("powershell.exe") |
| 53 | + .args([ |
| 54 | + "-NoProfile", |
| 55 | + "-ExecutionPolicy", |
| 56 | + "Bypass", |
| 57 | + "-Command", |
| 58 | + &windows_upgrade_script(parent_pid), |
| 59 | + ]) |
| 60 | + .stdin(Stdio::null()) |
| 61 | + .spawn() |
| 62 | + .context("start install.ps1 for devo upgrade")?; |
| 63 | + |
| 64 | + println!("Started devo upgrade with install.ps1."); |
| 65 | + println!("The installer will continue after this devo.exe process exits."); |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(windows)] |
| 70 | +fn windows_upgrade_script(parent_pid: u32) -> String { |
| 71 | + format!( |
| 72 | + r#"$ErrorActionPreference = 'Stop' |
| 73 | +$parent = Get-Process -Id {parent_pid} -ErrorAction SilentlyContinue |
| 74 | +if ($parent) {{ |
| 75 | + Wait-Process -Id {parent_pid} |
| 76 | +}} |
| 77 | +$script = Invoke-WebRequest -UseBasicParsing -Uri '{INSTALL_PS1_URL}' |
| 78 | +Invoke-Expression $script.Content |
| 79 | +"# |
| 80 | + ) |
| 81 | +} |
0 commit comments