Making lua minimizing optional. The minimizing is not optimal yet, so help would be appreciated.

master
Icedream 2014-12-11 04:44:22 +01:00
parent eb36e6013c
commit d9942319a5
4 changed files with 93 additions and 59 deletions

View File

@ -222,10 +222,13 @@ namespace GarrysMod.AddonCreator.Addon
throw new FileNotFoundException("Addon building requires a valid addon.json file.");
}
var files = Files
var files = Files.ToDictionary(i => i.Key, i => i.Value);
if (MinimizeLua)
files = files
// minimize lua code
.Select(f => f.Key.EndsWith(".lua", StringComparison.OrdinalIgnoreCase)
? new KeyValuePair<string, AddonFileInfo>(f.Key, new LuaAddonFileInfo(f.Value))
? new KeyValuePair<string, AddonFileInfo>(f.Key, new MinifiedLuaAddonFileInfo(f.Value))
: f)
.ToDictionary(i => i.Key, i => i.Value);
@ -340,5 +343,10 @@ namespace GarrysMod.AddonCreator.Addon
}
}
}
/// <summary>
/// Indicates whether Lua files will have comments and unnecessary whitespace stripped out on export.
/// </summary>
public bool MinimizeLua { get; set; }
}
}

View File

@ -11,7 +11,7 @@ namespace GarrysMod.AddonCreator.Addon
/// <summary>
/// Wrapper around another file info for optimizing Lua files.
/// </summary>
public class LuaAddonFileInfo : AddonFileInfo
public class MinifiedLuaAddonFileInfo : AddonFileInfo
{
private readonly AddonFileInfo _fi;
@ -20,16 +20,21 @@ namespace GarrysMod.AddonCreator.Addon
private readonly string _stripCommentsRegex = string.Join("|", new[]
{
// block comments
@"/\*(.*?)\*/",
@"\-\-\[\[(.*?)\]\]",
@"\/\*.*\*\/",
@"\-\-\[\[.*\]\]",
// line comments
@"//(.*?)(?<linebreak>\r?\n)",
@"\-\-(.*?)(?<linebreak>\r?\n)"
@"//([^\r\n]*?)(?<linebreak>[\r\n]+)",
@"\-\-([^\r\n]*?)(?<linebreak>[\r\n]+)",
// Unnecessary whitespace
@"^[\s]*$",
@"[\s]*$",
@"^[\s]*"
});
private string _luaCode;
public LuaAddonFileInfo(AddonFileInfo actual)
public MinifiedLuaAddonFileInfo(AddonFileInfo actual)
{
_fi = actual;
}
@ -40,7 +45,14 @@ namespace GarrysMod.AddonCreator.Addon
return _content;
_luaCode = Encoding.UTF8.GetString(_fi.GetContents());
_luaCode = Regex.Replace(_luaCode, _stripCommentsRegex, m => m.Groups["linebreak"] != null ? m.Groups["linebreak"].Value : "");
string _oldLuaCode;
do
{
_oldLuaCode = _luaCode;
_luaCode = Regex.Replace(_luaCode, _stripCommentsRegex,
m => m.Groups["linebreak"] != null ? m.Groups["linebreak"].Value : "", RegexOptions.Multiline | RegexOptions.Singleline);
_luaCode = _luaCode.Trim();
} while (_oldLuaCode != _luaCode);
_content = Encoding.UTF8.GetBytes(_luaCode);

View File

@ -56,7 +56,7 @@
<Compile Include="Addon\AddonFileInfo.cs" />
<Compile Include="Addon\AddonJson.cs" />
<Compile Include="Addon\AddonWhitelist.cs" />
<Compile Include="Addon\LuaAddonFileInfo.cs" />
<Compile Include="Addon\MinifiedLuaAddonFileInfo.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Addon\JsonAddonFileInfo.cs" />
<Compile Include="Hashing\Crc32.cs" />

View File

@ -11,10 +11,20 @@ namespace GarrysMod.AddonCreator
{
private static void Main(string[] args)
{
var minimizeLua = false;
while (args.Any())
{
switch (args.Length == 0 ? "" : args[0])
{
case "--minimize-lua":
minimizeLua = true;
args = args.Skip(1).ToArray();
break;
case "--no-minimize-lua":
minimizeLua = false;
args = args.Skip(1).ToArray();
break;
case "create":
{
if (args.Length < 3)
@ -24,7 +34,7 @@ namespace GarrysMod.AddonCreator
var folder = new DirectoryInfo(args[1]);
var output = args[2];
var addon = new AddonFile();
var addon = new AddonFile {MinimizeLua = minimizeLua};
if (!folder.Exists)
{
@ -133,7 +143,7 @@ namespace GarrysMod.AddonCreator
}
default:
Console.WriteLine("Usage: {0} <command> <arguments>", Process.GetCurrentProcess().ProcessName);
Console.WriteLine("Usage: {0} <options> <command> <arguments>", Process.GetCurrentProcess().ProcessName);
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine("\t{0}\t{1}", "extract", "Extracts a GMA file and shows information about it.");
@ -141,6 +151,10 @@ namespace GarrysMod.AddonCreator
Console.WriteLine("\t{0}\t{1}", "create", "Creates a GMA file.");
Console.WriteLine("\t\tArguments: Input folder path, output GMA file path");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine("\t{0}\t{1}", "--minimize-lua", "Causes exported GMAs to have all Lua comments and unneeded whitespace in Lua stripped out.");
Console.WriteLine("\t{0}\t{1}", "--no-minimize-lua", "(default) Will prevent Lua files getting minimized.");
Console.WriteLine();
return;
}
}