CheckSourceHeader.hx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import sys.FileSystem;
  2. final FILE = "out/main.c";
  3. function deleteDirectory(path:String) {
  4. if (!FileSystem.isDirectory(path))
  5. return FileSystem.deleteFile(path);
  6. for (item in FileSystem.readDirectory(path))
  7. deleteDirectory('$path/$item');
  8. FileSystem.deleteDirectory(path);
  9. }
  10. function main() {
  11. final args = Sys.args();
  12. final code =
  13. try {
  14. switch args {
  15. case [""]:
  16. checkForEmptySourceHeader(FILE);
  17. case [expected]:
  18. checkSourceHeader(FILE, expected);
  19. case _:
  20. throw "Incorrect number of arguments to script.";
  21. }
  22. 0;
  23. } catch (e){
  24. Sys.stderr().writeString(e + "\n");
  25. 1;
  26. }
  27. deleteDirectory("out");
  28. Sys.exit(code);
  29. }
  30. function checkForEmptySourceHeader(path:String) {
  31. final content = getCSourceContent(path);
  32. if (StringTools.startsWith(content, "// "))
  33. throw "File has a source header when none was expected: " + content.split("\n")[0];
  34. }
  35. function checkSourceHeader(path:String, expected:String) {
  36. final content = getCSourceContent(path);
  37. if (!StringTools.startsWith(content, "// " + expected))
  38. throw "File source header does not start with expected: // " + expected +
  39. "\nSource header: " + content.split("\n")[0];
  40. }
  41. function getCSourceContent(path:String) {
  42. // have to skip the BOM character
  43. return sys.io.File.getContent(path).substr(1);
  44. }