lib/lhj/command/yapi.rb in lhj-tools-0.1.9 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.10
- old
+ new
@@ -1,9 +1,10 @@
require 'net/https'
require 'uri'
require 'json'
require 'yaml'
+require 'lhj/command/yapi/formatters/service'
module Lhj
class Command
# generate model from yapi
class Yapi < Command
@@ -20,27 +21,57 @@
def initialize(argv)
@id = argv.option('id')
@model_pre_name = argv.option('model-pre')
@save = argv.flag?('save', false)
+ @debug = argv.flag?('debug', false)
@http_url = ''
@http_headers = []
- @data_json = {}
- @models = []
@config_id = ''
@config_model_pre = 'ML'
@model_default_suffix = 'Model'
@type_trans = {}
@config_model_names = []
@model_names = []
+ # <<<===== model for template
+ @result_model_name = ''
+ @param_model_name = ''
+ @model_template = ''
+ @desc = ''
+ @path = ''
+ @path_name = ''
+ @path_key = ''
+ @method = 'GET'
+ @title = ''
+ @username = ''
+ @req_body_is_json_schema = false
+ # =====>>>
super
end
+ def begin_title
+ '读取配置文件~/.lhj/yapi.yml'
+ end
+
def handle
- load_config
- fetch_model
- print_methods
+ process(yml_file) if File.exist?(yml_file)
+ end
+
+ def process(config_file)
+ load_config(config_file)
+ res_body = req_api_model
+ return unless res_body && res_body['errcode'].to_i.zero? && !res_body.empty?
+
+ # 1.print request result
+ print_res_body_model(res_body)
+ # 2.print request json body
+ print_req_body_model(res_body)
+ # 3.print request param
+ print_req_query(res_body['data'])
+ # 4.print request method
+ print_http_method(res_body['data'])
+ # 5.save to file
save_to_file if @save
end
def test_ding
require 'net/http'
@@ -53,10 +84,16 @@
puts str.magenta
@h_file_array ||= []
@h_file_array << str
end
+ def puts_h_red(str)
+ puts str.red
+ @h_file_array ||= []
+ @h_file_array << str
+ end
+
def puts_m(str)
puts str.blue
@m_file_array ||= []
@m_file_array << str
end
@@ -73,13 +110,16 @@
def url_str
"#{@http_url}#{api_id}"
end
- def load_config
- yml = File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
- config = YAML.load_file(yml)
+ def yml_file
+ File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
+ end
+
+ def load_config(file)
+ config = YAML.load_file(file)
config.each do |k, v|
@http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
end
@http_url = config['url']
@config_id = config['id']
@@ -99,83 +139,85 @@
def model_suffix
@config_model_suffix || @model_default_suffix
end
- def req_model
+ def req_api_model
uri = URI.parse(url_str)
req = Net::HTTP::Get.new(uri)
req['Cookie'] = @http_headers.join('; ')
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
- puts res.body unless res.body['errcode'].to_i.zero?
- JSON.parse(res.body)
+ res_json = JSON.parse(res.body)
+ puts res.body unless res_json['errcode'].to_i.zero?
+ puts res.body if @debug
+ res_json
end
- def fetch_model
- res_json = req_model
- begin
- puts "\n<===============打印返回数据模型-Begin=====================>\n".green
- fetch_res_boy(res_json)
- print_models
- print_models_implementation
- puts "\n<===============打印返回数据模型-End=====================>\n".green
+ def print_res_body_model(res_json)
+ res_body = fetch_res_boy(res_json)
+ return unless res_body
+
+ puts "\n<===============打印返回数据模型-Begin=====================>\n".green
+ models = []
+ handle_model(res_body) do |model|
+ models << model
end
- begin
- puts "\n<===============打印请求模型-Begin=====================>\n".green
- @models = []
- @model_names = []
- fetch_req_body(res_json)
- print_models
- print_models_implementation
- puts "\n<===============打印请求模型-End=====================>\n".green
+ print_models(models)
+ print_models_impl(models)
+ @result_model_name = models.last[:name]
+ puts "\n<===============打印返回数据模型-End=====================>\n".green
+ end
+
+ def print_req_body_model(res_json)
+ req_body = fetch_req_body(res_json)
+ return unless req_body
+
+ puts "\n<===============打印请求模型-Begin=====================>\n".green
+ models = []
+ handle_model(req_body) do |model|
+ models << model
end
+ print_models(models)
+ print_models_impl(models)
+ @param_model_name = models.last[:name]
+ puts "\n<===============打印请求模型-End=====================>\n".green
end
def fetch_res_boy(res_json)
- if res_json && res_json['data']
- @data_json = res_json['data']
- if @data_json['res_body']
- begin
- res_body = JSON.parse(@data_json['res_body'])
- detail_obj = res_body['properties']['detailMsg'] || {}
- detail_obj['name'] = gen_model_name('')
- handle_model(detail_obj)
- rescue => ex
- puts ex
- end
- end
- end
+ return if !res_json || !res_json['data'] || !res_json['data']['res_body']
+
+ res_body = JSON.parse(res_json['data']['res_body'])
+ return if !res_body['properties'] || !res_body['properties']['detailMsg']
+
+ result = res_body['properties']['detailMsg']
+ return unless result['type'] == 'object' || result['type'] == 'array'
+
+ result['name'] = gen_model_name('')
+ result
end
def fetch_req_body(res_json)
- if res_json && res_json['data']
- @data_json = res_json['data']
- if @data_json['req_body_other']
- begin
- res_body = JSON.parse(@data_json['req_body_other'])
- res_body['name'] = gen_model_name('')
- handle_model(res_body)
- rescue => ex
- puts ex
- end
- end
- end
+ return if !res_json || !res_json['data'] || !res_json['data']['req_body_other']
+
+ result = JSON.parse(res_json['data']['req_body_other'])
+ result['name'] = gen_model_name('')
+ result
end
def gen_model_name(name)
n = name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { $&.upcase }
if n.length <= 0
- n = @config_model_names.detect { |c| !@model_names.any? { |n| n.gsub(/#{model_pre}(.*)Model/, '\1').eql?(c) } }
+ n = @config_model_names.detect { |c| @model_names.none? { |na| na.gsub(/#{model_pre}(.*)Model/, '\1').eql?(c) } }
end
model_name = "#{model_pre}#{n}#{model_suffix}"
@model_names << model_name
model_name
end
- def handle_model(model)
+ def handle_model(model, &block)
p_type = model['type']
p_name = model['name']
p_properties = model['properties']
p_model = { name: p_name }
@@ -190,38 +232,38 @@
o['name'] = gen_model_name(k)
if v['type'].eql?('array') && v['items']['type'].eql?('string')
c_model[:type_name] = 'NSString'
else
c_model[:type_name] = o['name']
- handle_model(o)
+ handle_model(o, &block)
end
end
properties << c_model
end
p_model[:properties] = properties
- @models << p_model
+ block[p_model] if block_given?
when 'array'
t = model['items']
t['name'] = p_name
- handle_model(t)
+ handle_model(t, &block)
end
end
- def print_models
- @models.each do |model|
+ def print_models(models)
+ models.each do |model|
model_name = model[:name] || ''
model_properties = model[:properties]
puts_h "@interface #{model_name} : NSObject"
model_properties.each do |m|
print_model(m)
end
puts_h "@end\n\n\n"
end
end
- def print_models_implementation
- @models.each do |model|
+ def print_models_impl(models)
+ models.each do |model|
puts_m "@implementation #{model[:name]}"
str = model[:properties].filter { |p| p[:type].eql?('array') && !p[:type_name].eql?('NSString') }.map { |p| "@\"#{p[:key]}\": #{p[:type_name]}.class" }.join(', ')
if str && str.length.positive?
puts_m '+(NSDictionary *)modelContainerPropertyGenericClass {'
puts_m " return @{#{str}};"
@@ -241,13 +283,13 @@
default = m[:default]
puts_h "///#{des} #{default}"
case type
when 'integer'
puts_h "@property (nonatomic, assign) NSInteger #{key};"
- if des.include?('分')
- puts_h '/////////==========删掉其中一个属性'
- puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
+ if des.include?('分') || des.include?('0.01')
+ puts_h_red '/////////==========删掉其中一个属性'
+ puts_h_red "@property (nonatomic, strong) MLCentNumber *#{key};"
end
when 'cent'
puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
when 'string'
puts_h "@property (nonatomic, copy) NSString *#{key};"
@@ -266,42 +308,37 @@
else
puts_h "@property (nonatomic, copy) NSString *#{key};"
end
end
- def print_methods
- puts "\n<===============方法调用=====================>\n".green
- puts_m '/**'
- puts_m " * #{@data_json['title']} -- #{@data_json['username']}"
- puts_m ' */'
- key_str = @data_json['path'].split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { $&.upcase } }.join('')
- key = "k#{key_str}URL"
- puts_m "static NSString * const #{key} = @\"#{@data_json['path']}\";"
- puts_m "\n\n"
+ # @param [Object] data
+ def print_req_query(data)
+ return unless data && data['req_query']
+
puts_h '@interface MLParamModel : NSObject'
- @data_json['req_query'].each do |h|
+ data['req_query'].each do |h|
des = h['desc']
puts_h "///#{des} #{h['example']}"
puts_h "@property (nonatomic, copy) NSString *#{h['name']};"
end
puts_h '@end'
puts "\n\n"
- model = @models.last
- if @data_json['method'].eql?('GET')
- puts_m " [MLNetworkingManager getWithUrl:#{key} params:nil response:^(MLResponseMessage *responseMessage) {"
- puts_m ' if (response.resultCode == 0 && !response.error){'
- puts_m ' NSDictionary *detailMsg = response.detailMsg'
- puts_m " #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
- puts_m ' }'
- puts_m ' }];'
- else
- puts_m " [MLNetworkingManager postWithUrl:#{key} params:nil response:^(MLResponseMessage *responseMessage) {"
- puts_m ' if (response.resultCode == 0 && !response.error){'
- puts_m ' NSDictionary *detailMsg = response.detailMsg'
- puts_m " #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
- puts_m ' }'
- puts_m ' }];'
- end
+ end
+
+ def print_http_method(data)
+ return unless data
+
+ @path = data['path']
+ @method = data['method']
+ @title = data['title']
+ @username = data['username']
+ @desc = data['desc']
+ @method = 'JSON' if data['req_body_is_json_schema']
+ @path_name = @path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { $&.upcase } }.join('') if @path
+ @path_key = "k#{@path_name}URL"
+ puts "\n<===============方法调用=====================>\n".green
+ @model_template = Lhj::ErbFormatter::Service.new(self).render('model')
+ puts Lhj::ErbFormatter::Service.new(self).render('yapi').blue
end
end
end
end