Advertisement
Guest User

Untitled

a guest
May 27th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace MergeLUA
  7. {
  8. class Program
  9. {
  10. static Regex requireRegex = new Regex(@"local\s+(?<fieldName>\S+)\s*=\s*require\s+""(?<path>[^""]+)""");
  11. static bool needsSave = false;
  12.  
  13. static void Main(string[] args)
  14. {
  15. var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.lua", SearchOption.AllDirectories);
  16.  
  17. foreach (var file in files)
  18. {
  19. needsSave = false;
  20. try
  21. {
  22.  
  23. Console.WriteLine("processing {0}", file);
  24. var patched = LoadContent(file);
  25.  
  26. if (needsSave)
  27. {
  28.  
  29. var combinedFile = Path.GetFullPath("output/" + Path.GetFileNameWithoutExtension(file) + ".lua");
  30. Console.WriteLine("combining to {0}", combinedFile);
  31. File.WriteAllText(combinedFile, patched, Encoding.UTF8);
  32. Console.WriteLine("combined");
  33. }
  34. else
  35. {
  36. Console.WriteLine("nothing to do");
  37. }
  38. }
  39. catch
  40. {
  41. Console.WriteLine("failed");
  42. }
  43. }
  44. }
  45.  
  46. private static string LoadContent(string path)
  47. {
  48. var content = File.ReadAllText(path);
  49. MatchEvaluator replacer = match => Replace(match, path);
  50. return requireRegex.Replace(content, replacer);
  51.  
  52. }
  53.  
  54. private static string Replace(Match match, string path)
  55. {
  56. needsSave = true;
  57. var targetPath = match.Result("${path}");
  58. Console.WriteLine(" found {0}", targetPath);
  59. if (targetPath.StartsWith(".")) // enabled relative requires
  60. {
  61. targetPath = Path.Combine(Path.GetDirectoryName(path), targetPath);
  62. }
  63. targetPath = Path.GetFullPath(targetPath);
  64. Console.WriteLine(" resolving to {0}", targetPath);
  65.  
  66. return match.Result(
  67. "local ${fieldName} = (function()" + Environment.NewLine +
  68. "-- inlined " + match.Result("${path}") + Environment.NewLine +
  69. LoadContent(targetPath + ".lua") + Environment.NewLine +
  70. "end)()");
  71.  
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement