2017-09-25 6 views
0

.targetsファイル(XML)の1つの値を変更して文書を保存するNuGetパッケージのinit.ps1スクリプトを作成しようとしています。スクリプトはすべて正常にエラーなく実行しますが、ドキュメントをチェックすると変更されません。ここでNuGet初期化スクリプト中にXML文書を保存できない

はスクリプトです:

Param($installPath, $toolsPath, $package) 

$proj = Get-Project 
$pack = $package 

# Detect if the project installing the NuGet package is a web application and 
# alters an xml element value in the .targets value to prevent duplication 
# of application resources when published. 

if ($proj.ExtenderNames -contains "WebApplication") { 
    # Begin to build neccessary path strings to find the .targets file. 
    # The targets file is currently located in 
    # \packages\packageName.packageVersion\build\packageName.targets. 
    $packageName = [string]$pack.Id 

    $packageRootDir = $installPath 

    # packageName.Version\build 
    $packageBuildFolderPath = Join-Path $packageRootDir "build" 

    # packageName.Version\build\packageName 
    $targetsFilePath = Join-Path $packageBuildFolderPath ($packageName + ".targets") 
    "$targetsFilePath" 

    if (Test-Path $targetsFilePath) { 
     # If the targets file path has correctly located the file then 
     # we edit the targets file to alter the CopyToOutputDirectory element. 

     # Load the targets file as an xml object 
     $xml = New-Object System.Xml.XmlDocument 
     $xml.Load($targetsFilePath) 
     "xml loaded" 
     # Search each ItemGroup element for the one containing a Content element. 
     foreach ($group in $xml.Project.ItemGroup) { 
      $nodeExists = $group.Content.CopyToOutputDirectory 

      if ($nodeExists) { 
       "$nodeExists" 
       # Edits the value when we find the correct node 
       $nodeExists = "Never" 
       "$nodeExists" 
      } 
     } 
     "xml modified" 

     # Save the updated document to the correct place. 
     $savePath = [string]$targetsFilePath 
     "$savePath" 
     $xml.Save($savePath) 
     "xml Saved to $savePath" 
    } 
} 

そして、ここでは最初からパッケージマネージャの出力は、スクリプトブロックの末尾にある:

 
Executing script file 'path to tools/Init.ps1' 
'correct path to package/build/package.targets' 
xml loaded 
Always 
Never 
xml modified 
'correct path to package/build/package.targets' 
xml Saved to 'correct path to package/build/package.targets' 
+0

実際の質問とは別に、ファイルで何を修正しようとしていますか? /なぜそれが必要ですか? –

答えて

1

あなたのコードは、変数$nodeExistsの値を変更し、その値が由来するXMLノードは含まれません。

$xml.Save([Console]::Out) 

を実際にノードの値を変更するには、このような何かにあなたのコードを変更します:あなたは、ループの後、実際のXMLデータを見てとることであることを確認することができます

foreach ($group in $xml.Project.ItemGroup) { 
    if ($group.Content.CopyToOutputDirectory) { 
     $group.Content.CopyToOutputDirectory = 'Never' 
    } 
} 

または、このような:

$xml.SelectNodes('//Content/CopyToOutputDirectory') | ForEach-Object { 
    $_.'#text' = 'Never' 
} 

XMLが名前空間を使用する場合、後者はnamespace managerが必要であることに注意してください。

+0

返事ありがとうございます、これは確かに問題の一つでした。 VS 2012ではinit.ps1スクリプトが呼び出され、.targetsファイルが上書きされるようにコピーされました。それはこの質問の範囲内ではありません。 – BHigzz

関連する問題