【问题标题】:Can NuGet edit a config file or only add to it?NuGet 可以编辑配置文件还是只添加到它?
【发布时间】:2011-08-01 17:12:57
【问题描述】:

我一直在为我的公司开发 NuGet 包,其中一项要求是能够更新我们的一些配置文件。

我知道可以添加到配置文件,但可以编辑吗?

例子:

<add name="conn" connectionString="Data Source=.\;Initial Catalog=DB;Integrated Security=True" />

改为下面

<add name="conn" connectionString="Data Source=.\;Initial Catalog=DB;User ID=ex;Password=example" />

【问题讨论】:

    标签: nuget nuget-package


    【解决方案1】:

    NuGet 转换无法编辑现有值。但是 NuGet 允许您在安装包时运行 Powershell 脚本,因此您可以通过这种方式编辑配置文件。

    创建一个 Install.ps1 文件并使用此代码:

    # Install.ps1
    param($installPath, $toolsPath, $package, $project)
    
    $xml = New-Object xml
    
    # find the Web.config file
    $config = $project.ProjectItems | where {$_.Name -eq "Web.config"}
    
    # find its path on the file system
    $localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
    
    # load Web.config as XML
    $xml.Load($localPath.Value)
    
    # select the node
    $node = $xml.SelectSingleNode("configuration/connectionStrings/add[@name='gveconn']")
    
    # change the connectionString value
    $node.SetAttribute("connectionString", "Data Source=.\;Initial Catalog=GVE;User ID=ex;Password=example")
    
    # save the Web.config file
    $xml.Save($localPath.Value)
    

    【讨论】:

      【解决方案2】:

      从 NuGet 2.6 及更高版本开始,您实际上可以使用用于 Visual Studio 中 Web.config 转换的 XDT 语法来转换 Web.config 文件。

      见http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations:

      支持 XML 文档转换 (XDT)

      从 NuGet 2.6 开始,支持 XDT 在项目内转换 XML 文件。 XDT 语法可以在包的 Content 文件夹下的 .install.xdt 和 .uninstall.xdt 文件中使用,它们将分别在包安装和卸载时应用。

      例如,要将 MyNuModule 添加到 web.config 文件中,如上所示,可以在 web.config.install.xdt 文件中使用以下部分:

      <?xml version="1.0"?>
      <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
          <system.webServer>
              <modules>
                  <add name="MyNuModule" type="Sample.MyNuModule" xdt:Transform="Insert" />
              </modules>
          </system.webServer>
      </configuration>
      

      另一方面,要在软件包卸载期间仅删除 MyNuModule 元素,可以在 web.config.uninstall.xdt 文件中使用以下部分:

      <?xml version="1.0"?>
      <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
          <system.webServer>
              <modules>
                  <add name="MyNuModule" xdt:Transform="Remove" xdt:Locator="Match(name)" />
              </modules>
          </system.webServer>
      </configuration>
      

      【讨论】:

      • 有什么方法可以防止将其应用于使用 te nuget 包的项目?
      • 这不是原始问题的实际目标吗?要将 ti 应用于消费项目?
      【解决方案3】:

      编辑:从 NUGET 2.6 及更高版本开始,答案现在是 YES。

      答案是否。从 nuget 网站我找到了以下答案:

      “NuGet 将转换文件合并到项目的配置文件中时,它只会在配置文件中添加元素或向现有元素添加属性;它不会以任何其他方式更改现有元素或属性。”

      http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations

      【讨论】:

      • 是的,没错。转换引擎并未涵盖所有场景,但我们可能会在未来对其进行扩展。
      • 这个答案不再正确。请参阅 Erik 的回答。
      【解决方案4】:

      是的,这是可能的,但您必须将 install.ps1 文件包含到 tools 文件夹中。然后,当您从 nuget 服务器获取包时,Visual Studio 会运行​​ Powershell 脚本。 我用这个脚本

      # fileName can be App.Config Or Web.Config or something else 
      $fileName = "App.Config" 
      $file=$project.ProjectItems.Item($fileName)
      
      if($file.Properties){
          # Get localpath
          $localPath = $file.Properties.Item("LocalPath")
          if($localPath){
              $localPath = $localPath.Value   
          }
      }
      
      if ($localPath -eq $null) {
          Exit
      }
      
      #Load our config file as XML file
      [xml]$file = Get-Content $localPath
      if($file){
      
          # Create node 
          $childNode = $file.CreateElement("add")
          $childNode.SetAttribute("connectionString", "DataSource=.\;InitialCatalog=GVE;User ID=ex;Password=example")
      
          #Get parent node   
          $node = $file.SelectSingleNode("configuration/connectionStrings")
      
          #Insert our node into parent
          $node.AppendChild($childNode)
      
          $file.Save($localPath)
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-01
        • 2022-08-15
        • 2021-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-23
        • 2013-02-02
        • 2015-12-16
        相关资源
        最近更新 更多