summaryrefslogtreecommitdiff
path: root/make/bootstrap.lua
blob: 43dcdf5f0381cf6e7fbf75be4e446c638d736b51 (plain)
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
local main
local i = 1
while arg[i] do
    if arg[i] == '-E' then
    elseif arg[i] == '-e' then
        i = i + 1
        local expr = assert(arg[i], "'-e' needs argument")
        assert(load(expr, "=(command line)"))()
        -- exit after the executing
        return
    elseif not main and arg[i]:sub(1, 1) ~= '-' then
        main = i
    end
    i = i + 1
end

if main then
    for i = -1, -999, -1 do
        if not arg[i] then
            for j = i + 1, -1 do
                arg[j - main + 1] = arg[j]
            end
            break
        end
    end
    for j = 1, #arg do
        arg[j - main] = arg[j]
    end
    for j = #arg - main + 1, #arg do
        arg[j] = nil
    end
else
    arg[0] = 'main.lua'
end

local root; do
    local sep = package.config:sub(1,1)
    if sep == '\\' then
        sep = '/\\'
    end
    local pattern = "["..sep.."][^"..sep.."]+"
    root = package.cpath:match("([^;]+)"..pattern..pattern..pattern.."$")
end

local fs = require 'bee.filesystem'
fs.current_path(fs.path(root))
package.path = table.concat({
    "script/?.lua",
    "script/?/init.lua",
}, ";"):gsub('/', package.config:sub(1,1))

loadfile = function (name)
    local f, e = io.open(root .. '/' .. name)
    if not f then
        return false, e
    end
    local content = f:read 'a'
    f:close()
    return load(content, '@' .. name)
end

assert(loadfile(arg[0]))(table.unpack(arg))