-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmenu.lua
More file actions
202 lines (155 loc) · 4.54 KB
/
menu.lua
File metadata and controls
202 lines (155 loc) · 4.54 KB
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
--[[
-- RUN THIS FILE WITH YOUR PREFERRED LUA BINARY TO BUILD SIMPLOO
--]]
dofile("util/build.lua")
dofile("util/shell.lua")
dofile("util/merger.lua")
dofile("util/tests.lua")
dofile("util/misc.lua")
--
-- Globals
--
BUILD_HEADER = [[
SIMPLOO - Simple Lua Object Orientation
The MIT License (MIT)
Copyright (c) 2016 maurits.tv
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the \"Software\"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
DIST_FILE = "dist/simploo.lua"
WATCHER_INTERVAL_MS = 300
--
-- Menu
--
menu = {}
function menu:init()
while true do
local action = shell:createMenu({
"Build simpoo",
"Start watching monitor + run code on change (io heavy!)",
"Start watching monitor + runs tests on change",
"Run tests",
"Exit"
}, "Choose an action")
if action == 1 then
self:build()
elseif action == 2 then
self:watch("run")
elseif action == 3 then
self:watch("test")
elseif action == 4 then
local failures = self:tests()
if failures > 0 then
os.exit(1)
end
elseif action == 5 then
return
end
end
end
function menu:build()
build:execute(DIST_FILE)
print("build successful! See " .. DIST_FILE)
end
function menu:watch(mode)
print("Watching..")
local lastContent = {}
while true do
local files, err = build:getSourceFiles()
if not files then
print("[watch] failed: " .. tostring(err))
end
for k, name in pairs(files) do
local file, err = io.open("src/" .. name, "r")
if file then
local content = file:read("*all")
if not lastContent[name] then -- First boot
lastContent[name] = content
elseif lastContent[name] ~= content then
for i=0, 5 do
print("--- RELOADING ---- [@ " .. os.clock() .. "]")
end
if mode == "test" then
self:tests()
elseif mode == "run" then
self:run()
end
lastContent[name] = content
end
file:close()
end
end
-- Force all discarded instances to be finalized constantly
if collectgarbage then
collectgarbage()
end
shell:sleep(WATCHER_INTERVAL_MS)
end
end
function menu:run()
local status, err = pcall(function()
local files = build:getSourceFiles()
for k, v in pairs(files) do
dofile("src/" .. v)
end
end)
if not status then
print("[watch] failed: " .. tostring(err))
end
end
function menu:tests()
-- Execute test files
local testfiles, err = tests:getSourceFiles()
if not testfiles then
print("[tests] no tests ran: " .. tostring(err))
return 1
end
local totalFailures = 0
for _, testproduction in pairs({false, true}) do -- Test in both production mode and non-production
print("\n\n\n\n\n===================================================")
print("== Running test with production mode " .. (testproduction and "ON" or "OFF"))
print("===================================================")
for k, v in pairs(testfiles) do
-- Wipe simploo reference but preserve config for next load
simploo = {config = {production = testproduction}}
Test = {}
-- Run the simploo files
local simploofiles, err = build:getSourceFiles()
if not simploofiles then
print("[exec] failed: " .. tostring(err))
end
for k, name in pairs(simploofiles) do
local file, err = io.open("src/" .. name, "r")
if file then
local status, err = pcall(function()
dofile("src/" .. name)
end)
if not status then
print("[exec] failed: " .. tostring(err))
end
file:close()
end
end
-- Execute the test files
dofile("tests/" .. v)
-- Run luaunit
local failures = LuaUnit:run("Test")
totalFailures = totalFailures + failures
end
end
return totalFailures
end
menu:init()