scm_rev_gen.js 2.2 KB

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