make_scmrev.h.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. var cmd_commits_ahead = " rev-list --count HEAD ^master";
  8. var cmd_get_tag = " describe --exact-match HEAD";
  9. function GetGitExe()
  10. {
  11. try
  12. {
  13. gitexe = wshShell.RegRead("HKCU\\Software\\GitExtensions\\gitcommand");
  14. wshShell.Exec(gitexe);
  15. return gitexe;
  16. }
  17. catch (e)
  18. {}
  19. for (var gitexe in {"git.cmd":1, "git":1, "git.bat":1})
  20. {
  21. try
  22. {
  23. wshShell.Exec(gitexe);
  24. return gitexe;
  25. }
  26. catch (e)
  27. {}
  28. }
  29. // last try - msysgit not in path (vs2015 default)
  30. msyspath = "\\Git\\cmd\\git.exe";
  31. gitexe = wshShell.ExpandEnvironmentStrings("%PROGRAMFILES(x86)%") + msyspath;
  32. if (oFS.FileExists(gitexe)) {
  33. return gitexe;
  34. }
  35. gitexe = wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%") + msyspath;
  36. if (oFS.FileExists(gitexe)) {
  37. return gitexe;
  38. }
  39. WScript.Echo("Cannot find git or git.cmd, check your PATH:\n" +
  40. wshShell.ExpandEnvironmentStrings("%PATH%"));
  41. WScript.Quit(1);
  42. }
  43. function GetFirstStdOutLine(cmd)
  44. {
  45. try
  46. {
  47. return wshShell.Exec(cmd).StdOut.ReadLine();
  48. }
  49. catch (e)
  50. {
  51. // catch "the system cannot find the file specified" error
  52. WScript.Echo("Failed to exec " + cmd + " this should never happen");
  53. WScript.Quit(1);
  54. }
  55. }
  56. function AttemptToExecuteCommand(cmd)
  57. {
  58. try
  59. {
  60. var exec = wshShell.Exec(cmd)
  61. // wait until the command has finished
  62. while (exec.Status == 0) {}
  63. return exec.ExitCode;
  64. }
  65. catch (e)
  66. {
  67. // catch "the system cannot find the file specified" error
  68. WScript.Echo("Failed to exec " + cmd + " this should never happen");
  69. WScript.Quit(1);
  70. }
  71. }
  72. function GetFileContents(f)
  73. {
  74. try
  75. {
  76. return oFS.OpenTextFile(f).ReadAll();
  77. }
  78. catch (e)
  79. {
  80. // file doesn't exist
  81. return "";
  82. }
  83. }
  84. // get info from git
  85. var gitexe = GetGitExe();
  86. var revision = GetFirstStdOutLine(gitexe + cmd_revision);
  87. var describe = GetFirstStdOutLine(gitexe + cmd_describe);
  88. var branch = GetFirstStdOutLine(gitexe + cmd_branch);
  89. var commits_ahead = GetFirstStdOutLine(gitexe + cmd_commits_ahead);
  90. // Get environment information.
  91. var distributor = wshShell.ExpandEnvironmentStrings("%DOLPHIN_DISTRIBUTOR%");
  92. if (distributor == "%DOLPHIN_DISTRIBUTOR%") distributor = "None";
  93. var default_update_track = wshShell.ExpandEnvironmentStrings("%DOLPHIN_DEFAULT_UPDATE_TRACK%");
  94. if (default_update_track == "%DOLPHIN_DEFAULT_UPDATE_TRACK%") default_update_track = "";
  95. // remove hash (and trailing "-0" if needed) from description
  96. describe = describe.replace(/(-0)?-[^-]+(-dirty)?$/, '$2');
  97. // set commits ahead to zero if on a tag
  98. if (AttemptToExecuteCommand(gitexe + cmd_get_tag) == 0)
  99. {
  100. commits_ahead = "0";
  101. }
  102. var out_contents =
  103. "#define SCM_REV_STR \"" + revision + "\"\n" +
  104. "#define SCM_DESC_STR \"" + describe + "\"\n" +
  105. "#define SCM_BRANCH_STR \"" + branch + "\"\n" +
  106. "#define SCM_COMMITS_AHEAD_MASTER " + commits_ahead + "\n" +
  107. "#define SCM_DISTRIBUTOR_STR \"" + distributor + "\"\n" +
  108. "#define SCM_UPDATE_TRACK_STR \"" + default_update_track + "\"\n";
  109. // check if file needs updating
  110. if (out_contents == GetFileContents(outfile))
  111. {
  112. WScript.Echo(outfile + " current at " + describe);
  113. }
  114. else
  115. {
  116. // needs updating - writeout current info
  117. oFS.CreateTextFile(outfile, true).Write(out_contents);
  118. WScript.Echo(outfile + " updated to " + describe);
  119. }