2323__homebrew_funcs () {
2424 # Install a specific version of a Homebrew formula
2525 # @param {string} $1 Formula name
26- # @param {string} $2 Formula version
26+ # @param {string} $2 Formula version (exact)
2727 vintage () {
28- # Ensure homebrew/core is tapped and up to date
29- brew tap | grep -xq homebrew/core \
30- && brew update \
31- || brew tap --force homebrew/core
28+ # Figure out the relevant tap
29+ local brew_tap
30+ local is_cask=false
31+ if brew search --cask " /^${1:? } $/" & > /dev/null; then
32+ brew_tap=" homebrew/cask"
33+ is_cask=true
34+ else
35+ brew_tap=" homebrew/core"
36+ fi
37+
38+ # Ensure the appropriate tap is tapped and up to date
39+ if brew tap | grep -xq " ${brew_tap} " ; then
40+ brew update
41+ else
42+ brew tap --force " ${brew_tap} "
43+ fi
3244
3345 # Ensure homebrew/local is created
3446 brew tap | grep -xq homebrew/local \
3547 || brew tap homebrew/local
3648
37- # Extract the formula
38- brew extract --force " --version=${2:? } " " ${1:? } " homebrew/local
39-
40- # If the formula is already installed, re-link it
41- if brew list -1 | grep -xq " ${1:? } @${2:? } " ; then
42- brew unlink " ${1:? } @${2:? } "
43- brew link --overwrite " ${1:? } @${2:? } "
44- return 0
45- fi
46-
47- # Install the formula and ensure it's linked
48- brew install " homebrew/local/${1:? } @${2:? } " \
49- || brew link --overwrite " ${1:? } @${2:? } "
49+ if [ " ${is_cask} " = false ]; then
50+ # If the formula is already installed, re-link it
51+ if brew list -1 | grep -xq " ${1:? } @${2:? } " ; then
52+ brew unlink " ${1:? } @${2:? } "
53+ brew link --overwrite " ${1:? } @${2:? } "
54+ return 0
55+ fi
56+
57+ # Install the formula and ensure it's linked
58+ brew install " homebrew/local/${1:? } @${2:? } " \
59+ || brew link --overwrite " ${1:? } @${2:? } "
60+ else (
61+ # Sub shell to make `cd` safe
62+ cd " $( brew --repository " ${brew_tap} " ) " || return 1
63+
64+ # Emulate `brew extract` for casks
65+ local cask_path
66+ cask_path=$( git ls-files ' Casks/*' | grep -E " /${1:? } \.rb$" )
67+ local version_match
68+ version_match=$( git rev-list --all " ${cask_path} " \
69+ | xargs -n1 -I% git --no-pager grep --fixed-strings " version \" ${2:? } \" " % -- " ${cask_path} " \
70+ 2> /dev/null | head -1)
71+ local commit_hash=" ${version_match%%:* } "
72+ local local_cask_dir
73+ local_cask_dir=" $( brew --repository homebrew/local) /Casks"
74+ if [ ! -d " ${local_cask_dir} " ]; then
75+ mkdir -p " ${local_cask_dir} "
76+ fi
77+ local local_cask_file=" ${local_cask_dir} /${1:? } @${2:? } .rb"
78+ git show " ${commit_hash} :${cask_path} " \
79+ | sed " s/cask \" ${1:? } \" /cask \" ${1:? } @${2:? } \" /" \
80+ > " ${local_cask_file} "
81+
82+ # Install the formula
83+ brew install --cask " homebrew/local/${1:? } @${2:? } "
84+ ) fi
5085 }
5186}
5287__homebrew_funcs
@@ -56,9 +91,29 @@ __homebrew_funcs
5691
5792# Homebrew packages
5893if command -v brew & > /dev/null; then
59- # Apps
60- [[ -d /Applications/LinearMouse.app ]] || brew install --cask linearmouse
61- [[ -d /Applications/Rectangle.app ]] || brew install --cask rectangle
94+ # Apps (only install if shell is interactive, in case of admin password prompt)
95+ if [[ $- == * i* ]]; then
96+ for cask in $(
97+ echo " 1password"
98+ # echo "discord"
99+ echo " firefox"
100+ echo " github"
101+ # echo "inkscape"
102+ echo " libreoffice"
103+ echo " linearmouse"
104+ echo " rectangle"
105+ # echo "signal"
106+ echo " spotify"
107+ # echo "steam"
108+ echo " visual-studio-code"
109+ echo " wine-stable"
110+ ) ; do
111+ app_artifact=$( brew info --json=v2 --cask " ${cask} " \
112+ | jq --raw-output " .casks[] | select(.token==\" ${cask} \" ) | .artifacts[] | .app // empty | .[]" \
113+ | head -1)
114+ [[ -d " /Applications/${app_artifact} " ]] || echo brew install --cask " ${cask} "
115+ done
116+ fi
62117
63118 # CLI tools
64119 command -v dive > /dev/null || brew install dive
@@ -70,7 +125,6 @@ if command -v brew &> /dev/null; then
70125 command -v rename > /dev/null || brew install rename
71126 command -v tree > /dev/null || brew install tree
72127 command -v watch > /dev/null || brew install watch
73- command -v wine > /dev/null || brew install --cask --no-quarantine wine-stable
74128 command -v wget > /dev/null || brew install wget
75129
76130 if ! command -v hstr & > /dev/null; then
@@ -110,7 +164,7 @@ if command -v mas &> /dev/null; then
110164 # Grammarly for Safari
111165 echo " 1462114288"
112166 ) ; do
113- echo " ${mas_list} " | grep " ^${app_id} " & > /dev/null || mas install " ${app_id} "
167+ echo " ${mas_list} " | grep -Eq " ^${app_id} " || mas install " ${app_id} "
114168 done
115169fi
116170
0 commit comments