scm_rev_gen.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var wshShell = new ActiveXObject("WScript.Shell")
  2. var oFS = new ActiveXObject("Scripting.FileSystemObject");
  3. var outfile = "scm_rev.cpp";
  4. var cmd_revision = " rev-parse HEAD";
  5. var cmd_describe = " describe --always --long --dirty";
  6. var cmd_branch = " rev-parse --abbrev-ref HEAD";
  7. function GetGitExe() {
  8. for (var gitexe in { "git.cmd": 1, "git": 1 }) {
  9. try {
  10. wshShell.Exec(gitexe);
  11. return gitexe;
  12. } catch (e) {
  13. }
  14. }
  15. WScript.Echo("Cannot find git or git.cmd, check your PATH:\n" +
  16. wshShell.ExpandEnvironmentStrings("%PATH%"));
  17. WScript.Quit(1);
  18. }
  19. function GetFirstStdOutLine(cmd) {
  20. try {
  21. return wshShell.Exec(cmd).StdOut.ReadLine();
  22. } catch (e) {
  23. // catch "the system cannot find the file specified" error
  24. WScript.Echo("Failed to exec " + cmd + " this should never happen");
  25. WScript.Quit(1);
  26. }
  27. }
  28. function GetFileContents(f) {
  29. try {
  30. return oFS.OpenTextFile(f).ReadAll();
  31. } catch (e) {
  32. // file doesn't exist
  33. return "";
  34. }
  35. }
  36. // get info from git
  37. var gitexe = GetGitExe();
  38. var revision = GetFirstStdOutLine(gitexe + cmd_revision);
  39. var describe = GetFirstStdOutLine(gitexe + cmd_describe);
  40. var branch = GetFirstStdOutLine(gitexe + cmd_branch);
  41. var isMaster = +("master" == branch);
  42. // remove hash (and trailing "-0" if needed) from description
  43. describe = describe.replace(/(-0)?-[^-]+(-dirty)?$/, '$2');
  44. var out_contents =
  45. "#include \"common/scm_rev.h\"\n" +
  46. "namespace Common {\n" +
  47. " const char g_scm_rev[] = \"" + revision + "\";\n" +
  48. " const char g_scm_branch[] = \"" + branch + "\";\n" +
  49. " const char g_scm_desc[] = \"" + describe + "\";\n" +
  50. "}\n";
  51. // check if file needs updating
  52. if (out_contents == GetFileContents(outfile)) {
  53. WScript.Echo(outfile + " current at " + describe);
  54. } else {
  55. // needs updating - writeout current info
  56. oFS.CreateTextFile(outfile, true).Write(out_contents);
  57. WScript.Echo(outfile + " updated to " + describe);
  58. }