-
Notifications
You must be signed in to change notification settings - Fork 20
/
Update-References.ps1
55 lines (44 loc) · 1.73 KB
/
Update-References.ps1
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
<#
.SYNOPSIS
This script updates the version number of a specified project in all referencing .csproj files.
.DESCRIPTION
The script recursively searches for all .csproj files and checks if they have a reference to the specified project.
If a reference is found, the version number of the reference is updated to the new version number.
.PARAMETER ProjectToFind
The name of the project to check references for.
.PARAMETER NewVersion
The new version number to set for the project references.
.EXAMPLE
.\Update-References.ps1 -ProjectToFind "ProjectName" -NewVersion "1.0.0"
#>
param(
[Parameter(Mandatory = $true)]
[string]$ProjectToFind,
[Parameter(Mandatory = $true)]
[string]$NewVersion
)
# Get all .csproj files recursively
$projectFiles = Get-ChildItem -Recurse -Filter '*.csproj'
# Loop through each project file to see if it has a reference to the project we are looking for
forEach ($projectFile in $projectFiles)
{
# Create an XmlDocument object
$projectXml = New-Object System.Xml.XmlDocument
# Set PreserveWhitespace to true
$projectXml.PreserveWhitespace = $true
# Load the XML file
$projectXml.Load($projectFile)
# Find the PackageReference nodes
$packageReferences = $projectXml.Project.ItemGroup.PackageReference
foreach ($packageReference in $packageReferences)
{
# Check if the Include attribute contains the project name
if ($packageReference.Include -like "*$ProjectToFind*" -and $packageReference.Version)
{
# Change the version if it has a version (is a nuget reference)
$packageReference.Version = $NewVersion
# Save the modified .csproj file
$projectXml.Save($projectFile)
}
}
}