Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow self-install to update running executables #20

Merged
merged 2 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!