Main.hx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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)
  22. lines[i] = (spaces + lines[i]).rtrim();
  23. lines.join("\n");
  24. });
  25. if (!matchRunnable.match(newData)) return;
  26. final first = "# DO NOT EDIT. Generated from /extra/github-actions\n";
  27. newData = first + newData;
  28. final relativeDir = dir.replace(folder, "");
  29. File.saveContent('$outFolder$relativeDir/$name', newData);
  30. });
  31. }
  32. static function iterFolderItems(dir:String, func:(dir:String, name:String)->Void):Void {
  33. for (name in FileSystem.readDirectory(dir)) {
  34. if (FileSystem.isDirectory(name)) iterFolderItems('$dir/$name', func);
  35. func(dir, name);
  36. }
  37. }
  38. }