Class: Lhj::ImageOssCheckHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/lhj/helper/image_oss_check_helper.rb

Constant Summary collapse

OSS_PATH_REGEX =
/(?<var>[a-zA-Z0-9_]+)\W*=.*ml_addOssPathIgnoreGif:.*/.freeze
SD_IMAGE_OSS_REGEX =
/sd_setImageWithURL:\s*\[NSURL\s*URLWithString:(?<url>([^\[\]])*(\[[^\]]+\])*)\]/.freeze

Class Method Summary collapse

Class Method Details

.check(path, type = 'm') ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lhj/helper/image_oss_check_helper.rb', line 8

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
  result
  # show_result(result)
end

.fetch_oss_path(line) ⇒ Object



58
59
60
61
# File 'lib/lhj/helper/image_oss_check_helper.rb', line 58

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

.fetch_sd_image_url(file, line, idx, oss_var) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/lhj/helper/image_oss_check_helper.rb', line 63

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, var: oss_url }
end

.handle_file(file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lhj/helper/image_oss_check_helper.rb', line 32

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

.show_result(result) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/lhj/helper/image_oss_check_helper.rb', line 21

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