make_scmrev.h.js 2.0 KB

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