首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不丢失内容的情况下使用groovy编辑vsixmanifest?

如何在不丢失内容的情况下使用groovy编辑vsixmanifest?
EN

Stack Overflow用户
提问于 2018-03-15 18:54:44
回答 1查看 70关注 0票数 1

我的问题可能听起来很愚蠢,但我正在努力实现我的结果。我想在不丢失任何内容的情况下编辑和保存版本的vsixmanifest文件。这个article几乎解决了我的问题,但它删除了一些标记。

代码语言:javascript
复制
<?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脚本

代码语言:javascript
复制
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)
}

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

代码语言:javascript
复制
<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>

一些不需要的编辑:

代码语言:javascript
复制
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..

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

EN

回答 1

Stack Overflow用户

发布于 2018-03-15 23:28:54

那是因为XmlNodePrinter

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

代码语言:javascript
复制
def vsixmanifest = new XmlSlurper().parse(vsixmanifestFile)
vsixmanifest.Identifier[0].Version.replaceBody ( "YYYYY" )
println groovy.xml.XmlUtil.serialize(vsixmanifest)

您可以快速试用demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49297593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档