Skip to content

Instantly share code, notes, and snippets.

@LPGhatguy
Last active January 1, 2016 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LPGhatguy/8193875 to your computer and use it in GitHub Desktop.
Save LPGhatguy/8193875 to your computer and use it in GitHub Desktop.
Minifies Lua from the command line.
--[[
Lua Minify 1.0
Usage:
lua minify.lua comments DIRECTORY
strips comments from all .lua files in the directory, recursively, and puts them in DIRECTORY/minified
lua minify.lua minify DIRECTORY
strips comments and minifies code from all .lua files and places them like the prior command.
]]
local arg = {...}
if (#arg == 0) then
print("Invalid usage")
--return
end
local cmd = arg[1]
local lfs = require("lfs")
local root = arg[#arg]
local out_dir = "minified"
local file_pattern = "%.lua$"
local shortable = "%s*([" .. (("[]()=*+-^{}/;,#~"):gsub(".", "%%%0")) .. "])%s*"
local function escape(str)
return str:gsub("([%-%+%?%*%.%$%[%]%(%)%%])", "%%%0")
end
local function rescape(str)
str = str:gsub("%%", "%%%%")
return str
end
local function strip_comments(source)
source = source .. "\n"
source = source:gsub("%s*%-%-NOMINI START.-%-%-NOMINI END", "")
source = source:gsub("%s*%-%-%[(=*)%[.-%]%1%]", "")
source = source:gsub("%s*%-%-.-\n", "\n")
return source
end
local function strip_whitespace(source)
local strings = {}
for string in source:gmatch("[^-]?(%[(=*)%[.-%]%2%])") do
local id = #strings + 1
strings[id] = string
source = source:gsub(escape(string), "$" .. id .. "$")
end
for string in source:gmatch("(([\"']).-[^\\]?%2)") do
local id = #strings + 1
strings[id] = string
source = source:gsub(escape(string), "$" .. id .. "$")
end
source = source:gsub("^\n+", "")
source = source:gsub("\n+$", "")
source = source:gsub("\n\n+", "\n")
source = source:gsub("\t", "")
source = source:gsub(shortable, "%1")
for index, string in next, strings do
source = source:gsub("%$" .. index .. "%$", rescape(string))
end
return source
end
local function strip_dir(dir, all, ext)
ext = ext or ""
for item in lfs.dir(dir) do
if (item ~= "." and item ~= ".." and item ~= out_dir and item ~= "ldoc") then
local path = dir .. "/" .. item
local mode = lfs.attributes(path, "mode")
if (mode == "file" and item:match(file_pattern)) then
local handle = io.open(path, "r")
if (handle) then
local body = handle:read("*all")
handle:close()
local ohandle, err = io.open(root .. "/" .. out_dir .. "/" .. ext .. "/" .. item, "w")
if (ohandle) then
body = strip_comments(body)
if (all) then
body = strip_whitespace(body)
end
local good, err = loadstring(body)
if (not good) then
print(item, "failed after minify:", err)
end
ohandle:write(body)
ohandle:close()
else
print("I/O error writing " .. root .. "/" .. out_dir .. "/" .. ext .. "/" .. item, err)
end
else
print("I/O error reading " .. path)
end
elseif (mode == "directory") then
lfs.mkdir(root .. "/" .. out_dir .. "/" .. ext .. "/" .. item)
strip_dir(path, all, ext .. "/" .. item)
end
end
end
end
if (cmd == "comments") then
lfs.mkdir(root .. "/" .. out_dir)
strip_dir(root)
print("Stripped comments to", out_dir)
elseif (cmd == "minify") then
lfs.mkdir(root .. "/" .. out_dir)
strip_dir(root, true)
print("Minified to", out_dir)
else
print("INVALID COMMAND", cmd)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment