-
Notifications
You must be signed in to change notification settings - Fork 7
/
Update-SolutionTargetFramework.ps1
83 lines (62 loc) · 3.35 KB
/
Update-SolutionTargetFramework.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
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
<#
.SYNOPSIS
Updates all projects in a solution to the same target framework version.
.DESCRIPTION
This will make copies of the project files to a temp folder, create new proj files from those with the alterations to another
temp folder, and then copy those into the original location. Even with this protection, this script sets its
ErrorActionPreference to STOP to avoid mangling a proj file.
.PARAMETER $SolutionFilePath
The full path to the solution
.PARAMETER $TargetFrameworkVersion
The target framework version ("v4.7.2" , "v4.6.1" , etc)
#>
function Update-SolutionTargetFramework
{
[CmdletBinding()]
Param(
[string] $SolutionFilePath,
[string] $TargetFrameworkVersion
)
$script:ErrorActionPreference = 'Stop'
$sourceProjFiles = Get-ProjectFiles -SolutionFilePath $SolutionFilePath
$replacementTargets = @{}
$tempPath = [IO.Path]::GetTempPath()
$projFilesTemp = "$tempPath\projfileswitch"
if (-Not (Test-Path $projFilesTemp)) { New-Item $projFilesTemp -ItemType Directory | Out-Null }
$projFilesBeforePath = Join-Path -Path $projFilesTemp -ChildPath "Before"
if (-Not (Test-Path $projFilesBeforePath)) { New-Item $projFilesBeforePath -ItemType Directory | Out-Null }
$projFilesAfterPath = Join-Path -Path $projFilesTemp -ChildPath "After"
if (-Not (Test-Path $projFilesAfterPath)) { New-Item $projFilesAfterPath -ItemType Directory | Out-Null }
Get-ChildItem -Path $projFilesTemp -Recurse -File | Remove-Item -Force -Recurse -Confirm:$false
Write-Output "Copying proj files to staging area '$projFilesBeforePath'"
#copying them to a "before" path makes it easier to diff the changes between them.
#modifying the file in place is also an option, but I had pending changes for some of these.
foreach ($sourceProjFile in $sourceProjFiles)
{
Copy-Item -Path $sourceProjFile -Destination $projFilesBeforePath
$fileName = Split-Path -Path $sourceProjFile -Leaf
$replacementTargets.Add($fileName, $sourceProjFile)
}
$beforeProjFiles = @(Get-ChildItem -Path $projFilesBeforePath -Filter *.csproj | Select-Object -ExpandProperty FullName)
Write-Output "Updating files from staging path to '$projFilesAfterPath'"
foreach ($beforeProjFile in $beforeProjFiles)
{
Copy-Item -Path $beforeProjFile -Destination $projFilesAfterPath
$fileName = Split-Path -Path $beforeProjFile -Leaf
$afterProjFile = Join-Path $projFilesAfterPath -ChildPath $fileName
Set-ItemProperty -Path $afterProjFile -Name IsReadOnly -Value $false
Update-ProjectProperty -ProjectFilePath $afterProjFile -PropertyName "TargetFrameworkVersion" -Value $TargetFrameworkVersion
}
$afterProjFiles = @(Get-ChildItem -Path $projFilesAfterPath -Filter *.csproj | Select-Object -ExpandProperty FullName)
Write-Output "Updating original source files"
#copying them back as a separate step
foreach ($afterProjFile in $afterProjFiles)
{
$fileName = Split-Path -Path $afterProjFile -Leaf
$sourceFilePath = $replacementTargets[$fileName]
Write-Output "Checking out source file at $sourceFilePath"
Edit-TfVcFile -Path $sourceFilePath
Write-Output "Overwriting source file at $sourceFilePath with `n`t $afterProjFile"
Copy-Item -Path $afterProjFile -Destination $sourceFilePath -Force -Confirm:$false
}
}