Skip to content

Commit

Permalink
Allow self-install to update running executables (#20)
Browse files Browse the repository at this point in the history
* Allow self-install to update running executables

* Update snapshot, no fast-fail, rust-version field
  • Loading branch information
LPGhatguy committed Jul 2, 2022
1 parent dca85a2 commit 072cf5d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
rust_version: [stable, 1.58.0]

Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "aftman"
description = "Aftman is a command line toolchain manager"
version = "0.2.4"
rust-version = "1.58.0"
license = "MIT"
edition = "2021"
repository = "https://github.com/LPGhatguy/aftman"
Expand All @@ -24,6 +25,7 @@ semver = { version = "1.0.4", features = ["serde"] }
serde = { version = "1.0.129", features = ["derive"] }
serde_json = "1.0.67"
structopt = "0.3.22"
tempfile = "3.3.0"
toml = "0.5.8"
toml_edit = "0.14.4"
zip = "0.5.13"
Expand Down
34 changes: 32 additions & 2 deletions src/tool_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,51 @@ impl ToolStorage {
Ok(code)
}

/// Update all executables managed by Aftman, which might include Aftman
/// itself.
pub fn update_links(&self) -> anyhow::Result<()> {
let self_path =
current_exe().context("Failed to discover path to the Aftman executable")?;
let self_name = self_path.file_name().unwrap();

log::info!("Updating all Aftman binaries...");

// Copy our current executable into a temp directory. That way, if it
// ends up replaced by this process, we'll still have the file that
// we're supposed to be copying.
log::debug!("Copying own executable into temp dir");
let source_dir = tempfile::tempdir()?;
let source_path = source_dir.path().join(self_name);
fs_err::copy(&self_path, &source_path)?;
let self_path = source_path;

let junk_dir = tempfile::tempdir()?;
let aftman_name = format!("aftman{EXE_SUFFIX}");
let mut found_aftman = false;

for entry in fs_err::read_dir(&self.bin_dir)? {
let entry = entry?;
let path = entry.path();
let name = path.file_name().unwrap().to_str().unwrap();

if name == aftman_name {
found_aftman = true;
}

log::debug!("Updating {:?}", name);

// Copy the executable into a temp directory so that we can replace
// it even if it's currently running.
fs_err::rename(&path, junk_dir.path().join(name))?;
fs_err::copy(&self_path, path)?;
}

let aftman_path = self.bin_dir.join(format!("aftman{}", EXE_SUFFIX));
fs_err::copy(&self_path, aftman_path)?;
// If we didn't find and update Aftman already, install it.
if !found_aftman {
log::info!("Installing Aftman...");
let aftman_path = self.bin_dir.join(aftman_name);
fs_err::copy(&self_path, aftman_path)?;
}

log::info!("Updated Aftman binaries successfully!");

Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/ui__self-install stderr.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ assertion_line: 10
expression: output.stderr
---
[INFO aftman::tool_storage] Updating all Aftman binaries...
[INFO aftman::tool_storage] Installing Aftman...
[INFO aftman::tool_storage] Updated Aftman binaries successfully!

0 comments on commit 072cf5d

Please sign in to comment.