lhj-tools 0.1.74 → 0.1.76

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: f86764f14d88348e59221a4e339765a6ec4488a815d179a710d15e7c415471a1
4
- data.tar.gz: 9f5635a3e149ce527aea1c298fa7a39316c446e75aaed941d23c6010ce0896de
3
+ metadata.gz: 3573173cffbb9453ae8e205034e2c28e9d7f4bde8600574cb42787f671716f91
4
+ data.tar.gz: ec7333d4b1078c07846ea533fade87a44ae18cfcbeab5311a078671af14d9904
5
5
  SHA512:
6
- metadata.gz: 144bfb6d35d9190b81ed1715dd518c2b707971815a3412f279f673faffb4c6dd36768c05c86da1249d9eed7328620a0588fe62a3753b249fd9fd8625726bb0d3
7
- data.tar.gz: 6d875c726247374420c5b2856614283a817078e073517a3dc5849b7a9258567eefffc9df76f3d54ec58836f647ef922ea6169c5fe9211d53630ab39a2fe78570
6
+ metadata.gz: 387b4adaaad5aa277799f85de8a2de6d898adaafee296c33e406b4c6818ef2a14651a4c191eadff85839880357a7a83b6b95cbb7c9827d38d392782303743209
7
+ data.tar.gz: 0aae4e701adb0b50446bad22b80bb23cc4eb7b55a920514d0f53b7f9a618de781b95fd61b7669d245a88337f3ba18298e72740e0a1b81c924686ef651c663f96
@@ -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,19 +64,19 @@ 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
76
77
  push_to_git if @sync
77
78
  # 7.notify robot
78
- notify_robot(file_map) if @notify
79
+ notify_robot(file_map, res_body, project_info) if @notify
79
80
  end
80
81
 
81
82
  def puts_h(str)
@@ -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, api_id: api_id }
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,19 +130,27 @@ module Lhj
127
130
  Actions.sh('git push')
128
131
  end
129
132
 
130
- def notify_robot(template_vars)
131
- 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
+ temp_vars = upload_info.merge({
138
+ api_id: api_id,
139
+ title: title,
140
+ username: username,
141
+ interface_url: i_url
142
+ })
132
143
  template = Lhj::ErbTemplateHelper.load('oc_code_notify')
133
- output = Lhj::ErbTemplateHelper.render(template, template_vars, '-')
134
- Lhj::Dingtalk.post_message_robot(robot_url, '生成代码', output)
144
+ output = Lhj::ErbTemplateHelper.render(template, temp_vars, '-')
145
+ Lhj::Dingtalk.post_message_robot(robot_url, 'yapi generate', output)
135
146
  end
136
147
 
137
148
  def interface_url_str
138
- "#{@base_url}/#{API_INTERFACE_URL}#{api_id}"
149
+ "#{@config_base_url}/#{API_INTERFACE_URL}#{api_id}"
139
150
  end
140
151
 
141
152
  def project_url_str(project_id)
142
- "#{@base_url}/#{API_PROJECT_URL}#{project_id}"
153
+ "#{@config_base_url}/#{API_PROJECT_URL}#{project_id}"
143
154
  end
144
155
 
145
156
  def yml_file
@@ -151,11 +162,12 @@ module Lhj
151
162
  config.each do |k, v|
152
163
  @http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
153
164
  end
154
- @base_url = config['base_url']
165
+ @config_base_url = config['base_url']
155
166
  @config_id = config['id']
156
167
  @config_model_pre = config['model_pre']
157
168
  @config_model_suffix = config['model_suffix']
158
169
  @config_model_name = config['model_name']
170
+ @config_robot_url = config['dingtalk']
159
171
  @type_trans = config['type_trans']
160
172
  end
161
173
 
@@ -172,11 +184,15 @@ module Lhj
172
184
  end
173
185
 
174
186
  def req_model_name
175
- 'Param'
187
+ 'RequestParam'
176
188
  end
177
189
 
178
190
  def model_suffix
179
- @config_model_suffix || @model_default_suffix
191
+ @config_model_suffix || 'Model'
192
+ end
193
+
194
+ def robot_url
195
+ @config_robot_url || 'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
180
196
  end
181
197
 
182
198
  def get_interface_api_model
@@ -220,7 +236,6 @@ module Lhj
220
236
  print_models_for_java(models)
221
237
  end
222
238
  puts "\n<===============打印返回数据模型-End=====================>\n".green
223
- models.last[:name]
224
239
  end
225
240
 
226
241
  def print_req_body_model(res_json)
@@ -240,16 +255,16 @@ module Lhj
240
255
  print_models_for_java(models)
241
256
  end
242
257
  puts "\n<===============打印请求模型-End=====================>\n".green
243
- models.last[:name]
244
258
  end
245
259
 
246
260
  def fetch_res_boy(res_json)
247
261
  return if !res_json || !res_json['data'] || !res_json['data']['res_body']
248
262
 
249
263
  res_body = JSON.parse(res_json['data']['res_body'])
250
- return if !res_body['properties'] || !res_body['properties']['detailMsg']
251
264
 
252
- result = res_body['properties']['detailMsg']
265
+ result = res_body
266
+ result = res_body['properties']['detailMsg'] if res_body['properties'] && res_body['properties']['detailMsg']
267
+
253
268
  return unless result['type'] == 'object' || result['type'] == 'array'
254
269
 
255
270
  result['name'] = gen_model_name
@@ -266,12 +281,15 @@ module Lhj
266
281
 
267
282
  def gen_model_name(property_name = nil, type = :res)
268
283
  name = model_name
269
- name = req_model_name if type == :req
270
284
  unless property_name.nil?
271
285
  name = property_name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { Regexp.last_match(0).upcase }
272
286
  name = property_name.gsub(/^\w/) { Regexp.last_match(0).upcase } if name.length <= 0
273
287
  end
274
- "#{model_pre}#{name}#{model_suffix}"
288
+ if type == :req
289
+ "#{model_pre}#{name}#{req_model_name}"
290
+ else
291
+ "#{model_pre}#{name}#{model_suffix}"
292
+ end
275
293
  end
276
294
 
277
295
  def handle_model(model, &block)
@@ -410,7 +428,8 @@ module Lhj
410
428
 
411
429
  case @language
412
430
  when 'oc'
413
- puts_h '@interface MLParamModel : NSObject'
431
+ param_model_name = gen_model_name(nil, :req)
432
+ puts_h "@interface #{param_model_name} : NSObject"
414
433
  data['req_query'].each do |h|
415
434
  des = h['desc']
416
435
  puts_h "///#{des} #{h['example']}"
@@ -422,14 +441,23 @@ module Lhj
422
441
  end
423
442
  end
424
443
 
425
- def print_http_method(data, project_info, result_model_name, param_model_name)
444
+ def print_http_method(data, project_info)
426
445
  return unless data
427
446
 
428
447
  path = data['path']
429
448
  if path
430
- path_name = path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { Regexp.last_match(0).upcase } }.join('')
449
+ arr = path.split('/').map do |s|
450
+ re = s.gsub(/[^A-Za-z0-9](.)/) { Regexp.last_match(0).upcase }
451
+ re = re.gsub(/[^A-Za-z0-9]/, '')
452
+ re.gsub(/^\w/) { Regexp.last_match(0).upcase }
453
+ end
454
+ path_name = arr.join('')
431
455
  end
432
456
  path_key = "k#{path_name}URL"
457
+ display_path = "#{project_info['data']['basepath']}#{path}"
458
+ display_path = display_path[1..-1] if display_path.index('/') == 0
459
+ result_model_name = gen_model_name
460
+ param_model_name = gen_model_name(nil, :req)
433
461
  mth = data['method']
434
462
  mth = 'JSON' if data['req_body_is_json_schema']
435
463
  case @language
@@ -442,11 +470,11 @@ module Lhj
442
470
  yapi_vars = { title: data['title'],
443
471
  desc: data['desc'],
444
472
  username: data['username'],
445
- path: "#{project_info['data']['basepath']}#{path}",
473
+ path: display_path,
446
474
  path_key: path_key,
447
475
  path_name: path_name,
448
476
  result_model_name: result_model_name,
449
- param_model_name: param_model_name || 'MLParamModel',
477
+ param_model_name: param_model_name,
450
478
  mth: mth,
451
479
  model_template: model_temp_result }
452
480
  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.74"
5
+ VERSION = "0.1.76"
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.74
4
+ version: 0.1.76
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-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xcodeproj