Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Mac OS fails to upgrade Homebrew in custom location (#930)
Browse files Browse the repository at this point in the history
Fixes #673
  • Loading branch information
r-darwish authored May 13, 2022
1 parent af3f5dd commit dcf5891
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
9 changes: 8 additions & 1 deletion .vscode/topgrade.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scope": "rust",
"prefix": "skipstep",
"body": [
"return Err(SkipStep(format!(\"$1\").into()));"
"return Err(SkipStep(format!(\"$1\")).into());"
]
},
"Step": {
Expand All @@ -31,5 +31,12 @@
" Ok(())",
"}"
]
},
"macos": {
"scope": "rust",
"prefix": "macos",
"body": [
"#[cfg(target_os = \"macos\")]"
]
}
}
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})?;
}

Expand All @@ -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))?;
}

Expand Down
34 changes: 27 additions & 7 deletions src/steps/os/unix.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,20 +21,25 @@ const ARM_BREW: &str = "/opt/homebrew/bin/brew";
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub enum BrewVariant {
Linux,
Path,
MacIntel,
MacArm,
}

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()
}
Expand Down Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down

0 comments on commit dcf5891

Please sign in to comment.