Class: Homebrew::DevCmd::Create Private

Inherits:
AbstractCommand show all
Defined in:
dev-cmd/create.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/create.rbi

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.

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

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

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#args ⇒ Homebrew::DevCmd::Create::Args

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.



10
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/create.rbi', line 10

def args; end

#find_app_in(download) ⇒ Array<(Pathname, [Cask::Appcast::Result, nil])>?

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.

Unpack the download to find the app it installs, relative to the unpacked download, and that app's appcast. This only prefills the template, so don't guess between the apps in a suite and warn rather than fail if the download can't be unpacked.

Parameters:

Returns:



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'dev-cmd/create.rb', line 275

def find_app_in(download)
  Dir.mktmpdir("brew-create", HOMEBREW_TEMP) do |dir|
    unpack_dir = Pathname(dir)
    UnpackStrategy.detect(download, merge_xattrs: true).extract_nestedly(to: unpack_dir)

    shallowest_apps = unpack_dir.glob("**/*.app")
                                .group_by { |app| app.each_filename.count }
                                .min_by(&:first)
                                &.last
    next unless shallowest_apps&.one?

    shallowest_app = shallowest_apps.fetch(0)
    [shallowest_app.relative_path_from(unpack_dir), find_appcast_for(shallowest_app)]
  end
rescue => e
  opoo "Unable to unpack the download to find its app."
  odebug e.message
  nil
end

#find_appcast_for(app) ⇒ Cask::Appcast::Result?

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.

Scan the app, from the download or an installed copy, for an appcast to prefill the livecheck block.

Parameters:

Returns:



262
263
264
265
266
267
268
# File 'dev-cmd/create.rb', line 262

def find_appcast_for(app)
  return unless app.directory?

  appcast = Cask::Appcast.find(app)
  ohai "Found #{appcast.strategy.inspect} appcast: #{appcast.url}" if appcast
  appcast
end

#generate_zap_for(app) ⇒ 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.

Scan for files left by an already-installed copy of the app to prefill the zap stanza.

Parameters:

Returns:



298
299
300
301
302
303
304
# File 'dev-cmd/create.rb', line 298

def generate_zap_for(app)
  return unless app.directory?

  Utils.popen_read_text(HOMEBREW_BREW_FILE, "generate-zap", "--name", app.basename(".app").to_s, err: :err)
       .slice(/^zap (?:trash|delete):.*/m)
       &.chomp
end

#run ⇒ 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.

Create a formula from a tarball URL.



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
113
114
115
116
117
118
119
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
# File 'dev-cmd/create.rb', line 81

def run
  if args.cask?
    url = args.named.fetch(0)
    set_name = args.set_name&.strip
    name = if set_name.blank?
      stem = Pathname.new(url).stem.rpartition("=").last
      print "Cask name [#{stem}]: "
      __gets || stem
    else
      set_name
    end
    token = Cask::Utils.token_from(name)
    Cask::TokenGenerator.warnings(token).each { |warning| opoo warning }

    cask_tap = Tap.fetch(args.tap || "homebrew/cask")
    raise TapUnavailableError, cask_tap.name unless cask_tap.installed?

    path = cask_tap.new_cask_path(token)
    path.dirname.mkpath unless path.dirname.exist?
    raise Cask::CaskAlreadyCreatedError, token if path.exist?

    set_version = args.set_version
    version = if set_version
      Version.new(set_version)
    else
      Version.detect(url.gsub(token, "").gsub(/x86(?:_64)?/, ""))
    end

    interpolated_url, sha256 = if version.null?
      [url, ""]
    else
      sha256 = if args.no_fetch?
        ""
      else
        strategy = DownloadStrategyDetector.detect(url)
        downloader = strategy.new(url, token, version.to_s, cache: Cask::Cache.path)
        downloader.fetch
        download = downloader.cached_location
        Downloadable.verification_cache.sha256(download)
      end

      [url.gsub(version.to_s, "\#{version}"), sha256]
    end

    app, appcast = find_app_in(download) if download
    installed_app = Pathname("/Applications")/(app&.basename || "#{name}.app")
    appcast ||= find_appcast_for(installed_app)
    livecheck_url = appcast ? appcast.url.inspect : "\"\""
    livecheck_strategy = appcast ? appcast.strategy.inspect : "\"\""
    zap_comment, zap = if (generated_zap = generate_zap_for(installed_app))
      ["Check that each path belongs to the app.", generated_zap.gsub("\n", "\n  ")]
    else
      ["Install and launch the app, then run `brew generate-zap #{token}`.", 'zap trash: ""']
    end

    path.atomic_write <<~RUBY
      # Documentation: https://docs.brew.sh/Cask-Cookbook
      # PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
      cask "#{token}" do
        version "#{version}"
        sha256 "#{sha256}"

        url "#{interpolated_url}"
        name "#{name}"
        desc ""
        homepage ""

        # Documentation: https://docs.brew.sh/Brew-Livecheck
        livecheck do
          url #{livecheck_url}
          strategy #{livecheck_strategy}
        end

        depends_on macos: ""

        app "#{app}"

        # #{zap_comment}
        # Documentation: https://docs.brew.sh/Cask-Cookbook#zap-creation
        #{zap}
      end
    RUBY

    puts "Please run `brew audit --cask --new #{token}` before submitting, thanks."
  else
    mode = if args.autotools?
      :autotools
    elsif args.cmake?
      :cmake
    elsif args.crystal?
      :crystal
    elsif args.go?
      :go
    elsif args.cabal?
      :cabal
    elsif args.meson?
      :meson
    elsif args.node?
      :node
    elsif args.perl?
      :perl
    elsif args.python?
      :python
    elsif args.ruby?
      :ruby
    elsif args.rust?
      :rust
    elsif args.zig?
      :zig
    end

    formula_creator = FormulaCreator.new(
      url:     args.named.fetch(0),
      name:    args.set_name,
      version: args.set_version,
      tap:     args.tap,
      mode:,
      license: args.set_license,
      fetch:   !args.no_fetch?,
      head:    args.HEAD?,
    )

    # ask for confirmation if name wasn't passed explicitly
    if args.set_name.blank?
      print "Formula name [#{formula_creator.name}]: "
      confirmed_name = __gets
      formula_creator.name = confirmed_name if confirmed_name.present?
    end

    formula_creator.verify_tap_available!

    # Check for disallowed formula, or names that shadow aliases,
    # unless --force is specified.
    unless args.force?
      if (reason = MissingFormula.disallowed_reason(formula_creator.name))
        odie <<~EOS
          The formula '#{formula_creator.name}' is not allowed to be created.
          #{reason}
          If you really want to create this formula use `--force`.
        EOS
      end

      Homebrew::API.with_no_api_env do
        if Formula.aliases.include?(formula_creator.name)
          realname = Formulary.canonical_name(formula_creator.name)
          odie <<~EOS
            The formula '#{realname}' is already aliased to '#{formula_creator.name}'.
            Please check that you are not creating a duplicate.
            To force creation use `--force`.
          EOS
        end
      end
    end

    path = formula_creator.write_formula!

    formula = Homebrew::API.with_no_api_env do
      CoreTap.instance.clear_cache
      Formula[formula_creator.name]
    end

    if args.python?
      Utils::GemSetup.install_bundler_gems!(groups: ["ast"])
      require "utils/pypi"
      PyPI.update_python_resources! formula, ignore_non_pypi_packages: true
    end

    puts <<~EOS
      Please audit and test formula before submitting:
        HOMEBREW_NO_INSTALL_FROM_API=1 brew audit --new #{formula_creator.name}
        HOMEBREW_NO_INSTALL_FROM_API=1 brew install --build-from-source --verbose --debug #{formula_creator.name}
        HOMEBREW_NO_INSTALL_FROM_API=1 brew test #{formula_creator.name}
    EOS
  end

  Utils::Editor.open path
end