作者:朱金灿
来源:http://blog.csdn.net/clever101
很多时候升级了第三方库,需要对很多工程修改设置。在VS中按住Ctrl键确实可以多选工程,但通过这样做也有麻烦的地方:一是需要将所有的工程都加到一个解决方案下,二是假如有多个版本如Debug、Release、UnicodeDebug和UnicodeRelease,操作起来也挺繁琐的。现在我恰好也有这样的需求,我将gdal库从1.9.1升级到1.10.0,我决定编写一个JScript脚本实现修改VC工程设置。具体就是在包含的头文件目录中我要将gdal_1_9_1_vc9/include替换为gdal_1_10_vc9/include,第三方库路径gdal_1_9_1_vc9/lib替换为gdal_1_10_vc9/lib,附加依赖库gdal19_i.lib替换为gdal110_i.lib,gdal19_i_D.lib替换为gdal110_i_D.lib。
VC工程文件实际上是一个xml文件,其包含头文件路径属性、第三方库路径属性以及附加依赖库都在Tool支项下设置。因此只需要读取VC工程文件,修改对应的属性就行。JScript脚本代码如下:
try { var WSShell = WScript.CreateObject("WScript.Shell"); var num = WScript.ScriptFullName.lastIndexOf("\"); // 获取当前路径 var strCurPath = WScript.ScriptFullName.substr(0,num); var FileSys = WScript.CreateObject("Scripting.FileSystemObject"); // 判断当前文件夹是否存在 if (FileSys.FolderExists(strCurPath)) { // 修改当前文件夹下的Plugin子文件夹下的所有vc工程 var strPluginFolder = strCurPath + "\Plugin"; var PluginFolder = FileSys.GetFolder(strPluginFolder); // 获取所有子文件夹,遍历所有的子文件夹,修改所有的工程文件 var PlugSet = new Enumerator(PluginFolder.SubFolders); for (var i = 0 ; !PlugSet.atEnd(); PlugSet.moveNext(),i++) { var strProjectFolder = strPluginFolder + "\"; strProjectFolder = strProjectFolder + PlugSet.item().Name; var ProjectFolder = FileSys.GetFolder(strProjectFolder); // 获取所有的文件 var fenum = new Enumerator(ProjectFolder.Files); for (var j = 0 ; !fenum.atEnd(); fenum.moveNext(),j++) { var strPrjFileName = fenum.item().Name; var strSuff = FileSys.GetExtensionName(strPrjFileName); // 假如文件是vc工程文件,就修改工程属性设置 if((strSuff=="vcproj")) { strProjectFolder = strProjectFolder + "\"; strPrjFileName = strProjectFolder + strPrjFileName; ChangePrjSetting(strPrjFileName); } } } } } catch(e) { WScript.Echo(e.description); } WScript.Echo("修改成功!"); // 修改工程文件,strPrjPath为工程文件路径 function ChangePrjSetting(strPrjPath) { try { var xmlDoc = null; var signatures = [ "Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument", "Microsoft.XmlDom"]; for (var index = 0; index < signatures.length; index++) { try { xmlDoc = new ActiveXObject(signatures[index]); } catch (e) { WScript.Echo(e.description); } } xmlDoc.async = false; xmlDoc.resolveExternals = false; var strXmlPath = strPrjPath; xmlDoc.load(strXmlPath); if (xmlDoc.parseError.errorCode != 0) { var myError = xmlDoc.parseError; WScript.Echo("打开xml文件失败: " + myError); } else { // 运用正则表达式进行匹配 var strPattern = /gdal_1_9_1_vc9/; // 定义一个匹配模板 // 修改包含头文件路径 ChangePrjAttribute(strXmlPath,xmlDoc,"AdditionalIncludeDirectories","include",strPattern); // 修改第三方库路径 ChangePrjAttribute(strXmlPath,xmlDoc,"AdditionalLibraryDirectories","lib",strPattern); // 修改release版的附加依赖库 strPattern = /gdal19_i.lib/; ChangePrjDependenLib(strXmlPath,xmlDoc,"gdal19_i.lib","gdal110_i.lib",strPattern) // 修改debug版的附加依赖库 strPattern = /gdal19_i_D.lib/; ChangePrjDependenLib(strXmlPath,xmlDoc,"gdal19_i_D.lib","gdal110_i_D.lib",strPattern) } } catch(e) { WScript.Echo(e.description); } } // 修改工程属性 function ChangePrjAttribute(strXmlPath,xmlDoc,strAttribute,strSuffix,strPattern) { var BinGroup = xmlDoc.getElementsByTagName("Tool"); for (var i = 0; i < BinGroup.length; i++) { var strIncDir = BinGroup.item(i).getAttribute(strAttribute); if(null!=strIncDir) { var vecIncPath = new Array(); vecIncPath = strIncDir.split(";"); var bChange = false; for(var j=0;j<vecIncPath.length;j++) { var result = strPattern.exec(vecIncPath[j]); if(null!=result) { // 设置新的文件路径 vecIncPath[j] = "$(THIRD_PARTY)\gdal_1_10_vc9\"+strSuffix; bChange = true; } } if(true==bChange) { // 将所有的路径拼起来,注意使用一个冒号分开 var NewIncPath = vecIncPath[0]; for(var k=1;k<vecIncPath.length;k++) { NewIncPath = NewIncPath + ";" NewIncPath = NewIncPath + vecIncPath[k]; } BinGroup.item(i).setAttribute(strAttribute,NewIncPath); xmlDoc.save(strXmlPath); } } } } // 修改附加依赖库 function ChangePrjDependenLib(strXmlPath,xmlDoc,strOldLib,strNewLib,strPattern) { var BinGroup = xmlDoc.getElementsByTagName("Tool"); for (var i = 0; i < BinGroup.length; i++) { var strDepLib = BinGroup.item(i).getAttribute("AdditionalDependencies"); if(null!=strDepLib) { var vecLibPath = new Array(); vecDepLib = strDepLib.split(" "); var bChange = false; for(var j=0;j<vecDepLib.length;j++) { var result = strPattern.exec(vecDepLib[j]); if(null!=result) { // 修改依赖库名 vecDepLib[j] = strNewLib; bChange = true; } } if(true==bChange) { // 将所有的依赖库拼起来,注意使用一个空格分开 var NewDepLib = vecDepLib[0]; for(var k=1;k<vecDepLib.length;k++) { NewDepLib = NewDepLib + " "; NewDepLib = NewDepLib + vecDepLib[k]; } BinGroup.item(i).setAttribute("AdditionalDependencies",NewDepLib); xmlDoc.save(strXmlPath); } } } }