Class: Homebrew::FormulaCreator Private

Inherits:
Object
  • Object
show all
Includes:
Utils::Output::Mixin
Defined in:
formula_creator.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Class for generating a formula from a template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

#initialize(url:, name: nil, version: nil, tap: nil, mode: nil, license: nil, fetch: false, head: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • url (String)
  • name (String, nil) (defaults to: nil)
  • version (String, nil) (defaults to: nil)
  • tap (String, nil) (defaults to: nil)
  • mode (Symbol, nil) (defaults to: nil)
  • license (String, nil) (defaults to: nil)
  • fetch (Boolean) (defaults to: false)
  • head (Boolean) (defaults to: false)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'formula_creator.rb', line 31

def initialize(url:, name: nil, version: nil, tap: nil, mode: nil, license: nil, fetch: false, head: false)
  @url = url
  @mode = mode
  @license = license
  @fetch = fetch

  tap = if tap.blank?
    CoreTap.instance
  else
    Tap.fetch(tap)
  end
  @tap = T.let(tap, Tap)

  if (match_github = url.match(%r{github\.com/(?<user>[^/]+)/(?<repo>[^/]+).*})) &&
     (user = match_github[:user]) && (repository = match_github[:repo])
    if repository.end_with?(".git")
      # e.g. https://github.com/Homebrew/brew.git
      repository.delete_suffix!(".git")
      head = true
    end
    odebug "github: #{user} #{repository} head:#{head}"
    if name.blank?
      name = repository
      odebug "name from github: #{name}"
    end
  elsif name.blank?
    stem = Pathname.new(url).stem
    name = if stem.start_with?("index.cgi") && stem.include?("=")
      # special cases first
      # gitweb URLs e.g. http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary
      stem.rpartition("=").last
    else
      # e.g. http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz
      pathver = Version.parse(stem).to_s
      stem.sub(/[-_.]?#{Regexp.escape(pathver)}$/, "")
    end
    odebug "name from url: #{name}"
  end
  @name = T.let(name, String)
  @head = head

  resolved_version = version.blank? ? Version::NULL : Version.new(version)
  @version = T.let(resolved_version, Version)
  @version_in_url = T.let(false, T::Boolean)

  if @version.present?
    odebug "version from user: #{@version}"
  else
    @version_in_url = Version.detect(url).present?
    odebug "version in url: #{@version_in_url}"
  end

  if fetch && user && repository
    github = GitHub.repository(user, repository)

    if @version.blank? && !@version_in_url && !head
      begin
        tag = GitHub.get_latest_release(user, repository).fetch("tag_name")
        @url = "https://github.com/#{user}/#{repository}/archive/refs/tags/#{tag}.tar.gz"
        odebug "github: url changed to source archive #{@url}"

        # detect(@url) removes v prefix from tags, and new(tag) doesn't
        tag_version = Version.new(tag.delete_prefix("v"))
        if Version.detect(@url) == tag_version
          @version_in_url = true
        else
          @version = tag_version
          odebug "github: version #{@version} from tag"
        end
        odebug "version in url (github): #{@version_in_url}"
      rescue GitHub::API::HTTPNotFoundError
        odebug "github: latest_release lookup failed: #{url}"
      end
    end
  end
  @github = T.let(github, T.untyped)

  @sha256 = T.let(nil, T.nilable(String))
  @desc = T.let("", String)
  @homepage = T.let(nil, T.nilable(String))
  @license = T.let(nil, T.nilable(String))
end

Instance Attribute Details

#head ⇒ Boolean (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


25
26
27
# File 'formula_creator.rb', line 25

def head
  @head
end

#name ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



16
17
18
# File 'formula_creator.rb', line 16

def name
  @name
end

#url ⇒ String (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



22
23
24
# File 'formula_creator.rb', line 22

def url
  @url
end

#version ⇒ Version (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



19
20
21
# File 'formula_creator.rb', line 19

def version
  @version
end

Instance Method Details

#verify_tap_available! ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



115
116
117
# File 'formula_creator.rb', line 115

def verify_tap_available!
  raise TapUnavailableError, @tap.name unless @tap.installed?
end

#write_formula! ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'formula_creator.rb', line 120

def write_formula!
  raise ArgumentError, "name is blank!" if @name.blank?
  raise ArgumentError, "tap is blank!" if @tap.blank?

  path = @tap.new_formula_path(@name)
  raise "#{path} already exists" if path.exist?

  if @version.blank? && !@version_in_url
    odie "Version cannot be determined from URL. Explicitly set the version with `--set-version` instead."
  end

  if @fetch
    unless @head
      r = Resource.new
      r.url(@url)
      r.owner = self
      filepath = r.fetch
      html_doctype_prefix = "<!doctype html"
      # Number of bytes to read from file start to ensure it is not HTML.
      # HTML may start with arbitrary number of whitespace lines.
      bytes_to_read = 100
      if File.read(filepath, bytes_to_read).strip.downcase.start_with?(html_doctype_prefix)
        raise "Downloaded URL is not archive"
      end

      # The missing-checksum warning during the fetch above has already
      # hashed the file, so reuse its digest.
      @sha256 = T.let(Downloadable.verification_cache.sha256(filepath), T.nilable(String))
    end

    if @github
      # Convert null to empty string. Strip trailing dot if present.
      @desc = @github["description"].to_s.delete_suffix(".")
      @homepage = @github["homepage"].presence || "https://github.com/#{@github["full_name"]}"
      @license = @github["license"]["spdx_id"] if @github["license"]
    end
  end

  path.dirname.mkpath
  path.write ERB.new(<<~'ERB', trim_mode: "<>").result(binding)
    # Documentation: https://docs.brew.sh/Formula-Cookbook
    #                https://docs.brew.sh/rubydoc/Formula
    # PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
    class <%= Formulary.class_s(name) %> < Formula
    <% if @mode == :python %>
      include Language::Python::Virtualenv

    <% end %>
      desc <%= @desc.inspect %>
      homepage <%= @homepage.to_s.inspect %>
    <% unless @head %>
      url <%= @url.inspect %>
    <% unless @version_in_url %>
      version <%= @version.to_s.delete_prefix("v").inspect %>
    <% end %>
      sha256 <%= @sha256.to_s.inspect %>
    <% end %>
      license <%= @license.to_s.inspect %>
    <% if @head %>
      head <%= @url.inspect %>
    <% end %>

    <% if @mode == :cabal %>
      depends_on "cabal-install" => :build
      depends_on "ghc" => :build
      depends_on "gmp"

      uses_from_macos "libffi"
    <% elsif @mode == :cmake %>
      depends_on "cmake" => :build
    <% elsif @mode == :crystal %>
      depends_on "crystal" => :build
    <% elsif @mode == :go %>
      depends_on "go" => :build
    <% elsif @mode == :meson %>
      depends_on "meson" => :build
      depends_on "ninja" => :build
    <% elsif @mode == :node %>
      depends_on "node"
    <% elsif @mode == :perl %>
      uses_from_macos "perl"

      # Add CPAN dependencies as resources
      # resource "" do
      #   url ""
      #   sha256 ""
      # end
    <% elsif @mode == :python %>
      depends_on <%= latest_versioned_formula("python").inspect %>
    <% elsif @mode == :ruby %>
      depends_on "ruby"
    <% elsif @mode == :rust %>
      depends_on "rust" => :build
    <% elsif @mode == :zig %>
      depends_on "zig" => :build
    <% elsif @mode.nil? %>
      # depends_on "cmake" => :build
    <% end %>

    <% if [:node, :python].exclude? @mode %>
      deny_network_access!

    <% end %>
    <% if @mode == :cabal %>
      def fetch
        system "cabal", "v2-update"
        system "cabal", "v2-install", "--only-download", *std_cabal_v2_args
      end

    <% elsif @mode == :crystal %>
      def fetch
        system "shards", "install", "--production", "--skip-postinstall"
      end

    <% elsif @mode == :go %>
      def fetch
        system "go", "mod", "download"
      end

    <% elsif @mode == :ruby %>
      def fetch
        ENV["BUNDLE_PATH"] = ".bundle"

        system "bundle", "cache", "--no-install"
      end

    <% elsif @mode == :rust %>
      def fetch
        system "cargo", "fetch", *std_cargo_fetch_args
      end

    <% elsif @mode == :zig %>
      def fetch
        system "zig", "build", "--fetch"
      end

    <% end %>
      def install
    <% if @mode == :cabal %>
        system "cabal", "v2-install", *std_cabal_v2_args
    <% elsif @mode == :cmake %>
        system "cmake", "-S", ".", "-B", "build", *std_cmake_args
        system "cmake", "--build", "build"
        system "cmake", "--install", "build"
    <% elsif @mode == :autotools %>
        # Remove unrecognized options if they cause configure to fail
        # https://docs.brew.sh/rubydoc/Formula.html#std_configure_args-instance_method
        system "./configure", "--disable-silent-rules", *std_configure_args
        system "make", "install" # if this fails, try separate make/make install steps
    <% elsif @mode == :crystal %>
        system "shards", "build", *std_shards_args
        bin.install <%= "bin/#{name}".inspect %>
    <% elsif @mode == :go %>
        system "go", "build", *std_go_args
    <% elsif @mode == :meson %>
        system "meson", "setup", "build", *std_meson_args
        system "meson", "compile", "-C", "build", "--verbose"
        system "meson", "install", "-C", "build"
    <% elsif @mode == :node %>
        system "npm", "install", *std_npm_args
        bin.install_symlink libexec.glob("bin/*")
    <% elsif @mode == :perl %>
        ENV.prepend_create_path "PERL5LIB", libexec/"lib/perl5"
        ENV.prepend_path "PERL5LIB", libexec/"lib"

        # Stage additional dependency (`Makefile.PL` style).
        # resource("").stage do
        #   system "perl", "Makefile.PL", "INSTALL_BASE=#{libexec}"
        #   system "make"
        #   system "make", "install"
        # end

        # Stage additional dependency (`Build.PL` style).
        # resource("").stage do
        #   system "perl", "Build.PL", "--install_base", libexec
        #   system "./Build"
        #   system "./Build", "install"
        # end

        bin.install name
        bin.env_script_all_files(libexec/"bin", PERL5LIB: ENV["PERL5LIB"])
    <% elsif @mode == :python %>
        virtualenv_install_with_resources
    <% elsif @mode == :ruby %>
        ENV["GEM_HOME"] = libexec

        system "bundle", "install", "--local"
        system "gem", "build", "#{name}.gemspec"
        system "gem", "install", "--ignore-dependencies", "#{name}-#{version}.gem"

        bin.install libexec/"bin/#{name}"
        bin.env_script_all_files(libexec/"bin", GEM_HOME: ENV["GEM_HOME"])
    <% elsif @mode == :rust %>
        system "cargo", "install", *std_cargo_args
    <% elsif @mode == :zig %>
        system "zig", "build", *std_zig_args
    <% else %>
        # Remove unrecognized options if they cause configure to fail
        # https://docs.brew.sh/rubydoc/Formula.html#std_configure_args-instance_method
        system "./configure", "--disable-silent-rules", *std_configure_args
        # system "cmake", "-S", ".", "-B", "build", *std_cmake_args
    <% end %>
      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 <%= name %>`.
        #
        # 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
  ERB
  path
end