Last active
February 16, 2021 23:20
-
-
Save AMD-NICK/06f8bfe3bee1eac6e9effa72ba195fe7 to your computer and use it in GitHub Desktop.
Strips comments and convert glua script into single line file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local pat_mcomment_lua = "(%-%-%[(=*)%[.-%]%2%])" | |
local pat_mcomment_cpp = "(/%*.-%*/)" | |
local function stripMultiline(content) | |
return content | |
:gsub(pat_mcomment_lua, "") | |
:gsub(pat_mcomment_cpp, "") | |
end | |
-- https://img.qweqwe.ovh/1556969630528.png | |
-- #TODO shitcoded heavy function | |
local pat_scomment_lua = "(%-%-[^\n]*)" | |
local pat_scomment_cpp = "[^:?](//[^\n]*)" | |
local function stripSingleline(content) | |
local str = "" | |
for _,line in ipairs(content:Split("\n")) do | |
if string.sub(line, 1, 2) == '//' then line = '' end | |
str = str .. line | |
:gsub(pat_scomment_lua, "") | |
:gsub(pat_scomment_cpp, "") .. "\n" | |
end | |
return str | |
end | |
local function removeComments(content) | |
local a = stripMultiline(content) | |
local b = stripSingleline(a) | |
return b | |
end | |
local function removeUselessNewlines(content) -- compresses code | |
local res = "" | |
content:gsub("[^\n]+", function(line) | |
if line:match("^%s*(.-)%s*$") ~= "" then | |
res = res .. line:TrimRight() .. "\n" -- Trim даже слева табы уберет | |
end | |
end) | |
return res | |
end | |
-- Use only after removing comments | |
local function multilineToSingle(content) | |
return content:gsub("%c+", " "):TrimRight() | |
end | |
local function cleanupCode(content) | |
local no_comments = removeComments(content) | |
local no_extra_newlines = removeUselessNewlines(no_comments) | |
return no_extra_newlines | |
end | |
local function minifyFile(path, realm) | |
local content = file.Read(path, realm) -- original | |
local clear = cleanupCode(content) -- without comments, trailing spaces and unecessary newlines | |
local single = multilineToSingle(clear) -- 1 line file | |
return single or clear | |
end | |
local function minifyAndSave(path, realm, save_to) | |
local minified = minifyFile(path, realm) | |
local destination = save_to .. "/" .. path:match("^addons/[^/]+/lua/(.+)$"):Replace(".lua", ".txt") -- gmod can't save with .lua ext | |
file.AdvWrite(destination, minified) | |
end | |
local function minifyFolder(path, realm, save_to) | |
local files = file.Index(path, realm) | |
for _,file_path in ipairs(files) do | |
minifyAndSave(file_path, realm, save_to) | |
end | |
end | |
-- local HEADER = "-- You don't have permissions to use this code without a permit of admin@trigon.im\n-- TRIGON.IM 2018 (c)\n" | |
local dest_path = "igs/" .. IGS.Version | |
-- file.DeleteDir(dest_path) | |
-- minifyFolder("addons/wip-ingameshop/lua/igs_orig", "MOD", dest_path) -- find . -name "*.txt" -exec rename 's/\.txt$/.lua/' '{}' \; | |
-- minifyAndSave("addons/wip-ingameshop/lua/igs/apinator.lua", "MOD", dest_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Travis CI Workflow для упаковки кода в .gma архив
https://travis-ci.com/github/wiremod/advdupe2/jobs/482763437