Module: CPAN Private

Defined in:
utils/cpan.rb

Overview

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

Helper functions for updating CPAN resources.

Defined Under Namespace

Classes: Package

Class Method Summary collapse

Class Method Details

.update_perl_resources!(formula, print_only: false, silent: false, verbose: false, ignore_errors: false) ⇒ Boolean?

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.

Update CPAN resources in a formula.

Parameters:

  • formula (Formula)
  • print_only (Boolean, nil) (defaults to: false)
  • silent (Boolean, nil) (defaults to: false)
  • verbose (Boolean, nil) (defaults to: false)
  • ignore_errors (Boolean, nil) (defaults to: false)

Returns:

  • (Boolean, nil)


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
# File 'utils/cpan.rb', line 91

def self.update_perl_resources!(formula, print_only: false, silent: false, verbose: false, ignore_errors: false)
  cpan_resources = formula.resources.select { |resource| resource.url.start_with?(METACPAN_URL_PREFIX) }

  odie "\"#{formula.name}\" has no CPAN resources to update." if cpan_resources.empty?

  show_info = !print_only && !silent

  non_cpan_resources = formula.resources.reject { |resource| resource.url.start_with?(METACPAN_URL_PREFIX) }
  ohai "Skipping #{non_cpan_resources.length} non-CPAN resources" if non_cpan_resources.any? && show_info
  ohai "Found #{cpan_resources.length} CPAN resources to update" if show_info

  new_resource_blocks = ""
  package_errors = ""
  updated_count = 0

  cpan_resources.each do |resource|
    package = Package.new(resource.name, resource.url)

    unless package.valid_cpan_package?
      if ignore_errors
        package_errors += "  # RESOURCE-ERROR: \"#{resource.name}\" is not a valid CPAN resource\n"
        next
      else
        odie "\"#{resource.name}\" is not a valid CPAN resource"
      end
    end

    ohai "Checking \"#{resource.name}\" for updates..." if show_info

    info = package.latest_cpan_info

    unless info
      if ignore_errors
        package_errors += "  # RESOURCE-ERROR: Unable to resolve \"#{resource.name}\"\n"
        next
      else
        odie "Unable to resolve \"#{resource.name}\""
      end
    end

    name, url, checksum, new_version = info
    current_version = package.current_version

    if current_version && new_version && current_version != new_version
      ohai "\"#{resource.name}\": #{current_version} -> #{new_version}" if show_info
      updated_count += 1
    elsif show_info
      ohai "\"#{resource.name}\": already up to date (#{current_version})" if current_version
    end

    new_resource_blocks += <<-EOS
resource "#{name}" do
  url "#{url}"
  sha256 "#{checksum}"
end

    EOS
  end

  package_errors += "\n" if package_errors.present?
  resource_section = "#{package_errors}#{new_resource_blocks}"

  if print_only
    puts resource_section.chomp
    return true
  end

  if formula.resources.all? { |resource| resource.name.start_with?("homebrew-") }
    inreplace_regex = /  def install/
    resource_section += "  def install"
  else
    inreplace_regex = /
      \ \ (
      (\#\ RESOURCE-ERROR:\ .*\s+)*
      resource\ .*\ do\s+
        url\ .*\s+
        sha256\ .*\s+
        ((\#.*\s+)*
        patch\ (.*\ )?do\s+
          url\ .*\s+
          sha256\ .*\s+
        end\s+)*
      end\s+)+
    /x
    resource_section += "  "
  end

  ohai "Updating resource blocks" unless silent
  Utils::Inreplace.inreplace formula.path do |s|
    if T.must(s.inreplace_string.split(/^  test do\b/, 2).first).scan(inreplace_regex).length > 1
      odie "Unable to update resource blocks for \"#{formula.name}\" automatically. Please update them manually."
    end
    s.sub! inreplace_regex, resource_section
  end

  if package_errors.present?
    ofail "Unable to resolve some dependencies. Please check #{formula.path} for RESOURCE-ERROR comments."
  elsif updated_count.positive?
    ohai "Updated #{updated_count} CPAN resource#{"s" if updated_count != 1}" unless silent
  end

  true
end