Skip to content

Commit

Permalink
Add postinstall support for brew and cask
Browse files Browse the repository at this point in the history
This allows `postinstall` to be used to run a command after a formula
or cask is installed.

While we're here, also adjust the `restart_service` behaviour to
correctly match the comment i.e. require `always` to restart every
time and otherwise just restart on an install or upgrade of a formula.
  • Loading branch information
MikeMcQuaid committed Jan 8, 2025
1 parent 6c7f828 commit 5559271
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ cask_args appdir: "~/Applications", require_sha: true

# 'brew install'
brew "imagemagick"
# 'brew install --with-rmtp', 'brew link --overwrite', 'brew services restart' on version changes
brew "denji/nginx/nginx-full", link: :overwrite, args: ["with-rmtp"], restart_service: :changed
# 'brew install --with-rmtp', 'brew link --overwrite', 'brew services restart' even if no install/upgrade
brew "denji/nginx/nginx-full", link: :overwrite, args: ["with-rmtp"], restart_service: :always
# 'brew install', always 'brew services restart', 'brew link', 'brew unlink mysql' (if it is installed)
brew "[email protected]", restart_service: true, link: true, conflicts_with: ["mysql"]
# 'brew install' and run a command if installer or upgraded.
brew "postgresql@16", postinstall: "${HOMEBREW_PREFIX}/opt/postgresql@16/bin/postgres -D ${HOMEBREW_PREFIX}/var/postgresql@16"

Check failure on line 45 in README.md

View workflow job for this annotation

GitHub Actions / tests (macOS-latest)

Line is too long. [126/118]
# install only on specified OS
brew "gnupg" if OS.mac?
brew "glibc" if OS.linux?
Expand All @@ -55,6 +57,8 @@ cask "firefox", args: { no_quarantine: true }
cask "opera", greedy: true
# 'brew install --cask' only if '/usr/libexec/java_home --failfast' fails
cask "java" unless system "/usr/libexec/java_home", "--failfast"
# 'brew install --cask' and run a command if installer or upgraded.
cask "google-cloud-sdk", postinstall: "${HOMEBREW_PREFIX}/bin/gcloud components update"

# 'mas install'
mas "1Password", id: 443_987_910
Expand Down
15 changes: 14 additions & 1 deletion lib/bundle/brew_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize(name, options = {})
@restart_service = options[:restart_service]
@start_service = options.fetch(:start_service, @restart_service)
@link = options.fetch(:link, nil)
@postinstall = options.fetch(:postinstall, nil)
@changed = nil
end

Expand All @@ -50,8 +51,12 @@ def install(preinstall: true, no_upgrade: false, verbose: false, force: false)
service_result = service_change_state!(verbose:)
result &&= service_result
end

link_result = link_change_state!(verbose:)
result &&= link_result

postinstall_result = postinstall_change_state!(verbose:)
result &&= postinstall_result
end

result
Expand Down Expand Up @@ -83,7 +88,7 @@ def restart_service_needed?
return false unless restart_service?

# Restart if `restart_service: :always`, or if the formula was installed or upgraded
@restart_service.to_s != "changed" || changed?
@restart_service.to_s == "always" || changed?
end

def changed?
Expand Down Expand Up @@ -132,6 +137,14 @@ def link_change_state!(verbose: false)
true
end

def postinstall_change_state!(verbose:)
return true unless @postinstall.present?
return true unless changed?

puts "Running postinstall for #{@name}." if verbose
Bundle.system(@postinstall, verbose:)
end

def self.formula_installed_and_up_to_date?(formula, no_upgrade: false)
return false unless formula_installed?(formula)
return true if no_upgrade
Expand Down
2 changes: 2 additions & 0 deletions lib/bundle/cask_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def install(name, preinstall: true, no_upgrade: false, verbose: false, force: fa

return false unless Bundle.brew("install", "--cask", full_name, *args, verbose:)

# TODO: run postinstall if installed or upgraded

installed_casks << name
true
end
Expand Down

0 comments on commit 5559271

Please sign in to comment.