Create an alias in a homebrew formula

Viewed 1802

I am creating some formulae for homebrew in a tap room (https://github.com/IvoNet/homebrew-cli). I have one massively usefull script IMHO :-) that can scan all my git projects and make navigating between them very easy. But in order to use it it needs to be sourceed into the current shell otherwize the cd command will not stick.

Is it possible to create aliasses in a brew formula?

e.g.

alias gcd="source /usr/local/bin/gcd"

I'm very new to creating formulae for brew and I have not found a way to do this. This alias needs to be set for every terminal session so should land in something like .zshrc / .profile or somesuch?!

Is this possible?

1 Answers

I would strongly advise against modifying users’ ~/.zshrc/~/.profile. Technically, nothing prevents you from doing so, but you may mess up with these files, and there’s no way to undo your edits if one removes the formula.

Outside of this, there’s no way to create aliases in formulae that are valid in the current shell. One thing you can do, though, is to advise users to do so:

def caveats
  <<~EOS
    Add the following in your ~/.zshrc or ~/.profile:

      alias gcd="source #{opt_bin}/gcd"
  EOS
end

This text is printed after the installation and everytime one runs brew info <your_formula>. opt_bin is a variable that contains a path to your formula’s bin directory.

See for example the following formulae: kube-ps1.rb, haxe.rb, thefuck.rb.

Related