<commit_before>require "hbc/cask_loader"
require "hbc/download"

module UpdateMigrator
  class << self
    def formula_resources(formula)
      specs = [formula.stable, formula.devel, formula.head].compact

      [*formula.bottle&.resource] + specs.flat_map do |spec|
        [
          spec,
          *spec.resources.values,
          *spec.patches.select(&:external?).map(&:resource),
        ]
      end
    end
    private :formula_resources

    def parse_extname(url)
      uri_path = if URI::DEFAULT_PARSER.make_regexp =~ url
        uri = URI(url)
        uri.query ? "#{uri.path}?#{uri.query}" : uri.path
      else
        url
      end

      # Given a URL like https://example.com/download.php?file=foo-1.0.tar.gz
      # the extension we want is ".tar.gz", not ".php".
      Pathname.new(uri_path).ascend do |path|
        ext = path.extname[/[^?&]+/]
        return ext if ext
      end

      nil
    end
    private :parse_extname

    def migrate_cache_entries_to_double_dashes(initial_version)
      return if initial_version && initial_version > "1.7.1"

      return if ENV.key?("HOMEBREW_DISABLE_LOAD_FORMULA")

      ohai "Migrating cache entries..."

      Formula.each do |formula|
        formula_resources(formula).each do |resource|
          downloader = resource.downloader

          url = downloader.url
          name = resource.download_name
          version = resource.version

          extname = parse_extname(url)
          old_location = downloader.cache/"#{name}-#{version}#{extname}"
          new_location = downloader.cache/"#{name}--#{version}#{extname}"

          next unless old_location.file?

          if new_location.exist?
            begin
              FileUtils.rm_rf old_location
            rescue Errno::EACCES
              opoo "Could not remove #{old_location}, please do so manually."
            end
          else
            begin
              FileUtils.mv old_location, new_location
            rescue Errno::EACCES
              opoo "Could not move #{old_location} to #{new_location}, please do so manually."
            end
          end
        end
      end
    end

    def migrate_cache_entries_to_symlinks(initial_version)
      return if initial_version && initial_version > "1.7.2"

      return if ENV.key?("HOMEBREW_DISABLE_LOAD_FORMULA")

      ohai "Migrating cache entries..."

      cache_entries = lambda do |path|
        if path.directory?
          path.children
              .reject(&:symlink?)
              .select(&:file?)
              .map { |child| child.basename.to_s }
              .select { |basename| basename.include?("--") }
              .map { |basename| basename.sub(/\-\-.*/, "") }<commit_msg>Skip migration if cache is empty or doesn’t exist.<commit_after>require "hbc/cask_loader"
require "hbc/download"

module UpdateMigrator
  class << self
    def formula_resources(formula)
      specs = [formula.stable, formula.devel, formula.head].compact

      [*formula.bottle&.resource] + specs.flat_map do |spec|
        [
          spec,
          *spec.resources.values,
          *spec.patches.select(&:external?).map(&:resource),
        ]
      end
    end
    private :formula_resources

    def parse_extname(url)
      uri_path = if URI::DEFAULT_PARSER.make_regexp =~ url
        uri = URI(url)
        uri.query ? "#{uri.path}?#{uri.query}" : uri.path
      else
        url
      end

      # Given a URL like https://example.com/download.php?file=foo-1.0.tar.gz
      # the extension we want is ".tar.gz", not ".php".
      Pathname.new(uri_path).ascend do |path|
        ext = path.extname[/[^?&]+/]
        return ext if ext
      end

      nil
    end
    private :parse_extname

    def migrate_cache_entries_to_double_dashes(initial_version)
      return if initial_version && initial_version > "1.7.1"

      return if ENV.key?("HOMEBREW_DISABLE_LOAD_FORMULA")

      return unless HOMEBREW_CACHE.directory?
      return if HOMEBREW_CACHE.children.empty?

      ohai "Migrating cache entries..."

      Formula.each do |formula|
        formula_resources(formula).each do |resource|
          downloader = resource.downloader

          url = downloader.url
          name = resource.download_name
          version = resource.version

          extname = parse_extname(url)
          old_location = downloader.cache/"#{name}-#{version}#{extname}"
          new_location = downloader.cache/"#{name}--#{version}#{extname}"

          next unless old_location.file?

          if new_location.exist?
            begin
              FileUtils.rm_rf old_location
            rescue Errno::EACCES
              opoo "Could not remove #{old_location}, please do so manually."
            end
          else
            begin
              FileUtils.mv old_location, new_location
            rescue Errno::EACCES
              opoo "Could not move #{old_location} to #{new_location}, please do so manually."
            end
          end
        end
      end
    end

    def migrate_cache_entries_to_symlinks(initial_version)
      return if initial_version && initial_version > "1.7.2"

      return if ENV.key?("HOMEBREW_DISABLE_LOAD_FORMULA")

      return unless HOMEBREW_CACHE.directory?
      return if HOMEBREW_CACHE.children.empty?

      ohai "Migrating cache entries..."

      cache_entries = lambda do |path|
        if path.directory?
          path.children
              .reject(&:symlink?)
              .select(&:file?)
              .map { |child| child.basename.to_s }
              .select { |basename| basename.include?("--") }
              .map { |basename| basename.sub(/\-\-.*/, "") }
{'fraction_non_alphanumeric': 0.10769504176292874, 'fraction_numerical': 0.0028434334458859074, 'mean_word_length': 2.0454545454545454, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6326018', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1658, 'n_words': 370}