From dcf58911bdba80db1cc47fe2891d1b82c1e965f9 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Fri, 13 May 2022 20:59:13 +0300 Subject: [PATCH] Mac OS fails to upgrade Homebrew in custom location (#930) Fixes #673 --- .vscode/topgrade.code-snippets | 9 ++++++++- src/main.rs | 8 +++++++- src/steps/os/unix.rs | 34 +++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/.vscode/topgrade.code-snippets b/.vscode/topgrade.code-snippets index 002d26f2..40af7213 100644 --- a/.vscode/topgrade.code-snippets +++ b/.vscode/topgrade.code-snippets @@ -19,7 +19,7 @@ "scope": "rust", "prefix": "skipstep", "body": [ - "return Err(SkipStep(format!(\"$1\").into()));" + "return Err(SkipStep(format!(\"$1\")).into());" ] }, "Step": { @@ -31,5 +31,12 @@ " Ok(())", "}" ] + }, + "macos": { + "scope": "rust", + "prefix": "macos", + "body": [ + "#[cfg(target_os = \"macos\")]" + ] } } diff --git a/src/main.rs b/src/main.rs index c06d4a94..310a7d2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -153,7 +153,7 @@ fn run() -> Result<()> { runner.execute(Step::ConfigUpdate, "config-update", || linux::run_config_update(&ctx))?; runner.execute(Step::BrewFormula, "Brew", || { - unix::run_brew_formula(&ctx, unix::BrewVariant::Linux) + unix::run_brew_formula(&ctx, unix::BrewVariant::Path) })?; } @@ -172,12 +172,18 @@ fn run() -> Result<()> { runner.execute(Step::BrewFormula, "Brew (Intel)", || { unix::run_brew_formula(&ctx, unix::BrewVariant::MacIntel) })?; + runner.execute(Step::BrewFormula, "Brew", || { + unix::run_brew_formula(&ctx, unix::BrewVariant::Path) + })?; runner.execute(Step::BrewCask, "Brew Cask (ARM)", || { unix::run_brew_cask(&ctx, unix::BrewVariant::MacArm) })?; runner.execute(Step::BrewCask, "Brew Cask (Intel)", || { unix::run_brew_cask(&ctx, unix::BrewVariant::MacIntel) })?; + runner.execute(Step::BrewCask, "Brew Cask", || { + unix::run_brew_cask(&ctx, unix::BrewVariant::Path) + })?; runner.execute(Step::Macports, "MacPorts", || macos::run_macports(&ctx))?; } diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index 32e546be..e1bb5fcf 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -1,6 +1,4 @@ -#[cfg(target_os = "linux")] -use crate::error::SkipStep; -use crate::error::TopgradeError; +use crate::error::{SkipStep, TopgradeError}; use crate::execution_context::ExecutionContext; use crate::executor::{CommandExt, Executor, ExecutorExitStatus, RunType}; use crate::terminal::print_separator; @@ -23,7 +21,7 @@ const ARM_BREW: &str = "/opt/homebrew/bin/brew"; #[derive(Copy, Clone, Debug)] #[allow(dead_code)] pub enum BrewVariant { - Linux, + Path, MacIntel, MacArm, } @@ -31,12 +29,17 @@ pub enum BrewVariant { impl BrewVariant { fn binary_name(self) -> &'static str { match self { - BrewVariant::Linux => "brew", + BrewVariant::Path => "brew", BrewVariant::MacIntel => INTEL_BREW, BrewVariant::MacArm => ARM_BREW, } } + #[cfg(target_os = "macos")] + fn is_path(&self) -> bool { + matches!(self, BrewVariant::Path) + } + fn both_both_exist() -> bool { Path::new(INTEL_BREW).exists() && Path::new(ARM_BREW).exists() } @@ -65,6 +68,11 @@ impl BrewVariant { _ => run_type.execute(self.binary_name()), } } + + #[cfg(target_os = "macos")] + fn is_macos_custom(binary_name: PathBuf) -> bool { + !(binary_name.as_os_str() == INTEL_BREW || binary_name.as_os_str() == ARM_BREW) + } } pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> { @@ -178,7 +186,16 @@ pub fn upgrade_gnome_extensions(ctx: &ExecutionContext) -> Result<()> { } pub fn run_brew_formula(ctx: &ExecutionContext, variant: BrewVariant) -> Result<()> { - require(variant.binary_name())?; + #[allow(unused_variables)] + let binary_name = require(variant.binary_name())?; + + #[cfg(target_os = "macos")] + { + if variant.is_path() && !BrewVariant::is_macos_custom(binary_name) { + return Err(SkipStep("Not a custom brew for macOS".to_string()).into()); + } + } + print_separator(variant.step_title()); let run_type = ctx.run_type(); @@ -197,7 +214,10 @@ pub fn run_brew_formula(ctx: &ExecutionContext, variant: BrewVariant) -> Result< #[cfg(target_os = "macos")] pub fn run_brew_cask(ctx: &ExecutionContext, variant: BrewVariant) -> Result<()> { - require(variant.binary_name())?; + let binary_name = require(variant.binary_name())?; + if variant.is_path() && !BrewVariant::is_macos_custom(binary_name) { + return Err(SkipStep("Not a custom brew for macOS".to_string()).into()); + } print_separator(format!("{} - Cask", variant.step_title())); let run_type = ctx.run_type();