lhj-tools 0.1.0 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ require 'lhj/helper/oss_helper'
2
+
3
+ module Lhj
4
+ class Command
5
+ class Local < Command
6
+ class Upload < Local
7
+ self.summary = '上传中英文对照csv文件'
8
+
9
+ def self.options
10
+ [
11
+ %w[--upload-file 上传中英文对照csv文件名]
12
+ ]
13
+ end
14
+
15
+ def initialize(argv)
16
+ @pwd_path = argv.shift_argument || Dir.pwd
17
+ @upload_csv_file = argv.option('upload-file', '*.csv')
18
+ super
19
+ end
20
+
21
+ def csv_file_name
22
+ file_name = @upload_csv_file
23
+ file_name = "#{@upload_csv_file}.csv" unless /.csv$/ =~ @upload_csv_file
24
+ file_name
25
+ end
26
+
27
+ def csv_oss_key(file_name)
28
+ "csv/#{Time.now.to_i}/#{file_name}"
29
+ end
30
+
31
+ def run
32
+ csv_files = File.join(@pwd_path, '**', csv_file_name)
33
+ Dir.glob(csv_files).each do |f|
34
+ file_name = File.basename(f)
35
+ oss_key = csv_oss_key file_name
36
+ Lhj::OSS::Helper.instance.upload(oss_key, f)
37
+ url = Lhj::OSS::Helper.instance.object_url(oss_key)
38
+ puts "云端上传成功.下载Url:#{url}\n"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ module Lhj
6
+ class Command
7
+ class Service < Command
8
+ self.summary = '微服务名变更'
9
+
10
+ def initialize(argv)
11
+ @current_path = argv.shift_argument || Dir.pwd
12
+ @file_type = argv.option('file-type', 'm,h')
13
+ @file_name = argv.option('file-name', 'service_map.csv')
14
+ @service_map = {}
15
+ super
16
+ end
17
+
18
+ def run
19
+ read_csv
20
+ update_source
21
+ end
22
+
23
+ def read_csv
24
+ path = File.join(@current_path, csv_file_name)
25
+ Dir.glob(path).each do |p|
26
+ CSV.foreach(p) do |row|
27
+ @service_map[row[0]] = row[1] if row[0]
28
+ end
29
+ end
30
+ end
31
+
32
+ def csv_file_name
33
+ file_name = @file_name
34
+ file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
35
+ file_name
36
+ end
37
+
38
+ def update_source
39
+ Dir.glob("#{@current_path}/**/*.{m,h}").each do |f|
40
+ if f =~ /Pods/
41
+ update_file(f) if f =~ %r{Pods/ML}
42
+ else
43
+ update_file(f)
44
+ end
45
+ end
46
+ end
47
+
48
+ def update_file(file)
49
+ File.chmod(0o644, file)
50
+ str = file_string(file)
51
+ File.open(file, 'w+') do |f|
52
+ f.write(str)
53
+ end
54
+ File.chmod(0o444, file) if file =~ /Pods/
55
+ end
56
+
57
+ def file_string(file)
58
+ str = ''
59
+ File.open(file, 'r+') do |f|
60
+ f.each_line do |line|
61
+ str += format_string(f, line)
62
+ end
63
+ end
64
+ str
65
+ end
66
+
67
+ def format_string(file, line)
68
+ result = line
69
+ if url_reg =~ line
70
+ line.scan(url_reg).flatten.each do |key|
71
+ result = result.gsub(key, @service_map[key]) if key && @service_map[key]
72
+ end
73
+ end
74
+ result
75
+ end
76
+
77
+ def url_reg
78
+ @url_key_reg ||= begin
79
+ keys = @service_map.keys.join('|')
80
+ /(#{keys})/
81
+ end
82
+ @url_key_reg
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ require 'lhj/helper/oss_helper'
3
+
4
+ module Lhj
5
+ class Command
6
+ class OSS < Command
7
+ class Del < OSS
8
+ self.summary = '删除OSS的key'
9
+
10
+ self.arguments = [
11
+ CLAide::Argument.new('--key=XX', true)
12
+ ]
13
+
14
+ def self.options
15
+ [
16
+ %w[--key OSS对应的key]
17
+ ]
18
+ end
19
+
20
+ def initialize(argv)
21
+ @key = argv.option('key')
22
+ super
23
+ end
24
+
25
+ def validate!
26
+ help! '请输入key' unless @key
27
+ super
28
+ end
29
+
30
+ def run
31
+ Lhj::OSS::Helper.instance.delete(@key)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ require 'lhj/helper/oss_helper'
2
+
3
+ module Lhj
4
+ class Command
5
+ class OSS < Command
6
+ class List < OSS
7
+ self.summary = '查看oss列表'
8
+
9
+ def run
10
+ objects = Lhj::OSS::Helper.instance.list
11
+ objects.each do |o|
12
+ path = "#{Lhj::OSS::Helper.instance.url_path}/#{o.key}"
13
+ puts path
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lhj/helper/oss_helper'
4
+
5
+ module Lhj
6
+ class Command
7
+ class OSS < Command
8
+ # OSS file upload
9
+ class Upload < OSS
10
+ self.summary = '上传文件'
11
+
12
+ self.arguments = [
13
+ CLAide::Argument.new('type', false),
14
+ CLAide::Argument.new('name', false)
15
+ ]
16
+
17
+ def self.options
18
+ [
19
+ %w[--type 文件类型],
20
+ %w[--name 文件名]
21
+ ]
22
+ end
23
+
24
+ def validate!
25
+ super
26
+ help! '类型或名字必须输入' unless @name && @type
27
+ end
28
+
29
+ def initialize(argv)
30
+ @current_path = argv.shift_argument || Dir.pwd
31
+ @type = argv.option('type')
32
+ @name = argv.option('name')
33
+ super
34
+ end
35
+
36
+ def run
37
+ Dir.glob(@current_path).each do |f|
38
+ file_name = File.basename(f)
39
+ if @name && /#{@name}/ =~ file_name
40
+ upload(f, file_name)
41
+ elsif @type && /#{@type}/ =~ file_name
42
+ upload(f, file_name)
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def upload(file, file_name)
50
+ oss_key = file_name
51
+ Lhj::OSS::Helper.instance.upload(oss_key, file)
52
+ url = Lhj::OSS::Helper.instance.object_url(oss_key)
53
+ puts "云端上传成功:#{url}\n"
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,10 @@
1
+
2
+ module Lhj
3
+ class Command
4
+ # sync config
5
+ class OSS < Command
6
+ self.summary = 'oss操作'
7
+ self.abstract_command = true
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'find'
4
+ require 'fileutils'
5
+
6
+ module Lhj
7
+ class Command
8
+ # refactor global name
9
+ class Refactor < Command
10
+ self.summary = '重命名全局变量'
11
+
12
+ self.arguments = [
13
+ CLAide::Argument.new('--key=xx', true),
14
+ CLAide::Argument.new('--other=xx', true)
15
+ ]
16
+
17
+ def self.options
18
+ [
19
+ %w[--key 变量名],
20
+ %w[--other 新的变量名]
21
+ ]
22
+ end
23
+
24
+ def validate!
25
+ super
26
+ help! '输入变量名' unless @key
27
+ help! '新的变量名' unless @other
28
+ end
29
+
30
+ def initialize(argv)
31
+ @current_path = argv.shift_argument || Dir.pwd
32
+ @key = argv.option('key')
33
+ @other = argv.option('other')
34
+ super
35
+ end
36
+
37
+ def run
38
+ update_source_file
39
+ end
40
+
41
+ def update_source_file
42
+ Dir.glob("#{@current_path}/**/*.{m,h,pch}").each do |f|
43
+ if f =~ /Pods/
44
+ modify_file(f) if f =~ %r{Pods/ML}
45
+ else
46
+ modify_file(f)
47
+ end
48
+ end
49
+ end
50
+
51
+ def modify_file(file)
52
+ File.chmod(0o644, file)
53
+ str = file_string(file)
54
+ File.open(file, 'w+') do |f|
55
+ f.write(str)
56
+ end
57
+ File.chmod(0o444, file) if file =~ /Pods/
58
+ end
59
+
60
+ def file_string(file)
61
+ str = ''
62
+ File.open(file, 'r+') do |f|
63
+ f.each_line do |line|
64
+ str += format_string(f, line)
65
+ end
66
+ end
67
+ str
68
+ end
69
+
70
+ def format_string(file, line)
71
+ result = line
72
+ result = result.gsub(/(\W)(#{@key})(\W)/, "\\1#{@other}\\3") if /(\W)(#{@key})(\W)/ =~ line
73
+ result
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,17 +1,59 @@
1
-
2
1
  module Lhj
3
2
  class Command
4
- class Rename < Command
3
+ class RenameImage < Command
4
+ self.summary = '重命名图片'
5
+
6
+ self.arguments = [
7
+ CLAide::Argument.new('--pre=xx', false),
8
+ CLAide::Argument.new('--name=xx', false),
9
+ CLAide::Argument.new('--other=xx', false)
10
+ ]
11
+
12
+ def self.options
13
+ [
14
+ %w[--pre 图片前缀],
15
+ %w[--name 图片名称],
16
+ %w[--other 图片变化后的名称]
17
+ ]
18
+ end
19
+
20
+ def validate!
21
+ super
22
+ help! '输入图片信息' unless @image_pre || @image_name || @image_other_name
23
+ end
24
+
25
+ def initialize(argv)
26
+ @current_path = argv.shift_argument || Dir.pwd
27
+ @image_pre = argv.option('pre')
28
+ @image_name = argv.option('name')
29
+ @image_other_name = argv.option('other')
30
+ super
31
+ end
32
+
5
33
  def run
6
- rename
34
+ rename_image
7
35
  end
8
36
 
9
- def rename
10
- folder_path = "/Users/lihaijian/Downloads/ss"
11
- Dir.glob("#{folder_path}/**/*.{png}").sort.each do |f|
37
+ def rename_image
38
+ Dir.glob("#{@current_path}/**/*.{png}").sort.each do |f|
12
39
  filename = File.basename(f, File.extname(f))
13
- File.rename(f, "#{folder_path}/aomi_soldout_" + filename.capitalize + File.extname(f))
40
+ m_filename = modify_name(filename, File.extname(f))
41
+ target = File.join(@current_path, m_filename)
42
+ File.rename(f, target)
43
+ end
44
+ end
45
+
46
+ def modify_name(file_name, extname)
47
+ name = file_name.downcase + extname
48
+ if @image_pre
49
+ name = @image_pre + file_name.downcase + extname
50
+ elsif @image_name && @image_other_name
51
+ if file_name =~ /#{@image_name}/
52
+ other_name = file_name.gsub(/#{@image_name}/, @image_other_name)
53
+ name = other_name + extname
54
+ end
14
55
  end
56
+ name
15
57
  end
16
58
  end
17
59
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'find'
4
+ require 'fileutils'
5
+
6
+ module Lhj
7
+ class Command
8
+ class ReverseImport < Command
9
+ self.summary = '更改头文件引入'
10
+
11
+ def initialize(argv)
12
+ @current_path = argv.shift_argument || Dir.pwd
13
+ @framework_names = []
14
+ super
15
+ end
16
+
17
+ def run
18
+ get_all_frameworks
19
+ get_import_headers
20
+ end
21
+
22
+ def get_import_headers
23
+ Dir.glob("#{@current_path}/**/*.{m,h,pch}").each do |f|
24
+ header_handler_file(f) unless f =~ /Pods/
25
+ end
26
+ end
27
+
28
+ def get_all_frameworks
29
+ folders = child_dir
30
+ folders.each { |name| @framework_names << name }
31
+ end
32
+
33
+ def framework_name_reg
34
+ @url_key_reg ||= begin
35
+ keys = @framework_names.join('|')
36
+ /#{keys}/
37
+ end
38
+ @url_key_reg
39
+ end
40
+
41
+ def pod_folder_name
42
+ File.join(@current_path, 'MacauLife', 'CustomPods')
43
+ end
44
+
45
+ def child_dir
46
+ dirs = Dir.entries(pod_folder_name)
47
+ dirs.reject! { |d| File.directory?(d) }
48
+ dirs
49
+ end
50
+
51
+ def import_reg
52
+ /#import\s*<(.*)\/(.*)>$/
53
+ end
54
+
55
+ def header_handler_file(f)
56
+ str = ''
57
+ File.readlines(f).each do |l|
58
+ if import_reg =~ l
59
+ ma = l.match(import_reg)
60
+ if framework_name_reg =~ ma[1]
61
+ str += "#import \"#{ma[2]}\"\n"
62
+ else
63
+ str += l.dup
64
+ end
65
+ else
66
+ str += l.dup
67
+ end
68
+ end
69
+ File.write(f, str)
70
+ end
71
+
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,21 @@
1
+
2
+ module Lhj
3
+ class Command
4
+ class Trans < Command
5
+ self.summary = '模板'
6
+ self.description = '模板'
7
+
8
+ def initialize(argv)
9
+ super
10
+ end
11
+
12
+ def validate!
13
+ super
14
+ end
15
+
16
+ def run
17
+ p 'hello world'
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,21 +1,63 @@
1
+ # frozen_string_literal: true
2
+ require 'lhj/helper/trans_helper'
1
3
 
2
4
  module Lhj
3
5
  class Command
4
6
  class Trans < Command
5
- self.summary = '用于中英转换'
6
- self.description = '中英转换'
7
+ self.summary = '源码中的简繁体转换'
8
+ self.description = '当前目录简繁体转换 `lhj trans --zh-cn`'
7
9
 
8
- def initialize(argv)
9
- super
10
+ def self.options
11
+ [
12
+ %w[--file-type 文件扩展名,默认为m,h,pch,xib],
13
+ %w[--zh-cn 转成简体中文,默认转成繁体]
14
+ ]
10
15
  end
11
16
 
12
- def validate!
17
+ def initialize(argv)
18
+ @current_path = argv.shift_argument || Dir.pwd
19
+ @file_type = argv.option('file-type', 'm,h,pch,xib')
20
+ @zh_cn = argv.flag?('zh-cn', false)
13
21
  super
14
22
  end
15
23
 
16
24
  def run
17
- p 'hello world'
25
+ handler_files
26
+ end
27
+
28
+ def handler_files
29
+ Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
30
+ handler_file f
31
+ end
32
+ end
33
+
34
+ def handler_file(file)
35
+ File.chmod(0o644, file) unless File.writable?(file)
36
+ str = format_file_string(file)
37
+ File.open(file, 'w+') do |f|
38
+ f.write(str)
39
+ end
40
+ end
41
+
42
+ def format_file_string(file)
43
+ str = ''
44
+ File.open(file, 'r+') do |f|
45
+ f.each_line do |line|
46
+ str += format_line_string(line)
47
+ end
48
+ end
49
+ str
18
50
  end
51
+
52
+ def format_line_string(line)
53
+ result = line
54
+ if line =~ /[\u4e00-\u9fa5]/
55
+ result = Lhj::Trans::Helper.instance.trans_zh_cn_str(line) if @zh_cn
56
+ result = Lhj::Trans::Helper.instance.trans_zh_hk_str(line) unless @zh_cn
57
+ end
58
+ result
59
+ end
60
+
19
61
  end
20
62
  end
21
63
  end
@@ -1,7 +1,8 @@
1
1
  module Lhj
2
2
  class Command
3
3
  class View < Command
4
- self.summary = '生成源码'
4
+ self.summary = '生成iOS组件源码'
5
+ self.description = '使用`lhj view --type=UILabel --name=titleLabel`'
5
6
 
6
7
  def initialize(argv)
7
8
  @name = argv.option('name', 'titleLabel')
@@ -10,24 +11,25 @@ module Lhj
10
11
  end
11
12
 
12
13
  def names
13
- @name.split(",").map(&:strip)
14
+ @name.split(',').map(&:strip)
14
15
  end
15
16
 
16
17
  def type
17
18
  @ele_type ||= begin
18
- if @type =~ /image/i
19
+ case @type
20
+ when /image/i
19
21
  'UIImageView'
20
- elsif @type =~ /stack/i
22
+ when /stack/i
21
23
  'UIStackView'
22
- elsif @type =~ /label/i
24
+ when /label/i
23
25
  'UILabel'
24
- elsif @type =~ /table/i
26
+ when /table/i
25
27
  'UITableView'
26
- elsif @type =~ /text/i
28
+ when /text/i
27
29
  'UITextField'
28
- elsif @type =~ /button/i
30
+ when /button/i
29
31
  'UIButton'
30
- elsif @type =~ /view/i
32
+ when /view/i
31
33
  'UIView'
32
34
  else
33
35
  @type
@@ -47,7 +49,7 @@ module Lhj
47
49
 
48
50
  def print_declare
49
51
  names.each do |name|
50
- puts "///"
52
+ puts '///'
51
53
  puts "@property (nonatomic, strong) #{type} *#{name};"
52
54
  end
53
55
  end
@@ -55,24 +57,25 @@ module Lhj
55
57
  def print_instance
56
58
  names.each do |name|
57
59
  puts "-(#{type} *)#{name}"
58
- puts "{"
60
+ puts '{'
59
61
  puts " if(!_#{name}){"
60
62
  print_alloc(name)
61
63
  puts " _#{name}.translatesAutoresizingMaskIntoConstraints = NO;"
62
64
  print_property(name)
63
- puts " }"
65
+ puts ' }'
64
66
  puts " return _#{name};"
65
- puts "}"
67
+ puts '}'
66
68
  puts "\n"
67
69
  end
68
70
  end
69
71
 
70
72
  def print_alloc(name)
71
- if type.eql?('UIImageView')
73
+ case type
74
+ when 'UIImageView'
72
75
  puts " _#{name} = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"xxxx\"]];"
73
- elsif type.eql?('UIButton')
76
+ when 'UIButton'
74
77
  puts " _#{name} = [UIButton buttonWithType:UIButtonTypeCustom];"
75
- elsif type.eql?('UITableView')
78
+ when 'UITableView'
76
79
  puts " _#{name} = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];"
77
80
  else
78
81
  puts " _#{name} = [[#{type} alloc] init];"
@@ -80,34 +83,35 @@ module Lhj
80
83
  end
81
84
 
82
85
  def print_property(name)
83
- if type.eql?('UILabel')
86
+ case type
87
+ when 'UILabel'
84
88
  puts " _#{name}.textColor = kSetCOLOR(0x333333);"
85
89
  puts " _#{name}.text = @\"xxxxxxxx\";"
86
90
  puts " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
87
91
  puts " _#{name}.textAlignment = NSTextAlignmentCenter;"
88
- elsif type.eql?('UIImageView')
92
+ when 'UIImageView'
89
93
  puts " _#{name}.backgroundColor = kBackgroundColor;"
90
94
  puts " _#{name}.contentMode = UIViewContentModeScaleAspectFit;"
91
95
  puts " _#{name}.clipsToBounds = YES;"
92
96
  puts " _#{name}.layer.cornerRadius = 6.0f;"
93
97
  puts " _#{name}.layer.borderColor = kLineColor.CGColor;"
94
98
  puts " _#{name}.layer.borderWidth = 0.5;"
95
- elsif type.eql?('UITextField')
99
+ when 'UITextField'
96
100
  puts " _#{name}.textColor = kSetCOLOR(0x333333);"
97
101
  puts " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
98
- elsif type.eql?('UIView')
102
+ when 'UIView'
99
103
  puts " _#{name}.backgroundColor = kBackgroundColor;"
100
- elsif type.eql?('UIStackView')
104
+ when 'UIStackView'
101
105
  puts " _#{name}.axis = UILayoutConstraintAxisHorizontal;"
102
106
  puts " _#{name}.distribution = UIStackViewDistributionFillEqually;"
103
- elsif type.eql?('UITableView')
107
+ when 'UITableView'
104
108
  puts " _#{name}.backgroundColor = kBackgroundColor;"
105
109
  puts " _#{name}.delegate = self;"
106
110
  puts " _#{name}.delegate = self;"
107
111
  puts " _#{name}.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
108
112
  puts " _#{name}.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
109
113
  puts " _#{name}.separatorStyle = UITableViewCellSeparatorStyleNone;"
110
- elsif type.eql?('UIButton')
114
+ when 'UIButton'
111
115
  puts " _#{name}.backgroundColor = kBackgroundColor;"
112
116
  puts " [_#{name} setTitle:@\"xxx\" forState:UIControlStateNormal];"
113
117
  puts " [_#{name} setTitleColor:kSetCOLOR(0x999999) forState:UIControlStateNormal];"