lib/lhj/command/yapi.rb in lhj-tools-0.1.75 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.76

- old
+ new

@@ -19,11 +19,13 @@ def self.options [ %w[--id api的id], %w[--model-pre 模型的前缀], %w[--model-name 模型的名称], - %w[--save 保存生成文件] + %w[--save 保存生成文件], + %w[--sync 同步到github], + %w[--notify 通知到钉钉] ] end def initialize(argv) @id = argv.option('id') @@ -38,13 +40,12 @@ @save = true if @sync @http_headers = [] @config_id = '' @config_model_pre = 'ML' @config_model_name = 'Result' - @model_default_suffix = 'Model' + @config_base_url = 'http://yapi.xx.com' @type_trans = {} - @base_url = 'http://yapi.xx.com' super end def handle process(yml_file) if File.exist?(yml_file) @@ -61,17 +62,17 @@ # get project info project_id = res_body['data']['project_id'] project_info = get_project_info(project_id) # 1.print request result - result_model_name = print_res_body_model(res_body) + print_res_body_model(res_body) # 2.print request json body - param_model_name = print_req_body_model(res_body) + print_req_body_model(res_body) if res_body['data']['req_body_is_json_schema'] # 3.print request param - print_req_query(res_body['data']) + print_req_query(res_body['data']) unless res_body['data']['req_body_is_json_schema'] # 4.print request method - service_code = print_http_method(res_body['data'], project_info, result_model_name, param_model_name) + service_code = print_http_method(res_body['data'], project_info) # 5.save to file file_map = save_to_file(service_code) if @save # 6.push to git push_to_git if @sync # 7.notify robot @@ -105,47 +106,53 @@ end def save_to_file(service_code) name = model_name file_name = gen_model_name - unless File.exist?(File.expand_path(sub_folder_name, '.')) - FileUtils.mkdir_p(File.expand_path(sub_folder_name, '.')) - end + FileUtils.mkdir_p(File.expand_path(sub_folder_name, '.')) unless File.exist?(File.expand_path(sub_folder_name, '.')) h_file = File.join('.', sub_folder_name, "#{file_name}.h") m_file = File.join('.', sub_folder_name, "#{file_name}.m") service_file = File.join('.', sub_folder_name, "#{model_pre}#{name}Service.m") File.write(h_file, @h_file_array.join("\n")) if @h_file_array.count.positive? File.write(m_file, @m_file_array.join("\n")) if @m_file_array.count.positive? File.write(service_file, service_code) if service_code - puts "\n\n生成文件成功!所在路径:\n#{File.expand_path(h_file)} \n#{File.expand_path(m_file)}".green - { h_file: h_file, m_file: m_file, s_file: service_file } + puts "\n\n生成文件成功!所在路径:\n#{File.expand_path(h_file)} \n#{File.expand_path(m_file)}".green if @save && !@notify + { + h_file: "#{sub_folder_name}/#{file_name}.h", + m_file: "#{sub_folder_name}/#{file_name}.m", + s_file: "#{sub_folder_name}/#{model_pre}#{name}Service.m" + } end def push_to_git Actions.sh('git checkout master') Actions.sh('git add .') Actions.sh("git commit -m 'generate yapi code'") Actions.sh('git push') end - def notify_robot(file_info, interface_info, project_info) - temp_vars = file_info.merge({ - api_id: api_id, - interface_url: "#{project_info['data']['basepath']}#{interface_info['data']['path']}" - }) - robot_url = 'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807' + def notify_robot(upload_info, interface_info, project_info) + i_url = "#{project_info['data']['basepath']}#{interface_info['data']['path']}" + username = interface_info['data']['username'] + title = interface_info['data']['title'] + temp_vars = upload_info.merge({ + api_id: api_id, + title: title, + username: username, + interface_url: i_url + }) template = Lhj::ErbTemplateHelper.load('oc_code_notify') output = Lhj::ErbTemplateHelper.render(template, temp_vars, '-') - Lhj::Dingtalk.post_message_robot(robot_url, '生成代码', output) + Lhj::Dingtalk.post_message_robot(robot_url, 'yapi generate', output) end def interface_url_str - "#{@base_url}/#{API_INTERFACE_URL}#{api_id}" + "#{@config_base_url}/#{API_INTERFACE_URL}#{api_id}" end def project_url_str(project_id) - "#{@base_url}/#{API_PROJECT_URL}#{project_id}" + "#{@config_base_url}/#{API_PROJECT_URL}#{project_id}" end def yml_file File.join(Lhj::Config.instance.home_dir, 'yapi.yml') end @@ -153,15 +160,16 @@ 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 - @base_url = config['base_url'] + @config_base_url = config['base_url'] @config_id = config['id'] @config_model_pre = config['model_pre'] @config_model_suffix = config['model_suffix'] @config_model_name = config['model_name'] + @config_robot_url = config['dingtalk'] @type_trans = config['type_trans'] end def api_id @id || @config_id.to_s @@ -174,17 +182,21 @@ def model_name @model_result_name || @config_model_name end def req_model_name - 'Param' + 'RequestParam' end def model_suffix - @config_model_suffix || @model_default_suffix + @config_model_suffix || 'Model' end + def robot_url + @config_robot_url || 'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807' + end + def get_interface_api_model uri = URI.parse(interface_url_str) req = Net::HTTP::Get.new(uri) req['Cookie'] = @http_headers.join('; ') res = Net::HTTP.start(uri.hostname, uri.port) do |http| @@ -222,11 +234,10 @@ print_models_impl(models) when 'java' print_models_for_java(models) end puts "\n<===============打印返回数据模型-End=====================>\n".green - models.last[:name] end def print_req_body_model(res_json) req_body = fetch_req_body(res_json) return unless req_body @@ -242,20 +253,20 @@ print_models_impl(models) when 'java' print_models_for_java(models) end puts "\n<===============打印请求模型-End=====================>\n".green - models.last[:name] end def fetch_res_boy(res_json) 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'] + result = res_body + result = res_body['properties']['detailMsg'] if res_body['properties'] && res_body['properties']['detailMsg'] + return unless result['type'] == 'object' || result['type'] == 'array' result['name'] = gen_model_name result end @@ -268,16 +279,19 @@ result end def gen_model_name(property_name = nil, type = :res) name = model_name - name = req_model_name if type == :req unless property_name.nil? name = property_name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { Regexp.last_match(0).upcase } name = property_name.gsub(/^\w/) { Regexp.last_match(0).upcase } if name.length <= 0 end - "#{model_pre}#{name}#{model_suffix}" + if type == :req + "#{model_pre}#{name}#{req_model_name}" + else + "#{model_pre}#{name}#{model_suffix}" + end end def handle_model(model, &block) p_type = model['type'] p_name = model['name'] @@ -412,11 +426,12 @@ def print_req_query(data) return unless data && data['req_query'] case @language when 'oc' - puts_h '@interface MLParamModel : NSObject' + param_model_name = gen_model_name(nil, :req) + puts_h "@interface #{param_model_name} : NSObject" data['req_query'].each do |h| des = h['desc'] puts_h "///#{des} #{h['example']}" puts_h "@property (nonatomic, copy) NSString *#{h['name']};" end @@ -424,18 +439,27 @@ puts "\n\n" when 'java' end end - def print_http_method(data, project_info, result_model_name, param_model_name) + def print_http_method(data, project_info) return unless data path = data['path'] if path - path_name = path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { Regexp.last_match(0).upcase } }.join('') + arr = path.split('/').map do |s| + re = s.gsub(/[^A-Za-z0-9](.)/) { Regexp.last_match(0).upcase } + re = re.gsub(/[^A-Za-z0-9]/, '') + re.gsub(/^\w/) { Regexp.last_match(0).upcase } + end + path_name = arr.join('') end path_key = "k#{path_name}URL" + display_path = "#{project_info['data']['basepath']}#{path}" + display_path = display_path[1..-1] if display_path.index('/') == 0 + result_model_name = gen_model_name + param_model_name = gen_model_name(nil, :req) mth = data['method'] mth = 'JSON' if data['req_body_is_json_schema'] case @language when 'oc' puts "\n<===============方法调用=====================>\n".green @@ -444,14 +468,14 @@ yapi_temp = Lhj::ErbTemplateHelper.load('oc_code_service') yapi_vars = { title: data['title'], desc: data['desc'], username: data['username'], - path: "#{project_info['data']['basepath']}#{path}", + path: display_path, path_key: path_key, path_name: path_name, result_model_name: result_model_name, - param_model_name: param_model_name || 'MLParamModel', + param_model_name: param_model_name, mth: mth, model_template: model_temp_result } yapi_temp_result = Lhj::ErbTemplateHelper.render(yapi_temp, yapi_vars, '-') puts yapi_temp_result.green return yapi_temp_result