lhj-tools 0.1.9 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55ea8babd6d7b1aa8bdbc376bd7c624620dc4b868be6fa6decf8ba46443f4e4d
4
- data.tar.gz: e802b2a688c86d442a6f2776b7d6ce0abf93edf82ef3b3fe2bdf4624ba8f4aa4
3
+ metadata.gz: 9899645c85f8e5b8b2163954f95949dafaa7be4730ae80cf8db25a9a9a1523a3
4
+ data.tar.gz: bdbb99bb2a141216e287f856d54c8f812e29fa45133ffd0af54374f48dada496
5
5
  SHA512:
6
- metadata.gz: e23b3398d6c629c30814c5a3b0354430b215ce0d0b9f6cab8e499a87da74d0fc6d3a7c174e81d31d53e89a6eab603bc2f572e428f6715e3ab71a739f7e03a24d
7
- data.tar.gz: 11872fd8fefb0e3dc88e344f20755bbb1a2615e4e28c91e09e20ff9c616379d35f6a33d57cd0b670a115e31e5725f121919ec74cd5db512423b9a36a28dfb70a
6
+ metadata.gz: 145eecc8706741b98073e87390c56a818e718f13e7ea6cd7f8b0aafc2c346f5b219ca075c1516db22cb438cb52cf5c2210f5ebd5c51f12ae9dabdc5db979c6aa
7
+ data.tar.gz: 83d61d33a01c96d897227e692b60ef2c56d920172193f8d9754daa2db47b7a0edc5ea8f4ea1bf26a623d110e4066b11509b7676075b4d03dd100895246eb43d3
@@ -3,43 +3,94 @@ require 'lhj/config'
3
3
 
4
4
  module Lhj
5
5
  class Command
6
+ # config
6
7
  class Config < Command
8
+ def initialize(argv)
9
+ @cli = HighLine.new
10
+ super
11
+ end
12
+
13
+ def show_config
14
+ config_arr = Dir.glob("#{config_dir}/**/*.yml")
15
+ config_arr.each_index { |i| puts "#{i}.#{File.basename(config_arr[i])}".yellow }
16
+ idx = ask_which_one
17
+ config_file = config_arr[idx]
18
+ handle_yaml(config_file)
19
+ end
20
+
21
+ def handle_yaml(file)
22
+ '子类实现'
23
+ end
24
+
25
+ def handle
26
+ show_config
27
+ end
28
+
29
+ def config_dir
30
+ Lhj::Config.instance.home_dir
31
+ end
32
+
33
+ def print_table(title, table_rows)
34
+ table = Terminal::Table.new title: title, headings: %w[键 值], rows: table_rows
35
+ puts table
36
+ end
37
+
7
38
  # show config info
8
39
  class Info < Config
9
40
  self.summary = '查看配置信息'
10
41
 
11
- def initialize(argv)
12
- @cli = HighLine.new
13
- super
14
- end
15
42
 
16
- def handle
17
- show_config
18
- end
43
+ def handle_yaml(file)
44
+ table_rows = []
45
+ content = YAML.load_file(file)
19
46
 
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
47
+ case content
48
+ when Array
49
+ content.each do |row|
50
+ next unless row.is_a?(Hash)
51
+
52
+ row.each do |key, value|
53
+ table_rows << [key, value]
54
+ end
55
+ table_rows << :separator
56
+ end
57
+ when Hash
58
+ content.each do |key, value|
59
+ table_rows << [key, value]
60
+ end
61
+ end
27
62
 
28
- def show_yaml(file)
29
- table_rows = []
30
- yaml = YAML.load_file(file)
31
- yaml.each { |key, value| table_rows << [key, value] }
32
63
  title = File.basename(file).to_s
33
- table = Terminal::Table.new title: title, headings: %w[键 值], rows: table_rows
34
- puts table
64
+ print_table(title, table_rows) if table_rows.count.positive?
35
65
  end
36
66
 
37
67
  def ask_which_one
38
68
  @cli.ask('查看哪个配置: '.green).strip.to_i
39
69
  end
70
+ end
71
+
72
+ class Update < Config
73
+
74
+ def ask_which_one
75
+ @cli.ask('更新哪个配置: '.green).strip.to_i
76
+ end
77
+
78
+ def handle_yaml(file)
79
+ table_rows = []
80
+ content = YAML.load_file(file)
81
+
82
+ if content.is_a?(Hash)
83
+ content.each do |key, value|
84
+ table_rows << [key, value]
85
+ end
86
+ end
87
+
88
+ title = File.basename(file).to_s
89
+ print_table(title, table_rows) if table_rows.count.positive?
40
90
 
41
- def config_dir
42
- Lhj::Config.instance.home_dir
91
+ cmd = ['open']
92
+ cmd << file
93
+ Actions.sh(cmd.join(' '))
43
94
  end
44
95
  end
45
96
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'faraday'
2
3
  require 'faraday_middleware'
3
4
  require 'net/http'
@@ -9,20 +10,41 @@ module Lhj
9
10
  # sync config
10
11
  class Init < Command
11
12
  self.summary = '初始化控件'
12
- self.description = '使用工具前先执行`lhj init`'
13
+ self.description = '使用工具前先执行`lhj init --url=http://xxx`'
13
14
 
14
- def handle
15
- FileUtils.mkdir_p(target_folder) unless File.exist?(target_folder)
16
- download_file
15
+ self.arguments = [
16
+ CLAide::Argument.new('url', true)
17
+ ]
18
+
19
+ def self.options
20
+ [
21
+ %w[--url 配置文件的url]
22
+ ]
23
+ end
24
+
25
+ def validate!
26
+ super
27
+ unless @url
28
+ path = File.join(target_folder, 'config_file.yml')
29
+ help! '配置url必须输入' unless File.exist?(path)
30
+ end
17
31
  end
18
32
 
19
- def down_load_urls
20
- %w[http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/localizable_config.yml
21
- http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/oss_config.yml
22
- http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/yapi.yml
23
- http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/zh2hant.yml
24
- http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/pod_config.yml
25
- http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/main_config.yml]
33
+ def initialize(argv)
34
+ @url = argv.option('url')
35
+ super
36
+ end
37
+
38
+ def handle
39
+ FileUtils.mkdir_p(target_folder) unless File.exist?(target_folder)
40
+ if @url
41
+ file_path = file_name_from_url(@url)
42
+ save_url_to_file(@url, file_path)
43
+ else
44
+ file_path = File.join(target_folder, 'config_file.yml')
45
+ end
46
+ content = YAML.load_file(file_path)
47
+ download_file(content) if content.is_a?(Array)
26
48
  end
27
49
 
28
50
  def target_folder
@@ -41,14 +63,22 @@ module Lhj
41
63
  url.scan(%r{/([^/]+)}).flatten.last
42
64
  end
43
65
 
44
- def download_file
45
- down_load_urls.each do |url|
46
- file_name = file_name_with_url(url)
47
- file_path = File.join(target_folder, file_name)
48
- save_file(url, file_path) unless File.exist?(file_path)
66
+ def download_file(urls)
67
+ urls.each do |url|
68
+ file_path = file_name_from_url(url)
69
+ save_url_to_file(url, file_path)
49
70
  end
50
71
  puts "工具初始化完成 \n"
51
72
  end
73
+
74
+ def save_url_to_file(url, file_path)
75
+ save_file(url, file_path) unless File.exist?(file_path)
76
+ end
77
+
78
+ def file_name_from_url(url)
79
+ file_name = file_name_with_url(url)
80
+ File.join(target_folder, file_name)
81
+ end
52
82
  end
53
83
  end
54
84
  end
@@ -12,6 +12,10 @@ module Lhj
12
12
  super
13
13
  end
14
14
 
15
+ def begin_title
16
+ '读取映射文件~/.lhj/pod_config.yml'
17
+ end
18
+
15
19
  def handle
16
20
  sync
17
21
  end
@@ -47,102 +47,118 @@ module Lhj
47
47
  print_value
48
48
  end
49
49
 
50
+ def puts_d(str)
51
+ puts str.blue
52
+ end
53
+
54
+ def puts_i(str)
55
+ puts str.blue
56
+ end
57
+
58
+ def puts_l(str)
59
+ puts str.blue
60
+ end
61
+
62
+ def puts_v(str)
63
+ puts str.blue
64
+ end
65
+
50
66
  def print_declare
51
67
  names.each do |name|
52
- puts '///'
53
- puts "@property (nonatomic, strong) #{type} *#{name};"
68
+ puts_d '///'
69
+ puts_d "@property (nonatomic, strong) #{type} *#{name};"
54
70
  end
55
71
  end
56
72
 
57
73
  def print_instance
58
74
  names.each do |name|
59
- puts "-(#{type} *)#{name}"
60
- puts '{'
61
- puts " if(!_#{name}){"
75
+ puts_i "-(#{type} *)#{name}"
76
+ puts_i '{'
77
+ puts_i " if(!_#{name}){"
62
78
  print_alloc(name)
63
- puts " _#{name}.translatesAutoresizingMaskIntoConstraints = NO;"
79
+ puts_i " _#{name}.translatesAutoresizingMaskIntoConstraints = NO;"
64
80
  print_property(name)
65
- puts ' }'
66
- puts " return _#{name};"
67
- puts '}'
68
- puts "\n"
81
+ puts_i ' }'
82
+ puts_i " return _#{name};"
83
+ puts_i '}'
84
+ puts_i "\n"
69
85
  end
70
86
  end
71
87
 
72
88
  def print_alloc(name)
73
89
  case type
74
90
  when 'UIImageView'
75
- puts " _#{name} = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"xxxx\"]];"
91
+ puts_i " _#{name} = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"xxxx\"]];"
76
92
  when 'UIButton'
77
- puts " _#{name} = [UIButton buttonWithType:UIButtonTypeCustom];"
93
+ puts_i " _#{name} = [UIButton buttonWithType:UIButtonTypeCustom];"
78
94
  when 'UITableView'
79
- puts " _#{name} = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];"
95
+ puts_i " _#{name} = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];"
80
96
  else
81
- puts " _#{name} = [[#{type} alloc] init];"
97
+ puts_i " _#{name} = [[#{type} alloc] init];"
82
98
  end
83
99
  end
84
100
 
85
101
  def print_property(name)
86
102
  case type
87
103
  when 'UILabel'
88
- puts " _#{name}.textColor = kSetCOLOR(0x333333);"
89
- puts " _#{name}.text = @\"xxxxxxxx\";"
90
- puts " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
91
- puts " _#{name}.textAlignment = NSTextAlignmentCenter;"
104
+ puts_i " _#{name}.textColor = kSetCOLOR(0x333333);"
105
+ puts_i " _#{name}.text = @\"xxxxxxxx\";"
106
+ puts_i " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
107
+ puts_i " _#{name}.textAlignment = NSTextAlignmentCenter;"
92
108
  when 'UIImageView'
93
- puts " _#{name}.backgroundColor = kBackgroundColor;"
94
- puts " _#{name}.contentMode = UIViewContentModeScaleAspectFit;"
95
- puts " _#{name}.clipsToBounds = YES;"
96
- puts " _#{name}.layer.cornerRadius = 6.0f;"
97
- puts " _#{name}.layer.borderColor = kLineColor.CGColor;"
98
- puts " _#{name}.layer.borderWidth = 0.5;"
109
+ puts_i " _#{name}.backgroundColor = kBackgroundColor;"
110
+ puts_i " _#{name}.contentMode = UIViewContentModeScaleAspectFit;"
111
+ puts_i " _#{name}.clipsToBounds = YES;"
112
+ puts_i " _#{name}.layer.cornerRadius = 6.0f;"
113
+ puts_i " _#{name}.layer.borderColor = kLineColor.CGColor;"
114
+ puts_i " _#{name}.layer.borderWidth = 0.5;"
99
115
  when 'UITextField'
100
- puts " _#{name}.textColor = kSetCOLOR(0x333333);"
101
- puts " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
116
+ puts_i " _#{name}.textColor = kSetCOLOR(0x333333);"
117
+ puts_i " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
102
118
  when 'UIView'
103
- puts " _#{name}.backgroundColor = kBackgroundColor;"
119
+ puts_i " _#{name}.backgroundColor = kBackgroundColor;"
104
120
  when 'UIStackView'
105
- puts " _#{name}.axis = UILayoutConstraintAxisHorizontal;"
106
- puts " _#{name}.distribution = UIStackViewDistributionFillEqually;"
121
+ puts_i " _#{name}.axis = UILayoutConstraintAxisHorizontal;"
122
+ puts_i " _#{name}.distribution = UIStackViewDistributionFillEqually;"
107
123
  when 'UITableView'
108
- puts " _#{name}.backgroundColor = kBackgroundColor;"
109
- puts " _#{name}.delegate = self;"
110
- puts " _#{name}.delegate = self;"
111
- puts " _#{name}.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
112
- puts " _#{name}.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
113
- puts " _#{name}.separatorStyle = UITableViewCellSeparatorStyleNone;"
124
+ puts_i " _#{name}.backgroundColor = kBackgroundColor;"
125
+ puts_i " _#{name}.delegate = self;"
126
+ puts_i " _#{name}.delegate = self;"
127
+ puts_i " _#{name}.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
128
+ puts_i " _#{name}.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
129
+ puts_i " _#{name}.separatorStyle = UITableViewCellSeparatorStyleNone;"
114
130
  when 'UIButton'
115
- puts " _#{name}.backgroundColor = kBackgroundColor;"
116
- puts " [_#{name} setTitle:@\"xxx\" forState:UIControlStateNormal];"
117
- puts " [_#{name} setTitleColor:kSetCOLOR(0x999999) forState:UIControlStateNormal];"
118
- puts " _#{name}.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightRegular];"
119
- puts " [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateNormal];"
120
- puts " [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateSelected];"
121
- puts " [_#{name} addTarget:self action:@selector(actionHandler:) forControlEvents:UIControlEventTouchUpInside];"
131
+ puts_i " _#{name}.backgroundColor = kBackgroundColor;"
132
+ puts_i " [_#{name} setTitle:@\"xxx\" forState:UIControlStateNormal];"
133
+ puts_i " [_#{name} setTitleColor:kSetCOLOR(0x999999) forState:UIControlStateNormal];"
134
+ puts_i " _#{name}.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightRegular];"
135
+ puts_i " [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateNormal];"
136
+ puts_i " [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateSelected];"
137
+ puts_i " [_#{name} addTarget:self action:@selector(actionHandler:) forControlEvents:UIControlEventTouchUpInside];"
122
138
  end
123
139
  end
124
140
 
125
141
  def print_layout
126
142
  names.each do |name|
127
- puts "[contentView addSubview:self.#{name}];"
128
- puts "[self.#{name}.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor constant:0].active = YES;"
129
- puts "[self.#{name}.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor constant:0].active = YES;"
130
- puts "[self.#{name}.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = YES;"
131
- puts "[self.#{name}.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor].active = YES;"
132
- puts "[self.#{name}.widthAnchor constraintEqualToConstant:80].active = YES;"
133
- puts "[self.#{name}.heightAnchor constraintEqualToConstant:80].active = YES;"
143
+ puts_l "[contentView addSubview:self.#{name}];"
144
+ puts_l "[self.#{name}.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor constant:0].active = YES;"
145
+ puts_l "[self.#{name}.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor constant:0].active = YES;"
146
+ puts_l "[self.#{name}.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = YES;"
147
+ puts_l "[self.#{name}.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor].active = YES;"
148
+ puts_l "[self.#{name}.widthAnchor constraintEqualToConstant:80].active = YES;"
149
+ puts_l "[self.#{name}.heightAnchor constraintEqualToConstant:80].active = YES;"
134
150
  if type.eql?('UILabel')
135
- puts "[self.#{name} setContentHuggingPriority:300 forAxis:UILayoutConstraintAxisHorizontal];"
136
- puts "[self.#{name} setContentCompressionResistancePriority:300 forAxis:UILayoutConstraintAxisHorizontal];"
151
+ puts_l "[self.#{name} setContentHuggingPriority:300 forAxis:UILayoutConstraintAxisHorizontal];"
152
+ puts_l "[self.#{name} setContentCompressionResistancePriority:300 forAxis:UILayoutConstraintAxisHorizontal];"
137
153
  end
138
- puts "\n\n"
154
+ puts_l "\n\n"
139
155
  end
140
156
  end
141
157
 
142
158
  def print_value
143
159
  names.each do |name|
144
160
  if type.eql?('UILabel')
145
- puts "self.#{name}.text = @\"xxxxx\";"
161
+ puts_v "self.#{name}.text = @\"xxxxx\";"
146
162
  end
147
163
  end
148
164
  end
@@ -0,0 +1,15 @@
1
+ module Lhj
2
+ # format
3
+ module ErbFormatter
4
+ # base
5
+ class Base
6
+ def initialize(runner)
7
+ @runner = runner
8
+ end
9
+
10
+ def render
11
+ 'Implement global help here'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lhj
4
+ module ErbFormatter
5
+ # context
6
+ class Context
7
+ def initialize(target)
8
+ @target = target
9
+ end
10
+
11
+ def fetch_binding
12
+ @target.instance_eval { binding }.tap do |bind|
13
+ decorate_binding(bind)
14
+ end
15
+ end
16
+
17
+ # 空方法
18
+ def decorate_binding(bind)
19
+ bind.eval('save = 1')
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'erb'
2
+ require_relative 'base'
3
+ require_relative 'command_context'
4
+
5
+ module Lhj
6
+ module ErbFormatter
7
+ # service
8
+ class Service < Base
9
+ # @param [String] name
10
+ def render(name = 'yapi')
11
+ template(name).result(Context.new(@runner).fetch_binding)
12
+ end
13
+
14
+ def template(name)
15
+ if RUBY_VERSION < '2.6'
16
+ ERB.new(File.read(File.join(File.dirname(__FILE__), 'template', "#{name}.erb")), nil, '-')
17
+ else
18
+ ERB.new(File.read(File.join(File.dirname(__FILE__), 'template', "#{name}.erb")), trim_mode: '-')
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ <% if @result_model_name %>
2
+ if (responseMessage.resultCode == 0 && responseMessage.error == nil) {
3
+ if ([responseMessage.detailMsg isKindOfClass:[NSDictionary class]]) {
4
+ <%= @result_model_name %> *info = <%= @result_model_name %> yy_modelWithDictionary:responseMessage.detailMsg];
5
+ callback(info,responseMessage);
6
+ }
7
+ } else {
8
+ callback(nil,responseMessage);
9
+ }
10
+ <% end %>
@@ -0,0 +1,28 @@
1
+ /**
2
+ * <%= @title %> -- <%= @username %>
3
+ * <%= @desc %>
4
+ */
5
+ static NSString * const <%= @path_key %> = @"<%= @path %>";
6
+
7
+ <% if @method == 'GET' -%>
8
+ +(void)get<%= @path_name %>:(<%= @param_model_name %> *)param callback:(void (^)(<% unless @result_model_name.empty? -%><%= @result_model_name %> *model, <% end -%>MLResponseMessage *response))callback
9
+ {
10
+ [MLNetworkingManager getWithUrl:<%= @path_key %> params:params response:^(MLResponseMessage *responseMessage) {
11
+ <%= @model_template %>
12
+ }];
13
+ }
14
+ <% elsif @method == 'JSON' -%>
15
+ +(void)postJson<%= @path_name %>:(<%= @param_model_name %> *)param callback:(void (^)(<% unless @result_model_name.empty? -%><%= @result_model_name %> *model, <% end -%>MLResponseMessage *response))callback
16
+ {
17
+ [MLNetworkingManager requestJsonWithUrl:<%= @path_key %> httpMedth:kRequestMethodPOST params:param response:^(MLResponseMessage *responseMessage) {
18
+ <%= @model_template %>
19
+ }];
20
+ }
21
+ <% else -%>
22
+ +(void)post<%= @path_name %>:(<%= @param_model_name %> *)param callback:(void (^)(<% unless @result_model_name.empty? -%><%= @result_model_name %> *model, <% end -%>MLResponseMessage *response))callback
23
+ {
24
+ [MLNetworkingManager postWithUrl:<%= @path_key %> params:params response:^(MLResponseMessage *response) {
25
+ <%= @model_template %>
26
+ }];
27
+ }
28
+ <% end -%>
@@ -2,6 +2,7 @@ require 'net/https'
2
2
  require 'uri'
3
3
  require 'json'
4
4
  require 'yaml'
5
+ require 'lhj/command/yapi/formatters/service'
5
6
 
6
7
  module Lhj
7
8
  class Command
@@ -22,23 +23,58 @@ module Lhj
22
23
  @id = argv.option('id')
23
24
  @model_pre_name = argv.option('model-pre')
24
25
  @save = argv.flag?('save', false)
26
+ @debug = argv.flag?('debug', false)
27
+ @language = argv.option('lan', 'oc')
25
28
  @http_url = ''
26
29
  @http_headers = []
27
- @data_json = {}
28
- @models = []
29
30
  @config_id = ''
30
31
  @config_model_pre = 'ML'
31
32
  @model_default_suffix = 'Model'
32
33
  @type_trans = {}
33
34
  @config_model_names = []
34
35
  @model_names = []
36
+ # <<<===== model for template
37
+ @result_model_name = ''
38
+ @param_model_name = ''
39
+ @model_template = ''
40
+ @desc = ''
41
+ @path = ''
42
+ @path_name = ''
43
+ @path_key = ''
44
+ @method = 'GET'
45
+ @title = ''
46
+ @username = ''
47
+ @req_body_is_json_schema = false
48
+ # =====>>>
35
49
  super
36
50
  end
37
51
 
52
+ def begin_title
53
+ '读取配置文件~/.lhj/yapi.yml'
54
+ end
55
+
38
56
  def handle
39
- load_config
40
- fetch_model
41
- print_methods
57
+ process(yml_file) if File.exist?(yml_file)
58
+ end
59
+
60
+ def process(config_file)
61
+ load_config(config_file)
62
+ res_body = req_api_model
63
+ unless res_body['errcode'].to_i.zero?
64
+ puts '请修改 ~/.lhj/yapi.yml文件的对应的__wpkreporterwid_,_yapi_token,_yapi_uid这三个值'.red
65
+ return
66
+ end
67
+ return unless res_body && !res_body.empty?
68
+
69
+ # 1.print request result
70
+ print_res_body_model(res_body)
71
+ # 2.print request json body
72
+ print_req_body_model(res_body)
73
+ # 3.print request param
74
+ print_req_query(res_body['data'])
75
+ # 4.print request method
76
+ print_http_method(res_body['data'])
77
+ # 5.save to file
42
78
  save_to_file if @save
43
79
  end
44
80
 
@@ -55,6 +91,12 @@ module Lhj
55
91
  @h_file_array << str
56
92
  end
57
93
 
94
+ def puts_h_red(str)
95
+ puts str.red
96
+ @h_file_array ||= []
97
+ @h_file_array << str
98
+ end
99
+
58
100
  def puts_m(str)
59
101
  puts str.blue
60
102
  @m_file_array ||= []
@@ -75,9 +117,12 @@ module Lhj
75
117
  "#{@http_url}#{api_id}"
76
118
  end
77
119
 
78
- def load_config
79
- yml = File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
80
- config = YAML.load_file(yml)
120
+ def yml_file
121
+ File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
122
+ end
123
+
124
+ def load_config(file)
125
+ config = YAML.load_file(file)
81
126
  config.each do |k, v|
82
127
  @http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
83
128
  end
@@ -101,79 +146,91 @@ module Lhj
101
146
  @config_model_suffix || @model_default_suffix
102
147
  end
103
148
 
104
- def req_model
149
+ def req_api_model
105
150
  uri = URI.parse(url_str)
106
151
  req = Net::HTTP::Get.new(uri)
107
152
  req['Cookie'] = @http_headers.join('; ')
108
153
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
109
154
  http.request(req)
110
155
  end
111
- puts res.body unless res.body['errcode'].to_i.zero?
112
- JSON.parse(res.body)
156
+ res_json = JSON.parse(res.body)
157
+ puts res.body unless res_json['errcode'].to_i.zero?
158
+ puts res.body if @debug
159
+ res_json
113
160
  end
114
161
 
115
- def fetch_model
116
- res_json = req_model
117
- begin
118
- puts "\n<===============打印返回数据模型-Begin=====================>\n".green
119
- fetch_res_boy(res_json)
120
- print_models
121
- print_models_implementation
122
- puts "\n<===============打印返回数据模型-End=====================>\n".green
162
+ def print_res_body_model(res_json)
163
+ res_body = fetch_res_boy(res_json)
164
+ return unless res_body
165
+
166
+ puts "\n<===============打印返回数据模型-Begin=====================>\n".green
167
+ models = []
168
+ handle_model(res_body) do |model|
169
+ models << model
123
170
  end
124
- begin
125
- puts "\n<===============打印请求模型-Begin=====================>\n".green
126
- @models = []
127
- @model_names = []
128
- fetch_req_body(res_json)
129
- print_models
130
- print_models_implementation
131
- puts "\n<===============打印请求模型-End=====================>\n".green
171
+ case @language
172
+ when 'oc'
173
+ print_models(models)
174
+ print_models_impl(models)
175
+ when 'java'
176
+ print_models_for_java(models)
132
177
  end
178
+ @result_model_name = models.last[:name]
179
+ puts "\n<===============打印返回数据模型-End=====================>\n".green
133
180
  end
134
181
 
135
- def fetch_res_boy(res_json)
136
- if res_json && res_json['data']
137
- @data_json = res_json['data']
138
- if @data_json['res_body']
139
- begin
140
- res_body = JSON.parse(@data_json['res_body'])
141
- detail_obj = res_body['properties']['detailMsg'] || {}
142
- detail_obj['name'] = gen_model_name('')
143
- handle_model(detail_obj)
144
- rescue => ex
145
- puts ex
146
- end
147
- end
182
+ def print_req_body_model(res_json)
183
+ req_body = fetch_req_body(res_json)
184
+ return unless req_body
185
+
186
+ puts "\n<===============打印请求模型-Begin=====================>\n".green
187
+ models = []
188
+ handle_model(req_body) do |model|
189
+ models << model
190
+ end
191
+ case @language
192
+ when 'oc'
193
+ print_models(models)
194
+ print_models_impl(models)
195
+ when 'java'
196
+ print_models_for_java(models)
148
197
  end
198
+ @param_model_name = models.last[:name]
199
+ puts "\n<===============打印请求模型-End=====================>\n".green
200
+ end
201
+
202
+ def fetch_res_boy(res_json)
203
+ return if !res_json || !res_json['data'] || !res_json['data']['res_body']
204
+
205
+ res_body = JSON.parse(res_json['data']['res_body'])
206
+ return if !res_body['properties'] || !res_body['properties']['detailMsg']
207
+
208
+ result = res_body['properties']['detailMsg']
209
+ return unless result['type'] == 'object' || result['type'] == 'array'
210
+
211
+ result['name'] = gen_model_name('')
212
+ result
149
213
  end
150
214
 
151
215
  def fetch_req_body(res_json)
152
- if res_json && res_json['data']
153
- @data_json = res_json['data']
154
- if @data_json['req_body_other']
155
- begin
156
- res_body = JSON.parse(@data_json['req_body_other'])
157
- res_body['name'] = gen_model_name('')
158
- handle_model(res_body)
159
- rescue => ex
160
- puts ex
161
- end
162
- end
163
- end
216
+ return if !res_json || !res_json['data'] || !res_json['data']['req_body_other']
217
+
218
+ result = JSON.parse(res_json['data']['req_body_other'])
219
+ result['name'] = gen_model_name('')
220
+ result
164
221
  end
165
222
 
166
223
  def gen_model_name(name)
167
224
  n = name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { $&.upcase }
168
225
  if n.length <= 0
169
- n = @config_model_names.detect { |c| !@model_names.any? { |n| n.gsub(/#{model_pre}(.*)Model/, '\1').eql?(c) } }
226
+ n = @config_model_names.detect { |c| @model_names.none? { |na| na.gsub(/#{model_pre}(.*)Model/, '\1').eql?(c) } }
170
227
  end
171
228
  model_name = "#{model_pre}#{n}#{model_suffix}"
172
229
  @model_names << model_name
173
230
  model_name
174
231
  end
175
232
 
176
- def handle_model(model)
233
+ def handle_model(model, &block)
177
234
  p_type = model['type']
178
235
  p_name = model['name']
179
236
  p_properties = model['properties']
@@ -192,22 +249,22 @@ module Lhj
192
249
  c_model[:type_name] = 'NSString'
193
250
  else
194
251
  c_model[:type_name] = o['name']
195
- handle_model(o)
252
+ handle_model(o, &block)
196
253
  end
197
254
  end
198
255
  properties << c_model
199
256
  end
200
257
  p_model[:properties] = properties
201
- @models << p_model
258
+ block[p_model] if block_given?
202
259
  when 'array'
203
260
  t = model['items']
204
261
  t['name'] = p_name
205
- handle_model(t)
262
+ handle_model(t, &block)
206
263
  end
207
264
  end
208
265
 
209
- def print_models
210
- @models.each do |model|
266
+ def print_models(models)
267
+ models.each do |model|
211
268
  model_name = model[:name] || ''
212
269
  model_properties = model[:properties]
213
270
  puts_h "@interface #{model_name} : NSObject"
@@ -218,8 +275,20 @@ module Lhj
218
275
  end
219
276
  end
220
277
 
221
- def print_models_implementation
222
- @models.each do |model|
278
+ def print_models_for_java(models)
279
+ models.each do |model|
280
+ model_name = model[:name] || ''
281
+ model_properties = model[:properties]
282
+ puts_h "public class #{model_name} extends BaseModel implements BusinessEvent.PorductDataCollect {"
283
+ model_properties.each do |m|
284
+ print_model_for_java(m)
285
+ end
286
+ puts_h "}\n\n\n"
287
+ end
288
+ end
289
+
290
+ def print_models_impl(models)
291
+ models.each do |model|
223
292
  puts_m "@implementation #{model[:name]}"
224
293
  str = model[:properties].filter { |p| p[:type].eql?('array') && !p[:type_name].eql?('NSString') }.map { |p| "@\"#{p[:key]}\": #{p[:type_name]}.class" }.join(', ')
225
294
  if str && str.length.positive?
@@ -243,9 +312,9 @@ module Lhj
243
312
  case type
244
313
  when 'integer'
245
314
  puts_h "@property (nonatomic, assign) NSInteger #{key};"
246
- if des.include?('分')
247
- puts_h '/////////==========删掉其中一个属性'
248
- puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
315
+ if des.include?('分') || des.include?('0.01')
316
+ puts_h_red '/////////==========删掉其中一个属性'
317
+ puts_h_red "@property (nonatomic, strong) MLCentNumber *#{key};"
249
318
  end
250
319
  when 'cent'
251
320
  puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
@@ -268,38 +337,73 @@ module Lhj
268
337
  end
269
338
  end
270
339
 
271
- def print_methods
272
- puts "\n<===============方法调用=====================>\n".green
273
- puts_m '/**'
274
- puts_m " * #{@data_json['title']} -- #{@data_json['username']}"
275
- puts_m ' */'
276
- key_str = @data_json['path'].split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { $&.upcase } }.join('')
277
- key = "k#{key_str}URL"
278
- puts_m "static NSString * const #{key} = @\"#{@data_json['path']}\";"
279
- puts_m "\n\n"
280
- puts_h '@interface MLParamModel : NSObject'
281
- @data_json['req_query'].each do |h|
282
- des = h['desc']
283
- puts_h "///#{des} #{h['example']}"
284
- puts_h "@property (nonatomic, copy) NSString *#{h['name']};"
285
- end
286
- puts_h '@end'
287
- puts "\n\n"
288
- model = @models.last
289
- if @data_json['method'].eql?('GET')
290
- puts_m " [MLNetworkingManager getWithUrl:#{key} params:nil response:^(MLResponseMessage *responseMessage) {"
291
- puts_m ' if (response.resultCode == 0 && !response.error){'
292
- puts_m ' NSDictionary *detailMsg = response.detailMsg'
293
- puts_m " #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
294
- puts_m ' }'
295
- puts_m ' }];'
340
+ def print_model_for_java(m)
341
+ key = m[:key]
342
+ type_name = m[:type_name]
343
+ type = m[:type]
344
+ des = m[:description] || ''
345
+ des.gsub!(/\n/, ' ')
346
+ default = m[:default]
347
+ case type
348
+ when 'integer'
349
+ puts_h "int #{key};//#{des} #{default}"
350
+ when 'cent'
351
+ puts_h "int #{key};//#{des} #{default}"
352
+ when 'string'
353
+ puts_h "String #{key};//#{des} #{default}"
354
+ when 'number'
355
+ puts_h "double #{key};//#{des} #{default}"
356
+ when 'float'
357
+ puts_h "double #{key};//#{des} #{default}"
358
+ when 'double'
359
+ puts_h "double #{key};//#{des} #{default}"
360
+ when 'boolean'
361
+ puts_h "boolean #{key};//#{des} #{default}"
362
+ when 'object'
363
+ puts_h "#{type_name} #{key};//#{des} #{default}"
364
+ when 'array'
365
+ puts_h "#{type_name}[] #{key};//#{des} #{default}"
296
366
  else
297
- puts_m " [MLNetworkingManager postWithUrl:#{key} params:nil response:^(MLResponseMessage *responseMessage) {"
298
- puts_m ' if (response.resultCode == 0 && !response.error){'
299
- puts_m ' NSDictionary *detailMsg = response.detailMsg'
300
- puts_m " #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
301
- puts_m ' }'
302
- puts_m ' }];'
367
+ puts_h "@property (nonatomic, copy) NSString *#{key};//#{des} #{default}"
368
+ end
369
+ end
370
+
371
+ # @param [Object] data
372
+ def print_req_query(data)
373
+ return unless data && data['req_query']
374
+
375
+ case @language
376
+ when 'oc'
377
+ puts_h '@interface MLParamModel : NSObject'
378
+ data['req_query'].each do |h|
379
+ des = h['desc']
380
+ puts_h "///#{des} #{h['example']}"
381
+ puts_h "@property (nonatomic, copy) NSString *#{h['name']};"
382
+ end
383
+ puts_h '@end'
384
+ puts "\n\n"
385
+ when 'java'
386
+ end
387
+ @param_model_name = 'MLParamModel' if @param_model_name.empty?
388
+ end
389
+
390
+ def print_http_method(data)
391
+ return unless data
392
+
393
+ @path = data['path']
394
+ @method = data['method']
395
+ @title = data['title']
396
+ @username = data['username']
397
+ @desc = data['desc']
398
+ @method = 'JSON' if data['req_body_is_json_schema']
399
+ @path_name = @path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { $&.upcase } }.join('') if @path
400
+ @path_key = "k#{@path_name}URL"
401
+ case @language
402
+ when 'oc'
403
+ puts "\n<===============方法调用=====================>\n".green
404
+ @model_template = Lhj::ErbFormatter::Service.new(self).render('model')
405
+ puts Lhj::ErbFormatter::Service.new(self).render('yapi').blue
406
+ when 'java'
303
407
  end
304
408
  end
305
409
  end
data/lib/lhj/command.rb CHANGED
@@ -30,8 +30,12 @@ module Lhj
30
30
  self.abstract_command = true
31
31
  self.command = 'lhj'
32
32
 
33
+ def begin_title
34
+ '开始处理...'
35
+ end
36
+
33
37
  def auto_spin
34
- puts "开始处理...\n".green
38
+ puts begin_title.green
35
39
  # @spinner.auto_spin
36
40
  end
37
41
 
@@ -42,7 +46,7 @@ module Lhj
42
46
 
43
47
  def initialize(argv)
44
48
  super(argv)
45
- @spinner = TTY::Spinner.new('...', output: $stdout, format: :dots, clear: true)
49
+ @spinner = TTY::Spinner.new('[:spinner]...', output: $stdout, format: :dots, clear: true)
46
50
  end
47
51
 
48
52
  def run
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lhj
4
4
  module Tools
5
- VERSION = "0.1.9"
5
+ VERSION = "0.1.13"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhj-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-23 00:00:00.000000000 Z
11
+ date: 2022-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide
@@ -142,16 +142,16 @@ dependencies:
142
142
  name: colored
143
143
  requirement: !ruby/object:Gem::Requirement
144
144
  requirements:
145
- - - ">="
145
+ - - '='
146
146
  - !ruby/object:Gem::Version
147
- version: '0'
147
+ version: '1.2'
148
148
  type: :runtime
149
149
  prerelease: false
150
150
  version_requirements: !ruby/object:Gem::Requirement
151
151
  requirements:
152
- - - ">="
152
+ - - '='
153
153
  - !ruby/object:Gem::Version
154
- version: '0'
154
+ version: '1.2'
155
155
  - !ruby/object:Gem::Dependency
156
156
  name: excon
157
157
  requirement: !ruby/object:Gem::Requirement
@@ -297,6 +297,11 @@ files:
297
297
  - lib/lhj/command/trans.rb
298
298
  - lib/lhj/command/view.rb
299
299
  - lib/lhj/command/yapi.rb
300
+ - lib/lhj/command/yapi/formatters/base.rb
301
+ - lib/lhj/command/yapi/formatters/command_context.rb
302
+ - lib/lhj/command/yapi/formatters/service.rb
303
+ - lib/lhj/command/yapi/formatters/template/model.erb
304
+ - lib/lhj/command/yapi/formatters/template/yapi.erb
300
305
  - lib/lhj/config.rb
301
306
  - lib/lhj/helper/local_config.rb
302
307
  - lib/lhj/helper/oss_config.rb