-
Notifications
You must be signed in to change notification settings - Fork 1
/
beans.lua
executable file
·258 lines (226 loc) · 7.92 KB
/
beans.lua
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package.path = package.path .. ";.lua/?.lua;migrations/?.lua;specs/?.lua"
require("utilities")
lester = require("lester")
ENV = {}
for _, var in pairs(unix.environ()) do
var = string.split(var, "=")
ENV[var[1]] = var[2]
end
BeansEnv = ENV["BEANS_ENV"] or "development"
DBConfig = DecodeJson(Slurp("config/database.json"))
Adb = require "arango"
RecursiveFiles = function(dir, files_found)
files_found = files_found or {}
for name, kind, ino, off in assert(unix.opendir(unix.getcwd() .. dir)) do
if name ~= "." and name ~= ".." then
if path.isdir(dir:sub(2) .. "/" .. name) then
files_found = RecursiveFiles(dir .. "/" .. name, files_found)
else
files_found = table.append(files_found, {dir .. "/" .. name})
end
end
end
return files_found
end
JSONPrint = function(value)
print(EncodeJson(value))
end
DefineWords = function(name)
return {
model = Camelize(name),
plural = Pluralize(name),
singular = name
}
end
ProcessTemplate = function(template, words)
template = string.gsub(template, "##model_plural##", words.plural)
template = string.gsub(template, "##model_singular##", words.singular)
return string.gsub(template, "##model_singular_capitalized##", words.model)
end
CreateView = function(controller_name)
local words = DefineWords(controller_name)
unix.mkdir("views/" .. words.plural)
local template = Slurp(".templates/views/index.etlua")
template = ProcessTemplate(template, words)
Barf("app/views/" .. words.plural .. "/index.etlua", template)
template = Slurp(".templates/views/new.etlua")
template = ProcessTemplate(template, words)
Barf("app/views/" .. words.plural .. "/new.etlua", template)
template = Slurp(".templates/views/edit.etlua")
template = ProcessTemplate(template, words)
Barf("app/views/" .. words.plural .. "/edit.etlua", template)
template = Slurp(".templates/views/show.etlua")
template = ProcessTemplate(template, words)
Barf("app/views/" .. words.plural .. "/show.etlua", template)
end
CreateController = function(controller_name)
local words = DefineWords(controller_name)
local template = Slurp(".templates/controller.lua")
local spec = Slurp(".templates/empty_spec.lua")
template = ProcessTemplate(template, words)
Barf("app/controllers/" .. words.plural .. "_controller.lua", template)
Barf("specs/controllers/" .. words.plural .. "_spec.lua", spec)
print("✅ app/controller/" .. words.plural .. "_controller.lua created")
print("✅ specs/controllers/" .. words.plural .. "_spec.lua created")
end
CreateMigration = function(name)
local words = DefineWords(name)
local template = Slurp(".templates/migration.lua")
template = ProcessTemplate(template, words)
Barf("migrations/" .. os.date("%Y%m%d-%H%I%S_") .. Slugify(name) .. ".lua", template)
print("✅ migrations/" .. os.date("%Y%m%d-%H%I%S_") .. Slugify(name) .. ".lua created")
end
CreateModel = function(model_name)
local words = DefineWords(model_name)
local template = Slurp(".templates/model.lua")
local spec = Slurp(".templates/empty_spec.lua")
template = ProcessTemplate(template, words)
Barf("app/models/" .. words.singular .. ".lua", template)
Barf("specs/models/" .. words.singular .. "_spec.lua", spec)
CreateMigration("create " .. words.plural .. " table")
print("✅ app/models/" .. words.singular .. ".lua created")
print("✅ specs/models/" .. words.singular .. "_spec.lua created")
end
if arg[1] == "create" then
if arg[2] == "migration" then
CreateMigration(arg[3])
end
if arg[2] == "controller" then
CreateController(Singularize(arg[3]))
end
if arg[2] == "model" then
CreateModel(Singularize(arg[3]))
end
if arg[2] == "scaffold" then
singular = Singularize(arg[3])
CreateController(singular)
CreateView(singular)
CreateModel(singular)
end
end
SetupDB = function(env)
print("Setup " .. env .. " DB")
assert(Adb.Auth(DBConfig.system) ~= nil)
if env == "test" then
Adb.DeleteDatabase(DBConfig[env].db_name)
end
Adb.CreateDatabase(DBConfig[env].db_name)
assert(Adb.Auth(DBConfig[env]) ~= nil)
Adb.CreateCollection("migrations")
Adb.CreateIndex("migrations", {type = "persistent", unique = true, fields = {"filename"}})
Adb.CreateCollection("uploads")
Adb.CreateIndex("uploads", {type = "persistent", unique = true, fields = {"uuid"}})
Adb.CreateDocument("migrations", {filename = "0"})
end
if arg[1] == "db:setup" then
if DBConfig["engine"] == "arangodb" then
SetupDB(BeansEnv)
end
end
ArangoDB_DBMigrate = function()
print("Running migrations ...")
assert(Adb.Auth(DBConfig[BeansEnv]) ~= nil)
local latest_version = Adb.Aql("FOR m IN migrations SORT m.filename DESC LIMIT 1 RETURN m.filename").result[1]
for name, kind, ino, off in assert(unix.opendir(unix.getcwd() .. "/migrations")) do
if name ~= "." and name ~= ".." then
if name > latest_version then
print("Processing " .. name)
local migration = require(string.gsub(name, ".lua", ""))
if migration.up() then
Adb.CreateDocument("migrations", {filename = name})
end
end
end
end
end
Sqlite_DBMigrate = function()
print("Running migrations ...")
local sqlite3 = require "lsqlite3"
local db = sqlite3.open(DBConfig[BeansEnv]["db_name"] .. ".sqlite3")
local latest_version = ""
for row in db:nrows([[
SELECT filename FROM migrations ORDER BY migrations.filename DESC LIMIT 1
]]) do
latest_version = row["filename"]
end
for name, kind, ino, off in assert(unix.opendir(unix.getcwd() .. "/migrations")) do
if name ~= "." and name ~= ".." and name ~= ".keep" then
if name > latest_version then
print("Processing " .. name)
local migration = require(string.gsub(name, ".lua", ""))
if migration.up() then
local stm = db:prepare [[
INSERT INTO migrations (filename) VALUES (?)
]]
stm:bind_values(name)
for r in stm:nrows() do
return r
end
end
end
end
end
end
if arg[1] == "db:migrate" then
if DBConfig["engine"] == "arangodb" then
ArangoDB_DBMigrate()
elseif DBConfig["engine"] == "sqlite" then
Sqlite_DBMigrate()
end
end
if arg[1] == "db:rollback" then
if DBConfig["engine"] == "arangodb" then
assert(Adb.Auth(DBConfig[BeansEnv]) ~= nil)
local latest_version = Adb.Aql("FOR m IN migrations SORT TO_NUMBER(m._key) DESC LIMIT 1 RETURN m").result[1]
if latest_version.filename ~= "0" then
print("Processing " .. latest_version.filename)
local migration = require(string.gsub(latest_version.filename, ".lua", ""))
if migration.down() then
Adb.DeleteDocument(latest_version._id)
end
else
print("Nothing to rollback!")
end
elseif DBConfig["engine"] == "sqlite" then
local sqlite3 = require "lsqlite3"
local db = sqlite3.open(DBConfig[BeansEnv]["db_name"] .. ".sqlite3")
local latest_version = ""
for row in db:nrows([[
SELECT filename FROM migrations ORDER BY migrations.filename DESC LIMIT 1
]]) do
latest_version = row["filename"]
end
print("Processing " .. latest_version)
if latest_version ~= "" then
local migration = require(string.gsub(latest_version, ".lua", ""))
if migration.down() then
local stm, err = db:prepare([[
DELETE FROM migrations WHERE filename = ?
]])
stm:bind_values(latest_version)
for r in stm:nrows() do
return print(EncodeJson(r))
end
end
end
end
end
if arg[1] == "specs" then
BeansEnv = "test"
SetupDB("test")
if DBConfig["engine"] == "arangodb" then
ArangoDB_DBMigrate()
elseif DBConfig["engine"] == "sqlite" then
Sqlite_DBMigrate()
end
describe, it, expect = lester.describe, lester.it, lester.expect
for _, spec in pairs(RecursiveFiles("/specs")) do
if string.find(spec, "_spec.lua") then
spec = spec:sub(8)
spec = require(string.gsub(spec, ".lua", ""))
spec.run()
end
end
lester.report()
lester.exit()
end