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.

93 lines
2.4 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))
local dstf = assert(io.open(dstpath, 'w'))
--for now, run a very minimal minifier
for line in srcf:lines() do
local comment = line:find('--', nil, true)
if comment then
assert(line:sub(comment, comment + 3) ~= '--[', 'TODO block comments')
line = line:sub(1, comment-1)
end
line = assert(line:match('^%s*(.-)%s*$'))
if #line > 0 then
dstf:write(line, '\n')
end
end
end
fs.makeDirectory(outdir)
exportFile('boot.lua')
-- vi: set ts=2: