|
| 1 | +-- @file plugin/export_plugin.lua EditorExportPlugin for minifying Lua scripts on release exports |
| 2 | +-- This file is part of Godot Lua PluginScript: https://github.com/gilzoide/godot-lua-pluginscript |
| 3 | +-- |
| 4 | +-- Copyright (C) 2021 Gil Barbosa Reis. |
| 5 | +-- |
| 6 | +-- Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +-- of this software and associated documentation files (the “Software”), to |
| 8 | +-- deal in the Software without restriction, including without limitation the |
| 9 | +-- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | +-- sell copies of the Software, and to permit persons to whom the Software is |
| 11 | +-- furnished to do so, subject to the following conditions: |
| 12 | +-- |
| 13 | +-- The above copyright notice and this permission notice shall be included in |
| 14 | +-- all copies or substantial portions of the Software. |
| 15 | +-- |
| 16 | +-- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | +-- IN THE SOFTWARE. |
| 23 | +local package_path = package.path |
| 24 | +package.path = 'res://addons/godot-lua-pluginscript/plugin/?.lua;res://addons/godot-lua-pluginscript/plugin/?/init.lua;' .. package_path |
| 25 | +local luasrcdiet = require 'luasrcdiet' |
| 26 | +package.path = package_path |
| 27 | + |
| 28 | +local LuaExportPlugin = { |
| 29 | + is_tool = true, |
| 30 | + extends = 'EditorExportPlugin', |
| 31 | +} |
| 32 | + |
| 33 | +local SHOULD_MINIFY_RELEASE_SETTING = 'lua_pluginscript/export/minify_on_release_export' |
| 34 | + |
| 35 | +local function add_project_setting(name, initial_value) |
| 36 | + if not ProjectSettings:has_setting(name) then |
| 37 | + ProjectSettings:set_setting(name, initial_value) |
| 38 | + end |
| 39 | + ProjectSettings:set_initial_value(name, initial_value) |
| 40 | +end |
| 41 | +add_project_setting(SHOULD_MINIFY_RELEASE_SETTING, true) |
| 42 | + |
| 43 | +function LuaExportPlugin:_export_begin(features, is_debug, path, flags) |
| 44 | + self.ignore_path = self:get_script().resource_path:get_base_dir() |
| 45 | + self.should_minify = not is_debug and ProjectSettings:get_setting(SHOULD_MINIFY_RELEASE_SETTING) |
| 46 | + self.file = File:new() |
| 47 | +end |
| 48 | + |
| 49 | +function LuaExportPlugin:_export_file(path, type, features) |
| 50 | + if path:begins_with(self.ignore_path) then |
| 51 | + self:skip() |
| 52 | + elseif self.should_minify and path:ends_with('.lua') then |
| 53 | + if self.file:open(path, File.READ) == GD.OK then |
| 54 | + local source = tostring(self.file:get_as_text()) |
| 55 | + self.file:close() |
| 56 | + local optsource = luasrcdiet.optimize(luasrcdiet.MAXIMUM_OPTS, source) |
| 57 | + print(string.format('[LuaPluginScript] Minified %s: %s -> %s (%d%% reduction)', |
| 58 | + path, |
| 59 | + String.humanize_size(#source), |
| 60 | + String.humanize_size(#optsource), |
| 61 | + 100 - math.floor(#optsource / #source * 100)) |
| 62 | + ) |
| 63 | + self:add_file(path, PoolByteArray.from(optsource), false) |
| 64 | + end |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +function LuaExportPlugin:_export_end() |
| 69 | + self.ignore_path = nil |
| 70 | + self.file = nil |
| 71 | +end |
| 72 | + |
| 73 | +return LuaExportPlugin |
0 commit comments