Skip to content

Instantly share code, notes, and snippets.

@apainintheneck
Created May 12, 2023 06:38
Show Gist options
  • Save apainintheneck/3107449b6ad1385916e7dfbf9a76d280 to your computer and use it in GitHub Desktop.
Save apainintheneck/3107449b6ad1385916e7dfbf9a76d280 to your computer and use it in GitHub Desktop.
Testing the new service name DSL.

Overview

The idea here is to test the new service name DSL on the two formulae in homebrew/core that will need it right away. The xinit and dbus packages previously redefined the Formula#plist_name method which worked previously but now doesn't work at all with the API. To improve the situation and make it easier to serialize we're in the process of adding a new DSL.

service do
  plist_name "name"
  service_name "name"
end

More info:

Testing

First, we need to update the xinit and dbus formulae.

diff --git a/Formula/dbus.rb b/Formula/dbus.rb
index 5981a313b88..75e04f03532 100644
--- a/Formula/dbus.rb
+++ b/Formula/dbus.rb
@@ -72,15 +72,15 @@ class Dbus < Formula
     system "make", "install"
   end
 
-  def plist_name
-    "org.freedesktop.dbus-session"
-  end
-
   def post_install
     # Generate D-Bus's UUID for this machine
     system "#{bin}/dbus-uuidgen", "--ensure=#{var}/lib/dbus/machine-id"
   end
 
+  service do
+    plist_name "org.freedesktop.dbus-session"
+  end
+
   test do
     system "#{bin}/dbus-daemon", "--version"
   end
diff --git a/Formula/xinit.rb b/Formula/xinit.rb
index aa7c8dabe58..0abfbae811f 100644
--- a/Formula/xinit.rb
+++ b/Formula/xinit.rb
@@ -92,10 +92,6 @@ class Xinit < Formula
                    "bindir=#{bin}", "install"
   end
 
-  def plist_name
-    "homebrew.mxcl.startx"
-  end
-
   def caveats
     <<~EOS
       To start privileged xinit now and restart at login:
@@ -103,6 +99,10 @@ class Xinit < Formula
     EOS
   end
 
+  service do
+    plist_name "homebrew.mxcl.startx"
+  end
+
   test do
     (testpath/"test.c").write <<~EOS
       #include <assert.h>

Then, we need to check my PR branch and run the brew generate-formula-api command to generate the JSON for all formulae (this isn't strictly necessary but it's easier than fiddling around with a script to only generate things for these two packages).

Finally, you can load the package from the JSON file directly using the following script.

# Test loading formulae with the new service name DSL from the API JSON.

require "pathname"
require "json"
require "formula"

NAMES = %w[dbus xinit].freeze

puts ">>> Faking Formula API"

API_JSON = NAMES.to_h do |name|
  puts "- Adding #{name}"
  path = Pathname.new("~/Desktop/#{name}.json").expand_path
  content = path.read
  json = JSON.parse(content)
  name = json.delete("name")
  [name, json]
end
puts

module Homebrew
  module API
    module Formula
      def self.all_formulae
        API_JSON
      end
    end
  end
end

puts ">>> Loading Formulae"
NAMES.each_with_index do |name, idx|
  puts "#{idx}. Loading #{name}"

  formula = Formulary::FormulaAPILoader
    .new(name)
    .get_formula(:stable)

  puts "- plist_name : #{formula.plist_name}"
  puts "- service#serialize :"
  pp formula.service.serialize
  puts
end

The results are not that surprising but important nonetheless. The old code doesn't play well with this change so we'll have to wait a bit before applying this change to packages.

/u/l/Homebrew (master|✔) $ brew ruby '/Users/kevinrobell/Desktop/service-name-dsl-load-test.rb'
>>> Faking Formula API
- Adding dbus
- Adding xinit

>>> Loading Formulae
0. Loading dbus
/usr/local/Homebrew/Library/Homebrew/service.rb:571:in `deserialize': Unexpected run command:  (ArgumentError)
        from /usr/local/Homebrew/Library/Homebrew/formulary.rb:260:in `block in load_formula_from_api'
        from /usr/local/Homebrew/Library/Homebrew/formulary.rb:149:in `initialize'
        from /usr/local/Homebrew/Library/Homebrew/formulary.rb:149:in `new'
        from /usr/local/Homebrew/Library/Homebrew/formulary.rb:149:in `load_formula_from_api'
        from /usr/local/Homebrew/Library/Homebrew/formulary.rb:632:in `load_from_api'
        from /usr/local/Homebrew/Library/Homebrew/formulary.rb:623:in `klass'
        from /usr/local/Homebrew/Library/Homebrew/formulary.rb:403:in `get_formula'
        from /Users/kevinrobell/Desktop/service-name-dsl-load-test.rb:37:in `block in <main>'
        from /Users/kevinrobell/Desktop/service-name-dsl-load-test.rb:32:in `each'
        from /Users/kevinrobell/Desktop/service-name-dsl-load-test.rb:32:in `each_with_index'
        from /Users/kevinrobell/Desktop/service-name-dsl-load-test.rb:32:in `<main>'
/u/l/Homebrew (master|✔) [1]$ git checkout custom-service-name
Switched to branch 'custom-service-name'
/u/l/Homebrew (custom-service-name|✔) $ brew ruby '/Users/kevinrobell/Desktop/service-name-dsl-load-test.rb'
>>> Faking Formula API
- Adding dbus
- Adding xinit

>>> Loading Formulae
0. Loading dbus
- plist_name : org.freedesktop.dbus-session
- service#serialize :
{:plist_name=>"org.freedesktop.dbus-session"}

1. Loading xinit
- plist_name : homebrew.mxcl.startx
- service#serialize :
{:plist_name=>"homebrew.mxcl.startx"}

Then, we need to check my PR branch and run the brew generate-formula-api command to generate the JSON for all formulae (this isn't strictly necessary but it's easier than fiddling around with a script to only generate things for these two packages).

{
"name": "dbus",
"full_name": "dbus",
"tap": "homebrew/core",
"oldname": "d-bus",
"oldnames": [
"d-bus"
],
"aliases": [
],
"versioned_formulae": [
],
"desc": "Message bus system, providing inter-application communication",
"license": "AFL-2.1 or GPL-2.0-or-later",
"homepage": "https://wiki.freedesktop.org/www/Software/dbus",
"versions": {
"stable": "1.14.6",
"head": "HEAD",
"bottle": true
},
"urls": {
"stable": {
"url": "https://dbus.freedesktop.org/releases/dbus/dbus-1.14.6.tar.xz",
"tag": null,
"revision": null,
"checksum": "fd2bdf1bb89dc365a46531bff631536f22b0d1c6d5ce2c5c5e59b55265b3d66b"
},
"head": {
"url": "https://gitlab.freedesktop.org/dbus/dbus.git",
"branch": "master"
}
},
"revision": 0,
"version_scheme": 0,
"bottle": {
"stable": {
"rebuild": 0,
"root_url": "https://ghcr.io/v2/homebrew/core",
"files": {
"arm64_ventura": {
"cellar": "/opt/homebrew/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/dbus/blobs/sha256:ec0d9980cd5e80731bcf807a35719afb88b6178acbcd0c8ca04fc7bc430ca13b",
"sha256": "ec0d9980cd5e80731bcf807a35719afb88b6178acbcd0c8ca04fc7bc430ca13b"
},
"arm64_monterey": {
"cellar": "/opt/homebrew/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/dbus/blobs/sha256:aaadcaa68cbb6cc782692ef476562302da02318c80395b1aaf8207a0dc5ccafa",
"sha256": "aaadcaa68cbb6cc782692ef476562302da02318c80395b1aaf8207a0dc5ccafa"
},
"arm64_big_sur": {
"cellar": "/opt/homebrew/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/dbus/blobs/sha256:392325157689cb7e867e0eb1960207a1743bc8e07fafc53788abd85d6f3ca11c",
"sha256": "392325157689cb7e867e0eb1960207a1743bc8e07fafc53788abd85d6f3ca11c"
},
"ventura": {
"cellar": "/usr/local/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/dbus/blobs/sha256:3b543b7dd34df33bcd4d1bf169663e2a44e5d564ae44aa301887257ad05c6faf",
"sha256": "3b543b7dd34df33bcd4d1bf169663e2a44e5d564ae44aa301887257ad05c6faf"
},
"monterey": {
"cellar": "/usr/local/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/dbus/blobs/sha256:33c126b61e16c4c26af8db9cf01e8e8e9a0f0b7d8abed530204f65192cfe1bbd",
"sha256": "33c126b61e16c4c26af8db9cf01e8e8e9a0f0b7d8abed530204f65192cfe1bbd"
},
"big_sur": {
"cellar": "/usr/local/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/dbus/blobs/sha256:d17fb587941944b04a8ed8f1974bf2848f977ae2edef69b9f3ea76bfa4842259",
"sha256": "d17fb587941944b04a8ed8f1974bf2848f977ae2edef69b9f3ea76bfa4842259"
},
"x86_64_linux": {
"cellar": "/home/linuxbrew/.linuxbrew/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/dbus/blobs/sha256:f63b8565981e3c11b67f8801daf0f56eec357bc7bafabd481e29042f9c3cae93",
"sha256": "f63b8565981e3c11b67f8801daf0f56eec357bc7bafabd481e29042f9c3cae93"
}
}
}
},
"keg_only": false,
"keg_only_reason": null,
"options": [
],
"build_dependencies": [
"pkg-config",
"xmlto"
],
"dependencies": [
],
"test_dependencies": [
],
"recommended_dependencies": [
],
"optional_dependencies": [
],
"uses_from_macos": [
"expat"
],
"requirements": [
],
"conflicts_with": [
],
"conflicts_with_reasons": [
],
"link_overwrite": [
],
"caveats": null,
"installed": [
{
"version": "1.14.6",
"used_options": [
],
"built_as_bottle": false,
"poured_from_bottle": false,
"time": 1683855930,
"runtime_dependencies": [
],
"installed_as_dependency": false,
"installed_on_request": true
}
],
"linked_keg": "1.14.6",
"pinned": false,
"outdated": false,
"deprecated": false,
"deprecation_date": null,
"deprecation_reason": null,
"disabled": false,
"disable_date": null,
"disable_reason": null,
"service": {
"plist_name": "org.freedesktop.dbus-session"
},
"tap_git_head": "fa474ecf181763a4bea30bbc4ce0f3aadbcca412",
"ruby_source_path": "Formula/dbus.rb",
"ruby_source_checksum": {
"sha256": "faca79b41f44906dfededfe4dd20330cc24c973637d7da6d57a606100464a5ff"
},
"variations": {
}
}
{
"name": "xinit",
"full_name": "xinit",
"tap": "homebrew/core",
"oldname": null,
"oldnames": [
],
"aliases": [
],
"versioned_formulae": [
],
"desc": "Start the X Window System server",
"license": "MIT and APSL-2.0",
"homepage": "https://gitlab.freedesktop.org/xorg/app/xinit",
"versions": {
"stable": "1.4.2",
"head": null,
"bottle": true
},
"urls": {
"stable": {
"url": "https://www.x.org/releases/individual/app/xinit-1.4.2.tar.xz",
"tag": null,
"revision": null,
"checksum": "b7d8dc8d22ef9f15985a10b606ee4f2aad6828befa437359934647e88d331f23"
}
},
"revision": 0,
"version_scheme": 0,
"bottle": {
"stable": {
"rebuild": 0,
"root_url": "https://ghcr.io/v2/homebrew/core",
"files": {
"arm64_ventura": {
"cellar": ":any",
"url": "https://ghcr.io/v2/homebrew/core/xinit/blobs/sha256:0fbd33c0f3e8a01224d5f4c2f1437236957d9f9b80d0199f6bf729fe783320c9",
"sha256": "0fbd33c0f3e8a01224d5f4c2f1437236957d9f9b80d0199f6bf729fe783320c9"
},
"arm64_monterey": {
"cellar": ":any",
"url": "https://ghcr.io/v2/homebrew/core/xinit/blobs/sha256:b32fd947d6ab4e3d27cae884ecba3d25d618cc5df48869995db8211857a75cf9",
"sha256": "b32fd947d6ab4e3d27cae884ecba3d25d618cc5df48869995db8211857a75cf9"
},
"arm64_big_sur": {
"cellar": "/opt/homebrew/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/xinit/blobs/sha256:e3fa6b976ee03fddeea911fb37cf872c72b23b8a4b00ed11299925656b983fd5",
"sha256": "e3fa6b976ee03fddeea911fb37cf872c72b23b8a4b00ed11299925656b983fd5"
},
"ventura": {
"cellar": "/usr/local/Cellar",
"url": "https://ghcr.io/v2/homebrew/core/xinit/blobs/sha256:3db4e377fbe430f3ea074f0ec1f433b3b7278aa451da736d95a8a1ff72e87047",
"sha256": "3db4e377fbe430f3ea074f0ec1f433b3b7278aa451da736d95a8a1ff72e87047"
},
"monterey": {
"cellar": ":any",
"url": "https://ghcr.io/v2/homebrew/core/xinit/blobs/sha256:b206deb4ff3200499ab32a8984c90901277094697eaed812ec0e8d10765e64d6",
"sha256": "b206deb4ff3200499ab32a8984c90901277094697eaed812ec0e8d10765e64d6"
},
"big_sur": {
"cellar": ":any",
"url": "https://ghcr.io/v2/homebrew/core/xinit/blobs/sha256:1b62cbaab6ec39e95a11057e5ce26209c5b4f5696eaab24c0a59e1b7374a7fe0",
"sha256": "1b62cbaab6ec39e95a11057e5ce26209c5b4f5696eaab24c0a59e1b7374a7fe0"
},
"x86_64_linux": {
"cellar": ":any_skip_relocation",
"url": "https://ghcr.io/v2/homebrew/core/xinit/blobs/sha256:540015cba432ff1f7e719b37f9c2c3af6d8f40784840eeb8e8774cf8575b82a0",
"sha256": "540015cba432ff1f7e719b37f9c2c3af6d8f40784840eeb8e8774cf8575b82a0"
}
}
}
},
"keg_only": false,
"keg_only_reason": null,
"options": [
],
"build_dependencies": [
"pkg-config",
"tradcpp",
"lndir",
"mkfontscale"
],
"dependencies": [
"libx11",
"xauth",
"xmodmap",
"xrdb",
"xterm",
"quartz-wm"
],
"test_dependencies": [
"xorg-server"
],
"recommended_dependencies": [
],
"optional_dependencies": [
],
"uses_from_macos": [
],
"requirements": [
],
"conflicts_with": [
],
"conflicts_with_reasons": [
],
"link_overwrite": [
],
"caveats": "To start privileged xinit now and restart at login:\n sudo brew services start xinit --file=$HOMEBREW_PREFIX/opt/xinit/homebrew.mxcl.privileged_startx.plist\n",
"installed": [
{
"version": "1.4.2",
"used_options": [
],
"built_as_bottle": false,
"poured_from_bottle": false,
"time": 1683858335,
"runtime_dependencies": [
{
"full_name": "xorgproto",
"version": "2022.2",
"declared_directly": false
},
{
"full_name": "libxau",
"version": "1.0.11",
"declared_directly": false
},
{
"full_name": "libxdmcp",
"version": "1.1.4",
"declared_directly": false
},
{
"full_name": "libxcb",
"version": "1.15",
"declared_directly": false
},
{
"full_name": "libx11",
"version": "1.8.4",
"declared_directly": true
},
{
"full_name": "libxext",
"version": "1.3.5",
"declared_directly": false
},
{
"full_name": "libice",
"version": "1.1.1",
"declared_directly": false
},
{
"full_name": "libsm",
"version": "1.2.4",
"declared_directly": false
},
{
"full_name": "libxt",
"version": "1.3.0",
"declared_directly": false
},
{
"full_name": "libxmu",
"version": "1.1.4",
"declared_directly": false
},
{
"full_name": "xauth",
"version": "1.1.2",
"declared_directly": true
},
{
"full_name": "xmodmap",
"version": "1.0.11",
"declared_directly": true
},
{
"full_name": "xrdb",
"version": "1.2.1",
"declared_directly": true
},
{
"full_name": "libpng",
"version": "1.6.39",
"declared_directly": false
},
{
"full_name": "freetype",
"version": "2.13.0",
"declared_directly": false
},
{
"full_name": "fontconfig",
"version": "2.14.2",
"declared_directly": false
},
{
"full_name": "gettext",
"version": "0.21.1",
"declared_directly": false
},
{
"full_name": "libxpm",
"version": "3.5.16",
"declared_directly": false
},
{
"full_name": "libxaw",
"version": "1.0.15",
"declared_directly": false
},
{
"full_name": "libxrender",
"version": "0.9.11",
"declared_directly": false
},
{
"full_name": "libxft",
"version": "2.3.8",
"declared_directly": false
},
{
"full_name": "libxinerama",
"version": "1.1.5",
"declared_directly": false
},
{
"full_name": "xterm",
"version": "380",
"declared_directly": true
},
{
"full_name": "libapplewm",
"version": "1.4.1",
"declared_directly": false
},
{
"full_name": "libxrandr",
"version": "1.5.3",
"declared_directly": false
},
{
"full_name": "pixman",
"version": "0.42.2",
"declared_directly": false
},
{
"full_name": "quartz-wm",
"version": "1.3.2",
"declared_directly": true
}
],
"installed_as_dependency": false,
"installed_on_request": true
}
],
"linked_keg": "1.4.2",
"pinned": false,
"outdated": false,
"deprecated": false,
"deprecation_date": null,
"deprecation_reason": null,
"disabled": false,
"disable_date": null,
"disable_reason": null,
"service": {
"plist_name": "homebrew.mxcl.startx"
},
"tap_git_head": "fa474ecf181763a4bea30bbc4ce0f3aadbcca412",
"ruby_source_path": "Formula/xinit.rb",
"ruby_source_checksum": {
"sha256": "c39f73857c5e2c7830c191cc518db7e87f8912464452d745707faa6bc39b0edd"
},
"variations": {
"x86_64_linux": {
"build_dependencies": [
"pkg-config",
"tradcpp"
],
"dependencies": [
"libx11",
"xauth",
"xmodmap",
"xrdb",
"xterm",
"twm",
"util-linux"
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment