Sha256: fc78ec19050d694d8084e5dd5d7ef1279a7752794f7ca9efdb78d074245e4cd0

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module Lhj
  class ImageOssCheckHelper

    OSS_PATH_REGEX = /(?<var>[a-zA-Z0-9_]+)\W*=.*ml_addOssPathIgnoreGif:.*/.freeze

    SD_IMAGE_OSS_REGEX = /sd_setImageWithURL:\s*\[NSURL\s*URLWithString:(?<url>[^\]]+)\]/.freeze

    def self.check(path, type = 'm')
      all_files = Dir.glob("#{path}/**/*.{#{type}}").reject do |p|
        p =~ /Pods/
      end
      result = {}
      all_files.each do |f|
        infos = handle_file(f)
        result[File.basename(f)] = infos if infos.length.positive?
      end
      show_result(result)
    end

    def self.show_result(result)
      result.each do |k, v|
        puts k
        v.each do |o|
          puts "第#{o[:idx]}行:"
          puts o[:line]
        end
      end
    end

    def self.handle_file(file)
      result = []
      File.open(file, 'r') do |f|
        oss_var = nil
        multi_comment = false
        f.readlines.each_with_index do |line, idx|
          multi_comment = true if line =~ %r{/\*} && line !~ %r{\*/} && !multi_comment
          if line !~ %r{/\*} && line =~ %r{\*/} && multi_comment
            multi_comment = false
            next
          end

          next if multi_comment
          next if line =~ %r{\s*//}

          oss_var = fetch_oss_path(line) if line =~ OSS_PATH_REGEX
          next unless line =~ SD_IMAGE_OSS_REGEX

          oss_info = fetch_sd_image_url(file, line, idx, oss_var)
          result << oss_info if oss_info
          oss_var = nil if oss_info
        end
      end
      result
    end

    def self.fetch_oss_path(line)
      oss_match = OSS_PATH_REGEX.match(line)
      oss_match[:var]
    end

    def self.fetch_sd_image_url(file, line, idx, oss_var)
      url_match = SD_IMAGE_OSS_REGEX.match(line)
      oss_url = url_match[:url]

      return nil if oss_var && oss_var == oss_url
      return nil if oss_url =~ /ml_addOssPathIgnoreGif/
      return nil if oss_url =~ /x-oss-process=style/

      { idx: idx + 1, line: line.strip }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lhj-tools-0.2.58 lib/lhj/helper/image_oss_check_helper.rb