-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNuspecAutoUpdate.ps1
More file actions
101 lines (86 loc) · 3.26 KB
/
NuspecAutoUpdate.ps1
File metadata and controls
101 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<#
.DESCRIPTION
A build script that can be included in TFS that update the versions of the dependencies in the '*.nuspec' file from the 'packages.config' file.
.EXAMPLE
NuspecAutoUpdate.ps1 -NuspecPath "myPackage.nuspec" -PackagesConfigPath "packages.config"
.EXAMPLE
NuspecAutoUpdate.ps1 -NuspecPath "C:\CCT\main\API\API\Cytra.API.nuspec" -PackagesConfigPath "C:\CCT\main\API\API\packages.config"
#>
Param
(
# The path to the *.nuspec file.
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$NuspecPath,
# The path to the packages.config file.
[Parameter(Mandatory=$true, Position=1)]
[ValidateNotNullOrEmpty()]
[string]$PackagesConfigPath
)
$ErrorActionPreference = "Stop"; #Make all errors terminating
function ReadXmlContent($filePath)
{
try {
[xml]$xmlContent = Get-Content $filePath;
return $xmlContent;
}
catch {
Write-Host "Failed to read file : $filePath";
Write-Host $Error[0].Exception;
exit 1;
}
}
# Reading all packages name & version from the "packages.Config" file
Write-Host "`nReading all packages name & version from the 'packages.Config' file";
$packagesDictionary = @{};
$packagesConfigXml = ReadXmlContent $PackagesConfigPath;
foreach($packageEntry in $packagesConfigXml.packages.package)
{
$packagesDictionary[$packageEntry.id] = $packageEntry.version;
}
# ReWrite the new version in the nuspec file.
Write-Host "ReWrite the new version in the nuspec file.";
$changesWhereMade = $False;
$versionRegex = [regex]"[0-9]+(.[0-9a-z]+)*";
$nuspecXml = ReadXmlContent $NuspecPath;
foreach($targetFrameworkGroup in $nuspecXml.package.metadata.dependencies.group)
{
Write-Host "";
Write-Host "Processing dependency group: $($targetFrameworkGroup.targetFramework)";
foreach($dependencyEntry in $targetFrameworkGroup.dependency)
{
$dependencyID = $($dependencyEntry.id);
$dependencyExistInPackageConfig = $packagesDictionary.ContainsKey($dependencyID);
if($dependencyExistInPackageConfig)
{
# We want to get the ' $versionFromConfigFile' and put it in the current '$dependencyEntry'.
$versionFromConfigFile = $($packagesDictionary[$dependencyID]);
$oldVersion = $($dependencyEntry.version);
#Notice: the fullNewVersion include "[ ] ( ) ," chars that doesn't exist in the package config file.
$fullNewVersion = $versionRegex.replace($oldVersion, $versionFromConfigFile, 1);
if($dependencyEntry.version -ne $fullNewVersion)
{
$dependencyEntry.version = $fullNewVersion;
$changesWhereMade = $True;
}
Write-Host "Dependency ID: $dependencyID , Old version: $oldVersion , New version: $fullNewVersion";
}
else
{
Write-Host "Dependency ID: $dependencyID Removed";
$dependencyEntry.ParentNode.RemoveChild($dependencyEntry);
$changesWhereMade = $True;
}
}
Write-Host "";
}
if($changesWhereMade)
{
Write-Host "Saving the new data in the original nuspec file.";
$nuspecXml.Save($NuspecPath);
}
else
{
Write-Host "All packages versions are up-to-date, no need to update the original nuspec file.";
}
Write-Host "Nuspec Auto Update Script Completed.";