lhj-tools 0.1.4 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lhj/action/sh_helper.rb +138 -0
  3. data/lib/lhj/command/config/info.rb +47 -0
  4. data/lib/lhj/command/config.rb +11 -0
  5. data/lib/lhj/command/file_path.rb +20 -0
  6. data/lib/lhj/command/head_import.rb +19 -3
  7. data/lib/lhj/command/http.rb +14 -0
  8. data/lib/lhj/command/init.rb +2 -2
  9. data/lib/lhj/command/local/fetch.rb +1 -1
  10. data/lib/lhj/command/local/filter.rb +1 -1
  11. data/lib/lhj/command/local/local.rb +1 -1
  12. data/lib/lhj/command/local/local_upload.rb +1 -1
  13. data/lib/lhj/command/local/micro_service.rb +1 -1
  14. data/lib/lhj/command/oss/del.rb +18 -5
  15. data/lib/lhj/command/oss/list.rb +6 -2
  16. data/lib/lhj/command/oss/upload.rb +9 -5
  17. data/lib/lhj/command/refactor_rename.rb +18 -2
  18. data/lib/lhj/command/rename_image.rb +1 -1
  19. data/lib/lhj/command/sync_pod_repo.rb +151 -0
  20. data/lib/lhj/command/trans.rb +1 -1
  21. data/lib/lhj/command/yapi.rb +15 -14
  22. data/lib/lhj/command.rb +33 -0
  23. data/lib/lhj/tools/version.rb +1 -1
  24. data/lib/lhj/tools.rb +1 -0
  25. data/lib/lhj/tree/directory_renderer.rb +45 -0
  26. data/lib/lhj/tree/hash_walker.rb +70 -0
  27. data/lib/lhj/tree/node.rb +90 -0
  28. data/lib/lhj/tree/number_renderer.rb +30 -0
  29. data/lib/lhj/tree/path_walker.rb +107 -0
  30. data/lib/lhj/tree/tree.rb +112 -0
  31. data/lib/lhj/ui/errors/lhj_common_error.rb +19 -0
  32. data/lib/lhj/ui/errors/lhj_crash.rb +11 -0
  33. data/lib/lhj/ui/errors/lhj_error.rb +25 -0
  34. data/lib/lhj/ui/errors/lhj_exception.rb +19 -0
  35. data/lib/lhj/ui/errors/lhj_shell_error.rb +11 -0
  36. data/lib/lhj/ui/errors.rb +1 -0
  37. data/lib/lhj/ui/implementations/shell.rb +148 -0
  38. data/lib/lhj/ui/interface.rb +205 -0
  39. data/lib/lhj/ui/ui.rb +26 -0
  40. metadata +77 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74f8ff17bf0bbd3cbb38c92aa203ecb3983da440b5bb158fa1af980adc9fc452
4
- data.tar.gz: c43f7f0ad99e94fd7602e64aaf1246ca1244c588b53794960b937a02ce4e6783
3
+ metadata.gz: 2ccdf622ec71e3f0f9b8c090e0587ecde300a61d05cccde837d3730b9b0fdebe
4
+ data.tar.gz: 92d0a661cf877bce61751e137f764155244cc3c8f629c910556c342b0766ae6d
5
5
  SHA512:
6
- metadata.gz: b200a275276a8b419427203042365a4641920df44420637d4be89d5f588a173d9a6a944fe65f00d2d956262aae0ebe3bf379dc2ce63afec749401923294f6f3f
7
- data.tar.gz: c3bb0f9d4cce6b7f789a00856efe2398274cadb9cbee4b8b5d1b7af6a7c6398f02c33422dff1527f1edeefe3353d65f341d5e637a6a87ead4e283a7e410c4e8c
6
+ metadata.gz: 6fd7f9f518499f766beb7afd778e1542d61c40522958ff02ffdf5f3d167ed1260ea5e915de33848e0b2fbfc042313bfd96920f5436c72648e5222eddeda76d6b
7
+ data.tar.gz: b9df71c7bb5d7cdb63e18319fe23278d08d13d9a39d94133be4cf99cb8e3cc4f7f04f4f4cce73f528dbe88f757dcd5c0e87fb04a302cd2dd56939a78d89fcd75
@@ -0,0 +1,138 @@
1
+ require "open3"
2
+
3
+ module Lhj
4
+ module Actions
5
+ # Execute a shell command
6
+ # This method will output the string and execute it
7
+ # Just an alias for sh_no_action
8
+ # When running this in tests, it will return the actual command instead of executing it
9
+ # @param log [Boolean] should fastlane print out the executed command
10
+ # @param error_callback [Block] a callback invoked with the command output if there is a non-zero exit status
11
+ def self.sh(*command, log: true, error_callback: nil, &b)
12
+ sh_control_output(*command, print_command: log, print_command_output: log, error_callback: error_callback, &b)
13
+ end
14
+
15
+ def self.sh_no_action(*command, log: true, error_callback: nil, &b)
16
+ sh_control_output(*command, print_command: log, print_command_output: log, error_callback: error_callback, &b)
17
+ end
18
+
19
+ def self.sh_enabled?
20
+ true
21
+ end
22
+
23
+ # @param command The command to be executed (variadic)
24
+ # @param print_command [Boolean] Should we print the command that's being executed
25
+ # @param print_command_output [Boolean] Should we print the command output during execution
26
+ # @param error_callback [Block] A block that's called if the command exits with a non-zero status
27
+ # @yield [status, result, cmd] The return status of the command, all output from the command and an equivalent shell command
28
+ # @yieldparam [Process::Status] status A Process::Status indicating the status of the completed command
29
+ # @yieldparam [String] result The complete output to stdout and stderr of the completed command
30
+ # @yieldparam [String] cmd A shell command equivalent to the arguments passed
31
+ # rubocop: disable Metrics/PerceivedComplexity
32
+ def self.sh_control_output(*command, print_command: true, print_command_output: true, error_callback: nil)
33
+ print_command = print_command_output = true if $troubleshoot
34
+ # Set the encoding first, the user might have set it wrong
35
+ previous_encoding = [Encoding.default_external, Encoding.default_internal]
36
+ Encoding.default_external = Encoding::UTF_8
37
+ Encoding.default_internal = Encoding::UTF_8
38
+
39
+ # Workaround to support previous Fastlane syntax.
40
+ # This has some limitations. For example, it requires the caller to shell escape
41
+ # everything because of usages like ["ls -la", "/tmp"] instead of ["ls", "-la", "/tmp"].
42
+ command = [command.first.join(" ")] if command.length == 1 && command.first.kind_of?(Array)
43
+
44
+ shell_command = shell_command_from_args(*command)
45
+ UI.command(shell_command) if print_command
46
+
47
+ result = ''
48
+ exit_status = nil
49
+ if sh_enabled?
50
+ # The argument list is passed directly to Open3.popen2e, which
51
+ # handles the variadic argument list in the same way as Kernel#spawn.
52
+ # (http://ruby-doc.org/core-2.4.2/Kernel.html#method-i-spawn) or
53
+ # Process.spawn (http://ruby-doc.org/core-2.4.2/Process.html#method-c-spawn).
54
+ #
55
+ # sh "ls -la /Applications/Xcode\ 7.3.1.app"
56
+ # sh "ls", "-la", "/Applications/Xcode 7.3.1.app"
57
+ # sh({ "FOO" => "Hello" }, "echo $FOO")
58
+ Open3.popen2e(*command) do |stdin, io, thread|
59
+ io.sync = true
60
+ io.each do |line|
61
+ UI.command_output(line.strip) if print_command_output
62
+ result << line
63
+ end
64
+ exit_status = thread.value
65
+ end
66
+
67
+ # Checking Process::Status#exitstatus instead of #success? makes for more
68
+ # testable code. (Tests mock exitstatus only.) This is also consistent
69
+ # with previous implementations of sh and... probably portable to all
70
+ # relevant platforms.
71
+ if exit_status.exitstatus != 0
72
+ message = if print_command
73
+ "Exit status of command '#{shell_command}' was #{exit_status.exitstatus} instead of 0."
74
+ else
75
+ "Shell command exited with exit status #{exit_status.exitstatus} instead of 0."
76
+ end
77
+ message += "\n#{result}" if print_command_output
78
+
79
+ if error_callback || block_given?
80
+ UI.error(message)
81
+ # block notified below, on success or failure
82
+ error_callback && error_callback.call(result)
83
+ else
84
+ UI.shell_error!(message)
85
+ end
86
+ end
87
+ else
88
+ result << shell_command # only for the tests
89
+ end
90
+
91
+ if block_given?
92
+ # Avoid yielding nil in tests. $? will be meaningless, but calls to
93
+ # it will not crash. There is no Process::Status.new. The alternative
94
+ # is to move this inside the sh_enabled? check and not yield in tests.
95
+ return yield(exit_status || $?, result, shell_command)
96
+ end
97
+ result
98
+ rescue => ex
99
+ raise ex
100
+ ensure
101
+ Encoding.default_external = previous_encoding.first
102
+ Encoding.default_internal = previous_encoding.last
103
+ end
104
+ # rubocop: enable Metrics/PerceivedComplexity
105
+
106
+ # Used to produce a shell command string from a list of arguments that may
107
+ # be passed to methods such as Kernel#system, Kernel#spawn and Open3.popen2e
108
+ # in order to print the command to the terminal. The same *args are passed
109
+ # directly to a system call (Open3.popen2e). This interpretation is not
110
+ # used when executing a command.
111
+ #
112
+ # @param args Any number of arguments used to construct a command
113
+ # @raise [ArgumentError] If no arguments passed
114
+ # @return [String] A shell command representing the arguments passed in
115
+ def self.shell_command_from_args(*args)
116
+ raise ArgumentError, "sh requires at least one argument" unless args.count > 0
117
+
118
+ command = ""
119
+
120
+ # Optional initial environment Hash
121
+ if args.first.kind_of?(Hash)
122
+ command = args.shift.map { |k, v| "#{k}=#{v.shellescape}" }.join(" ") + " "
123
+ end
124
+
125
+ # Support [ "/usr/local/bin/foo", "foo" ], "-x", ...
126
+ if args.first.kind_of?(Array)
127
+ command += args.shift.first.shellescape + " " + args.shelljoin
128
+ command.chomp!(" ")
129
+ elsif args.count == 1 && args.first.kind_of?(String)
130
+ command += args.first
131
+ else
132
+ command += args.shelljoin
133
+ end
134
+
135
+ command
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,47 @@
1
+ require 'highline'
2
+ require 'lhj/config'
3
+
4
+ module Lhj
5
+ class Command
6
+ class Config < Command
7
+ # show config info
8
+ class Info < Config
9
+ self.summary = '查看配置信息'
10
+
11
+ def initialize(argv)
12
+ @cli = HighLine.new
13
+ super
14
+ end
15
+
16
+ def handle
17
+ show_config
18
+ end
19
+
20
+ def show_config
21
+ config_arr = Dir.glob("#{config_dir}/**/*.yml")
22
+ config_arr.each_index { |i| puts "#{i}.#{File.basename(config_arr[i])}".yellow }
23
+ idx = ask_which_one
24
+ config_file = config_arr[idx]
25
+ show_yaml(config_file)
26
+ end
27
+
28
+ def show_yaml(file)
29
+ table_rows = []
30
+ yaml = YAML.load_file(file)
31
+ yaml.each { |key, value| table_rows << [key, value] }
32
+ title = File.basename(file).to_s
33
+ table = Terminal::Table.new title: title, headings: %w[键 值], rows: table_rows
34
+ puts table
35
+ end
36
+
37
+ def ask_which_one
38
+ @cli.ask('查看哪个配置: '.green).strip.to_i
39
+ end
40
+
41
+ def config_dir
42
+ Lhj::Config.instance.home_dir
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,11 @@
1
+ require 'lhj/command/config/info'
2
+
3
+ module Lhj
4
+ class Command
5
+ # sync config
6
+ class Config < Command
7
+ self.summary = '配置操作'
8
+ self.abstract_command = true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require 'lhj/tree/tree'
2
+
3
+ module Lhj
4
+ class Command
5
+ # show file path
6
+ class Filepath < Command
7
+ self.summary = '显示文件目录'
8
+
9
+ def initialize(argv)
10
+ @current_path = argv.shift_argument || Dir.pwd
11
+ super
12
+ end
13
+
14
+ def handle
15
+ tree = Lhj::Tree.new(@current_path)
16
+ puts tree.render
17
+ end
18
+ end
19
+ end
20
+ end
@@ -5,7 +5,8 @@ require 'fileutils'
5
5
 
6
6
  module Lhj
7
7
  class Command
8
- class Import < Command
8
+ # modify header
9
+ class HeaderImport < Command
9
10
  self.summary = '更改头文件引入'
10
11
 
11
12
  def initialize(argv)
@@ -13,14 +14,16 @@ module Lhj
13
14
  @framework = argv.option('framework')
14
15
  @header_map = {}
15
16
  @header_folder_map = {}
17
+ @modify_files = []
16
18
  super
17
19
  end
18
20
 
19
- def run
21
+ def handle
20
22
  generate_header_map
21
23
  # say_header_map
22
24
  # find_all_sub_folder
23
25
  update_source_header
26
+ # print_modify_info
24
27
  end
25
28
 
26
29
  # @return [@header_map]
@@ -109,7 +112,10 @@ module Lhj
109
112
  result = line
110
113
  if line =~ /#import/
111
114
  ma = find_head_key(line)
112
- result = line.gsub(ma[0], @header_map[ma[0].to_sym] || ma[0]) if ma && !exist_in_file(file, ma[0])
115
+ if ma && !exist_in_file(file, ma[0])
116
+ result = line.gsub(ma[0], @header_map[ma[0].to_sym] || ma[0])
117
+ # @modify_files << file unless @modify_files.include?(file)
118
+ end
113
119
  end
114
120
  result
115
121
  end
@@ -123,6 +129,16 @@ module Lhj
123
129
  header_reg = /"\D*.h"/
124
130
  line.match(header_reg)
125
131
  end
132
+
133
+ def print_modify_info
134
+ rows = []
135
+ @modify_files.each do |file|
136
+ rows << [File.basename(file), File.absolute_path(file)]
137
+ end
138
+ title = "修改了#{rows.count}个文件"
139
+ table = Terminal::Table.new title: title, headings: %w[文件 路径], rows: rows
140
+ puts table
141
+ end
126
142
  end
127
143
  end
128
144
  end
@@ -0,0 +1,14 @@
1
+ require 'excon'
2
+
3
+ module Lhj
4
+ class Command
5
+ # http client
6
+ class Http < Command
7
+
8
+ def handle
9
+ response = Excon.get('http://www.baidu.com')
10
+ puts response.body
11
+ end
12
+ end
13
+ end
14
+ end
@@ -11,7 +11,7 @@ module Lhj
11
11
  self.summary = '初始化控件'
12
12
  self.description = '使用工具前先执行`lhj init`'
13
13
 
14
- def run
14
+ def handle
15
15
  FileUtils.mkdir_p(target_folder) unless File.exist?(target_folder)
16
16
  download_file
17
17
  end
@@ -36,7 +36,7 @@ module Lhj
36
36
  end
37
37
 
38
38
  def file_name_with_url(url)
39
- url.scan(%r{(/.[^/]*)}).flatten.last
39
+ url.scan(%r{/([^/]+)}).flatten.last
40
40
  end
41
41
 
42
42
  def download_file
@@ -22,7 +22,7 @@ module Lhj
22
22
  super
23
23
  end
24
24
 
25
- def run
25
+ def handle
26
26
  handle_files
27
27
  gen_csv
28
28
  # update_source_header
@@ -16,7 +16,7 @@ module Lhj
16
16
  super
17
17
  end
18
18
 
19
- def run
19
+ def handle
20
20
  fetch_keys
21
21
  read_csv
22
22
  gen_csv
@@ -39,7 +39,7 @@ module Lhj
39
39
  super
40
40
  end
41
41
 
42
- def run
42
+ def handle
43
43
  down_load_csv_file if need_download
44
44
  read_csv
45
45
  if @key_map.keys.length.positive?
@@ -28,7 +28,7 @@ module Lhj
28
28
  "csv/#{Time.now.to_i}/#{file_name}"
29
29
  end
30
30
 
31
- def run
31
+ def handle
32
32
  csv_files = File.join(@pwd_path, '**', csv_file_name)
33
33
  Dir.glob(csv_files).each do |f|
34
34
  file_name = File.basename(f)
@@ -15,7 +15,7 @@ module Lhj
15
15
  super
16
16
  end
17
17
 
18
- def run
18
+ def handle
19
19
  read_csv
20
20
  update_source
21
21
  end
@@ -8,27 +8,40 @@ module Lhj
8
8
  self.summary = '删除OSS的key'
9
9
 
10
10
  self.arguments = [
11
- CLAide::Argument.new('--key=XX', true)
11
+ CLAide::Argument.new('--key=XX', true),
12
+ CLAide::Argument.new('--type=XX', true)
12
13
  ]
13
14
 
14
15
  def self.options
15
16
  [
16
- %w[--key OSS对应的key]
17
+ %w[--key OSS对应的key],
18
+ %w[--type OSS对应的类型]
17
19
  ]
18
20
  end
19
21
 
20
22
  def initialize(argv)
21
23
  @key = argv.option('key')
24
+ @type = argv.option('type')
22
25
  super
23
26
  end
24
27
 
25
28
  def validate!
26
- help! '请输入key' unless @key
29
+ help! '请输入key或者类型' unless @key || @type
27
30
  super
28
31
  end
29
32
 
30
- def run
31
- Lhj::OSS::Helper.instance.delete(@key)
33
+ def handle
34
+ if @type
35
+ objects = Lhj::OSS::Helper.instance.list
36
+ objects.each do |o|
37
+ if /#{@type}/ =~ o.key
38
+ puts "成功删除#{o.key}"
39
+ Lhj::OSS::Helper.instance.delete(o.key)
40
+ end
41
+ end
42
+ else
43
+ Lhj::OSS::Helper.instance.delete(@key)
44
+ end
32
45
  end
33
46
  end
34
47
  end
@@ -1,4 +1,5 @@
1
1
  require 'lhj/helper/oss_helper'
2
+ require 'terminal-table'
2
3
 
3
4
  module Lhj
4
5
  class Command
@@ -6,12 +7,15 @@ module Lhj
6
7
  class List < OSS
7
8
  self.summary = '查看oss列表'
8
9
 
9
- def run
10
+ def handle
10
11
  objects = Lhj::OSS::Helper.instance.list
12
+ rows = []
11
13
  objects.each do |o|
12
14
  path = "#{Lhj::OSS::Helper.instance.url_path}/#{o.key}"
13
- puts path
15
+ rows << [path]
14
16
  end
17
+ table = Terminal::Table.new title: 'OSS List', headings: ['URL'], rows: rows
18
+ puts table
15
19
  end
16
20
  end
17
21
  end
@@ -11,30 +11,33 @@ module Lhj
11
11
 
12
12
  self.arguments = [
13
13
  CLAide::Argument.new('type', false),
14
- CLAide::Argument.new('name', false)
14
+ CLAide::Argument.new('name', false),
15
+ CLAide::Argument.new('tag', false)
15
16
  ]
16
17
 
17
18
  def self.options
18
19
  [
19
20
  %w[--type 文件类型],
20
- %w[--name 文件名]
21
+ %w[--name 文件名],
22
+ %w[--tag 标签]
21
23
  ]
22
24
  end
23
25
 
24
26
  def validate!
25
27
  super
26
- help! '类型或名字必须输入' unless @name && @type
28
+ help! '类型或名字必须输入' unless @name || @type
27
29
  end
28
30
 
29
31
  def initialize(argv)
30
32
  @current_path = argv.shift_argument || Dir.pwd
31
33
  @type = argv.option('type')
32
34
  @name = argv.option('name')
35
+ @tag = argv.option('tag')
33
36
  super
34
37
  end
35
38
 
36
- def run
37
- Dir.glob(@current_path).each do |f|
39
+ def handle
40
+ Dir.glob("#{@current_path}/*").each do |f|
38
41
  file_name = File.basename(f)
39
42
  if @name && /#{@name}/ =~ file_name
40
43
  upload(f, file_name)
@@ -48,6 +51,7 @@ module Lhj
48
51
 
49
52
  def upload(file, file_name)
50
53
  oss_key = file_name
54
+ oss_key = "#{@tag}/#{file_name}" if @tag
51
55
  Lhj::OSS::Helper.instance.upload(oss_key, file)
52
56
  url = Lhj::OSS::Helper.instance.object_url(oss_key)
53
57
  puts "云端上传成功:#{url}\n"
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'find'
4
4
  require 'fileutils'
5
+ require 'terminal-table'
5
6
 
6
7
  module Lhj
7
8
  class Command
@@ -31,11 +32,13 @@ module Lhj
31
32
  @current_path = argv.shift_argument || Dir.pwd
32
33
  @key = argv.option('key')
33
34
  @other = argv.option('other')
35
+ @modify_files = []
34
36
  super
35
37
  end
36
38
 
37
- def run
39
+ def handle
38
40
  update_source_file
41
+ print_info
39
42
  end
40
43
 
41
44
  def update_source_file
@@ -69,9 +72,22 @@ module Lhj
69
72
 
70
73
  def format_string(file, line)
71
74
  result = line
72
- result = result.gsub(/(\W)(#{@key})(\W)/, "\\1#{@other}\\3") if /(\W)(#{@key})(\W)/ =~ line
75
+ if /(\W)(#{@key})(\W)/ =~ line
76
+ result = result.gsub(/(\W)(#{@key})(\W)/, "\\1#{@other}\\3")
77
+ @modify_files << file unless @modify_files.include?(file)
78
+ end
73
79
  result
74
80
  end
81
+
82
+ def print_info
83
+ rows = []
84
+ @modify_files.each do |file|
85
+ rows << [File.basename(file), File.absolute_path(file)]
86
+ end
87
+ title = "修改了#{rows.count}个文件"
88
+ table = Terminal::Table.new title: title, headings: %w[文件 路径], rows: rows
89
+ puts table
90
+ end
75
91
  end
76
92
  end
77
93
  end
@@ -30,7 +30,7 @@ module Lhj
30
30
  super
31
31
  end
32
32
 
33
- def run
33
+ def handle
34
34
  rename_image
35
35
  end
36
36