Implement self-update

This commit is contained in:
Roey Darwish Dror
2018-11-07 14:31:44 +02:00
committed by Your Name
parent 24f8053b17
commit 6108637477
7 changed files with 1095 additions and 5 deletions

1029
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -21,6 +21,7 @@ log = "0.4.6"
env_logger = "0.5.13"
walkdir = "2.2.6"
console = "0.6.2"
self_update = { git = "https://github.com/r-darwish/self_update", branch = "bump-reqwest", optional = true }
[target.'cfg(unix)'.dependencies]
nix = "0.11.0"
@@ -28,3 +29,8 @@ lazy_static = "1.1.0"
[profile.release]
lto = true
[features]
default = []
self-update = ["self_update"]

View File

@@ -18,7 +18,7 @@ Arch Linux users can use the [AUR](https://aur.archlinux.org/packages/topgrade/)
macOS users can install topgrade via Homebrew.
Other systems users can either use `cargo install` or use the compiled binaries from the release
page.
page. The compiled binaries contain a self-upgrading feature.
Topgrade isn't guaranteed to work on Rust versions older than the latest stable release. If you
intend to install Topgrade using Cargo then you should either install Rust using rustup or use a

View File

@@ -18,7 +18,7 @@ main() {
test -f Cargo.lock || cargo generate-lockfile
# TODO Update this to build the artifacts that matter to you
cross rustc --bin topgrade --target $TARGET --release -- -C lto
cross rustc --bin topgrade --target $TARGET --release --all-features -- -C lto
# TODO Update this to package the right artifacts
cp target/$TARGET/release/topgrade $stage/

View File

@@ -8,6 +8,8 @@ main() {
cargo clippy --all-targets --all-features -- -D warnings
cross check --target $TARGET
cross check --target $TARGET --release
cross check --target $TARGET --all-features
cross check --target $TARGET --release --all-features
if [ ! -z $DISABLE_TESTS ]; then
return

View File

@@ -229,3 +229,40 @@ pub fn run_composer_update(
None
}
#[must_use]
#[cfg(all(
feature = "self-update",
any(windows, target_os = "linux", target_os = "macos")
))]
pub fn self_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
terminal.print_separator("Self update");
let success = if !dry_run {
let result = || -> Result<(), Error> {
let target = self_update::get_target()?;
self_update::backends::github::Update::configure()?
.repo_owner("r-darwish")
.repo_name("topgrade")
.target(&target)
.bin_name("topgrade")
.show_download_progress(true)
.current_version(cargo_crate_version!())
.no_confirm(true)
.build()?
.update()?;
Ok(())
}();
match result {
Err(e) => {
println!("{}", e);
false
}
Ok(_) => (true),
}
} else {
true
};
Some(("Self update", success))
}

View File

@@ -19,6 +19,12 @@ extern crate nix;
#[cfg(unix)]
#[macro_use]
extern crate lazy_static;
#[cfg(all(
feature = "self-update",
any(windows, target_os = "linux", target_os = "macos")
))]
#[macro_use]
extern crate self_update;
extern crate walkdir;
#[cfg(target_os = "linux")]
@@ -114,20 +120,30 @@ fn run() -> Result<(), Error> {
}
env_logger::init();
let base_dirs = directories::BaseDirs::new().ok_or(NoBaseDirectories)?;
let git = Git::new();
let mut git_repos = Repositories::new(&git);
let mut execution_context = ExecutionContext {
terminal: Terminal::new(),
};
let base_dirs = directories::BaseDirs::new().ok_or(NoBaseDirectories)?;
let git = Git::new();
let mut git_repos = Repositories::new(&git);
let config = Config::read(&base_dirs)?;
let mut report = Report::new();
#[cfg(target_os = "linux")]
let sudo = utils::which("sudo");
#[cfg(all(
feature = "self-update",
any(windows, target_os = "linux", target_os = "macos")
))]
report.push_result(execute(
|terminal| generic::self_update(terminal, opt.dry_run),
&mut execution_context,
)?);
if let Some(commands) = config.pre_commands() {
for (name, command) in commands {
generic::run_custom_command(&name, &command, &mut execution_context.terminal, opt.dry_run)?;