如何使用groovy编辑vsixmanifest而不丢失内容?

时间:2018-03-15 10:54:44

标签: gradle groovy

我的问题可能听起来很愚蠢,但我很难实现我的结果。我想编辑并保存vsixmanifest文件的版本tage而不丢失任何内容。这个article几乎解决了我的目的,但它删除了一些标签。

<?xml version="1.0" encoding="utf-8"?>
<Vsix xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010">
  <Identifier Id="Visual Test">
    <Name>Visual Gallery Test</Name>
    <Author>Visual Studio Demo</Author>
    <Version>XXXXX</Version>
    <Description xml:space="preserve">Visual Studio Gallery Demo</Description>
    <Locale>1033</Locale>
    <AllUsers>true</AllUsers>
    <InstalledByMsi>false</InstalledByMsi>
    <Icon>Resources/Icon.png</Icon>
    <PreviewImage>Resources/Preview.png</PreviewImage>
    <SupportedProducts>
      <IsolatedShell Version="7.0">Visual Studio</IsolatedShell>
    </SupportedProducts>
    <SupportedFrameworkRuntimeEdition MinVersion="4.6" MaxVersion="4.9" />
  </Identifier>
  <Content>
    <VsPackage>XX.pkgdef</VsPackage>
  </Content>
</Vsix> 

这是我的gradle脚本

task updateExtensionManifest{
    def vsixmanifestFile = "build/source.extension.vsixmanifest"
    def vsixmanifest = new XmlParser().parse(vsixmanifestFile)
    vsixmanifest.Identifier[0].Version[0].value="YYYYY"
    def nodePrinter = new XmlNodePrinter(new PrintWriter(new FileWriter(vsixmanifestFile)))
    nodePrinter.preserveWhitespace = true
    nodePrinter.expandEmptyElements = true
    nodePrinter.print(vsixmanifest)
}

当我执行脚本时,它删除了一些标记定义清单文件,这是它在执行任务后的样子:

<Vsix xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010" Version="1.0.0">
  <Identifier Id="Visual Test">
    <Name>Visual Gallery Test</Name>
    <Author>Visual Studio Demo</Author>
    <Version>YYYYY</Version>
    <Description xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">Visual Studio Gallery Demo</Description>
    <Locale>1033</Locale>
    <AllUsers>true</AllUsers>
    <InstalledByMsi>false</InstalledByMsi>
    <Icon>Resources/Icon.png</Icon>
    <PreviewImage>Resources/Preview.png</PreviewImage>
    <SupportedProducts>
      <IsolatedShell Version="7.0">Visual Studio</IsolatedShell>
    </SupportedProducts>
    <SupportedFrameworkRuntimeEdition MinVersion="4.6" MaxVersion="4.9"></SupportedFrameworkRuntimeEdition>
  </Identifier>
  <Content>
    <VsPackage>XX.pkgdef</VsPackage>
  </Content>
</Vsix>

一些不需要的修改:

Line1 removed: <?xml version="1.0" encoding="utf-8"?>
Line2 modified to <Vsix xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010" Version="1.0.0">
Tag "Description" got edited too..

我怎么能避免这种编辑?我只是希望将版本从XXXXX修改为YYYYY而不通过我的gradle构建脚本更改任何其他内容。

1 个答案:

答案 0 :(得分:1)

那是因为XmlNodePrinter

如果需要xml标记声明,请使用XmlUtil.serialize()

def vsixmanifest = new XmlSlurper().parse(vsixmanifestFile)
vsixmanifest.Identifier[0].Version.replaceBody ( "YYYYY" )
println groovy.xml.XmlUtil.serialize(vsixmanifest)

您可以快速试用demo