'homebrew locate installed files path

I have a homebrew package that I was able to push through GitHub: diagnosticator

This is a simple ruby file (diagnosticator.rb) that points to the actual hosting repository: diagnosticator-mac and provides instructions on how to install files:

class Diagnosticator < Formula
  desc "Diagnosticator Mac OS homebrew package"
  homepage "https://diagnosticator.com"
  url "https://github.com/cccnrc/diagnosticator-mac/archive/refs/tags/v0.1.11.tar.gz"
  sha256 "f36987ce96c7be269da12b9dce8186c5245aef4046fe62173a145024b5e88b98"
  license "MIT"

  depends_on "docker"
  depends_on "docker-compose"
  depends_on "wget"
  depends_on "jq"

  def install
    bin.install "diagnosticator"
    bin.install "diagnosticator-mac.sh"
    bin.install Dir["files"]
    prefix.install "README.md"
  end
end

From the diagnosticator executable I have to refer to diagnosticator-mac.sh executable, I am now referring it as:

MAC_EXE=/usr/local/bin/diagnosticator-mac.sh

but I guess I can simply change it to:

MAC_EXE=diagnosticator-mac.sh

as it will be found on the $PATH after installation.

From diagnosticator-mac.sh I have to refer to files in the files folder that are installed through bin.install Dir["files"], I have now:

DIAGNOSTICATOR_FILES_DIR=/usr/local/opt/diagnosticator/bin/files

but I noticed that in a couple of different Mac machines they ended up in different locations based on how users installed homebrew.

How can I find a way to point the diagnosticator-mac.sh executable to that files folder however homebrew was installed?


If you want to try it:

brew install cccnrc/diagnosticator/diagnosticator


Solution 1:[1]

Homebrew determines the path for you, and stores it in the variable HOMEBREW_PREFIX

You could use inreplace to swap your hardcoded prefix with the homebrew prefix.

# Iterate through the files, replacing the prefix
%w[diagnosticator diagnosticator-mac.sh].each do |file|
  inreplace file, "/usr/local", HOMEBREW_PREFIX
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Haren S