/
jframe
193 lines (164 loc) · 5.02 KB
/
jframe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env luajit
local version = require("libs.version")
-- 使用说明
local usages = [[jframe v]] .. version .. [[, a Lua web framework based on OpenResty.
Usage: jframe COMMAND [OPTIONS]
Commands:
controller [name] Create a new controller
model [name] [table] Create a new model
version Show version of the framework
help Show help tips
]]
-- 控制器模板
local controller_tpl = [[
-- Copyright (C) fanqingxuan (Json)
-- https://github.com/fanqingxuan
local Base = require("controller.base")
local {{controllerName}} = Base:extend()
-- 获取列表
function {{controllerName}}:index()
self:json({
data = {
{
id = 1,
title = 'this is title'
},
{
id = 2,
title = '这是标题'
},
}
},'成功')
end
-- 新增
function {{controllerName}}:create()
self:json({
data = {}
},'创建成功')
end
-- 删除
function {{controllerName}}:delete()
local id = self.request.query.id or 0 --获取主键
self:json({
data = {}
},'删除成功')
end
-- 修改
function {{controllerName}}:update()
local id = self.request.query.id or 0 --获取主键
self:json({
data = {}
},'修改成功')
end
-- 查询
function {{controllerName}}:show()
local id = self.request.query.id or 0 --获取主键
self:json({
data = {
id = id,
title = 'this is test title',
content = '这是测试内容'
}
},'成功')
end
return {{controllerName}}
]]
-- model模板
local model_tpl = [[
-- Copyright (C) fanqingxuan (Json)
-- https://github.com/fanqingxuan
local Base = require "model.base"
local {{modelName}} = Base:extend()
-- function {{modelName}}:getList(name,pwd)
-- return self:query("select * from sls_p_user where rname=? and pwd=?",{name,pwd})
-- end
return {{modelName}}("{{tableName}}")
]]
-- 获取处理后的内容
local function get_content(tpl,pattern,replacement,Camelcase)
--首字符大小,其它字母消息
if Camelcase then
replacement = string.lower(replacement)
replacement = string.upper(string.sub(replacement,1,1)) .. string.sub(replacement,2)
end
local content,count = string.gsub(tpl,pattern,replacement)
return content,count
end
local function dirname(str)
if str:match(".-/.-") then
local name = string.gsub(str, "(.*/)(.*)", "%1")
return string.sub( name, 1,-2 )
else
return ''
end
end
-- 创建文件
local function create_files(parent,file_path,file_content)
local full_file_path = parent .. '/' .. file_path..'.lua'
local full_file_dirname = dirname(full_file_path)
local fw,err = io.open(full_file_path, 'w')
if err then
print("open file error,please confirm the directory permission ")
os.exit()
end
fw:write(file_content)
fw:close()
print(' created file ' .. full_file_path)
end
-- 检测是否都是字母
local function checkstring(str)
return string.match(str,'^%a+$')
end
-- 生成controller
local function generator_controller(controller)
if not checkstring(controller) then
print("generate controller failed,controller name all must be characters")
os.exit()
end
local content,count = get_content(controller_tpl,"{{controllerName}}",controller,true)
if count<=0 then
print("generate controller failed,please check the controller name")
os.exit()
end
create_files('controller',controller,content)
print(' created controller ' .. controller .. ' success')
end
-- 生成model
local function generator_model(model,table)
if not checkstring(model) then
print("generate model failed,model name all must be characters")
os.exit()
end
local content,count = get_content(model_tpl,"{{modelName}}",model,true)
if count<=0 then
print("generate model failed,please check the model name")
os.exit()
end
local tableName = table or model:lower()
local content,count = get_content(content,"{{tableName}}",tableName)
if count<=0 then
print("generate model failed,please check the table name")
os.exit()
end
create_files('model',model,content)
print(' created model ' .. model .. ' success')
end
-- 执行命令
local function exec(args)
local arg = table.remove(args, 1)
-- parse commands and options
if arg == 'controller' and args[1] then
generator_controller(args[1]:lower()) -- create controller
elseif arg == 'model' and args[1] then
generator_model(args[1]:lower(),args[2] or nil) -- create model
elseif arg == 'help' or arg == '-h' then
print(usages)
elseif arg == 'version' or arg == '-v' then
print(version) -- show framework version
elseif arg == nil then
print(usages)
else
print("[jframe] unsupported commands or options, `jframe -h` to check usages.")
end
end
exec(arg)