Class: Homebrew::DevCmd::BumpCaskPr

Inherits:
AbstractCommand show all
Defined in:
dev-cmd/bump-cask-pr.rb

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, #initialize, parser

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#runvoid

This method returns an undefined value.



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
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
# File 'dev-cmd/bump-cask-pr.rb', line 64

def run
  odeprecated "brew bump-cask-pr --online" if args.online?
  odisabled "brew bump-cask-pr --force" if args.force?

  # This will be run by `brew audit` or `brew style` later so run it first to
  # not start spamming during normal output.
  gem_groups = []
  gem_groups << "style" if !args.no_audit? || !args.no_style?
  gem_groups << "audit" unless args.no_audit?
  Homebrew.install_bundler_gems!(groups: gem_groups) unless gem_groups.empty?

  # As this command is simplifying user-run commands then let's just use a
  # user path, too.
  ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s

  # Use the user's browser, too.
  ENV["BROWSER"] = EnvConfig.browser

  cask = args.named.to_casks.first

  odie "This cask is not in a tap!" if cask.tap.blank?
  odie "This cask's tap is not a Git repository!" unless cask.tap.git?

  odie <<~EOS unless cask.tap.allow_bump?(cask.token)
    Whoops, the #{cask.token} cask has its version update
    pull requests automatically opened by BrewTestBot!
    We'd still love your contributions, though, so try another one
    that's not in the autobump list:
      #{Formatter.url("#{cask.tap.remote}/blob/master/.github/autobump.txt")}
  EOS

  odie "You have too many PRs open: close or merge some first!" if GitHub.too_many_open_prs?(cask.tap)

  new_version = BumpVersionParser.new(
    general: args.version,
    intel:   args.version_intel,
    arm:     args.version_arm,
  )

  new_hash = unless (new_hash = args.sha256).nil?
    raise UsageError, "`--sha256` must not be empty." if new_hash.blank?

    ["no_check", ":no_check"].include?(new_hash) ? :no_check : new_hash
  end

  new_base_url = unless (new_base_url = args.url).nil?
    raise UsageError, "`--url` must not be empty." if new_base_url.blank?

    begin
      URI(new_base_url)
    rescue URI::InvalidURIError
      raise UsageError, "`--url` is not valid."
    end
  end

  if new_version.blank? && new_base_url.nil? && new_hash.nil?
    raise UsageError, "No `--version`, `--url` or `--sha256` argument specified!"
  end

  check_pull_requests(cask, new_version:)

  replacement_pairs ||= []
  branch_name = "bump-#{cask.token}"
  commit_message = nil

  old_contents = File.read(cask.sourcefile_path)

  if new_base_url
    commit_message ||= "#{cask.token}: update URL"

    m = /^ +url "(.+?)"\n/m.match(old_contents)
    odie "Could not find old URL in cask!" if m.nil?

    old_base_url = m.captures.fetch(0)

    replacement_pairs << [
      /#{Regexp.escape(old_base_url)}/,
      new_base_url.to_s,
    ]
  end

  if new_version.present?
    # For simplicity, our naming defers to the arm version if we multiple architectures are specified
    branch_version = new_version.arm || new_version.general
    if branch_version.is_a?(Cask::DSL::Version)
      commit_version = shortened_version(branch_version, cask:)
      branch_name = "bump-#{cask.token}-#{branch_version.tr(",:", "-")}"
      commit_message ||= "#{cask.token} #{commit_version}"
    end
    replacement_pairs = replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
  end
  # Now that we have all replacement pairs, we will replace them further down

  commit_message ||= "#{cask.token}: update checksum" if new_hash

  # Remove nested arrays where elements are identical
  replacement_pairs = replacement_pairs.reject { |pair| pair[0] == pair[1] }.uniq.compact
  Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
                                   replacement_pairs,
                                   read_only_run: args.dry_run?,
                                   silent:        args.quiet?)

  run_cask_audit(cask, old_contents)
  run_cask_style(cask, old_contents)

  pr_info = {
    branch_name:,
    commit_message:,
    old_contents:,
    pr_message:      "Created with `brew bump-cask-pr`.",
    sourcefile_path: cask.sourcefile_path,
    tap:             cask.tap,
  }
  GitHub.create_bump_pr(pr_info, args:)
end