lib/lhj/command/yapi.rb in lhj-tools-0.1.65 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.66
- old
+ new
@@ -1,10 +1,12 @@
require 'net/https'
require 'uri'
require 'json'
require 'yaml'
require 'lhj/command/yapi/formatters/service'
+require 'lhj/helper/dingtalk_config'
+require 'lhj/helper/erb_template_helper'
module Lhj
class Command
# generate model from yapi
class Yapi < Command
@@ -20,41 +22,28 @@
end
def initialize(argv)
@id = argv.option('id')
@model_pre_name = argv.option('model-pre')
+ @language = argv.option('lan', 'oc')
@save = argv.flag?('save', false)
+ @sync = argv.flag?('sync', false)
+ @notify = argv.flag?('notify', false)
@debug = argv.flag?('debug', false)
- @language = argv.option('lan', 'oc')
- @http_url = ''
+ @sync = true if @notify
+ @save = true if @sync
@http_headers = []
@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
- # =====>>>
+ @http_url = ''
super
end
- def begin_title
- '读取配置文件~/.lhj/yapi.yml'
- end
-
def handle
process(yml_file) if File.exist?(yml_file)
end
def process(config_file)
@@ -65,28 +54,25 @@
return
end
return unless res_body && !res_body.empty?
# 1.print request result
- print_res_body_model(res_body)
+ result_model_name = print_res_body_model(res_body)
# 2.print request json body
- print_req_body_model(res_body)
+ param_model_name = 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'])
+ service_code = print_http_method(res_body['data'], result_model_name, param_model_name)
# 5.save to file
- save_to_file if @save
+ file_map = save_to_file(service_code) if @save
+ # 6.push to git
+ push_to_git if @sync
+ # 7.notify robot
+ notify_robot(file_map) if @notify
end
- def test_ding
- require 'net/http'
- require 'uri'
- body = { 'msgtype' => 'text', 'text' => { 'content' => 'error:上传蒲公英超时失败!' } }.to_json
- Net::HTTP.post(URI('https://oapi.dingtalk.com/robot/send?access_token=6a3519057170cdb1b7274edfe43934c84a0062ffe2c9bcced434699296a7e26e'), body, 'Content-Type' => 'application/json')
- end
-
def puts_h(str)
puts str.magenta
@h_file_array ||= []
@h_file_array << str
end
@@ -101,23 +87,49 @@
puts str.blue
@m_file_array ||= []
@m_file_array << str
end
- def save_to_file
+ def sub_folder_name
+ return @sub_folder_name if @sub_folder_name
+
+ time = Time.now
+ @sub_folder_name ||= time.strftime('%Y%m%d%H%M%S')
+ @sub_folder_name
+ end
+
+ def save_to_file(service_code)
@model_names = []
+ model_name = @config_model_names.frist
file_name = gen_model_name('')
- time = Time.now
- folder_name = time.strftime('%Y%m%d%H%M%S')
- FileUtils.mkdir_p(File.expand_path(folder_name, '.')) unless File.exist?(File.expand_path(folder_name, '.'))
- h_file = File.join('.', folder_name, "#{file_name}.h")
- m_file = File.join('.', folder_name, "#{file_name}.m")
+ unless File.exist?(File.expand_path(sub_folder_name, '.'))
+ FileUtils.mkdir_p(File.expand_path(sub_folder_name, '.'))
+ end
+ 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}#{model_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 }
end
+ def push_to_git
+ Actions.sh('git checkout master', log: false)
+ Actions.sh('git add .', log: false)
+ Actions.sh("git commit -m 'generate yapi code'", log: false)
+ Actions.sh('git push', log: false)
+ end
+
+ def notify_robot(template_vars)
+ robot_url = 'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
+ template = Lhj::ErbTemplateHelper.load('oc_code_notify')
+ output = Lhj::ErbTemplateHelper.render(template, template_vars, '-')
+ Lhj::Dingtalk.post_message_robot(robot_url, '生成代码', output)
+ end
+
def url_str
"#{@http_url}#{api_id}"
end
def yml_file
@@ -176,12 +188,12 @@
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
+ models.last[:name]
end
def print_req_body_model(res_json)
req_body = fetch_req_body(res_json)
return unless req_body
@@ -196,12 +208,12 @@
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
+ models.last[:name]
end
def fetch_res_boy(res_json)
return if !res_json || !res_json['data'] || !res_json['data']['res_body']
@@ -377,30 +389,42 @@
end
puts_h '@end'
puts "\n\n"
when 'java'
end
- @param_model_name = 'MLParamModel' if @param_model_name.empty?
end
- def print_http_method(data)
+ def print_http_method(data, result_model_name, param_model_name)
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"
+ path = data['path']
+ path_name = path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { $&.upcase } }.join('') if path
+ path_key = "k#{path_name}URL"
+ mth = data['method']
+ mth = 'JSON' if data['req_body_is_json_schema']
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
+ model_temp = Lhj::ErbTemplateHelper.load('oc_code_service_inner')
+ model_temp_result = Lhj::ErbTemplateHelper.render(model_temp, { result_model_name: result_model_name }, '-')
+
+ yapi_temp = Lhj::ErbTemplateHelper.load('oc_code_service')
+ yapi_vars = { title: data['title'],
+ desc: data['desc'],
+ username: data['username'],
+ path: path,
+ path_key: path_key,
+ path_name: path_name,
+ result_model_name: result_model_name,
+ param_model_name: param_model_name || 'MLParamModel',
+ 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
when 'java'
end
+
end
end
end
end