打开XML,再次修改和保存

时间:2015-09-04 10:54:29

标签: powershell

我有一个构建脚本来修改一些以前工作的Nuget * .nuspec依赖版本 - 但不知怎的,它已经不存在了。我不知道是因为我更新到Windows 10还是更改了Powershell版本。

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
} 

错误是$toFile.Save($filePath),其中包含:

  

异常通话"保存"用" 1"参数:" En del af stien   ' C:\ Users \用户简\ NerveFramework.EntityFramework \ NerveFramework.EntityFramework.nuspec'   blev ikke fundet。"在C:\ GitHub \ nerve-framework \ BuildFunctions.ps1:58   焦炭:13   + $ toFile.Save($ filePath)   + ~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:NotSpecified:(:) [],MethodInvocationException       + FullyQualifiedErrorId:DotNetMethodException

对于部分丹麦语言感到抱歉,但它说无法找到部分路径。

这里有一点是这条路:C:\Users\Jan\NerveFramework.EntityFramework\NerveFramework.EntityFramework.nuspec是错的。

文件位置是相对于执行命令的文件:C:\GitHub\nerve-framework\NerveFramework.EntityFramework\NerveFramework.EntityFramework.nuspec

但它可以打开文件,但是为什么save函数会提供错误的路径呢?

1 个答案:

答案 0 :(得分:1)

你为什么要使用Resolve-Path?您应该将完整路径传递给ChangeNugetSpecDepdencyVersion函数:

此外,在函数中,您调用$toFile.Save($filePath) foreach节点 - 而不是仅调用一次(在结束时)。

最后一个节点:您应该将approved verbs用于Update-NugetSpecDependyVersion等功能,并省略尾随()。例如:

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 }