Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

External Commands

adamv edited this page Feb 27, 2011 · 25 revisions

External Commands

Homebrew, like Git, supports external commands. This lets you create new commands that can be run like:

brew mycommand --option1 --option2 formula

without modifying Homebrew’s internals.

Command Types

External commands come in two flavors, Ruby commands and shell scripts.

In both cases, the command file should be +x (executable) and live somewhere in your $PATH.

Internally, Homebrew finds commands with /usr/bin/which.

Ruby Commands

An external command foo implemented as a Ruby command should be named brew-foo.rb. The command is executed by doing a require on the full pathname. As the command is require’d, it has full access to the Homebrew “environment”, ie, all global variables and modules that any internal command has access to.

The command may Kernel.exit with a status code if it needs to; if it doesn’t explicitly exit then Homebrew will return 0.

Shell Scripts

A shell script for an command named foo should be named brew-foo. This file will be run via exec with some Homebrew variables set as environmental variables, and passed any additional command-line arguments.

Variable Description
HOMEBREW_CACHE Where Homebrew caches downloaded tarballs to, typically ~/Library/Caches/Homebrew.
HOMEBREW_CELLAR The location of the Homebrew Cellar, where software is built to.
HOMEBREW_LIBRARY_PATH The folder containing Homebrew’s own application code.
HOMEBREW_PREFIX Where Homebrew installs software to, usually /usr/local.
HOMEBREW_REPOSITORY If installed from a Git clone, the repo folder. (That is, where Homebrew’s .git folder lives.)

Note that the script itself can use any suitable #! line, so an external “shell script” can be written for sh, bash, Ruby, or anything else.

Examples

Homebrew comes with some sample commands in Library/Contributions/examples.

User-submitted commands

NOTE: These commands are largely untested, and as always, be careful about running untested code on your machine.

brew-more

Scrapes a formula’s homepage to get more information: https://gist.github.com/475200

brew-refresh

Does brew update followed by brew outdated: https://gist.github.com/514247

brew-rebase

Does a rebase on “brew update” instead of a merge: https://gist.github.com/493669

brew-services

Simple support to start formulas using launchctl, has out of the box support for any formula which defines startup_plist (like e.g. mysql, postgres, redis u.v.m.): https://gist.github.com/766293

brew-gem

Install any gem into a self-contained Homebrew cellar location: https://github.com/josh/brew-gem

brew-desc

Get short descriptions for Homebrew formulas or search formulas by description: https://github.com/telemachus/brew-desc