scm_rev_gen.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var wshShell = new ActiveXObject("WScript.Shell")
  2. var oFS = new ActiveXObject("Scripting.FileSystemObject");
  3. var outfile = "scm_rev.h";
  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. {
  9. for (var gitexe in {"git.cmd":1, "git":1})
  10. {
  11. try
  12. {
  13. wshShell.Exec(gitexe);
  14. return gitexe;
  15. }
  16. catch (e)
  17. {}
  18. }
  19. WScript.Echo("Cannot find git or git.cmd, check your PATH:\n" +
  20. wshShell.ExpandEnvironmentStrings("%PATH%"));
  21. WScript.Quit(1);
  22. }
  23. function GetFirstStdOutLine(cmd)
  24. {
  25. try
  26. {
  27. return wshShell.Exec(cmd).StdOut.ReadLine();
  28. }
  29. catch (e)
  30. {
  31. // catch "the system cannot find the file specified" error
  32. WScript.Echo("Failed to exec " + cmd + " this should never happen");
  33. WScript.Quit(1);
  34. }
  35. }
  36. function GetFileContents(f)
  37. {
  38. try
  39. {
  40. return oFS.OpenTextFile(f).ReadAll();
  41. }
  42. catch (e)
  43. {
  44. // file doesn't exist
  45. return "";
  46. }
  47. }
  48. // get info from git
  49. var gitexe = GetGitExe();
  50. var revision = GetFirstStdOutLine(gitexe + cmd_revision);
  51. var describe = GetFirstStdOutLine(gitexe + cmd_describe);
  52. var branch = GetFirstStdOutLine(gitexe + cmd_branch);
  53. var isMaster = +("master" == branch);
  54. // remove hash (and trailing "-0" if needed) from description
  55. describe = describe.replace(/(-0)?-[^-]+(-dirty)?$/, '$2');
  56. var out_contents =
  57. "#define SCM_REV_STR \"" + revision + "\"\n" +
  58. "#define SCM_DESC_STR \"" + describe + "\"\n" +
  59. "#define SCM_BRANCH_STR \"" + branch + "\"\n" +
  60. "#define SCM_IS_MASTER " + isMaster + "\n";
  61. // check if file needs updating
  62. if (out_contents == GetFileContents(outfile))
  63. {
  64. WScript.Echo(outfile + " current at " + describe);
  65. }
  66. else
  67. {
  68. // needs updating - writeout current info
  69. oFS.CreateTextFile(outfile, true).Write(out_contents);
  70. WScript.Echo(outfile + " updated to " + describe);
  71. }