Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace MergeLUA
- {
- class Program
- {
- static Regex requireRegex = new Regex(@"local\s+(?<fieldName>\S+)\s*=\s*require\s+""(?<path>[^""]+)""");
- static bool needsSave = false;
- static void Main(string[] args)
- {
- var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.lua", SearchOption.AllDirectories);
- foreach (var file in files)
- {
- needsSave = false;
- try
- {
- Console.WriteLine("processing {0}", file);
- var patched = LoadContent(file);
- if (needsSave)
- {
- var combinedFile = Path.GetFullPath("output/" + Path.GetFileNameWithoutExtension(file) + ".lua");
- Console.WriteLine("combining to {0}", combinedFile);
- File.WriteAllText(combinedFile, patched, Encoding.UTF8);
- Console.WriteLine("combined");
- }
- else
- {
- Console.WriteLine("nothing to do");
- }
- }
- catch
- {
- Console.WriteLine("failed");
- }
- }
- }
- private static string LoadContent(string path)
- {
- var content = File.ReadAllText(path);
- MatchEvaluator replacer = match => Replace(match, path);
- return requireRegex.Replace(content, replacer);
- }
- private static string Replace(Match match, string path)
- {
- needsSave = true;
- var targetPath = match.Result("${path}");
- Console.WriteLine(" found {0}", targetPath);
- if (targetPath.StartsWith(".")) // enabled relative requires
- {
- targetPath = Path.Combine(Path.GetDirectoryName(path), targetPath);
- }
- targetPath = Path.GetFullPath(targetPath);
- Console.WriteLine(" resolving to {0}", targetPath);
- return match.Result(
- "local ${fieldName} = (function()" + Environment.NewLine +
- "-- inlined " + match.Result("${path}") + Environment.NewLine +
- LoadContent(targetPath + ".lua") + Environment.NewLine +
- "end)()");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement