Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create homebrew formula with only scripts

Tags:

homebrew

I want to package up a few shell scripts + support files into a homebrew formula that installs these scripts somewhere on the user $PATH. I will serve the formula from my own tap.

Reading through the formula cookbook the examples seem to assume cmake or autotools system in the upstream library. What if my project only consists of a few scripts and config files? Should I just manually copy those into #{prefix}/ in the Formula?

like image 206
Jeroen Ooms Avatar asked Sep 28 '17 21:09

Jeroen Ooms


People also ask

Where does Homebrew store formulas?

Packages are installed according to their formulae, which live in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula .

Does Homebrew build from source?

Homebrew's pre-built binary packages (known as bottles) of many formulae can only be used if you install in the default installation prefix, otherwise they have to be built from source. Building from source takes a long time, is prone to failure, and is not supported.

What is a Homebrew formulae?

Homebrew Formulae is an online package browser for Homebrew – the macOS (and Linux) package manager.


1 Answers

There are two cases here:

Standalone Scripts

Install them under bin using bin.install. You can optionally rename them, e.g. to strip the extension:

class MyFormula < Formula
  # ...

  def install
    # move 'myscript.sh' under #{prefix}/bin/
    bin.install "myscript.sh"

    # OR move 'myscript.sh' to #{prefix}/bin/mybettername
    bin.install "myscript.sh" => "mybettername"

    # OR move *.sh under bin/
    bin.install Dir["*.sh"]
  end
end

Scripts with Support Files

This case is tricky because you need to get all the paths right. The simplest way is to install everything under #{libexec}/ then write exec scripts under #{bin}/. That’s a very common pattern in Homebrew formulae.

class MyFormula < Formula
  # ...

  def install
    # Move everything under #{libexec}/
    libexec.install Dir["*"]

    # Then write executables under #{bin}/
    bin.write_exec_script (libexec/"myscript.sh")
  end
end

Given a tarball (or a git repo) that contains the following content:

  • script.sh
  • supportfile.txt

The above formula will create the following hierarchy:

#{prefix}/
  libexec/
    script.sh
    supportfile.txt
  bin/
    script.sh

Homebrew creates that #{prefix}/bin/script.sh with the following content:

#!/bin/bash
exec "#{libexec}/script.sh" "$@"

This means that your script can expect to have a support file in its own directory while not polluting bin/ and not making any assumption regarding the install path (e.g. you don’t need to use things like ../libexec/supportfile.txt in your script).

See this answer of mine for an example with a Ruby script and that one for an example with manpages.

Note Homebrew also have other helpers to e.g. not only write an exec script but also set environment variables or execute a .jar.

like image 157
bfontaine Avatar answered Sep 27 '22 22:09

bfontaine