lib/lhj/command/yapi.rb in lhj-tools-0.1.12 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.13

- old
+ new

@@ -22,10 +22,11 @@ def initialize(argv) @id = argv.option('id') @model_pre_name = argv.option('model-pre') @save = argv.flag?('save', false) @debug = argv.flag?('debug', false) + @language = argv.option('lan', 'oc') @http_url = '' @http_headers = [] @config_id = '' @config_model_pre = 'ML' @model_default_suffix = 'Model' @@ -165,12 +166,17 @@ puts "\n<===============打印返回数据模型-Begin=====================>\n".green models = [] handle_model(res_body) do |model| models << model end - print_models(models) - print_models_impl(models) + case @language + when 'oc' + print_models(models) + print_models_impl(models) + when 'java' + print_models_for_java(models) + end @result_model_name = models.last[:name] puts "\n<===============打印返回数据模型-End=====================>\n".green end def print_req_body_model(res_json) @@ -180,12 +186,17 @@ puts "\n<===============打印请求模型-Begin=====================>\n".green models = [] handle_model(req_body) do |model| models << model end - print_models(models) - print_models_impl(models) + case @language + when 'oc' + print_models(models) + print_models_impl(models) + when 'java' + print_models_for_java(models) + end @param_model_name = models.last[:name] puts "\n<===============打印请求模型-End=====================>\n".green end def fetch_res_boy(res_json) @@ -262,10 +273,22 @@ end puts_h "@end\n\n\n" end end + def print_models_for_java(models) + models.each do |model| + model_name = model[:name] || '' + model_properties = model[:properties] + puts_h "public class #{model_name} extends BaseModel implements BusinessEvent.PorductDataCollect {" + model_properties.each do |m| + print_model_for_java(m) + end + puts_h "}\n\n\n" + end + end + 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? @@ -312,23 +335,57 @@ else puts_h "@property (nonatomic, copy) NSString *#{key};" end end + def print_model_for_java(m) + key = m[:key] + type_name = m[:type_name] + type = m[:type] + des = m[:description] || '' + des.gsub!(/\n/, ' ') + default = m[:default] + case type + when 'integer' + puts_h "int #{key};//#{des} #{default}" + when 'cent' + puts_h "int #{key};//#{des} #{default}" + when 'string' + puts_h "String #{key};//#{des} #{default}" + when 'number' + puts_h "double #{key};//#{des} #{default}" + when 'float' + puts_h "double #{key};//#{des} #{default}" + when 'double' + puts_h "double #{key};//#{des} #{default}" + when 'boolean' + puts_h "boolean #{key};//#{des} #{default}" + when 'object' + puts_h "#{type_name} #{key};//#{des} #{default}" + when 'array' + puts_h "#{type_name}[] #{key};//#{des} #{default}" + else + puts_h "@property (nonatomic, copy) NSString *#{key};//#{des} #{default}" + end + end + # @param [Object] data def print_req_query(data) return unless data && data['req_query'] - puts_h '@interface MLParamModel : NSObject' - data['req_query'].each do |h| - des = h['desc'] - puts_h "///#{des} #{h['example']}" - puts_h "@property (nonatomic, copy) NSString *#{h['name']};" + case @language + when 'oc' + puts_h '@interface MLParamModel : NSObject' + 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" + when 'java' end - puts_h '@end' - puts "\n\n" - @param_model_name = 'MLParamModel' if @param_model_name.empty? end def print_http_method(data) return unless data @@ -339,12 +396,16 @@ @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 + case @language + when 'oc' + puts "\n<===============方法调用=====================>\n".green + @model_template = Lhj::ErbFormatter::Service.new(self).render('model') + puts Lhj::ErbFormatter::Service.new(self).render('yapi').blue + when 'java' + end end end end end