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 34var ForReading = 1; 35var fso = new ActiveXObject("Scripting.FileSystemObject"); 36var input = "nul"; 37var output = "nul"; 38var files = new Array(); 39var env = new Array(); 40 41function initialize() { 42 for (var i=0;i<WScript.Arguments.length;i++) { 43 var arg = WScript.Arguments(i); 44 if (arg.match(/^--input=(.*)$/)) { 45 input=RegExp.$1; 46 } 47 else if (arg.match(/^--output=(.*)$/)) { 48 output=RegExp.$1; 49 } 50 else if (arg.match(/^--config=(.*)$/)) { 51 files.push(RegExp.$1); 52 } 53 else if (arg.match(/^--var=([^=]*)=(.*)$/)) { 54 env[RegExp.$1] = RegExp.$2; 55 } 56 } 57} 58 59function process_config(vars, file) { 60 try { 61 var fin = fso.OpenTextFile(file, ForReading); 62 63 while (!fin.AtEndOfStream) { 64 var content = fin.ReadLine(); 65 if (content.match(/^[ \t]*define\(\[(.*)\],[ \t]*\[(.*)\]\)[ \t]*/)) { 66 vars[RegExp.$1] = RegExp.$2; 67 } 68 } 69 } 70 catch(e) { 71 throw new Error(1, "Cannot process '" + file + "'."); 72 } 73} 74 75function process_file(vars, input, output) { 76 var fin = fso.OpenTextFile(input, ForReading); 77 var fout = fso.CreateTextFile(output); 78 var content = fin.ReadAll(); 79 80 for (var i in vars) { 81 content = content.replace(new RegExp("@"+i+"@", "g"), vars[i]); 82 } 83 84 fout.Write(content); 85} 86 87function build_vars() { 88 var vars = new Array(); 89 for (var f in files) { 90 process_config(vars, files[f]); 91 } 92 for (var e in env) { 93 vars[e] = env[e]; 94 } 95 return vars; 96} 97 98function main() { 99 try { 100 initialize(); 101 102 var vars = build_vars(); 103 104 process_file( 105 vars, 106 input, 107 output 108 ); 109 110 WScript.Quit(0); 111 } 112 catch(e) { 113 WScript.Echo("ERROR: when procssing " + output + ": " + e.description); 114 WScript.Quit(1); 115 } 116} 117 118main(); 119