You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.9 KiB
Lua

local fs = (function()
local ok, ocfs = pcall(require, 'filesystem')
if ok then
--in [openos, plan9k, standard lua], io.open() resolves paths relative to the current directory.
--in standard lua, lfs.mkdir() also resolves paths relative to the current directory.
--in [openos, plan9k], filesystem.makeDirectory() resolves paths relative to... the filesystem root.
--this script builds openos's filesystem apis accurately on top of luafilesystem because it's easier than
--the other way around, but in this case luafilesystem is right and we fix opencomputers to match.
local shell = require('shell')
return setmetatable({
makeDirectory = function(path)
return ocfs.makeDirectory(ocfs.concat(shell.getWorkingDirectory(), path))
end,
}, {__index = ocfs})
end
--if we're not in minecraft, implement the functions we need on top of LuaFileSystem
local lfs = require('lfs')
local fs = {}
function fs.list(path)
local entries = {}
for name in lfs.dir(path) do
if name ~= '.' and name ~= '..' then
if lfs.attributes(path .. '/' .. name, 'mode') == 'directory' then
name = name .. '/'
end
table.insert(entries, name)
end
end
return entries
end
function fs.makeDirectory(path)
--drop the third return on error
local ok, err, code = lfs.mkdir(path)
if ok then
return true
else
return nil, string.format('%s (error %d)', err, code)
end
end
return fs
end)()
local stderr = io.stderr or io.error() --openos does this weirdly
--configuration
local outdir = 'dist/'
--argument parsing
do
local args = {...}
for i, arg in ipairs(args) do
if arg == '-h' or arg == '--help' then
print('usage: lua build.lua [-h|--help]')
return
end
end
--for now, error on any arguments (after parsing -h/--help)
if args[1] then
stderr:write(string.format('error: unexpected argument %q\n', args[1]))
os.exit(64)
end
end
local function exportFile(srcpath, dstpath)
if not dstpath then
dstpath = outdir .. srcpath
end
local srcf = assert(io.open(srcpath))
--load the file to validate syntax, as a very minimal linter
do
local ok, err = loadfile(srcpath)
if not ok then
stderr:write(err, '\n')
os.exit(1)
end
end
local dstf = assert(io.open(dstpath, 'w'))
--for now, run a very minimal minifier
local longEnd --nil normally, some "]==]--" string if we're in a long comment
for line in srcf:lines() do
::stripComments:: --strip comments, including weird ones like "--[[ ]]-- --[=[ ]=]-- --text" all in one line
--first, if we're in a long comment, see if that's over
if longEnd then
local i1, i2 = line:find(longEnd, 1, true)
if i1 then
--strip the long comment and mark that it ended
line = line:sub(i2+1)
longEnd = nil
else
--still in a long comment, so don't output anything
goto nextLine
end
end
--look for the start of the next comment
local comment = line:find('--', 1, true)
if not comment then --nothing left to do, break out of the loop
goto emitLine
end
if line:byte(comment+2) == string.byte('[') then --start of a long comment
local depth = #line:sub(comment+2):match('^%[(%=*)%[') --validating syntax earlier ensures this will match
longEnd = string.format(']%s]--', string.rep('=', depth))
line = line:sub(comment + 4 + depth) -- #'--[' + depth + #'['
goto stripComments --start from the beginning in case the comment ends on the same line it starts on
else
--found a short comment, remove to end of line
line = line:sub(1, comment - 1)
end
::emitLine::
assert(not longEnd, 'shouldn\'t emit anything in a long comment')
line = assert(line:match('^%s*(.-)%s*$')) --strip leading and trailing whitespace
if #line > 0 then
dstf:write(line, '\n')
end
::nextLine::
end
assert(not longEnd, 'unclosed long comment')
end
fs.makeDirectory(outdir)
exportFile('boot.lua')
fs.makeDirectory(outdir .. 'kitn-core/')
exportFile('src/init.lua', outdir .. 'kitn-core/init.lua')
-- vi: set ts=2: