janhartmann
janhartmann

Reputation: 15003

Open XML, modify and save again

I have a build script to modify some Nuget *.nuspec dependency versions which used to work - but somehow it doesn't anymore. I do not know if its because of my updating to Windows 10 or the Powershell versions changed.

Function ChangeNugetSpecDependencyVersion() {

    Param(
        [Parameter(Mandatory=$true)]
        [string]$filePath,
        [Parameter(Mandatory=$true)]
        [string]$packageId,
        [Parameter(Mandatory=$true)]
        [string]$publishVersion
    )

    [xml] $toFile = (Get-Content $filePath)

    $nodes = $toFile.SelectNodes("//package/metadata/dependencies/dependency[starts-with(@id, $packageId)]")
    if ($nodes) {
        foreach ($node in $nodes) {
            $nodeId = $node.id
            Write-Host "-- Updating '$nodeId' in '$filePath' to version '$publishVersion'"
            $node.version = "[" + $publishVersion +"]"
            $toFile.Save($filePath)
        }
   }
}

# Version
$version = "2.0.0.0" 

# Assemblies
$assemblies = "NerveFramework", "NerveFramework.EntityFramework"

# Change dependency version on all depending assemblies
Write-Host "Changing the NuGet Spec version dependencies to '$version'..."
$nuspecFiles = Get-ChildItem $assemblies -Filter "NerveFramework*.nuspec" -Recurse | Resolve-Path -Relative
foreach ($nuspecFile in $nuspecFiles) {
    ChangeNugetSpecDependencyVersion $nuspecFile "NerveFramework" $version
} 

The error is the $toFile.Save($filePath) where it says:

Exception calling "Save" with "1" argument(s): "En del af stien 'C:\Users\Jan\NerveFramework.EntityFramework\NerveFramework.EntityFramework.nuspec' blev ikke fundet." At C:\GitHub\nerve-framework\BuildFunctions.ps1:58 char:13 + $toFile.Save($filePath) + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Sorry for the partial danish language, but it says it can not find parts of the path.

One thing here is that this path: C:\Users\Jan\NerveFramework.EntityFramework\NerveFramework.EntityFramework.nuspec is wrong.

The file location is relative to the file executing the command: C:\GitHub\nerve-framework\NerveFramework.EntityFramework\NerveFramework.EntityFramework.nuspec

But it can open the file, but why is the save function then providing the wrong path?

Upvotes: 2

Views: 428

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

Why are you using Resolve-Path at all? You should just pass the full path to the ChangeNugetSpecDepdencyVersion function:

Also, within the function, you call $toFile.Save($filePath) foreach node - instead of only one time (at the end).

And a last side node: you should use approved verbs for functions like Update-NugetSpecDependyVersion and omit the trailing (). Example:

Function Update-NugetSpecDependencyVersion 
{
    Param
    (
        [Parameter(Mandatory=$true)]
        [string]$filePath,
        [Parameter(Mandatory=$true)]
        [string]$packageId,
        [Parameter(Mandatory=$true)]
        [string]$publishVersion
    )

    [xml] $toFile = (Get-Content $filePath)

    $nodes = $toFile.SelectNodes("//package/metadata/dependencies/dependency[starts-with(@id, $packageId)]")
    foreach ($node in $nodes) 
    {
        Write-Host ("-- Updating '{0}' in '{1}' to version '{2}'" -f $node.id, $filePath, $publishVersion)
        $node.version = "[{0}]" -f $publishVersion
    }

    $toFile.Save($filePath)

}

# Version
$version = "2.0.0.0" 

# Assemblies
$assemblies = "NerveFramework", "NerveFramework.EntityFramework"

# Change dependency version on all depending assemblies
Write-Host "Changing the NuGet Spec version dependencies to '$version'..."
Get-ChildItem $assemblies -Filter "NerveFramework*.nuspec" -Recurse | 
        % { Update-NugetSpecDependencyVersion $_.FullName "NerveFramework" $version }

Upvotes: 1

Related Questions