lhj-tools 0.1.75 → 0.1.77

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: 7dc72ca4edb2bdcb5abd9ced2b89e97843bbd4393527e28be7b0c750d3f00be7
4
- data.tar.gz: 88b0b3444ba3c78ce03bb2bdcb32eef0fa77ea23eec27f41225ea1a7df9a6aa1
3
+ metadata.gz: d672d1cf1227b28c98ba5103e9ee855726948df87cd020304dcfd6e8a6ceee63
4
+ data.tar.gz: 2be43594360da9049569d7c5e1d2f60e68b4a837dce9d4c743f5d326dc2d0a15
5
5
  SHA512:
6
- metadata.gz: 27eb16b78e42d2ea284b68cf6678949367a489a15e9fb6961b8b0d3debe74e30daa76f96079416a7f00530c72c183b1ef842882e04bfb4ecbe4a1a974ba1e37a
7
- data.tar.gz: 6426a55e2043d21cfcd35d49b05400db2485e6433982d98348db95ef98f3d6bbce210a020d509cc9a1efe1a7fdf843be857279bc0dc72f0e8eaba490a9772304
6
+ metadata.gz: '039225bc897b4c4173d42291bd5a008ba0e4d7804d1fe3607357d28d9b10f030cbf9fdbbb60f12734b09c1ead843ed00481fb3e3e3dc3f52d64d7031e7f91cb1'
7
+ data.tar.gz: e39146285cceb19741b7bafead5d63dd422d05f0371838aef011ecf4417c12aee3041d6cd42f1317223b2a2c28ed42ac6232559c70662014c1bd59e71c7ad2aa
@@ -25,8 +25,8 @@ module Lhj
25
25
  end
26
26
 
27
27
  def render_template(template_name)
28
- temp_str = File.read(File.join(File.dirname(__FILE__), 'template', template_name))
29
- Lhj::ErbFormatter::Service.new(self).render_template(temp_str)
28
+ temp = File.read(File.join(File.dirname(__FILE__), 'template', template_name))
29
+ Lhj::ErbTemplateHelper.render(temp, nil, nil)
30
30
  end
31
31
 
32
32
  end
@@ -21,7 +21,9 @@ module Lhj
21
21
  %w[--id api的id],
22
22
  %w[--model-pre 模型的前缀],
23
23
  %w[--model-name 模型的名称],
24
- %w[--save 保存生成文件]
24
+ %w[--save 保存生成文件],
25
+ %w[--sync 同步到github],
26
+ %w[--notify 通知到钉钉]
25
27
  ]
26
28
  end
27
29
 
@@ -40,9 +42,8 @@ module Lhj
40
42
  @config_id = ''
41
43
  @config_model_pre = 'ML'
42
44
  @config_model_name = 'Result'
43
- @model_default_suffix = 'Model'
45
+ @config_base_url = 'http://yapi.xx.com'
44
46
  @type_trans = {}
45
- @base_url = 'http://yapi.xx.com'
46
47
  super
47
48
  end
48
49
 
@@ -63,13 +64,13 @@ module Lhj
63
64
  project_id = res_body['data']['project_id']
64
65
  project_info = get_project_info(project_id)
65
66
  # 1.print request result
66
- result_model_name = print_res_body_model(res_body)
67
+ print_res_body_model(res_body)
67
68
  # 2.print request json body
68
- param_model_name = print_req_body_model(res_body)
69
+ print_req_body_model(res_body) if res_body['data']['req_body_is_json_schema']
69
70
  # 3.print request param
70
- print_req_query(res_body['data'])
71
+ print_req_query(res_body['data']) unless res_body['data']['req_body_is_json_schema']
71
72
  # 4.print request method
72
- service_code = print_http_method(res_body['data'], project_info, result_model_name, param_model_name)
73
+ service_code = print_http_method(res_body['data'], project_info)
73
74
  # 5.save to file
74
75
  file_map = save_to_file(service_code) if @save
75
76
  # 6.push to git
@@ -107,17 +108,19 @@ module Lhj
107
108
  def save_to_file(service_code)
108
109
  name = model_name
109
110
  file_name = gen_model_name
110
- unless File.exist?(File.expand_path(sub_folder_name, '.'))
111
- FileUtils.mkdir_p(File.expand_path(sub_folder_name, '.'))
112
- end
111
+ FileUtils.mkdir_p(File.expand_path(sub_folder_name, '.')) unless File.exist?(File.expand_path(sub_folder_name, '.'))
113
112
  h_file = File.join('.', sub_folder_name, "#{file_name}.h")
114
113
  m_file = File.join('.', sub_folder_name, "#{file_name}.m")
115
114
  service_file = File.join('.', sub_folder_name, "#{model_pre}#{name}Service.m")
116
115
  File.write(h_file, @h_file_array.join("\n")) if @h_file_array.count.positive?
117
116
  File.write(m_file, @m_file_array.join("\n")) if @m_file_array.count.positive?
118
117
  File.write(service_file, service_code) if service_code
119
- puts "\n\n生成文件成功!所在路径:\n#{File.expand_path(h_file)} \n#{File.expand_path(m_file)}".green
120
- { h_file: h_file, m_file: m_file, s_file: service_file }
118
+ puts "\n\n生成文件成功!所在路径:\n#{File.expand_path(h_file)} \n#{File.expand_path(m_file)}".green if @save && !@notify
119
+ {
120
+ h_file: "#{sub_folder_name}/#{file_name}.h",
121
+ m_file: "#{sub_folder_name}/#{file_name}.m",
122
+ s_file: "#{sub_folder_name}/#{model_pre}#{name}Service.m"
123
+ }
121
124
  end
122
125
 
123
126
  def push_to_git
@@ -127,23 +130,34 @@ module Lhj
127
130
  Actions.sh('git push')
128
131
  end
129
132
 
130
- def notify_robot(file_info, interface_info, project_info)
131
- temp_vars = file_info.merge({
132
- api_id: api_id,
133
- interface_url: "#{project_info['data']['basepath']}#{interface_info['data']['path']}"
134
- })
135
- robot_url = 'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
133
+ def notify_robot(upload_info, interface_info, project_info)
134
+ i_url = "#{project_info['data']['basepath']}#{interface_info['data']['path']}"
135
+ username = interface_info['data']['username']
136
+ title = interface_info['data']['title']
137
+ p_id = project_info['data']['_id']
138
+ api_url = api_original_url(p_id)
139
+ temp_vars = upload_info.merge({
140
+ api_id: api_id,
141
+ title: title,
142
+ username: username,
143
+ interface_url: i_url,
144
+ api_url: api_url
145
+ })
136
146
  template = Lhj::ErbTemplateHelper.load('oc_code_notify')
137
147
  output = Lhj::ErbTemplateHelper.render(template, temp_vars, '-')
138
- Lhj::Dingtalk.post_message_robot(robot_url, '生成代码', output)
148
+ Lhj::Dingtalk.post_message_robot(robot_url, 'yapi generate', output)
139
149
  end
140
150
 
141
151
  def interface_url_str
142
- "#{@base_url}/#{API_INTERFACE_URL}#{api_id}"
152
+ "#{@config_base_url}/#{API_INTERFACE_URL}#{api_id}"
143
153
  end
144
154
 
145
155
  def project_url_str(project_id)
146
- "#{@base_url}/#{API_PROJECT_URL}#{project_id}"
156
+ "#{@config_base_url}/#{API_PROJECT_URL}#{project_id}"
157
+ end
158
+
159
+ def api_original_url(project_id)
160
+ "#{@config_base_url}/project/#{project_id}/interface/api/#{api_id}"
147
161
  end
148
162
 
149
163
  def yml_file
@@ -155,11 +169,12 @@ module Lhj
155
169
  config.each do |k, v|
156
170
  @http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
157
171
  end
158
- @base_url = config['base_url']
172
+ @config_base_url = config['base_url']
159
173
  @config_id = config['id']
160
174
  @config_model_pre = config['model_pre']
161
175
  @config_model_suffix = config['model_suffix']
162
176
  @config_model_name = config['model_name']
177
+ @config_robot_url = config['dingtalk']
163
178
  @type_trans = config['type_trans']
164
179
  end
165
180
 
@@ -176,11 +191,15 @@ module Lhj
176
191
  end
177
192
 
178
193
  def req_model_name
179
- 'Param'
194
+ 'RequestParam'
180
195
  end
181
196
 
182
197
  def model_suffix
183
- @config_model_suffix || @model_default_suffix
198
+ @config_model_suffix || 'Model'
199
+ end
200
+
201
+ def robot_url
202
+ @config_robot_url || 'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
184
203
  end
185
204
 
186
205
  def get_interface_api_model
@@ -224,7 +243,6 @@ module Lhj
224
243
  print_models_for_java(models)
225
244
  end
226
245
  puts "\n<===============打印返回数据模型-End=====================>\n".green
227
- models.last[:name]
228
246
  end
229
247
 
230
248
  def print_req_body_model(res_json)
@@ -244,16 +262,16 @@ module Lhj
244
262
  print_models_for_java(models)
245
263
  end
246
264
  puts "\n<===============打印请求模型-End=====================>\n".green
247
- models.last[:name]
248
265
  end
249
266
 
250
267
  def fetch_res_boy(res_json)
251
268
  return if !res_json || !res_json['data'] || !res_json['data']['res_body']
252
269
 
253
270
  res_body = JSON.parse(res_json['data']['res_body'])
254
- return if !res_body['properties'] || !res_body['properties']['detailMsg']
255
271
 
256
- result = res_body['properties']['detailMsg']
272
+ result = res_body
273
+ result = res_body['properties']['detailMsg'] if res_body['properties'] && res_body['properties']['detailMsg']
274
+
257
275
  return unless result['type'] == 'object' || result['type'] == 'array'
258
276
 
259
277
  result['name'] = gen_model_name
@@ -270,12 +288,15 @@ module Lhj
270
288
 
271
289
  def gen_model_name(property_name = nil, type = :res)
272
290
  name = model_name
273
- name = req_model_name if type == :req
274
291
  unless property_name.nil?
275
292
  name = property_name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { Regexp.last_match(0).upcase }
276
293
  name = property_name.gsub(/^\w/) { Regexp.last_match(0).upcase } if name.length <= 0
277
294
  end
278
- "#{model_pre}#{name}#{model_suffix}"
295
+ if type == :req
296
+ "#{model_pre}#{name}#{req_model_name}"
297
+ else
298
+ "#{model_pre}#{name}#{model_suffix}"
299
+ end
279
300
  end
280
301
 
281
302
  def handle_model(model, &block)
@@ -414,7 +435,8 @@ module Lhj
414
435
 
415
436
  case @language
416
437
  when 'oc'
417
- puts_h '@interface MLParamModel : NSObject'
438
+ param_model_name = gen_model_name(nil, :req)
439
+ puts_h "@interface #{param_model_name} : NSObject"
418
440
  data['req_query'].each do |h|
419
441
  des = h['desc']
420
442
  puts_h "///#{des} #{h['example']}"
@@ -426,14 +448,23 @@ module Lhj
426
448
  end
427
449
  end
428
450
 
429
- def print_http_method(data, project_info, result_model_name, param_model_name)
451
+ def print_http_method(data, project_info)
430
452
  return unless data
431
453
 
432
454
  path = data['path']
433
455
  if path
434
- path_name = path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { Regexp.last_match(0).upcase } }.join('')
456
+ arr = path.split('/').map do |s|
457
+ re = s.gsub(/[^A-Za-z0-9](.)/) { Regexp.last_match(0).upcase }
458
+ re = re.gsub(/[^A-Za-z0-9]/, '')
459
+ re.gsub(/^\w/) { Regexp.last_match(0).upcase }
460
+ end
461
+ path_name = arr.join('')
435
462
  end
436
463
  path_key = "k#{path_name}URL"
464
+ display_path = "#{project_info['data']['basepath']}#{path}"
465
+ display_path = display_path[1..-1] if display_path.index('/') == 0
466
+ result_model_name = gen_model_name
467
+ param_model_name = gen_model_name(nil, :req)
437
468
  mth = data['method']
438
469
  mth = 'JSON' if data['req_body_is_json_schema']
439
470
  case @language
@@ -446,11 +477,11 @@ module Lhj
446
477
  yapi_vars = { title: data['title'],
447
478
  desc: data['desc'],
448
479
  username: data['username'],
449
- path: "#{project_info['data']['basepath']}#{path}",
480
+ path: display_path,
450
481
  path_key: path_key,
451
482
  path_name: path_name,
452
483
  result_model_name: result_model_name,
453
- param_model_name: param_model_name || 'MLParamModel',
484
+ param_model_name: param_model_name,
454
485
  mth: mth,
455
486
  model_template: model_temp_result }
456
487
  yapi_temp_result = Lhj::ErbTemplateHelper.render(yapi_temp, yapi_vars, '-')
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lhj
4
4
  module Tools
5
- VERSION = "0.1.75"
5
+ VERSION = "0.1.77"
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.75
4
+ version: 0.1.77
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-11 00:00:00.000000000 Z
11
+ date: 2022-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xcodeproj