diff --git a/README.md b/README.md index 7ad789443..5ca4ae216 100644 --- a/README.md +++ b/README.md @@ -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 "mysql@5.6", 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" # install only on specified OS brew "gnupg" if OS.mac? brew "glibc" if OS.linux? @@ -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 diff --git a/lib/bundle/brew_installer.rb b/lib/bundle/brew_installer.rb index 02499ab8d..2f79061cd 100644 --- a/lib/bundle/brew_installer.rb +++ b/lib/bundle/brew_installer.rb @@ -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 @@ -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 @@ -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? @@ -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 diff --git a/lib/bundle/cask_installer.rb b/lib/bundle/cask_installer.rb index 2f0dd653d..8b6537f8b 100644 --- a/lib/bundle/cask_installer.rb +++ b/lib/bundle/cask_installer.rb @@ -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