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
|
local cqueues = require 'cqueues'
local cq_notify = require 'cqueues.notify'
local posix_dirent = require 'posix.dirent'
local server = require 'lib.server'
local app = require 'app'
local db = require 'lib.db'
local pprint = require'pprint'
-- should happen on code reload too probably
app:run_hooks('pre_start')
print'NEW CQUEUE'
local cq = cqueues.new()
server.run(cq, app)
local function reload(modname)
local oldmod = require(modname)
assert(type(oldmod) == "table","reloading only works on modules that return tables")
package.loaded[modname] = nil
local newmod = require(modname)
package.loaded[modname]=oldmod
assert(type(newmod) == "table","new module doesn't seem to be a table, cancelling")
print('','r',modname,oldmod.version,newmod.version)
for k in pairs(oldmod) do oldmod[k] = nil end
for k,v in pairs(newmod) do oldmod[k] = v end
end
local function reloader()
print('NEW RELOADER')
local path = '.'
local function modname(filename) return filename:match"^([a-z_]+)%.lua$" end
local notifier = cq_notify.opendir(path)
local seen = {}
local function add(filename)
if not seen[filename] then seen[filename]=true print('adding',filename) notifier:add(filename) end
end
for filename in posix_dirent.files(path) do
local m = modname(filename)
if m and m ~= 'main' then add(filename) end
end
for changes, filename in notifier:changes() do
pprint('changes',changes,filename)
local m = modname(filename)
if m then
print('!!! reloading ['..m..']')
print(pcall(reload, m))
end
end
end
cq:wrap(reloader)
assert(cq:loop())
|