用 libxml,ruby处理 .vcxproj文件 和 .vcproj文件

    .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
def IgnoreWarningAdd(filePath)
  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
  doc.save(filePath)
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)
        if File.extname(filePath) == '.vcproj'
         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
  # 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)
          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")
VcxprojWarningAdd(ARGV)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值