.vcproj文件对于visual studio(vc++)用户来说并不陌生,而.vcxproj是VS2010推出之后,将.vcproj文件统一改为
.vcxproj,它们都是基于xml的文件,那么对它们的解析,有许多库。这里我就用libxml。
我要实现的功能很简单就是项目中所有的工程加入一个 ignorewarning(消除warning),比如/ignore:4011 。
这里就要写个脚本对所有的.vcproj(vs2008以前的版本)或 所有的.vcxproj(vs2010)添加/ignor:4011 。
这里要特别小心的是:用libxml库对.vcxproj进行解析要给解析的节点(node)加上默认的命名空间前缀,而对.vcproj
不需要这样 。
还有一点:加/ignore:4011的位置,.vcxproj和.vcproj是不一样的。对于具体的ignore warning具体分析啊。
下面我分别给出了对.vcproj和 .vcxproj解析的例子代码(仅供参考):
# to the .vcproj
require 'libxml'
include LibXML
include LibXML
def IgnoreWarningAdd(filePath)
parser = LibXML::XML::Parser.file(filePath)
doc = parser.parse
parser = LibXML::XML::Parser.file(filePath)
doc = parser.parse
doc.find("//Tool").each do |tool|
puts tool['Name']
if tool['Name'] == "VCLinkerTool" && tool['AdditionalOptions'] == " "
tool['AdditionalOptions'] = "/ignore:4011"
end
end
puts tool['Name']
if tool['Name'] == "VCLinkerTool" && tool['AdditionalOptions'] == " "
tool['AdditionalOptions'] = "/ignore:4011"
end
end
doc.save(filePath)
end
end
#recur to find the .vcproj files
def VcprojWarningAdd(dirPath)
Dir.entries(dirPath).each do |sub|
if sub != '.' && sub != '..'
if File.directory?("#{dirPath}/#{sub}")
VcprojWarningAdd("#{dirPath}/#{sub}")
else
filePath = "#{dirPath}/#{sub}"
#if /*.vcproj/.match(filePath)
Dir.entries(dirPath).each do |sub|
if sub != '.' && sub != '..'
if File.directory?("#{dirPath}/#{sub}")
VcprojWarningAdd("#{dirPath}/#{sub}")
else
filePath = "#{dirPath}/#{sub}"
#if /*.vcproj/.match(filePath)
if File.extname(filePath) == '.vcproj'
IgnoreWarningAdd("#{dirPath}/#{sub}")
end
end
end
end
end
IgnoreWarningAdd("#{dirPath}/#{sub}")
end
end
end
end
end
#VcprojWarningAdd("E://vcproj_folders")
VcprojWarningAdd(ARGV)
#to the .vcxproj
require 'libxml'
include LibXML
def IgnoreWarningAdd(filePath)
document = LibXML::XML::Document.file(filePath)
root = document.root
include LibXML
def IgnoreWarningAdd(filePath)
document = LibXML::XML::Document.file(filePath)
root = document.root
# to .vcxproj, need the default namespace prefix to parse the .vcxproj file
root.namespaces.default_prefix = "parsexml_ns"
document.find('//parsexml_ns:Link//parsexml_ns:AdditionalOptions').each do |node|
puts "here I entered"
node << " /ignore:4011 "
end
document.save(filePath)
end
def VcxprojWarningAdd(dirPath)
Dir.entries(dirPath).each do |sub|
if sub != '.' && sub != '..'
if File.directory?("#{dirPath}/#{sub}")
VcxprojWarningAdd("#{dirPath}/#{sub}")
else
filePath = "#{dirPath}/#{sub}"
#if /.vcxproj/.match(filePath)
root.namespaces.default_prefix = "parsexml_ns"
document.find('//parsexml_ns:Link//parsexml_ns:AdditionalOptions').each do |node|
puts "here I entered"
node << " /ignore:4011 "
end
document.save(filePath)
end
def VcxprojWarningAdd(dirPath)
Dir.entries(dirPath).each do |sub|
if sub != '.' && sub != '..'
if File.directory?("#{dirPath}/#{sub}")
VcxprojWarningAdd("#{dirPath}/#{sub}")
else
filePath = "#{dirPath}/#{sub}"
#if /.vcxproj/.match(filePath)
if File.extname(filePath) == '.vcxproj'
puts filePath
IgnoreWarningAdd("#{dirPath}/#{sub}")
puts "here am I: #{dirPath}/#{sub}"
end
end
end
end
end
#VcxprojWarningAdd("E://vcxproj_folder")
puts filePath
IgnoreWarningAdd("#{dirPath}/#{sub}")
puts "here am I: #{dirPath}/#{sub}"
end
end
end
end
end
#VcxprojWarningAdd("E://vcxproj_folder")
VcxprojWarningAdd(ARGV)