Create a symlink pointing to a variable filepath

command linehomebrewsymlink

I am using MacVim installed via homebrew. The absolute path to the app is /usr/local/Cellar/macvim/xxxxxx/MacVim.app, with xxxxxx the version number. MacVim version number is updated often.

Now I like my app to be accessible through the Dock and the Launchpad. I have created a symlink in /Applications pointing to /usr/local/Cellar/macvim/8.2-163_2/MacVim.app (for now) so MacVim.app is visible in the Launchpad, and I dragged the symlink in the Dock to access it quickly. Of course, next time MacVim is updated, the version will change and I will have to fix the symlink.

What can I do to have a more permanent solution that does not require fixing the symlink every time MacVim is updated?

The solution I thought could work:

  1. Use a crontab to regularly update the symlink
  2. Or use launchctl to do the same
  3. Configure homebrew, if possible, to use the same directory always
  4. Configure homebrew to hook some script after updating MacVim to update symlink
  5. Create a node with custom driver that open(), read() and close() the right file

I implemented solution #1 and #2 in the past, but I find it quite messy. Every minute, the command to update the symlink is run, but 99.99% of the time it does nothing Looks more like a hacky solution to me. If possible, I would prefer to run the command when it is needed only. Solution #3 and #4 are much better, but I haven't found anything about hook in homebrew. And solution #5 seems a bit hardcore for such a trivial task.

Or am I missing something obvious?

A bit more explanations as requested in comment

Solution 1

A one line command in the crontab can be used:

* * * * * ln -fs "$(find '/usr/local/Cellar/macvim' -name 'MacVim.app' | head -n 1)" /Applications/MacVim.app

Solution 2

Same as crontab, but using a scheduled task. I don't find this solution particularly better than the crontab except it is using a more "modern" scheduler.

Solution 3 and 4

That's a guess, but I was hoping there were some feature in homebrew to run a command after a package is being updated. Then I would simply run the same command as in the crontab when MacVim is updated. What I like with this solution, is that the command is only run when it is needed, not every minute.

Solution 5

I haven't looked in depth in this solution as it is definitely more advanced. I only know it is possible to create a custom character device with a custom driver. The only requirement is to implement the open, read and close command. So technically, everytime the system asks for /Applications/MacVim.app, what it really does being the scene is to list the content of the folder /usr/local/Cellar/macvim, take the first folder in the list, and read the content of the MacVim.app file in the said folder. Similar to a symlink, but specific to my need.

Best Answer

Just symlink to /usr/local/opt/macvim/MacVim.app instead, this path remains stable (/usr/local/opt/macvim is a symlink maintained by HomeBrew which points to the currently installed version within /usr/local/Cellar/macvim).