Distributing Laravel Zero apps with Brew

Published in PHP, Laravel Zero on Jan 21, 2021

Laravel Zero supports publishing a Phar file of your application. This file can then be distributed however you want. For example, if your application will primarily be used on macOS, you may want to distribute it as a Brew formula.

To get started, make sure your Phar is published to your repository, and the repository is publicly accessible. You can build a Phar of your application using the following command:

$ php [my-application] app:build

Once this has been published, you'll then need to create a formula file using Ruby. I've provided an example of a Laravel Zero application formula below. Replace any relevant details, and make sure you fill in the SHA-256 checksum if it is a tagged release. The file should be named as your application binary is, and the class name should be using pascal case.

class MyApplication < Formula
  desc "A Laravel Zero app that does something"
  homepage "https://github.com/owenvoke/my-laravel-zero-app"
  url "https://github.com/owenvoke/my-laravel-zero-app/raw/v1.0.0/builds/my-application"
  sha256 ""
  license "MIT"

  bottle :unneeded

  depends_on "php"

  def install
    bin.install "my-application"
  end

  test do
    system "#{bin}/my-application", "--version"
  end
end

The depends_on "stanza" allows you to define a list of packages that your application depends on. Using depends_on "php" will make sure that the latest stable PHP version is always installed. If your application requires a specific version of PHP (e.g. PHP 7.3), you may need to use depends_on "[email protected]".

You can also add a livecheck stanza, which allows you to automatically update the formula with the latest version from your GitHub tags, and other methods. This is the code that would need to be added:

livecheck do
    url :stable
    regex(/^v?(\d+(?:\.\d+)+)$/i)
end

Check out the Livecheck documentation for more options that are available.

Once this formula has been added to either the Homebrew Core repository or your own custom repository under a Formula directory (e.g. my Casks and Formula repository) then you can install the formula via Brew with the following command:

$ brew install my-application

Note: If you are using a custom "tap", make sure that you have added it to your taps by running the following command first: $ brew tap [username/repository]

Once installed, your application will be available as the value after bin.install, and you can test it has worked successfully by running [my-application] --version.

For an example of a formula where I have used this, see my tapped formula for Takeout by Tighten.

0
0
0
0