Files
topgrade/src/steps/generic.rs

327 lines
9.4 KiB
Rust
Raw Normal View History

#![allow(unused_imports)]
2019-12-12 20:24:22 +02:00
use crate::error::{SkipStep, TopgradeError};
2020-02-08 22:13:56 +02:00
use crate::execution_context::ExecutionContext;
use crate::executor::{CommandExt, ExecutorOutput, RunType};
2019-08-22 21:46:06 +03:00
use crate::terminal::{print_separator, shell};
2019-01-01 22:22:07 +02:00
use crate::utils::{self, PathExt};
use anyhow::Result;
2018-08-19 14:45:23 +03:00
use directories::BaseDirs;
2019-12-12 20:24:22 +02:00
use log::debug;
2019-11-20 14:41:05 +02:00
use std::env;
2018-10-02 13:45:29 +03:00
use std::path::PathBuf;
use std::process::Command;
2019-12-12 20:24:22 +02:00
use tempfile::tempfile_in;
2018-08-19 14:45:23 +03:00
pub fn run_cargo_update(run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let cargo_update = utils::require("cargo-install-update")?;
2018-08-19 14:45:23 +03:00
2019-01-13 23:20:32 +02:00
print_separator("Cargo");
2018-08-19 14:45:23 +03:00
2019-01-13 23:20:32 +02:00
run_type
.execute(cargo_update)
.args(&["install-update", "--git", "--all"])
.check_run()
2018-08-19 14:45:23 +03:00
}
pub fn run_flutter_upgrade(run_type: RunType) -> Result<()> {
2019-11-20 13:35:41 +02:00
let flutter = utils::require("flutter")?;
2019-11-20 14:41:05 +02:00
print_separator("Flutter");
2019-11-20 13:35:41 +02:00
run_type.execute(&flutter).arg("upgrade").check_run()
}
pub fn run_go(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
2019-11-20 14:41:05 +02:00
let go = utils::require("go")?;
env::var("GOPATH")
.unwrap_or_else(|_| base_dirs.home_dir().join("go").to_str().unwrap().to_string())
.require()?;
print_separator("Go");
run_type
.execute(&go)
.args(&["get", "-u", "all"])
2020-03-09 09:41:53 +02:00
.env_remove("GO111MODULE")
.check_run()
2019-11-20 14:41:05 +02:00
}
pub fn run_gem(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let gem = utils::require("gem")?;
base_dirs.home_dir().join(".gem").require()?;
2018-09-06 16:46:49 +03:00
2019-01-13 23:20:32 +02:00
print_separator("RubyGems");
2018-09-06 16:46:49 +03:00
let mut command = run_type.execute(&gem);
command.arg("update");
if env::var_os("RBENV_SHELL").is_none() {
debug!("Detected rbenv. Avoiding --user-install");
command.arg("--user-install");
}
command.check_run()
2018-09-06 16:46:49 +03:00
}
2020-07-30 06:27:29 +03:00
pub fn run_sheldon(ctx: &ExecutionContext) -> Result<()> {
let sheldon = utils::require("sheldon")?;
print_separator("Sheldon");
ctx.run_type().execute(&sheldon).args(&["lock", "--update"]).check_run()
}
2018-10-29 14:32:33 +02:00
#[cfg(not(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly"
)))]
pub fn run_apm(run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let apm = utils::require("apm")?;
2018-08-19 14:45:23 +03:00
2019-01-13 23:20:32 +02:00
print_separator("Atom Package Manager");
2018-08-19 14:45:23 +03:00
2019-01-13 23:20:32 +02:00
run_type.execute(&apm).args(&["upgrade", "--confirm=false"]).check_run()
2018-08-19 14:45:23 +03:00
}
pub fn run_rustup(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let rustup = utils::require("rustup")?;
2018-08-19 14:45:23 +03:00
2019-01-13 23:20:32 +02:00
print_separator("rustup");
2018-08-19 14:45:23 +03:00
if rustup.canonicalize()?.is_descendant_of(base_dirs.home_dir()) {
2019-01-13 23:20:32 +02:00
run_type.execute(&rustup).args(&["self", "update"]).check_run()?;
2018-08-19 14:45:23 +03:00
}
2019-01-13 23:20:32 +02:00
run_type.execute(&rustup).arg("update").check_run()
2018-08-19 14:45:23 +03:00
}
2020-08-28 16:16:23 +03:00
pub fn run_choosenim(ctx: &ExecutionContext) -> Result<()> {
let choosenim = utils::require("choosenim")?;
print_separator("choosenim");
let run_type = ctx.run_type();
run_type.execute(&choosenim).args(&["update", "self"]).check_run()?;
run_type.execute(&choosenim).args(&["update", "stable"]).check_run()
}
pub fn run_krew_upgrade(run_type: RunType) -> Result<()> {
let krew = utils::require("kubectl-krew")?;
print_separator("Krew");
run_type.execute(&krew).args(&["upgrade"]).check_run()
}
pub fn run_gcloud_components_update(run_type: RunType) -> Result<()> {
let gcloud = utils::require("gcloud")?;
print_separator("gcloud");
run_type
.execute(&gcloud)
.args(&["components", "update", "--quiet"])
.check_run()
}
pub fn run_jetpack(run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let jetpack = utils::require("jetpack")?;
2018-10-18 16:05:27 +03:00
2019-01-13 23:20:32 +02:00
print_separator("Jetpack");
2018-10-18 16:05:27 +03:00
2019-01-13 23:20:32 +02:00
run_type.execute(&jetpack).args(&["global", "update"]).check_run()
2018-10-18 16:05:27 +03:00
}
pub fn run_rtcl(ctx: &ExecutionContext) -> Result<()> {
let rupdate = utils::require("rupdate")?;
print_separator("rtcl");
ctx.run_type().execute(&rupdate).check_run()
}
pub fn run_opam_update(run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let opam = utils::require("opam")?;
2019-01-13 23:20:32 +02:00
print_separator("OCaml Package Manager");
2019-01-13 23:20:32 +02:00
run_type.execute(&opam).arg("update").check_run()?;
run_type.execute(&opam).arg("upgrade").check_run()
}
pub fn run_vcpkg_update(run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let vcpkg = utils::require("vcpkg")?;
print_separator("vcpkg");
2018-11-10 20:22:26 +02:00
2019-01-13 23:20:32 +02:00
run_type.execute(&vcpkg).args(&["upgrade", "--no-dry-run"]).check_run()
2018-11-10 20:22:26 +02:00
}
pub fn run_pipx_update(run_type: RunType) -> Result<()> {
2019-01-13 23:20:32 +02:00
let pipx = utils::require("pipx")?;
print_separator("pipx");
2018-10-31 13:01:57 +02:00
2019-01-13 23:20:32 +02:00
run_type.execute(&pipx).arg("upgrade-all").check_run()
2018-10-31 13:01:57 +02:00
}
pub fn run_stack_update(run_type: RunType) -> Result<()> {
2019-10-07 19:13:29 +02:00
let stack = utils::require("stack")?;
print_separator("stack");
run_type.execute(&stack).arg("upgrade").check_run()
}
2020-06-03 22:12:27 +03:00
pub fn run_tlmgr_update(ctx: &ExecutionContext) -> Result<()> {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
if !ctx.config().enable_tlmgr_linux() {
2020-08-21 23:04:36 +03:00
return Err(SkipStep(String::from("tlmgr must be explicity enabled in the configuration to run in Linux")).into());
2020-06-03 22:12:27 +03:00
}
}
}
2019-12-12 20:24:22 +02:00
let tlmgr = utils::require("tlmgr")?;
let kpsewhich = utils::require("kpsewhich")?;
let tlmgr_directory = {
let mut d = PathBuf::from(
std::str::from_utf8(
&Command::new(&kpsewhich)
.arg("-var-value=SELFAUTOPARENT")
.output()?
.stdout,
)?
.trim(),
);
d.push("tlpkg");
d
}
.require()?;
let directory_writable = tempfile_in(&tlmgr_directory).is_ok();
debug!("{:?} writable: {}", tlmgr_directory, directory_writable);
print_separator("TeX Live package manager");
let mut command = if directory_writable {
2020-06-03 22:12:27 +03:00
ctx.run_type().execute(&tlmgr)
2019-12-12 20:24:22 +02:00
} else {
2020-06-03 22:12:27 +03:00
let mut c = ctx
.run_type()
.execute(ctx.sudo().as_ref().ok_or(TopgradeError::SudoRequired)?);
2019-12-12 20:24:22 +02:00
c.arg(&tlmgr);
c
};
command.args(&["update", "--self", "--all"]);
command.check_run()
}
pub fn run_myrepos_update(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
let myrepos = utils::require("mr")?;
base_dirs.home_dir().join(".mrconfig").require()?;
print_separator("myrepos");
run_type
.execute(&myrepos)
.arg("--directory")
.arg(base_dirs.home_dir())
.arg("checkout")
.check_run()?;
run_type
.execute(&myrepos)
.arg("--directory")
.arg(base_dirs.home_dir())
.arg("update")
.check_run()
}
2020-02-08 22:13:56 +02:00
pub fn run_custom_command(name: &str, command: &str, ctx: &ExecutionContext) -> Result<()> {
2018-12-05 11:34:08 +02:00
print_separator(name);
2020-02-08 22:13:56 +02:00
ctx.run_type().execute(shell()).arg("-c").arg(command).check_run()
2018-08-19 14:45:23 +03:00
}
2018-10-02 13:45:29 +03:00
pub fn run_composer_update(ctx: &ExecutionContext) -> Result<()> {
2019-01-13 23:20:32 +02:00
let composer = utils::require("composer")?;
let composer_home = Command::new(&composer)
2019-07-01 08:56:18 +03:00
.args(&["global", "config", "--absolute", "--quiet", "home"])
2019-01-13 23:20:32 +02:00
.check_output()
2020-08-21 23:04:36 +03:00
.map_err(|e| (SkipStep(format!("Error getting the composer directory: {}", e))))
.map(|s| PathBuf::from(s.trim()))?
.require()?;
2019-01-13 23:20:32 +02:00
if !composer_home.is_descendant_of(ctx.base_dirs().home_dir()) {
2020-08-21 23:04:36 +03:00
return Err(SkipStep(format!(
"Composer directory {} isn't a decandent of the user's home directory",
composer_home.display()
))
.into());
2019-01-13 23:20:32 +02:00
}
2019-01-13 23:20:32 +02:00
print_separator("Composer");
if ctx.config().composer_self_update() {
cfg_if::cfg_if! {
if #[cfg(unix)] {
// If self-update fails without sudo then there's probably an update
let has_update = match ctx.run_type().execute(&composer).arg("self-update").output()? {
ExecutorOutput::Wet(output) => !output.status.success(),
_ => false
};
if has_update {
ctx.run_type()
.execute(ctx.sudo().as_ref().unwrap())
.arg(&composer)
.arg("self-update")
.check_run()?;
}
} else {
ctx.run_type().execute(&composer).arg("self-update").check_run()?;
}
}
}
2020-05-11 06:23:43 +03:00
let output = Command::new(&composer).args(&["global", "update"]).output()?;
let status = output.status;
if !status.success() {
return Err(TopgradeError::ProcessFailed(status).into());
}
let stdout = String::from_utf8(output.stdout)?;
let stderr = String::from_utf8(output.stderr)?;
print!("{}\n{}", stdout, stderr);
if stdout.contains("valet") || stderr.contains("valet") {
if let Some(valet) = utils::which("valet") {
ctx.run_type().execute(&valet).arg("install").check_run()?;
}
2018-10-02 13:45:29 +03:00
}
2019-01-13 23:20:32 +02:00
Ok(())
2018-10-02 13:45:29 +03:00
}
2020-12-26 06:43:45 +02:00
pub fn run_dotnet_upgrade(ctx: &ExecutionContext) -> Result<()> {
let dotnet = utils::require("dotnet")?;
let output = Command::new(dotnet)
.args(&["tool", "list", "--global"])
.check_output()?;
let mut packages = output.split('\n').skip(2).filter(|line| !line.is_empty()).peekable();
if packages.peek().is_none() {
return Err(SkipStep(String::from("No dotnet global tools installed")).into());
}
2020-12-26 09:21:14 +02:00
print_separator(".NET");
2020-12-26 06:43:45 +02:00
for package in packages {
let package_name = package.split_whitespace().next().unwrap();
ctx.run_type()
.execute("dotnet")
.args(&["tool", "update", package_name, "--global"])
.check_run()?;
}
Ok(())
}