suntong/cascadia

Add to homebrew package manager for macOS

0xdevalias opened this issue · 11 comments

It would be awesome if this was installable using homebrew on macOS:

⇒  brew create --help
Usage: brew create [options] URL

Generate a formula or, with --cask, a cask for the downloadable file at URL
and open it in the editor. Homebrew will attempt to automatically derive the
formula name and version, but if it fails, you'll have to make your own
template. The wget formula serves as a simple example. For the complete API,
see: https://rubydoc.brew.sh/Formula

      --autotools                  Create a basic template for an
                                   Autotools-style build.
      --cask                       Create a basic template for a cask.
      --cmake                      Create a basic template for a CMake-style
                                   build.
      --crystal                    Create a basic template for a Crystal build.
      --go                         Create a basic template for a Go build.
      --meson                      Create a basic template for a Meson-style
                                   build.
      --node                       Create a basic template for a Node build.
      --perl                       Create a basic template for a Perl build.
      --python                     Create a basic template for a Python build.
      --ruby                       Create a basic template for a Ruby build.
      --rust                       Create a basic template for a Rust build.
      --no-fetch                   Homebrew will not download URL to the cache
                                   and will thus not add its SHA-256 to the
                                   formula for you, nor will it check the GitHub
                                   API for GitHub projects (to fill out its
                                   description and homepage).
      --HEAD                       Indicate that URL points to the package's
                                   repository rather than a file.
      --set-name                   Explicitly set the name of the new formula
                                   or cask.
      --set-version                Explicitly set the version of the new
                                   formula or cask.
      --set-license                Explicitly set the license of the new
                                   formula.
      --tap                        Generate the new formula within the given
                                   tap, specified as user/repo.
  -f, --force                      Ignore errors for disallowed formula names
                                   and names that shadow aliases.
  -d, --debug                      Display any debugging information.
  -q, --quiet                      Make some output more quiet.
  -v, --verbose                    Make some output more verbose.
  -h, --help                       Show this message.

Here is my WIP formula, originally created with brew create --go https://github.com/suntong/cascadia/archive/refs/tags/v1.2.6.zip:

# Documentation: https://docs.brew.sh/Formula-Cookbook
#                https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Cascadia < Formula
  desc "Go cascadia package command-line CSS selector"
  homepage "https://github.com/suntong/cascadia"
  url "https://github.com/suntong/cascadia/archive/refs/tags/v1.2.6.zip"
  sha256 "d46fd0b91aabab9789c093286dccae564ece9095bf74b6bc92e865364845cfbd"
  license "MIT"
  head "https://github.com/suntong/cascadia.git", branch: "master"

  bottle do
      
  end

  depends_on "go" => :build

  def install
    # ENV.deparallelize  # if your formula fails when building in parallel
    system "go", "build", *std_go_args(ldflags: "-s -w")
  end

  test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! For Homebrew/homebrew-core
    # this will need to be a test that verifies the functionality of the
    # software. Run the test with `brew test cascadia`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end

When running brew install --debug cascadia I get the following error though:

==> go build -ldflags=-s -w
Last 15 lines from /Users/devalias/Library/Logs/Homebrew/cascadia/01.go:
2023-01-08 01:25:31 +0000

go
build
-trimpath
-o=/usr/local/Cellar/cascadia/1.2.6/bin/cascadia
-ldflags=-s -w

go: go.mod file not found in current directory or any parent directory; see 'go help modules'

I created the following issue about the missing go.mod file:

According to this StackOverflow, it seems we can work around the missing go.mod file in the interim by setting GO111MODULE=off:

Setting that in the above homebrew formula like this:

def install
    ENV['GO111MODULE'] = "off"
    system "go", "build", *std_go_args(ldflags: "-s -w")
  end

Then leads to the following new errors when building with brew install --debug cascadia:

==> go build -ldflags=-s -w
Last 15 lines from /Users/devalias/Library/Logs/Homebrew/cascadia/01.go:
cascadia_main.go:19:2: cannot find package "github.com/PuerkitoBio/goquery" in any of:
	/usr/local/Cellar/go/1.19.4/libexec/src/github.com/PuerkitoBio/goquery (from $GOROOT)
	/Users/devalias/Library/Caches/Homebrew/go_mod_cache/src/github.com/PuerkitoBio/goquery (from $GOPATH)
cascadia_main.go:20:2: cannot find package "github.com/andybalholm/cascadia" in any of:
	/usr/local/Cellar/go/1.19.4/libexec/src/github.com/andybalholm/cascadia (from $GOROOT)
	/Users/devalias/Library/Caches/Homebrew/go_mod_cache/src/github.com/andybalholm/cascadia (from $GOPATH)
cascadia_cliDef.go:13:2: cannot find package "github.com/mkideal/cli" in any of:
	/usr/local/Cellar/go/1.19.4/libexec/src/github.com/mkideal/cli (from $GOROOT)
	/Users/devalias/Library/Caches/Homebrew/go_mod_cache/src/github.com/mkideal/cli (from $GOPATH)
cascadia_cliDef.go:15:2: cannot find package "github.com/mkideal/cli/ext" in any of:
	/usr/local/Cellar/go/1.19.4/libexec/src/github.com/mkideal/cli/ext (from $GOROOT)
	/Users/devalias/Library/Caches/Homebrew/go_mod_cache/src/github.com/mkideal/cli/ext (from $GOPATH)
cascadia_main.go:22:2: cannot find package "golang.org/x/net/html" in any of:
	/usr/local/Cellar/go/1.19.4/libexec/src/golang.org/x/net/html (from $GOROOT)
	/Users/devalias/Library/Caches/Homebrew/go_mod_cache/src/golang.org/x/net/html (from $GOPATH)

Looking at some of the other go-based formulae on Hombrew:

It seems as though we may be able to provide the dependencies using the go_resource syntax..

Though then I found this, which suggests that it's deprecated:

It seems like the go.mod file I created from go mod init github.com/suntong/cascadia + go mod tidy in the following PR makes it work:

As a hacky interim until that gets properly fixed, we can then seemingly change the install block of the formula to this:

def install
    # go mod init/tidy are needed until https://github.com/suntong/cascadia/issues/13 is fixed
    system "go", "mod", "init", "github.com/suntong/cascadia"
    system "go", "mod", "tidy"
    system "go", "build", *std_go_args(ldflags: "-s -w")
  end

Raised a PR to Homebrew to add this formula:

Yes, GO111MODULE=off is the way I do the build.

I'll hold off turning it on as long as I could, as I personally believe it'll bring more mess to my simple project than doing it good. Huge projects with tons of dependencies is another different story.

As a hacky interim until that gets properly fixed, we can then seemingly change the install block of the formula to this:

Yes, please do. thx.

I'll hold off turning it on as long as I could, as I personally believe it'll bring more mess to my simple project than doing it good.

To be honest, it's a tiny single file change, and super simplistic; with seemingly a single command required to keep it up to date if you change any dependencies.. i'd recommend maybe just merging the PR where I submitted it and being modern/compliant:

Looks like the changes from #14 will land in v1.2.7:

Updated the PR to submit the homebrew formula to use that version in Homebrew/homebrew-core@b6e643b (#120044)

Can this issue be closed now?

Generally speaking it should probably stay open until this PR lands:

But as project owner, I guess that's really up to you.

Can this issue be closed now?

Yup!

Was merged in: Homebrew/homebrew-core@52e7efb

@0xdevalias @suntong, thanks for your contribution to Homebrew! 🎉 🥇

Without awesome contributors like you, it would be impossible to maintain Homebrew to the high level of quality users have come to expect. Thank you!!!!

Originally posted by @chenrui333 in Homebrew/homebrew-core#120044 (comment)