-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.lua
More file actions
176 lines (157 loc) · 6.52 KB
/
Copy pathbuild.lua
File metadata and controls
176 lines (157 loc) · 6.52 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
--- Norm build script. Produces a SELF-CONTAINED single-file bundle of Norm with
--- light-class embedded, so the host needs nothing else and no `require`.
---
--- Run from anywhere with any Lua 5.4: lua build.lua
--- Outputs:
--- dist/norm.lua readable bundle (light-class + ORM, with comments)
--- dist/norm.min.lua minified bundle (comments/blank lines/indent stripped)
local VERSION = "1.0.0";
-- Resolve the project directory (where this script lives).
local script_path = (arg and arg[0]) or "build.lua";
local base = script_path:match("^(.*)[/\\]") or ".";
-- Module load list. Order is cosmetic (the resolver is lazy); `init` is the entry.
local MODULES = {
"utils", "dialect", "types", "sql", "promise", "json",
"adapter", "query", "model", "orm",
"adapters.nanos", "adapters.oxmysql", "init",
};
-- License / credit header embedded at the top of every generated bundle.
local LICENSE = {
("Norm v%s - a dependency-free Lua ORM. https://github.com/JustGodWork/norm"):format(VERSION),
"Generated single-file build (light-class embedded). Edit src/ then run build.lua;",
"do NOT edit this file directly.",
"",
"MIT License - Copyright (c) 2026 JustGodWork",
"",
"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. See LICENSE.",
"",
"Bundles light-class (https://github.com/JustGodWork/light-class) - MIT, (c) JustGodWork.",
};
local function read_file(path)
local f, err = io.open(path, "rb");
if (not f) then error("cannot read " .. path .. ": " .. tostring(err)); end
local content = f:read("*a");
f:close();
return content;
end
local function write_file(path, content)
local f, err = io.open(path, "wb");
if (not f) then error("cannot write " .. path .. ": " .. tostring(err)); end
f:write(content);
f:close();
end
local function module_path(id)
return base .. "/src/" .. id:gsub("%.", "/") .. ".lua";
end
local function header()
local lines = {};
for i = 1, #LICENSE do lines[#lines + 1] = "-- " .. LICENSE[i]; end
return table.concat(lines, "\n") .. "\n\n";
end
-- Build the bundle BODY (everything after the license header).
local function build_body()
local out = {};
local function emit(s) out[#out + 1] = s; end
-- 1) Embed light-class, wrapped so its `return class` does not exit the bundle.
-- It sets the global `class` that the modules below rely on.
emit("(function()\n");
emit(read_file(base .. "/class/light-class.lua"));
emit("\nend)();\n\n");
-- 2) Internal module resolver (replaces the host `require`).
emit("local __modules = {}\n");
emit("local __cache = {}\n");
emit("local function __require(name)\n");
emit(" local c = __cache[name]\n");
emit(" if c ~= nil then return c end\n");
emit(" local loader = __modules[name]\n");
emit(" if not loader then error(\"norm: unknown bundled module '\" .. tostring(name) .. \"'\") end\n");
emit(" local m = loader()\n");
emit(" if m == nil then m = true end\n");
emit(" __cache[name] = m\n");
emit(" return m\n");
emit("end\n\n");
-- 3) Each source module, wrapped with a local `require` bound to __require.
for i = 1, #MODULES do
local id = MODULES[i];
local src = read_file(module_path(id));
emit(("__modules[%q] = function()\n"):format(id));
emit("local require = __require\n");
emit(src);
if (src:sub(-1) ~= "\n") then emit("\n"); end
emit("end\n\n");
end
-- 4) Boot: expose the global `Norm` and return it.
emit("Norm = __require(\"init\") ---@type Norm\n");
emit("return Norm\n");
return table.concat(out);
end
-- Conservative minifier: drops blank lines and full-line comments, strips
-- indentation. Keeps newlines (one statement per line) and inline comments.
-- Safe for this codebase because it contains no [[ long-bracket ]] strings.
---@param code string
---@return string
local function minify(code)
local out, i, n = {}, 1, #code
local function peek(k) return code:sub(i + (k or 0), i + (k or 0)) end
local function long_open()
local eqs = code:match("^%[(=*)%[", i)
return eqs and #eqs or nil
end
local function skip_long(level)
local close = "]" .. ("="):rep(level) .. "]"
local j = code:find(close, i, true)
return j and (j + #close) or (n + 1)
end
while i <= n do
local c = peek()
if c == "-" and peek(1) == "-" then
i = i + 2
local lvl = long_open()
if lvl then
i = skip_long(lvl) -- --[[ ... ]]
else
i = (code:find("\n", i, true) or n + 1)
end
out[#out + 1] = " "
elseif c == '"' or c == "'" then
local q = c; out[#out + 1] = c; i = i + 1
while i <= n do
local ch = peek()
out[#out + 1] = ch; i = i + 1
if ch == "\\" then
out[#out + 1] = peek(); i = i + 1
elseif ch == q then
break
end
end
elseif c == "[" and long_open() then
local lvl = long_open(); local j = skip_long(lvl)
out[#out + 1] = code:sub(i, j - 1); i = j
elseif c:match("%s") then
out[#out + 1] = " "
i = i + 1
while i <= n and peek():match("%s") do i = i + 1 end
else
out[#out + 1] = c; i = i + 1
end
end
return (table.concat(out):gsub("%s+", " "):gsub("^%s+", ""):gsub("%s+$", "")) .. "\n"
end
local body = build_body();
local head = header();
local readable = head .. body;
local minified = head .. minify(body);
write_file(base .. "/dist/norm.lua", readable);
write_file(base .. "/dist/norm.min.lua", minified);
print(("[norm] built dist/norm.lua (%d bytes) and dist/norm.min.lua (%d bytes), %d modules, v%s")
:format(#readable, #minified, #MODULES, VERSION));