Class: Lhj::TraditionalCheckHelper

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

Class Method Summary collapse

Class Method Details

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



3
4
5
6
7
8
# File 'lib/lhj/helper/traditional_check_helper.rb', line 3

def self.check(path, ignore_path, type = 'm,h,pch')
  ignore_file = File.join(ignore_path, '.checkignore')
  ignore_list = []
  ignore_list = File.readlines(ignore_file).map { |f| f.gsub(/\n/, '') } if File.exist?(ignore_file)
  check_ignore(path, type: type, ignore: ignore_list)
end

.check_ignore(path, type: 'm,h,pch', ignore: []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lhj/helper/traditional_check_helper.rb', line 10

def self.check_ignore(path, type: 'm,h,pch', ignore: [])
  result = {}
  all_files = Dir.glob("#{path}/**/*.{#{type}}").reject do |p|
    p =~ /Pods/ || ignore.include?(File.basename(p))
  end
  all_files.each do |f|
    infos = handle_file(f)
    next unless infos.length.positive?

    result[File.basename(f)] = infos
  end
  # show_result(result)
  result.keys.each_slice(10) { |a| notify(result.slice(*a)) }
end

.git_branchObject



74
75
76
77
# File 'lib/lhj/helper/traditional_check_helper.rb', line 74

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

.handle_file(file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lhj/helper/traditional_check_helper.rb', line 36

def self.handle_file(file)
  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{/\*.*\*/}
      next if line =~ %r{//} && line.gsub(%r{//.*}, '') !~ /[\u4e00-\u9fa5]/
      next if line =~ /#pragma/
      next if line =~ /log|Log|LOG|NSCAssert/
      next unless line =~ /[\u4e00-\u9fa5]/
      next unless Lhj::Trans::Helper.instance.contain_zh_hk(line.gsub(%r{//.*}, ''))
      next if line =~ /\[param setObject:.*forKey:@".*"\];/
      next if line =~ %r{//.*ignore}

      result << { idx: idx + 1, cn: line.strip, hk: Lhj::Trans::Helper.instance.trans_zh_hk_str(line).strip }
    end
  end
  result
end

.notify(result) ⇒ Object



67
68
69
70
71
72
# File 'lib/lhj/helper/traditional_check_helper.rb', line 67

def self.notify(result)
  temp = Lhj::ErbTemplateHelper.load('traditional_code_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



63
64
65
# File 'lib/lhj/helper/traditional_check_helper.rb', line 63

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

.show_result(result) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/lhj/helper/traditional_check_helper.rb', line 25

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