lhj-tools 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,17 @@
1
+ require 'lhj/helper/oss_helper'
2
+
3
+ module Lhj
4
+ class Command
5
+ class OSS < Command
6
+ class List < OSS
7
+ def run
8
+ objects = Lhj::OSS::Helper.instance.list
9
+ objects.each do |o|
10
+ path = "#{Lhj::OSS::Helper.instance.url_path}/#{o.key}"
11
+ puts path
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ 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,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
@@ -7,7 +7,7 @@ module Lhj
7
7
  end
8
8
 
9
9
  def rename
10
- folder_path = "/Users/lihaijian/Downloads/ss"
10
+ folder_path = "~/Downloads/ss"
11
11
  Dir.glob("#{folder_path}/**/*.{png}").sort.each do |f|
12
12
  filename = File.basename(f, File.extname(f))
13
13
  File.rename(f, "#{folder_path}/aomi_soldout_" + filename.capitalize + File.extname(f))
@@ -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,62 @@
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 = '源码中的简繁体转换'
7
8
 
8
- def initialize(argv)
9
- super
9
+ def self.options
10
+ [
11
+ %w[--file-type 文件扩展名,默认为m,h,pch,xib],
12
+ %w[--zh-cn 转成简体中文,默认转成繁体]
13
+ ]
10
14
  end
11
15
 
12
- def validate!
16
+ def initialize(argv)
17
+ @current_path = argv.shift_argument || Dir.pwd
18
+ @file_type = argv.option('file-type', 'm,h,pch,xib')
19
+ @zh_cn = argv.flag?('zh-cn', false)
13
20
  super
14
21
  end
15
22
 
16
23
  def run
17
- p 'hello world'
24
+ handler_files
25
+ end
26
+
27
+ def handler_files
28
+ Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
29
+ handler_file f
30
+ end
31
+ end
32
+
33
+ def handler_file(file)
34
+ File.chmod(0o644, file) unless File.writable?(file)
35
+ str = format_file_string(file)
36
+ File.open(file, 'w+') do |f|
37
+ f.write(str)
38
+ end
39
+ end
40
+
41
+ def format_file_string(file)
42
+ str = ''
43
+ File.open(file, 'r+') do |f|
44
+ f.each_line do |line|
45
+ str += format_line_string(line)
46
+ end
47
+ end
48
+ str
18
49
  end
50
+
51
+ def format_line_string(line)
52
+ result = line
53
+ if line =~ /[\u4e00-\u9fa5]/
54
+ result = Lhj::Trans::Helper.instance.trans_zh_cn_str(line) if @zh_cn
55
+ result = Lhj::Trans::Helper.instance.trans_zh_hk_str(line) unless @zh_cn
56
+ end
57
+ result
58
+ end
59
+
19
60
  end
20
61
  end
21
62
  end
data/lib/lhj/command.rb CHANGED
@@ -2,6 +2,17 @@ require 'claide'
2
2
 
3
3
  module Lhj
4
4
  class Command < CLAide::Command
5
+ require 'lhj/command/init'
6
+ require 'lhj/command/head_import'
7
+ require 'lhj/command/refactor_rename'
8
+ require 'lhj/command/local/fetch'
9
+ require 'lhj/command/local/filter'
10
+ require 'lhj/command/local/local'
11
+ require 'lhj/command/local/micro_service'
12
+ require 'lhj/command/local/local_upload'
13
+ require 'lhj/command/oss/del'
14
+ require 'lhj/command/oss/upload'
15
+ require 'lhj/command/oss/list'
5
16
  require 'lhj/command/view'
6
17
  require 'lhj/command/rename_image'
7
18
  require 'lhj/command/trans'
@@ -0,0 +1,40 @@
1
+ require 'yaml'
2
+ require 'lhj/helper/oss_config'
3
+
4
+ module Lhj
5
+ class LocalConfig
6
+ def config_file
7
+ File.join(Lhj::Config.instance.home_dir, config_file_name)
8
+ end
9
+
10
+ def config_file_name
11
+ 'localizable_config.yml'
12
+ end
13
+
14
+ def syn_config_file
15
+ Lhj::OSS::Helper.instance.down_load(config_file_name, config_file)
16
+ end
17
+
18
+ def load_config
19
+ syn_config_file unless File.exist?(config_file)
20
+ YAML.load_file(config_file)
21
+ end
22
+
23
+ def get_col_by_name(file_name, col_name = 'csv_key_col')
24
+ m = config
25
+ col = m[col_name]
26
+ fo = m['read_targets'].find{ |o| /#{o['csv_file']}/ =~ file_name }
27
+ col = fo[col_name] if fo
28
+ col
29
+ end
30
+
31
+ def config
32
+ @config ||= load_config
33
+ end
34
+
35
+ def self.instance
36
+ @instance ||= new
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+ require 'yaml'
3
+ require 'lhj/config'
4
+
5
+ module Lhj
6
+ class OSSConfig
7
+ def self.config_file
8
+ file = 'oss_config.yml'
9
+ File.expand_path("#{Lhj::Config.instance.home_dir}/#{file}")
10
+ end
11
+
12
+ def self.config
13
+ @yaml ||= YAML.load_file(config_file)
14
+ end
15
+
16
+ def self.oss_endpoint
17
+ config['oss_endpoint']
18
+ end
19
+
20
+ def self.oss_access_key_id
21
+ config['oss_access_key_id']
22
+ end
23
+
24
+ def self.oss_access_key_secret
25
+ config['oss_access_key_secret']
26
+ end
27
+
28
+ def self.oss_bucket
29
+ config['oss_bucket']
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,43 @@
1
+ require 'aliyun/oss'
2
+ require 'lhj/helper/oss_config'
3
+
4
+ module Lhj
5
+ class OSS
6
+ class Helper
7
+ def initialize
8
+ @client = Aliyun::OSS::Client.new(endpoint: Lhj::OSSConfig.oss_endpoint,
9
+ access_key_id: Lhj::OSSConfig.oss_access_key_id,
10
+ access_key_secret: Lhj::OSSConfig.oss_access_key_secret)
11
+ @bucket = @client.get_bucket(Lhj::OSSConfig.oss_bucket)
12
+ end
13
+
14
+ def url_path
15
+ "http://#{Lhj::OSSConfig.oss_bucket}.#{Lhj::OSSConfig.oss_endpoint}"
16
+ end
17
+
18
+ def upload(key, file)
19
+ @bucket.put_object(key, :file => file)
20
+ end
21
+
22
+ def down_load(key, file, &block)
23
+ @bucket.get_object(key, :file => file, &block)
24
+ end
25
+
26
+ def object_url(key)
27
+ @bucket.object_url(key, false)
28
+ end
29
+
30
+ def list
31
+ @bucket.list_objects
32
+ end
33
+
34
+ def delete(key)
35
+ @bucket.delete_object(key)
36
+ end
37
+
38
+ def self.instance
39
+ @instance ||= new
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ require 'lhj/config'
2
+
3
+ module Lhj
4
+ class Trans
5
+ class Helper
6
+ def self.instance
7
+ @instance ||= new
8
+ end
9
+
10
+ def yaml_file
11
+ File.join(Lhj::Config.instance.home_dir, 'zh2hant.yml')
12
+ end
13
+
14
+ def load_trans_map
15
+ require 'yaml'
16
+ down_load_yaml unless File.exist?(yaml_file)
17
+ contents = YAML.safe_load(File.open(yaml_file))
18
+ contents.to_hash
19
+ end
20
+
21
+ def down_load_yaml
22
+ require 'open-uri'
23
+ URI.open('http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/zh2hant.yml') do |i|
24
+ File.open(yaml_file, 'w+') do |f|
25
+ f.write(i.read)
26
+ end
27
+ end
28
+ end
29
+
30
+ def trans_zh_cn_str(input)
31
+ @trans_map_invert ||= load_trans_map.invert
32
+ out = []
33
+ input.each_char do |c|
34
+ out << (@trans_map_invert[c] || c)
35
+ end
36
+ out.join('')
37
+ end
38
+
39
+ def trans_zh_hk_str(input)
40
+ @trans_map ||= load_trans_map
41
+ out = []
42
+ input.each_char do |c|
43
+ out << (@trans_map[c] || c)
44
+ end
45
+ out.join('')
46
+ end
47
+ end
48
+ end
49
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lhj
4
4
  module Tools
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end