Main.hx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package;
  2. import sys.FileSystem;
  3. import sys.io.File;
  4. import haxe.io.Path;
  5. using StringTools;
  6. class Main {
  7. static final matchImport = ~/^([ \t]*)@import (.+)$/gm;
  8. static final matchRunnable = ~/^([ \t]*)jobs:/gm;
  9. static function main():Void {
  10. final folder = FileSystem.absolutePath(".");
  11. final outFolder = "../../.github";
  12. iterFolderItems(folder, (dir, name) -> {
  13. final ext = Path.extension(name);
  14. if (ext != "yaml" && ext != "yml") return;
  15. final data = File.getContent('$dir/$name');
  16. var newData = matchImport.map(data, reg -> {
  17. final spaces = reg.matched(1);
  18. final path = reg.matched(2);
  19. final template = File.getContent('./$path');
  20. final lines = template.split("\n");
  21. for (i in 0...lines.length) lines[i] = spaces + lines[i];
  22. lines.join("\n");
  23. });
  24. if (!matchRunnable.match(newData)) return;
  25. final first = "# DO NOT EDIT. Generated from /extra/github-actions\n";
  26. newData = first + newData;
  27. final relativeDir = dir.replace(folder, "");
  28. File.saveContent('$outFolder$relativeDir/$name', newData);
  29. });
  30. }
  31. static function iterFolderItems(dir:String, func:(dir:String, name:String)->Void):Void {
  32. for (name in FileSystem.readDirectory(dir)) {
  33. if (FileSystem.isDirectory(name)) iterFolderItems('$dir/$name', func);
  34. func(dir, name);
  35. }
  36. }
  37. }