committed by
GitHub
parent
9c9c92796b
commit
3f80bee53a
@@ -66,5 +66,9 @@
|
|||||||
|
|
||||||
#[windows]
|
#[windows]
|
||||||
# Manually select Windows updates
|
# Manually select Windows updates
|
||||||
# accept_all_updates = false
|
#accept_all_updates = false
|
||||||
|
|
||||||
|
# Causes Topgrade to rename itself during the run to allow package managers
|
||||||
|
# to upgrade it. Use this only if you installed Topgrade by using a package
|
||||||
|
# manager such as Scoop to Cargo
|
||||||
|
#self_rename = true
|
||||||
|
|||||||
@@ -126,6 +126,7 @@ pub struct Vagrant {
|
|||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct Windows {
|
pub struct Windows {
|
||||||
accept_all_updates: Option<bool>,
|
accept_all_updates: Option<bool>,
|
||||||
|
self_rename: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Default, Debug)]
|
#[derive(Deserialize, Default, Debug)]
|
||||||
@@ -505,6 +506,16 @@ impl Config {
|
|||||||
.unwrap_or(true)
|
.unwrap_or(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether to self rename the Topgrade executable during the run
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn self_rename(&self) -> bool {
|
||||||
|
self.config_file
|
||||||
|
.windows
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|w| w.self_rename)
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
|
|
||||||
/// Whether Brew cask should be greedy
|
/// Whether Brew cask should be greedy
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn brew_cask_greedy(&self) -> bool {
|
pub fn brew_cask_greedy(&self) -> bool {
|
||||||
|
|||||||
12
src/main.rs
12
src/main.rs
@@ -6,6 +6,8 @@ mod execution_context;
|
|||||||
mod executor;
|
mod executor;
|
||||||
mod report;
|
mod report;
|
||||||
mod runner;
|
mod runner;
|
||||||
|
#[cfg(windows)]
|
||||||
|
mod self_renamer;
|
||||||
#[cfg(feature = "self-update")]
|
#[cfg(feature = "self-update")]
|
||||||
mod self_update;
|
mod self_update;
|
||||||
mod steps;
|
mod steps;
|
||||||
@@ -92,6 +94,16 @@ fn run() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let _self_rename = if config.self_rename() {
|
||||||
|
Some(crate::self_renamer::SelfRenamer::create()?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut s = String::new();
|
||||||
|
std::io::stdin().read_line(&mut s)?;
|
||||||
|
|
||||||
if let Some(commands) = config.pre_commands() {
|
if let Some(commands) = config.pre_commands() {
|
||||||
for (name, command) in commands {
|
for (name, command) in commands {
|
||||||
generic::run_custom_command(&name, &command, &ctx)?;
|
generic::run_custom_command(&name, &command, &ctx)?;
|
||||||
|
|||||||
43
src/self_renamer.rs
Normal file
43
src/self_renamer.rs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#![cfg(windows)]
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use log::{debug, error};
|
||||||
|
use std::{env::current_exe, fs, path::PathBuf};
|
||||||
|
|
||||||
|
pub struct SelfRenamer {
|
||||||
|
exe_path: PathBuf,
|
||||||
|
temp_path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SelfRenamer {
|
||||||
|
pub fn create() -> Result<Self> {
|
||||||
|
let tempdir = tempfile::tempdir()?;
|
||||||
|
let temp_path = tempdir.path().join("topgrade.exe");
|
||||||
|
let exe_path = current_exe()?;
|
||||||
|
|
||||||
|
debug!("Current exe in {:?}. Moving it to {:?}", exe_path, temp_path);
|
||||||
|
|
||||||
|
fs::rename(&exe_path, &temp_path)?;
|
||||||
|
|
||||||
|
Ok(SelfRenamer { exe_path, temp_path })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for SelfRenamer {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if self.exe_path.exists() {
|
||||||
|
debug!("{:?} exists. Topgrade was probably upgraded", self.exe_path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
match fs::rename(&self.temp_path, &self.exe_path) {
|
||||||
|
Ok(_) => debug!("Moved topgrade back from {:?} to {:?}", self.temp_path, self.exe_path),
|
||||||
|
Err(e) => error!(
|
||||||
|
"Could not move Topgrade from {} back to {}: {}",
|
||||||
|
self.temp_path.display(),
|
||||||
|
self.exe_path.display(),
|
||||||
|
e
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user