Skip to content

Commit

Permalink
rewrite brew commands to support linux
Browse files Browse the repository at this point in the history
and to better error on environments where homebrew is not supported (such as windows without WSL), or not installed (such as fresh macos, and typical linux)

- brew, dorothy, is-brew:
    - update check for linux compatible, without a false positive at `/bin/brew`
- brew:
    - support help text and install warning with or without brew
    - support running without HOMEBREW_ARCH which is macos only
- brew, setup-environment-commands:
    - move HOMEBREW_NO_AUTO_UPDATE (macos) and HOMEBREW_NO_ENV_HINTS (macos, linux) from `brew` to `setup-environment-commands` as that is the correct location
- setup-mac-brew, setup-shell, setup-util-brew, setup-util-ruby:
    - adjust for env var expectations

/ref https://github.com/bevry/dorothy/actions/runs/7499890232/job/20417515851#step:3:13
/ref 36790f9
  • Loading branch information
balupton committed Jan 12, 2024
1 parent 36790f9 commit 144892e
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 65 deletions.
2 changes: 1 addition & 1 deletion commands.beta/icloud-helper
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function icloud_helper() (
source "$DOROTHY/sources/bash.bash"

# check for compatibility
# assert compatibility
if ! is-mac; then
echo-style --notice="[$0] is only intended to be run on macOS systems, skipping." >/dev/stderr
return 0
Expand Down
5 changes: 5 additions & 0 deletions commands.beta/macos-settings
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ function macos_settings() (
esac
done

# assert compatibility
if ! is-mac; then
help 'This command is only purposeful on macOS, which you are not running.'
fi

# =====================================
# Action

Expand Down
7 changes: 6 additions & 1 deletion commands.beta/macos-state
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ function macos_state() (
esac
done

# assert compatibility
if ! is-mac; then
help 'This command is only purposeful on macOS, which you are not running.'
fi

# ensure
action="$(
choose-option \
Expand Down Expand Up @@ -389,7 +394,7 @@ function macos_state() (

process "DevDocs" "Library/Application Support/DevDocs/"
process "DiskMap" "Library/Preferences/com.fiplab.diskmap.plist"
process "DNSCrypt" "${HOMEBREW_PREFIX}/etc/dnscrypt-proxy.toml"
process "DNSCrypt" "$HOMEBREW_PREFIX/etc/dnscrypt-proxy.toml"

process "Donut" \
"Library/Application Support/Donut/" \
Expand Down
5 changes: 5 additions & 0 deletions commands.beta/macos-theme
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function macos_theme() (
esac
done

# assert compatibility
if ! is-mac; then
help 'This command is only purposeful on macOS, which you are not running.'
fi

# check
if test -z "$WALLPAPER_DIRECTORY_LIGHT" -o ! -d "$WALLPAPER_DIRECTORY_LIGHT"; then
help "Missing directory [WALLPAPER_DIRECTORY_LIGHT] = [$WALLPAPER_DIRECTORY_LIGHT]"
Expand Down
58 changes: 38 additions & 20 deletions commands/brew
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/usr/bin/env bash
# we can assume [HOMEBREW_ARCH, HOMEBREW_PREFIX] have already been provided on brew supported systems

# This is an internal command, no need for help handling.

# Without this command, using HOMEBREW_ARCH=x86_64 on Apple Silicon will fail with:
# ```
Expand All @@ -18,34 +15,55 @@ function brew_() (
source "$DOROTHY/sources/bash.bash"

# get the homebrew binary
local bin="${HOMEBREW_PREFIX-}/bin/brew"
if test ! -x "$bin"; then
echo-style --error='Homebrew is not installed.' ' ' --notice='Install it with:' ' ' --code='setup-mac-brew' >/dev/stderr
# ^ don't use get-installer, as bash v5 may not exist yet
return 74 # EPROGUNAVAIL 74 RPC prog. not avail
local bin=''
if test -n "${HOMEBREW_PREFIX-}"; then
bin="$HOMEBREW_PREFIX/bin/brew"
if test ! -x "$bin"; then
bin=''
fi
fi

# help
function help {
cat <<-EOF >/dev/stderr
$($bin --help || :)
if test -n "$bin"; then
"$bin" --help || :
print_line
fi
cat <<-EOF
Dorothy extends the brew command to:
- enforce the correct prefix on macOS and Linux via [HOMEBREW_PREFIX = ${HOMEBREW_PREFIX-}]
- this ensures brew invokes homebrew, even when PATH is incorrect and when default brew is not homebrew
- enforce the appropriate architecture on macOS via [HOMEBREW_ARCH = ${HOMEBREW_ARCH-}]
Dorothy extends the [brew] command to:
- enforce the correct prefix via [HOMEBREW_PREFIX = $HOMEBREW_PREFIX]
- enforce the appropriate architecture via [HOMEBREW_ARCH = $HOMEBREW_ARCH]
- disable auto updates, as Dorothy handles that better via [setup-mac-brew]
Dorothy shell environments are configured to:
- ensure all homebrew environment variables are configured correctly
- disable auto updates on macOS, as Dorothy handles that better via [setup-mac-brew]
- disable brew environment hints, as Dorothy handles that better via [setup-environment-commands]
EOF
if test -z "$bin"; then
print_line
echo-style --error='Homebrew is not installed.'
if is-mac; then
echo-style --error='Homebrew is not installed.' ' ' --notice='Install it with:' ' ' --code='setup-mac-brew'
else
echo-style --error='Homebrew is not installed.'
fi
# ^ don't use get-installer, as bash v5 may not exist yet
return 74 # EPROGUNAVAIL 74 RPC prog. not avail
fi
return 22 # EINVAL 22 Invalid argument
}
if test "$*" = '--help'; then
help
if test -z "$bin" -o "$*" = '--help'; then
help >/dev/stderr
fi

# disable brew auto updates, as Dorothy handles that better
# disable brew hints, as Dorothy handles that better
env HOMEBREW_NO_AUTO_UPDATE=1 HOMEBREW_NO_ENV_HINTS=1 \
arch "-$HOMEBREW_ARCH" "$bin" "$@"
# run brew
if test -n "${HOMEBREW_ARCH-}"; then
arch -"$HOMEBREW_ARCH" \
"$bin" "$@"
else
"$bin" "$@"
fi
)

# fire if invoked standalone
Expand Down
4 changes: 2 additions & 2 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ function dorothy() (
elif command_exists dnf; then
# for fedora
try_sudo dnf --assumeyes --refresh --best --allowerasing install bash curl git ncurses procps coreutils moreutils gawk
elif test -x "${HOMEBREW_PREFIX-}/bin/brew"; then
elif test -n "${HOMEBREW_PREFIX-}" -a -x "${HOMEBREW_PREFIX-}/bin/brew"; then
# for macos, don't use command_exists as dorothy has its own brew helper
# this is here for consistency only, as it is unnecessary, all the commands already exist on macos without any need for homebrew
"${HOMEBREW_PREFIX-}/bin/brew" install bash curl git ncurses coreutils moreutils gawk
"$HOMEBREW_PREFIX/bin/brew" install bash curl git ncurses coreutils moreutils gawk
elif command_exists xbps-install; then
# for void
try_sudo xbps-install --sync --yes bash curl git ncurses coreutils moreutils gawk
Expand Down
2 changes: 1 addition & 1 deletion commands/grub-helper
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function grub_helper() (
source "$DOROTHY/sources/bash.bash"

# check for compatibility
# assert compatibility
if ! is-linux; then
echo-style --notice="[$0] is only intended to be run on Linux systems, skipping." >/dev/stderr
return 0
Expand Down
2 changes: 1 addition & 1 deletion commands/is-brew
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function is_brew() (
source "$DOROTHY/sources/bash.bash"

# workaround for our [brew] helper
if is-mac && test -x "${HOMEBREW_PREFIX-}/bin/brew"; then
if test -n "${HOMEBREW_PREFIX-}" -a -x "${HOMEBREW_PREFIX-}/bin/brew"; then
return 0
else
return 1
Expand Down
11 changes: 6 additions & 5 deletions commands/setup-environment-commands
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ fi

# Homebrew
if is-mac; then
export HOMEBREW_ARCH HOMEBREW_PREFIX HOMEBREW_CELLAR HOMEBREW_REPOSITORY HOMEBREW_SHELLENV_PREFIX
# macos
export HOMEBREW_ARCH HOMEBREW_PREFIX HOMEBREW_CELLAR HOMEBREW_REPOSITORY HOMEBREW_SHELLENV_PREFIX HOMEBREW_NO_AUTO_UPDATE=1 HOMEBREW_NO_ENV_HINTS=1
if test -z "${HOMEBREW_ARCH-}"; then
if test "$(uname -p)" = 'arm' -o "$(uname -m)" = 'arm64'; then
HOMEBREW_ARCH='arm64e'
Expand Down Expand Up @@ -294,13 +295,13 @@ if is-mac; then
if test -z "${HOMEBREW_SHELLENV_PREFIX-}"; then
HOMEBREW_SHELLENV_PREFIX="$HOMEBREW_PREFIX"
fi
elif is-linux; then
# https://docs.brew.sh/Homebrew-on-Linux
export HOMEBREW_PREFIX="${HOMEBREW_PREFIX:-"$HOME/.linuxbrew"}" HOMEBREW_NO_ENV_HINTS=1
fi

# if homebrew is installed, then add its paths
if test -x "${HOMEBREW_PREFIX-}/bin/brew"; then
# linux
export HOMEBREW_PREFIX="${HOMEBREW_PREFIX:-"/usr/local"}"

if test -n "${HOMEBREW_PREFIX-}" -a -x "${HOMEBREW_PREFIX-}/bin/brew"; then
# /usr/local already added to PATH, so only add these if different
if test "$HOMEBREW_PREFIX" != '/usr/local'; then
if test -d "$HOMEBREW_PREFIX/bin"; then
Expand Down
4 changes: 2 additions & 2 deletions commands/setup-linux
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ function setup_linux() (
esac
done

# assert operating system
# assert compatibility
if ! is-linux; then
help "This command is only purposeful on Linux systems, which you are not running."
help 'This command is only purposeful on Linux systems, which you are not running.'
fi

# assert action
Expand Down
6 changes: 3 additions & 3 deletions commands/setup-linux-fonts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function setup_linux_fonts() (
source "$DOROTHY/sources/bash.bash"
source "$(type -P eval-helper)"

# check for compatibility
# assert compatibility
if ! is-linux; then
echo-style --notice="[$0] is only intended to be run on Linux systems, skipping." >/dev/stderr
return 0
Expand All @@ -14,7 +14,7 @@ function setup_linux_fonts() (
return 0
fi
if command-missing fc-cache; then
echo-style --notice="fc-cache is not installed, skipping font installation." >/dev/stderr
echo-style --notice='fc-cache is not installed, skipping font installation.' >/dev/stderr
return 45 # ENOTSUP 45 Operation not supported
fi

Expand All @@ -41,7 +41,7 @@ function setup_linux_fonts() (
return 1
}
function get_monoid {
down https://cdn.rawgit.com/larsenwork/monoid/2db2d289f4e61010dd3f44e09918d9bb32fb96fd/Monoid.zip \
down 'https://cdn.rawgit.com/larsenwork/monoid/2db2d289f4e61010dd3f44e09918d9bb32fb96fd/Monoid.zip' \
--archive-filter='*.ttf' \
--directory="$fonts"
}
Expand Down
4 changes: 2 additions & 2 deletions commands/setup-linux-recovery
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
function setup_linux_recovery() (
source "$DOROTHY/sources/bash.bash"

# check for compatibility
# assert compatibility
if ! is-linux; then
echo-style --notice="[$0] is only intended to be run on Linux systems, skipping." >/dev/stderr
return 0
Expand All @@ -30,7 +30,7 @@ function setup_linux_recovery() (
--field='kernel.sysrq' --no-quote --value=1

if grub-helper; then
print_line "💁🏼‍♀️ If your Linux machine ever locks up, you can follow the instructions here to safely shutdown/reboot: 💁🏼‍♀️"
print_line '💁🏼‍♀️ If your Linux machine ever locks up, you can follow the instructions here to safely shutdown/reboot: 💁🏼‍♀️'
echo-style ' ' --code='https://forum.manjaro.org/t/3855?u=balupton'
else
echo-style --error='Failed to upgrade grub with the recovery options. Please send a PR to Dorothy to add support. Continuing...' >/dev/stderr
Expand Down
4 changes: 2 additions & 2 deletions commands/setup-mac
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ function setup_mac() (
esac
done

# assert operating system
# assert compatibility
if ! is-mac; then
help "This command is only purposeful on macOS, which you are not running."
help 'This command is only purposeful on macOS, which you are not running.'
fi

# assert action
Expand Down
18 changes: 10 additions & 8 deletions commands/setup-mac-brew
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function setup_mac_brew() (
source "$DOROTHY/sources/config.sh"

# environment provides:
# HOMEBREW_ARCH, HOMEBREW_PREFIX, HOMEBREW_CELLAR
# HOMEBREW_ARCH (macos), HOMEBREW_PREFIX (macos, linux), HOMEBREW_CELLAR (macos)

# setup.bash provides:
local HOMEBREW_UNTAPS+=(
Expand Down Expand Up @@ -191,9 +191,9 @@ function setup_mac_brew() (
esac
done

# assert operating system
# assert compatibility
if ! is-mac; then
help "This command is only purposeful on macOS, which you are not running."
help 'This command is only purposeful on macOS, which you are not running.'
fi

# ensure valid action
Expand All @@ -215,10 +215,12 @@ function setup_mac_brew() (
# fix common permission errors
# Error: Could not cleanup old kegs! Fix your permissions on:
# /usr/local/Cellar/go/1.17.2
if test -d "$HOMEBREW_CELLAR/go"; then
fs-own --permissions='u+rwx,g+rwx' \
-- "$HOMEBREW_CELLAR/go"
# bin, libexec, and many other directories contain executables, so just make it all executable
if test -n "${HOMEBREW_CELLAR-}"; then
if test -d "$HOMEBREW_CELLAR/go"; then
fs-own --permissions='u+rwx,g+rwx' \
-- "$HOMEBREW_CELLAR/go"
# bin, libexec, and many other directories contain executables, so just make it all executable
fi
fi
if test -d "$HOME/Library/Caches/go-build"; then
fs-own --sudo --permissions='u+rwx,g+rwx' \
Expand Down Expand Up @@ -356,7 +358,7 @@ function setup_mac_brew() (
# install packages with brew, that are slow to install
function brew_install_slow {
# if we are on an official directory, then we are using bottles which are pre-compiled and fast
if test "${HOMEBREW_PREFIX}" = "/usr/local" -o "${HOMEBREW_PREFIX}" = "/opt/homebrew"; then
if test "$HOMEBREW_PREFIX" = '/usr/local' -o "$HOMEBREW_PREFIX" = '/opt/homebrew'; then
echo-segment --h2="Install $# extra formulas"
brew_install --formula -- "$@"
echo-segment --g2="Install $# extra formulas"
Expand Down
8 changes: 5 additions & 3 deletions commands/setup-shell
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function setup_shell() (
OPTIONS:
--no-brew
Do not use any shell installed via brew.
Do not use any shell installed via homebrew.
QUIRKS:
You can use a command name, such as [bash] to select the preferred variation of the shell.
Expand Down Expand Up @@ -134,8 +134,10 @@ function setup_shell() (
if test -z "$path"; then
continue
fi
if test "$option_brew" = 'no' && [[ $path == "$HOMEBREW_PREFIX"* ]]; then
continue
if test -n "${HOMEBREW_PREFIX-}"; then
if test "$option_brew" = 'no' && [[ $path == "$HOMEBREW_PREFIX"* ]]; then
continue
fi
fi
echo-style --success='Preference available on this system:' ' ' --code="$path"
eval_capture --statusvar=use_shell_status -- use_shell "$path"
Expand Down
32 changes: 20 additions & 12 deletions commands/setup-util-brew
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ function setup_util_brew() (
fi

# environment provides:
# HOMEBREW_ARCH, HOMEBREW_PREFIX
if test -z "${HOMEBREW_ARCH-}" -o -z "${HOMEBREW_PREFIX-}"; then
echo-error 'HOMEBREW_ARCH and HOMEBREW_PREFIX are missing, cannot setup brew, for some reason this was executed before the Dorothy environment was configured'
uname -a
uname -s
env >&2
# HOMEBREW_ARCH (macos), HOMEBREW_PREFIX (macosl, linux)
if test -z "${HOMEBREW_PREFIX-}"; then
echo-error --bold='setup-util-brew: cannot setup brew because HOMEBREW_PREFIX is missing, this oculd be due to:' \
$'\n- homebrew is only supported on macOS and Windows' \
$'\n- this command was executed before Dorothy could configure the shell environment, this most likely occured because a Dorothy maintainer invoked a dorothy command within the dorothy instal'
return 6 # ENXIO 6 Device not configured
fi

Expand All @@ -24,7 +23,7 @@ function setup_util_brew() (
# uninstall brew, and adapt the system accordingly
function brew_uninstall {
local remove removals=(
"${HOMEBREW_PREFIX}/"*
"$HOMEBREW_PREFIX"/*
/usr/local/*
/opt/homebrew/*
)
Expand All @@ -41,7 +40,12 @@ function setup_util_brew() (

# uninstall brew
if is-brew; then
arch "-${HOMEBREW_ARCH}" /bin/bash -c "$(fetch https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)" -- --force
if test -n "${HOMEBREW_ARCH-}"; then
arch -"$HOMEBREW_ARCH" \
/bin/bash -c "$(fetch https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)" -- --force
else
/bin/bash -c "$(fetch https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)" -- --force
fi
fi

# remove any leftover directories
Expand All @@ -53,11 +57,15 @@ function setup_util_brew() (
}

# recomended installation method, it is pretty much the only one that works properly
# https://github.com/balupton/dotfiles/commit/fff6fbc079aaa6ab9bb8438e02c307ebad46fd75
# https://github.com/balupton/dotfiles/commit/69dbbe81bf30f9e0d9a1dd1d00eca3f3c88b943b
# https://github.com/bevry/dorothy/commit/fff6fbc079aaa6ab9bb8438e02c307ebad46fd75
# https://github.com/bevry/dorothy/commit/69dbbe81bf30f9e0d9a1dd1d00eca3f3c88b943b
function brew_install {
env HOMEBREW_NO_AUTO_UPDATE=1 \
arch "-${HOMEBREW_ARCH}" /bin/bash -c "$(fetch https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
if test -n "${HOMEBREW_ARCH-}"; then
arch "-${HOMEBREW_ARCH}" \
/bin/bash -c "$(fetch https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
/bin/bash -c "$(fetch https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
source "$DOROTHY/sources/environment.sh" # child processes will also inherit these changes
}

Expand Down
Loading

0 comments on commit 144892e

Please sign in to comment.