Class: Lhj::LogHelper

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

Overview

log helper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



7
8
9
# File 'lib/lhj/helper/log_helper.rb', line 7

def branch
  @branch
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/lhj/helper/log_helper.rb', line 7

def env
  @env
end

#featureObject (readonly)

Returns the value of attribute feature.



7
8
9
# File 'lib/lhj/helper/log_helper.rb', line 7

def feature
  @feature
end

#logObject (readonly)

Returns the value of attribute log.



7
8
9
# File 'lib/lhj/helper/log_helper.rb', line 7

def log
  @log
end

Class Method Details

.instanceObject



9
10
11
# File 'lib/lhj/helper/log_helper.rb', line 9

def self.instance
  @instance ||= new
end

Instance Method Details

#commit_log(env) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/lhj/helper/log_helper.rb', line 13

def commit_log(env)
  @branch = fetch_branch
  @env = fetch_env(env)
  @log = fetch_log
  @feature = fetch_feature
  save_last_commit_hash
  render_template
end

#fetch_branchObject



22
23
24
25
# File 'lib/lhj/helper/log_helper.rb', line 22

def fetch_branch
  name = Lhj::Actions.git_branch || ''
  name.split('/').last
end

#fetch_env(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/lhj/helper/log_helper.rb', line 31

def fetch_env(env)
  case env
  when :release
    'release'
  when :gray
    'gray'
  else
    'uat'
  end
end

#fetch_featureObject



27
28
29
# File 'lib/lhj/helper/log_helper.rb', line 27

def fetch_feature
  Lhj::GitBranchFeatureConfig.feature(@branch)
end

#fetch_logObject



42
43
44
45
46
47
# File 'lib/lhj/helper/log_helper.rb', line 42

def fetch_log
  from = last_commit_hash
  to = 'HEAD'
  message = Lhj::Actions.git_log_between(' %an %ar - %s;', from, to, :exclude_merges, 'short', false)
  handle_message(message)
end

#handle_message(m) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lhj/helper/log_helper.rb', line 49

def handle_message(m)
  message = m
  if !message.nil? && !message.empty?
    message = message.gsub('seconds ', '')
    message = message.gsub('minutes ', '分钟')
    message = message.gsub('hours ', '小时')
    message = message.gsub('weeks ', '')
    message = message.gsub('days ', '')
    message = message.gsub('ago ', '')
    Lhj::TeamMemberConfig.config.each do |key, value|
      message = message.gsub(key, value) if message =~ /#{key}/
    end
  else
    message = '此次打包没代码更新'
  end
  message
end

#last_commit_fileObject



81
82
83
84
# File 'lib/lhj/helper/log_helper.rb', line 81

def last_commit_file
  branch_name = @branch.gsub(%r{/}, '')
  File.join(Lhj::Config.instance.home_dir, ".#{@env}_#{branch_name}_last_commit")
end

#last_commit_hashObject



73
74
75
76
77
78
79
# File 'lib/lhj/helper/log_helper.rb', line 73

def last_commit_hash
  if File.exist?(last_commit_file)
    File.read(last_commit_file)
  else
    'HEAD~3'
  end
end

#render_templateObject



86
87
88
89
# File 'lib/lhj/helper/log_helper.rb', line 86

def render_template
  temp_str = File.read(File.join(Lhj::Config.instance.home_dir, 'git.erb'))
  Lhj::ErbFormatter::Service.new(self).render_template(temp_str)
end

#save_last_commit_hashObject



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

def save_last_commit_hash
  last_commit_hash = Lhj::Actions.last_git_commit_hash(true)
  File.delete(last_commit_file) if File.exist?(last_commit_file)
  File.write(last_commit_file, last_commit_hash)
end