msvc-generate.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * msvc-generate.js - string transformation
  3. *
  4. * Copyright (C) 2008-2012 Alon Bar-Lev <alon.barlev@gmail.com>
  5. *
  6. * BSD License
  7. * ============
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * o Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * o Neither the name of the Alon Bar-Lev nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. var ForReading = 1;
  34. var fso = new ActiveXObject("Scripting.FileSystemObject");
  35. var input = "nul";
  36. var output = "nul";
  37. var files = new Array();
  38. var env = new Array();
  39. function initialize() {
  40. for (var i=0;i<WScript.Arguments.length;i++) {
  41. var arg = WScript.Arguments(i);
  42. if (arg.match(/^--input=(.*)$/)) {
  43. input=RegExp.$1;
  44. }
  45. else if (arg.match(/^--output=(.*)$/)) {
  46. output=RegExp.$1;
  47. }
  48. else if (arg.match(/^--config=(.*)$/)) {
  49. files.push(RegExp.$1);
  50. }
  51. else if (arg.match(/^--var=([^=]*)=(.*)$/)) {
  52. env[RegExp.$1] = RegExp.$2;
  53. }
  54. }
  55. }
  56. function process_config(vars, file) {
  57. try {
  58. var fin = fso.OpenTextFile(file, ForReading);
  59. while (!fin.AtEndOfStream) {
  60. var content = fin.ReadLine();
  61. if (content.match(/^[ \t]*define\(\[(.*)\],[ \t]*\[(.*)\]\)[ \t]*/)) {
  62. vars[RegExp.$1] = RegExp.$2;
  63. }
  64. }
  65. }
  66. catch(e) {
  67. throw new Error(1, "Cannot process '" + file + "'.");
  68. }
  69. }
  70. function process_file(vars, input, output) {
  71. var fin = fso.OpenTextFile(input, ForReading);
  72. var fout = fso.CreateTextFile(output);
  73. var content = fin.ReadAll();
  74. for (var i in vars) {
  75. content = content.replace(new RegExp("@"+i+"@", "g"), vars[i]);
  76. }
  77. fout.Write(content);
  78. }
  79. function build_vars() {
  80. var vars = new Array();
  81. for (var f in files) {
  82. process_config(vars, files[f]);
  83. }
  84. for (var e in env) {
  85. vars[e] = env[e];
  86. }
  87. return vars;
  88. }
  89. function main() {
  90. try {
  91. initialize();
  92. var vars = build_vars();
  93. process_file(
  94. vars,
  95. input,
  96. output
  97. );
  98. WScript.Quit(0);
  99. }
  100. catch(e) {
  101. WScript.Echo("ERROR: when procssing " + output + ": " + e.description);
  102. WScript.Quit(1);
  103. }
  104. }
  105. main();