Making lua minimizing optional. The minimizing is not optimal yet, so help would be appreciated.
parent
eb36e6013c
commit
d9942319a5
|
@ -222,12 +222,15 @@ namespace GarrysMod.AddonCreator.Addon
|
||||||
throw new FileNotFoundException("Addon building requires a valid addon.json file.");
|
throw new FileNotFoundException("Addon building requires a valid addon.json file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var files = Files
|
var files = Files.ToDictionary(i => i.Key, i => i.Value);
|
||||||
// minimize lua code
|
|
||||||
.Select(f => f.Key.EndsWith(".lua", StringComparison.OrdinalIgnoreCase)
|
if (MinimizeLua)
|
||||||
? new KeyValuePair<string, AddonFileInfo>(f.Key, new LuaAddonFileInfo(f.Value))
|
files = files
|
||||||
: f)
|
// minimize lua code
|
||||||
.ToDictionary(i => i.Key, i => i.Value);
|
.Select(f => f.Key.EndsWith(".lua", StringComparison.OrdinalIgnoreCase)
|
||||||
|
? new KeyValuePair<string, AddonFileInfo>(f.Key, new MinifiedLuaAddonFileInfo(f.Value))
|
||||||
|
: f)
|
||||||
|
.ToDictionary(i => i.Key, i => i.Value);
|
||||||
|
|
||||||
// Check for errors and ignores in addon.json
|
// Check for errors and ignores in addon.json
|
||||||
var addonJson =
|
var addonJson =
|
||||||
|
@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,51 +1,63 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace GarrysMod.AddonCreator.Addon
|
namespace GarrysMod.AddonCreator.Addon
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Wrapper around another file info for optimizing Lua files.
|
/// Wrapper around another file info for optimizing Lua files.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class LuaAddonFileInfo : AddonFileInfo
|
public class MinifiedLuaAddonFileInfo : AddonFileInfo
|
||||||
{
|
{
|
||||||
private readonly AddonFileInfo _fi;
|
private readonly AddonFileInfo _fi;
|
||||||
|
|
||||||
private byte[] _content;
|
private byte[] _content;
|
||||||
|
|
||||||
private readonly string _stripCommentsRegex = string.Join("|", new[]
|
private readonly string _stripCommentsRegex = string.Join("|", new[]
|
||||||
{
|
{
|
||||||
// block comments
|
// block comments
|
||||||
@"/\*(.*?)\*/",
|
@"\/\*.*\*\/",
|
||||||
@"\-\-\[\[(.*?)\]\]",
|
@"\-\-\[\[.*\]\]",
|
||||||
|
|
||||||
// line comments
|
// line comments
|
||||||
@"//(.*?)(?<linebreak>\r?\n)",
|
@"//([^\r\n]*?)(?<linebreak>[\r\n]+)",
|
||||||
@"\-\-(.*?)(?<linebreak>\r?\n)"
|
@"\-\-([^\r\n]*?)(?<linebreak>[\r\n]+)",
|
||||||
});
|
|
||||||
private string _luaCode;
|
// Unnecessary whitespace
|
||||||
|
@"^[\s]*$",
|
||||||
public LuaAddonFileInfo(AddonFileInfo actual)
|
@"[\s]*$",
|
||||||
{
|
@"^[\s]*"
|
||||||
_fi = actual;
|
});
|
||||||
}
|
private string _luaCode;
|
||||||
|
|
||||||
public override byte[] GetContents()
|
public MinifiedLuaAddonFileInfo(AddonFileInfo actual)
|
||||||
{
|
{
|
||||||
if (_content != null)
|
_fi = actual;
|
||||||
return _content;
|
}
|
||||||
|
|
||||||
_luaCode = Encoding.UTF8.GetString(_fi.GetContents());
|
public override byte[] GetContents()
|
||||||
_luaCode = Regex.Replace(_luaCode, _stripCommentsRegex, m => m.Groups["linebreak"] != null ? m.Groups["linebreak"].Value : "");
|
{
|
||||||
|
if (_content != null)
|
||||||
_content = Encoding.UTF8.GetBytes(_luaCode);
|
return _content;
|
||||||
|
|
||||||
return _content;
|
_luaCode = Encoding.UTF8.GetString(_fi.GetContents());
|
||||||
}
|
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);
|
||||||
|
|
||||||
|
return _content;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -56,7 +56,7 @@
|
||||||
<Compile Include="Addon\AddonFileInfo.cs" />
|
<Compile Include="Addon\AddonFileInfo.cs" />
|
||||||
<Compile Include="Addon\AddonJson.cs" />
|
<Compile Include="Addon\AddonJson.cs" />
|
||||||
<Compile Include="Addon\AddonWhitelist.cs" />
|
<Compile Include="Addon\AddonWhitelist.cs" />
|
||||||
<Compile Include="Addon\LuaAddonFileInfo.cs" />
|
<Compile Include="Addon\MinifiedLuaAddonFileInfo.cs" />
|
||||||
<Compile Include="Extensions.cs" />
|
<Compile Include="Extensions.cs" />
|
||||||
<Compile Include="Addon\JsonAddonFileInfo.cs" />
|
<Compile Include="Addon\JsonAddonFileInfo.cs" />
|
||||||
<Compile Include="Hashing\Crc32.cs" />
|
<Compile Include="Hashing\Crc32.cs" />
|
||||||
|
|
|
@ -11,10 +11,20 @@ namespace GarrysMod.AddonCreator
|
||||||
{
|
{
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
var minimizeLua = false;
|
||||||
|
|
||||||
while (args.Any())
|
while (args.Any())
|
||||||
{
|
{
|
||||||
switch (args.Length == 0 ? "" : args[0])
|
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":
|
case "create":
|
||||||
{
|
{
|
||||||
if (args.Length < 3)
|
if (args.Length < 3)
|
||||||
|
@ -24,7 +34,7 @@ namespace GarrysMod.AddonCreator
|
||||||
|
|
||||||
var folder = new DirectoryInfo(args[1]);
|
var folder = new DirectoryInfo(args[1]);
|
||||||
var output = args[2];
|
var output = args[2];
|
||||||
var addon = new AddonFile();
|
var addon = new AddonFile {MinimizeLua = minimizeLua};
|
||||||
|
|
||||||
if (!folder.Exists)
|
if (!folder.Exists)
|
||||||
{
|
{
|
||||||
|
@ -133,7 +143,7 @@ namespace GarrysMod.AddonCreator
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Console.WriteLine("Usage: {0} <command> <arguments>", Process.GetCurrentProcess().ProcessName);
|
Console.WriteLine("Usage: {0} <options> <command> <arguments>", Process.GetCurrentProcess().ProcessName);
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine("Commands:");
|
Console.WriteLine("Commands:");
|
||||||
Console.WriteLine("\t{0}\t{1}", "extract", "Extracts a GMA file and shows information about it.");
|
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{0}\t{1}", "create", "Creates a GMA file.");
|
||||||
Console.WriteLine("\t\tArguments: Input folder path, output GMA file path");
|
Console.WriteLine("\t\tArguments: Input folder path, output GMA file path");
|
||||||
Console.WriteLine();
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue