lhj-tools 0.1.18 → 0.1.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lhj/action/sh_helper.rb +3 -3
- data/lib/lhj/command/yapi/formatters/template/git.erb +1 -0
- data/lib/lhj/command.rb +0 -2
- data/lib/lhj/env.rb +9 -0
- data/lib/lhj/helper/git_helper.rb +145 -0
- data/lib/lhj/helper/log_helper.rb +68 -0
- data/lib/lhj/helper/pgyer_helper.rb +3 -0
- data/lib/lhj/tools/version.rb +1 -1
- data/lib/lhj/tools.rb +5 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d0b9e563d0ebfe15b800560545a987a274f9787522297f7721b2012ff132594
|
4
|
+
data.tar.gz: 61655a300019b6dc713a71053482ba03c5ac2bee09ccc4be8c3926217a38dada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e56808aae097aa5aaab4204aae1ea2bce2306c0aa8607be0d0ec57046c44c98576aa3d1a4c8313792f0764e37dfa0c7dd6d70f02a084fd7ea4eb3ef79ecd82d
|
7
|
+
data.tar.gz: 88c51758d568b44ad2ea8bcd4bae0572ade581ee87a146100afa9cfce6f53665dd2bd9a864e531f4b50b9d3b8af2c89236d2c406a142ac1dd1935b61339d08fa
|
data/lib/lhj/action/sh_helper.rb
CHANGED
@@ -119,17 +119,17 @@ module Lhj
|
|
119
119
|
|
120
120
|
# Optional initial environment Hash
|
121
121
|
if args.first.kind_of?(Hash)
|
122
|
-
command = args.shift.map { |k, v| "#{k}=#{v.
|
122
|
+
command = args.shift.map { |k, v| "#{k}=#{v.to_s}" }.join(" ") + " "
|
123
123
|
end
|
124
124
|
|
125
125
|
# Support [ "/usr/local/bin/foo", "foo" ], "-x", ...
|
126
126
|
if args.first.kind_of?(Array)
|
127
|
-
command += args.shift.first.
|
127
|
+
command += args.shift.first.to_s + " " + args.join(' ')
|
128
128
|
command.chomp!(" ")
|
129
129
|
elsif args.count == 1 && args.first.kind_of?(String)
|
130
130
|
command += args.first
|
131
131
|
else
|
132
|
-
command += args.
|
132
|
+
command += args.join(' ')
|
133
133
|
end
|
134
134
|
|
135
135
|
command
|
@@ -0,0 +1 @@
|
|
1
|
+
【代码分支:<%= @branch %>】【服务环境:<%= @env %>】-【最近更新日志】<%= @log %>
|
data/lib/lhj/command.rb
CHANGED
data/lib/lhj/env.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
module Lhj
|
2
|
+
module Actions
|
3
|
+
GIT_MERGE_COMMIT_FILTERING_OPTIONS = [:include_merges, :exclude_merges, :only_include_merges].freeze
|
4
|
+
|
5
|
+
module SharedValues
|
6
|
+
GIT_BRANCH_ENV_VARS = %w(GIT_BRANCH BRANCH_NAME TRAVIS_BRANCH BITRISE_GIT_BRANCH CI_BUILD_REF_NAME CI_COMMIT_REF_NAME WERCKER_GIT_BRANCH BUILDKITE_BRANCH APPCENTER_BRANCH).freeze
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.git_log_between(pretty_format, from, to, merge_commit_filtering, date_format = nil, ancestry_path)
|
10
|
+
command = %w(git log)
|
11
|
+
command << "--pretty=#{pretty_format}"
|
12
|
+
command << "--date=#{date_format}" if date_format
|
13
|
+
command << '--ancestry-path' if ancestry_path
|
14
|
+
command << "#{from}...#{to}"
|
15
|
+
command << git_log_merge_commit_filtering_option(merge_commit_filtering)
|
16
|
+
# "*command" syntax expands "command" array into variable arguments, which
|
17
|
+
# will then be individually shell-escaped by Actions.sh.
|
18
|
+
Actions.sh(*command.compact, log: false).chomp
|
19
|
+
rescue
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.git_log_last_commits(pretty_format, commit_count, merge_commit_filtering, date_format = nil, ancestry_path)
|
24
|
+
command = %w(git log)
|
25
|
+
command << "--pretty=#{pretty_format}"
|
26
|
+
command << "--date=#{date_format}" if date_format
|
27
|
+
command << '--ancestry-path' if ancestry_path
|
28
|
+
command << '-n' << commit_count.to_s
|
29
|
+
command << git_log_merge_commit_filtering_option(merge_commit_filtering)
|
30
|
+
Actions.sh(*command.compact, log: false).chomp
|
31
|
+
rescue
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.last_git_tag_hash(tag_match_pattern = nil)
|
36
|
+
tag_pattern_param = tag_match_pattern ? "=#{tag_match_pattern}" : ''
|
37
|
+
Actions.sh('git', 'rev-list', "--tags#{tag_pattern_param}", '--max-count=1').chomp
|
38
|
+
rescue
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.last_git_tag_name(match_lightweight = true, tag_match_pattern = nil)
|
43
|
+
hash = last_git_tag_hash(tag_match_pattern)
|
44
|
+
# If hash is nil (command fails), "git describe" command below will still
|
45
|
+
# run and provide some output, although it's definitely not going to be
|
46
|
+
# anything reasonably expected. Bail out early.
|
47
|
+
return unless hash
|
48
|
+
|
49
|
+
command = %w(git describe)
|
50
|
+
command << '--tags' if match_lightweight
|
51
|
+
command << hash
|
52
|
+
command << '--match' if tag_match_pattern
|
53
|
+
command << tag_match_pattern if tag_match_pattern
|
54
|
+
Actions.sh(*command.compact, log: false).chomp
|
55
|
+
rescue
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.last_git_commit_dict
|
60
|
+
return nil if last_git_commit_formatted_with('%an').nil?
|
61
|
+
|
62
|
+
{
|
63
|
+
author: last_git_commit_formatted_with('%an'),
|
64
|
+
author_email: last_git_commit_formatted_with('%ae'),
|
65
|
+
message: last_git_commit_formatted_with('%B'),
|
66
|
+
commit_hash: last_git_commit_formatted_with('%H'),
|
67
|
+
abbreviated_commit_hash: last_git_commit_formatted_with('%h')
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
# Gets the last git commit information formatted into a String by the provided
|
72
|
+
# pretty format String. See the git-log documentation for valid format placeholders
|
73
|
+
def self.last_git_commit_formatted_with(pretty_format, date_format = nil)
|
74
|
+
command = %w(git log -1)
|
75
|
+
command << "--pretty=#{pretty_format}"
|
76
|
+
command << "--date=#{date_format}" if date_format
|
77
|
+
Actions.sh(*command.compact, log: false).chomp
|
78
|
+
rescue
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
|
82
|
+
# @deprecated Use <tt>git_author_email</tt> instead
|
83
|
+
# Get the author email of the last git commit
|
84
|
+
# <b>DEPRECATED:</b> Use <tt>git_author_email</tt> instead.
|
85
|
+
def self.git_author
|
86
|
+
UI.deprecated('`git_author` is deprecated. Please use `git_author_email` instead.')
|
87
|
+
git_author_email
|
88
|
+
end
|
89
|
+
|
90
|
+
# Get the author email of the last git commit
|
91
|
+
def self.git_author_email
|
92
|
+
s = last_git_commit_formatted_with('%ae')
|
93
|
+
return s if s.to_s.length > 0
|
94
|
+
return nil
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns the unwrapped subject and body of the last commit
|
98
|
+
# <b>DEPRECATED:</b> Use <tt>last_git_commit_message</tt> instead.
|
99
|
+
def self.last_git_commit
|
100
|
+
last_git_commit_message
|
101
|
+
end
|
102
|
+
|
103
|
+
# Returns the unwrapped subject and body of the last commit
|
104
|
+
def self.last_git_commit_message
|
105
|
+
s = (last_git_commit_formatted_with('%B') || "").strip
|
106
|
+
return s if s.to_s.length > 0
|
107
|
+
nil
|
108
|
+
end
|
109
|
+
|
110
|
+
# Get the hash of the last commit
|
111
|
+
def self.last_git_commit_hash(short)
|
112
|
+
format_specifier = short ? '%h' : '%H'
|
113
|
+
string = last_git_commit_formatted_with(format_specifier).to_s
|
114
|
+
return string unless string.empty?
|
115
|
+
return nil
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns the current git branch, or "HEAD" if it's not checked out to any branch
|
119
|
+
# Can be replaced using the environment variable `GIT_BRANCH`
|
120
|
+
def self.git_branch
|
121
|
+
env_name = SharedValues::GIT_BRANCH_ENV_VARS.find { |env_var| Lhj::Env.truthy?(env_var) }
|
122
|
+
ENV.fetch(env_name.to_s) do
|
123
|
+
# Rescues if not a git repo or no commits in a git repo
|
124
|
+
begin
|
125
|
+
Actions.sh("git rev-parse --abbrev-ref HEAD", log: false).chomp
|
126
|
+
rescue => err
|
127
|
+
UI.verbose("Error getting git branch: #{err.message}")
|
128
|
+
nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
private_class_method
|
134
|
+
def self.git_log_merge_commit_filtering_option(merge_commit_filtering)
|
135
|
+
case merge_commit_filtering
|
136
|
+
when :exclude_merges
|
137
|
+
"--no-merges"
|
138
|
+
when :only_include_merges
|
139
|
+
"--merges"
|
140
|
+
when :include_merges
|
141
|
+
nil
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Lhj
|
2
|
+
# log helper
|
3
|
+
class LogHelper
|
4
|
+
attr_reader :branch, :env, :log
|
5
|
+
|
6
|
+
def self.instance
|
7
|
+
@instance ||= new
|
8
|
+
end
|
9
|
+
|
10
|
+
def commit_log(env)
|
11
|
+
@branch = fetch_branch
|
12
|
+
@env = fetch_env(env)
|
13
|
+
@log = fetch_log
|
14
|
+
save_last_commit_hash
|
15
|
+
render_template
|
16
|
+
end
|
17
|
+
|
18
|
+
def fetch_branch
|
19
|
+
Lhj::Actions.git_branch || ''
|
20
|
+
end
|
21
|
+
|
22
|
+
def fetch_env(env)
|
23
|
+
case env
|
24
|
+
when :release
|
25
|
+
'release'
|
26
|
+
when :gray
|
27
|
+
'gray'
|
28
|
+
else
|
29
|
+
'uat'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch_log
|
34
|
+
from = last_commit_hash
|
35
|
+
to = 'HEAD'
|
36
|
+
changelog = Lhj::Actions.git_log_between(' %an, %ar - %s;', from, to, :exclude_merges, 'short', false)
|
37
|
+
changelog ||= ''
|
38
|
+
changelog&.gsub("\n\n", "\n")
|
39
|
+
changelog = changelog.gsub('seconds ', '秒').gsub('minutes ', '分钟').gsub('hours ', '小时').gsub('days ', '天').gsub('ago ', '前')
|
40
|
+
changelog = '此次打包没代码更新' if changelog.empty?
|
41
|
+
changelog
|
42
|
+
end
|
43
|
+
|
44
|
+
def save_last_commit_hash
|
45
|
+
last_commit_hash = Lhj::Actions.last_git_commit_hash(true)
|
46
|
+
File.delete(last_commit_file) if File.exist?(last_commit_file)
|
47
|
+
File.write(last_commit_file, last_commit_hash)
|
48
|
+
end
|
49
|
+
|
50
|
+
def last_commit_hash
|
51
|
+
if File.exist?(last_commit_file)
|
52
|
+
File.read(last_commit_file)
|
53
|
+
else
|
54
|
+
'HEAD~3'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def last_commit_file
|
59
|
+
File.join(Lhj::Config.instance.home_dir, ".#{@branch}_last_commit")
|
60
|
+
end
|
61
|
+
|
62
|
+
def render_template
|
63
|
+
temp_str = File.read(File.join(Lhj::Config.instance.home_dir, 'git.erb'))
|
64
|
+
Lhj::ErbFormatter::Service.new(self).render_template(temp_str)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
data/lib/lhj/tools/version.rb
CHANGED
data/lib/lhj/tools.rb
CHANGED
@@ -3,10 +3,15 @@ require_relative "tools/version"
|
|
3
3
|
require 'colored'
|
4
4
|
|
5
5
|
module Lhj
|
6
|
+
require 'lhj/env'
|
7
|
+
require 'lhj/ui/ui'
|
6
8
|
require 'lhj/config'
|
7
9
|
require 'lhj/helper/pgyer_helper'
|
8
10
|
require 'lhj/helper/dingtalk_helper'
|
9
11
|
require 'lhj/helper/tb_helper'
|
12
|
+
require 'lhj/action/sh_helper'
|
13
|
+
require 'lhj/helper/git_helper'
|
14
|
+
require 'lhj/helper/log_helper'
|
10
15
|
|
11
16
|
class Error < StandardError; end
|
12
17
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhj-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.22
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lihaijian
|
@@ -321,13 +321,17 @@ files:
|
|
321
321
|
- lib/lhj/command/yapi/formatters/base.rb
|
322
322
|
- lib/lhj/command/yapi/formatters/command_context.rb
|
323
323
|
- lib/lhj/command/yapi/formatters/service.rb
|
324
|
+
- lib/lhj/command/yapi/formatters/template/git.erb
|
324
325
|
- lib/lhj/command/yapi/formatters/template/model.erb
|
325
326
|
- lib/lhj/command/yapi/formatters/template/pgyer.erb
|
326
327
|
- lib/lhj/command/yapi/formatters/template/yapi.erb
|
327
328
|
- lib/lhj/config.rb
|
329
|
+
- lib/lhj/env.rb
|
328
330
|
- lib/lhj/helper/dingtalk_config.rb
|
329
331
|
- lib/lhj/helper/dingtalk_helper.rb
|
332
|
+
- lib/lhj/helper/git_helper.rb
|
330
333
|
- lib/lhj/helper/local_config.rb
|
334
|
+
- lib/lhj/helper/log_helper.rb
|
331
335
|
- lib/lhj/helper/oss_config.rb
|
332
336
|
- lib/lhj/helper/oss_helper.rb
|
333
337
|
- lib/lhj/helper/pgyer_config.rb
|