Formula hooks for link/unlink?


(Mark Mindenhall) #1

I’m wondering if it’s possible to create hooks in a formula that will be called by brew link <FORMULA> and brew unlink <FORMULA>? The purpose here is to create/remove symlinks that are specific to folders (not just executables) within the specific formula being linked or unlinked.

Here’s a specific use case. Golang (go, go@1.8, go@1.7, etc.) requires a GOROOT environment variable that points to the current installation folder (libexec, in brew). Prior to the new style of versioning, I just set GOROOT=/usr/local/opt/go/libexec, and when I did brew switch go <VERSION>, my GOROOT was still correct, as the underlying symlink target had changed.

With the new versioning style, I switch versions using (for example) brew unlink go followed by brew link --force go@1.8. I want to be able to set GOROOT once (as before), and have this always be correct after unlinking one version and linking another.

I experimented with this in the go@1.8 formula locally. After adding the following line to the install method, I created a symlink at /usr/local/var/goroot that pointed to the go@1.8/libexec folder.

    var.install_symlink libexec => "goroot"

The problem is, the symlink created is permanent, whereas I want to be able to remove it on each brew unlink go@1.8 and create it on each brew link go@1.8. With this, I could set GOROOT=/usr/local/var/goroot, and the underlying target would be set by brew link.


(Mike McQuaid) #2

Sorry, we don’t provide such hooks. The best solution would be to write a wrapper script that calls your desired configuration yourself.


(Mark Mindenhall) #3

Is this a feature you would consider adding? I’d be happy to do the initial attempt and submit a PR (perhaps with a little initial guidance).


(Mike McQuaid) #4

Sorry, we would not. Customisation in this way unfortunately produces hard-to-debug errors for us. Instead let us know what ways we could make writing a wrapper script/tool easier.


(Mark Mindenhall) #5

Here’s something that would help. The current versioning system uses <name> for the current version, and <name>@<version> for previous versions. Would it be possible to add a flag to retrieve the prefix of the currently linked version of a package (if any)? For example:

# if latest go is linked:
$ brew --current-linked-prefix go
/usr/local/opt/go

# if go@1.8 is linked:
$ brew --current-linked-prefix go
/usr/local/opt/go@1.8/

Then, I could write a script that uses the output of that command to update the symlink:

#!/bin/bash
ln -sf $(brew --current-linked-prefix go)/libexec ~/goroot