Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerwzy committed Sep 18, 2017
1 parent dd084c1 commit 1518c0b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
50 changes: 32 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ let baseClasses = program.base.length || ['PMLResponseModelBaseHD', 'PMLMode
(async () => {
let content = await readFile(program.file),
$ = cheerio.load(content),
contentJSON = [];
responseJSON = [],
argsJSON = [];
$(".wiki-content>.table-wrap").each((i, table) => {
if(i%2 == 0) return;
processTable(table);
if(i%2 == 0) return parseRequestTable(table);
parseResponseTable(table);
});
if(program.debug) await fs.writeJson('./output.json', contentJSON);
await parseTemplate(contentJSON, $('.wiki-content p').text());
// 解析文本中的
if(program.debug) await fs.writeJson('./output.json', responseJSON).catch(console.log);
await parseTemplate(responseJSON, $('.wiki-content p').text(), argsJSON).catch(console.log);
if(program.verbose) console.log("done!");

function processTable(table, classMeta) {
function parseResponseTable(table, classMeta) {
let modelName = "",
baseName = baseClasses[0],
isRoot = true; // 子类如果需要继承不同的基类, 则利用此标识
Expand All @@ -79,9 +79,9 @@ function processTable(table, classMeta) {
if(rowIsTable){
// 进入这个方法,说明上一行标识这一行是子类
rowIsTable = false;
// return processTable($(tr).children("td").children(".table-wrap"), complexProperty);
// return parseResponseTable($(tr).children("td").children(".table-wrap"), complexProperty);
// 有时候文档把子表格写在了第二个 td 里...
return processTable($(tr).find(".table-wrap").eq(0), complexProperty);
return parseResponseTable($(tr).find(".table-wrap").eq(0), complexProperty);
}
let tds = $(tr).children('td');
let nameMatch = /[a-z]+/ig.exec(tds.eq(0).text());
Expand Down Expand Up @@ -119,9 +119,21 @@ function processTable(table, classMeta) {
}
props.push(prop);
}); // end of basetable > tr > foreach
contentJSON.push({"isRoot": isRoot, "className": modelName,"baseName": baseName, "props": props});
responseJSON.push({"isRoot": isRoot, "className": modelName,"baseName": baseName, "props": props});
if(program.verbose) console.log("生成模型:", modelName)
}
function parseRequestTable(table) {
let param_des = [];
$(table).children(".confluenceTable").children("tbody").children("tr")
.each((i, tr) => {
if(i<2) return;
let tds = Array.from($(tr).children('td'));
if($(tds[0]).text() == '业务参数') return;
let propdes = tds.map(t=>$(t).text()).join(' ');
param_des.push(propdes);
});
argsJSON.push(param_des);
}
})().catch(console.log);


Expand Down Expand Up @@ -165,7 +177,7 @@ function assumeVarType(str, isArray, model) {
else if(l_str == strlist) model_type = "NSArray<NSString *> *"
else if(l_str == intlist) model_type = "NSArray<NSInteger> *";
else {
console.log("====user defined type: =====", str);
if(program.verbose) console.log("====user defined type: =====", str);
model_type = model + " *";
}
var_type = isArray ? "NSArray<"+model_type+"> *" : model_type;;
Expand Down Expand Up @@ -193,9 +205,9 @@ async function genFileFromTemplate(filepath, tpl) {
return await fs.writeFile(filepath, tpl, 'utf8').catch(console.log);
}

async function parseTemplate(filejson, filetext) {
// 参数: 模型, 文本, 入参描述
async function parseTemplate(filejson, filetext, argdes) {
if(program.verbose) console.log("开始应用模板");
// 以后有多套模板生成的时候, 就用参数传入
let copyright = program.copyright,
projectname = program.project,
author = program.author,
Expand All @@ -215,8 +227,8 @@ async function parseTemplate(filejson, filetext) {
await fs.emptyDir(out_model); // 创建/清空输出文件夹
await fs.emptyDir(out_task); // 创建/清空输出文件夹
if(program.verbose) console.log("开始生成实体类")
for(let model of filejson) {
if(classCollect.filter(m=>m==model).length>1) {
filejson.forEach(async (model, index) => {
if(classCollect.filter(m=>m==model.className).length>1) {
if(exist_file.includes(model)) return;
exist_file.push(model);
}
Expand All @@ -225,16 +237,18 @@ async function parseTemplate(filejson, filetext) {
// 输出路径
let h_file = getPath(out_model, model.className+'.h'),
m_file = getPath(out_model, model.className+'.m');
await genFileFromTemplate(h_file, eval(h_content));
await genFileFromTemplate(m_file, eval(m_content));
}
await genFileFromTemplate(h_file, eval(h_content)).catch(console.log);
await genFileFromTemplate(m_file, eval(m_content)).catch(console.log);
});
// ===================
// gen http request (task) file
// ===================
if(program.verbose) console.log("开始生成请求类");
let h_file = getPath(out_task, `${modulename}.h`),
m_file = getPath(out_task, `${modulename}.m`),
endpoints = filetext.match(/(\/.*?\.json)/ig); // 从文档中提取接口地址
if(argdes.length!=endpoints.length)
return console.log("接口数量与入参数量不一致的", endpoints, argdes);
if(rootclasses.length!=typeDes.length)
return console.log("接口描述数量与接口返回值数量不一致", typeDes, rootclasses);
if(rootclasses.length!=endpoints.length)
Expand Down
5 changes: 3 additions & 2 deletions scripts/13.签约关系.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ models=(
'查询签约信息(团队签约):modelGroupBindResponse,modelGroupBindInfo,modelBindGroupRole,modelBindGroupDoctor'
'修改用户默认签约关系:modelModifyBindResponse'
'查询居民签约信息列表:modelUserBindListResponse,modelUBAgreementInfo'
'查询签约详情:modelAgreementDetailResponse,modelAgreementdetailInfo,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'查询签约详情(第三方,非登陆):modelThirdQueryBindDetailResponse,modelAgreementdetailInfo,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'查询签约详情:modelAgreementDetailResponse,modelAgreementDetailInfo,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'查询签约详情(第三方,非登陆):modelThirdQueryBindDetailResponse,modelAgreementDetailInfo3rd,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'签约套餐列表信息:modelServicePkgsListResponse,modelServicePkgsListItem'
'签约服务包详情:modelServicePkgDetailResponse,modelServicePkgDetailInfo,modelServicePkgDetailContent'
)
Expand All @@ -26,6 +26,7 @@ datakeys=(
'agreementServicePkgs'
'expertList'
'groupRoleList'
'servicePkgItemDOList'
)

for model in ${models[@]}; do
Expand Down
3 changes: 2 additions & 1 deletion scripts/15.签约申请.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ passkeys='code,flag,message'
fileuri='/Users/walker/Desktop/index.html'
debug=true
verbose=false
modulename=taskAgreementApply
models=(
'获取配置信息:modelAgreementConfigResponse,modelAgreementConfigInfo'
'查询签约申请详情:modelAgreementDetailResponse,modelAgreementDetailInfo,modelAgreementServicePkgItem,modelAgreementTagItem,modelAgreementAttathItem'
'查询签约申请详情:modelAgreementApplyDetailResponse,modelAgreementApplyDetailInfo,modelAgreementServicePkgItem,modelAgreementTagItem,modelAgreementAttathItem'
'新建签约申请(草稿):modelAgreementApplyAddResponse'
'修改签约申请(草稿):modelAgreementApplyModifyResponse'
'提交签约申请(审核):modelAgreementSubmitResponse,modelAgreementSubmitInfo'
Expand Down
4 changes: 2 additions & 2 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ models=(
'查询签约信息(团队签约):modelGroupBindResponse,modelGroupBindInfo,modelBindGroupRole,modelBindGroupDoctor'
'修改用户默认签约关系:modelModifyBindResponse'
'查询居民签约信息列表:modelUserBindListResponse,modelUBAgreementInfo'
'查询签约详情:modelAgreementDetailResponse,modelAgreementdetailInfo,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'查询签约详情(第三方,非登陆):modelThirdQueryBindDetailResponse,modelAgreementdetailInfo,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'查询签约详情:modelAgreementDetailResponse,modelAgreementDetailInfo,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'查询签约详情(第三方,非登陆):modelThirdQueryBindDetailResponse,modelAgreementDetailInfo3rd,modelAgreementServicePksInfo,modelAgreementTagInfo,modelAgreementAttatch'
'签约套餐列表信息:modelServicePkgsListResponse,modelServicePkgsListItem'
'签约服务包详情:modelServicePkgDetailResponse,modelServicePkgDetailInfo,modelServicePkgDetailContent'
)
Expand Down
7 changes: 5 additions & 2 deletions templates/httpclient/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

@interface ${modulename} : PMLRESTBaseHD
${
endpoints.map(e=>`
endpoints.map((e,i)=>`
/**
* ${e.des}
*/
${argdes[i].map(p=>
` * @param ${p}
`).join('')} */
- (NSURLSessionDataTask *)${e.method}:(NSDictionary *)params completion:(void (^)(${e.model} *response, NSError *error))completion;`).join('')}

@end`
10 changes: 5 additions & 5 deletions templates/httpclient/task.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "${modulename}.h"
${endpoints.map(e=>`
#define kEndpoint${e.method} @"${e.path}" // ${e.des}`).join('')}
#define kEndpoint${e.method} @"${e.path}" // ${e.des}`).join('')}

@implementation ${modulename}

Expand All @@ -17,13 +17,13 @@ @implementation ${modulename}
+ (NSDictionary<NSString *,id> *)modelClassesByResourcePath {
return @{
${endpoints.map(e=>
`kEndpoint${e.method}: [${e.model} class],`
).join('').slice(0,-1)}
`kEndpoint${e.method}: [${e.model} class],
`).join('').slice(0,-1)}
};
}

${endpoints.map(e=>
`PMTASK_EXPORT_IMP(${e.method}, ${e.model})`
).join('')}
`PMTASK_EXPORT_IMP(${e.method}, ${e.model})
`).join('')}

@end`

0 comments on commit 1518c0b

Please sign in to comment.