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_with_template('git_commit_log')
end

#current_branch(path) ⇒ Object



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

def current_branch(path)
  name = Lhj::Actions.git_branch || ''
  name.split('/').last
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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lhj/helper/log_helper.rb', line 36

def fetch_env(env)
  result = nil
  case env
  when :release
    result = 'release'
  when :gray
    result = 'gray'
  when :uat
    result = 'uat'
  end
  result
end

#fetch_featureObject



32
33
34
# File 'lib/lhj/helper/log_helper.rb', line 32

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

#fetch_logObject



49
50
51
52
53
54
# File 'lib/lhj/helper/log_helper.rb', line 49

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lhj/helper/log_helper.rb', line 56

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



88
89
90
91
# File 'lib/lhj/helper/log_helper.rb', line 88

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



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

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

#render_with_template(template_name = 'git') ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/lhj/helper/log_helper.rb', line 93

def render_with_template(template_name = 'git')
  temp = Lhj::ErbTemplateHelper.load(template_name)
  vars = { branch: @branch,
           env: @env,
           feature: @feature,
           log: @log }
  Lhj::ErbTemplateHelper.render(temp, vars, '-')
end

#save_last_commit_hashObject



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

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