lhj-tools 0.1.1 → 0.1.5

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,23 @@
1
+ require 'lhj/helper/oss_helper'
2
+ require 'terminal-table'
3
+
4
+ module Lhj
5
+ class Command
6
+ class OSS < Command
7
+ class List < OSS
8
+ self.summary = '查看oss列表'
9
+
10
+ def run
11
+ objects = Lhj::OSS::Helper.instance.list
12
+ rows = []
13
+ objects.each do |o|
14
+ path = "#{Lhj::OSS::Helper.instance.url_path}/#{o.key}"
15
+ rows << [path]
16
+ end
17
+ table = Terminal::Table.new title: 'OSS List', headings: ['URL'], rows: rows
18
+ puts table
19
+ end
20
+ end
21
+ end
22
+ end
23
+ 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,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'find'
4
+ require 'fileutils'
5
+ require 'terminal-table'
6
+
7
+ module Lhj
8
+ class Command
9
+ # refactor global name
10
+ class Refactor < Command
11
+ self.summary = '重命名全局变量'
12
+
13
+ self.arguments = [
14
+ CLAide::Argument.new('--key=xx', true),
15
+ CLAide::Argument.new('--other=xx', true)
16
+ ]
17
+
18
+ def self.options
19
+ [
20
+ %w[--key 变量名],
21
+ %w[--other 新的变量名]
22
+ ]
23
+ end
24
+
25
+ def validate!
26
+ super
27
+ help! '输入变量名' unless @key
28
+ help! '新的变量名' unless @other
29
+ end
30
+
31
+ def initialize(argv)
32
+ @current_path = argv.shift_argument || Dir.pwd
33
+ @key = argv.option('key')
34
+ @other = argv.option('other')
35
+ @modify_files = []
36
+ super
37
+ end
38
+
39
+ def run
40
+ update_source_file
41
+ print_info
42
+ end
43
+
44
+ def update_source_file
45
+ Dir.glob("#{@current_path}/**/*.{m,h,pch}").each do |f|
46
+ if f =~ /Pods/
47
+ modify_file(f) if f =~ %r{Pods/ML}
48
+ else
49
+ modify_file(f)
50
+ end
51
+ end
52
+ end
53
+
54
+ def modify_file(file)
55
+ File.chmod(0o644, file)
56
+ str = file_string(file)
57
+ File.open(file, 'w+') do |f|
58
+ f.write(str)
59
+ end
60
+ File.chmod(0o444, file) if file =~ /Pods/
61
+ end
62
+
63
+ def file_string(file)
64
+ str = ''
65
+ File.open(file, 'r+') do |f|
66
+ f.each_line do |line|
67
+ str += format_string(f, line)
68
+ end
69
+ end
70
+ str
71
+ end
72
+
73
+ def format_string(file, line)
74
+ result = 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
79
+ result
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
91
+ end
92
+ end
93
+ 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