Class: Lhj::IosApiAvailableCheckHelper

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

Constant Summary collapse

API_AVAILABLE_KEY_BEGIN_REGEX =
/@available/.freeze
BLOCK_KEY_BEGIN_REGEX =
/\^/.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
20
21
22
23
24
25
26
# File 'lib/lhj/helper/ios_api_available_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|
    file_name = File.basename(f)
    wrapper_map = find_available_wrapper(f)
    # block循环引用提醒
    # wrapper_map[:blocks].each { |l| puts l[:line] }
    handle_block_notify(file_name, wrapper_map[:blocks]) if wrapper_map[:blocks].length.positive?
    # 处理高版本api提示
    infos = handle_available_file(f, wrapper_map[:avails])
    result[file_name] = infos if infos.length.positive?
  end
  # result
  # show_result(result)
  result.keys.each_slice(10) { |a| notify(result.slice(*a)) }
end

.find_available_wrapper(file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 38

def self.find_available_wrapper(file)
  available_lines = []
  block_lines = []
  File.open(file, 'r') do |f|
    multi_comment = false
    strong_key = false
    available_idx = 0
    block_idx = 0
    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*//} && line =~ %r{[^\S]+//}

      if line =~ API_AVAILABLE_KEY_BEGIN_REGEX || available_idx.positive?
        available_idx += 1 if line =~ /\{/
        available_idx -= 1 if line =~ /\}/
        available_lines << { idx: idx }
      end
      # 此行包含^符号,且不是方法声明,且不是主线程方法
      if (line =~ BLOCK_KEY_BEGIN_REGEX && line !~ /^\s*[+-]/ && line !~ /dispatch_get_main_queue/) || block_idx.positive?
        line_position = block_idx.zero? ? :start : :process
        strong_key = false if block_idx.zero?
        strong_key = true if line =~ /@strongify/
        block_idx += 1 if line =~ /\{/
        block_idx -= 1 if line =~ /\}/
        line_position = :end if block_idx.zero?
        strong_key = false if block_idx.zero?
        block_lines << { idx: idx, line: line, position: line_position, strong: strong_key }
      end
    end
  end
  { avails: available_lines, blocks: block_lines }
end

.git_branchObject



144
145
146
147
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 144

def self.git_branch
  branch ||= Lhj::LogHelper.instance.fetch_branch
  branch
end

.handle_available_file(file, wrapper_lines) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 98

def self.handle_available_file(file, wrapper_lines)
  result = []
  File.open(file, 'r') do |f|
    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*//}
      # 1. 白名单查找
      next unless match_white_list_reg(line)
      # 2. 找到的行是否已available代码块
      next if wrapper_lines.length.positive? && wrapper_lines.any? { |w| w[:idx] == idx }

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

.handle_block_notify(file_name, wrapper_lines) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 77

def self.handle_block_notify(file_name, wrapper_lines)
  arr = wrapper_lines.filter { |l| l[:position] != :start }
  arr.filter! { |l| l[:position] != :end }
  arr.filter! { |l| !l[:strong] }
  arr.filter! { |l| l[:line] =~ /\W+self\W+/ || l[:line] =~ /\W+_/ }
  arr.filter! { |l| l[:line] !~ /\^/ }
  arr.filter! { |l| l[:line] !~ /__weak/ }
  arr.filter! { |l| l[:line] !~ /__strong/ }
  arr.filter! { |l| l[:line] !~ /mas/ }
  arr.filter! { |l| l[:line] !~ /equalTo/ }
  arr.filter! { |l| l[:line] !~ %r{//} }
  return if arr.empty?

  puts "源文件:#{file_name}"
  arr.each do |l|
    puts "#{l[:idx] + 1}行:"
    puts l[:line].strip
  end
  puts "\n"
end

.load_white_api_listObject



127
128
129
130
131
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 127

def self.load_white_api_list
  require 'yaml'
  yaml_file = File.join(Lhj::Config.instance.home_dir, 'ios_available_api_white_list.yml')
  YAML.safe_load(File.open(yaml_file))
end

.match_white_list_reg(line) ⇒ Object



122
123
124
125
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 122

def self.match_white_list_reg(line)
  @regs ||= load_white_api_list
  @regs.any? { |r| /#{r}/ =~ line }
end

.notify(result) ⇒ Object



137
138
139
140
141
142
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 137

def self.notify(result)
  temp = Lhj::ErbTemplateHelper.load('ios_avaliable_api_notify')
  temp_result = Lhj::ErbTemplateHelper.render(temp, { result: result, branch: git_branch }, '-')
  puts temp_result
  Lhj::Dingtalk.post_message_robot(robot_url, 'check code', temp_result)
end

.robot_urlObject



133
134
135
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 133

def self.robot_url
  'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
end

.show_result(result) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/lhj/helper/ios_api_available_check_helper.rb', line 28

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