-
Notifications
You must be signed in to change notification settings - Fork 3
/
MigrateThisC#ProjectToCore.ps1
241 lines (207 loc) · 8.83 KB
/
MigrateThisC#ProjectToCore.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
$ErrorActionPreference = "Inquire"
. "$PSScriptRoot\ITV.util.ps1"
. "$PSScriptRoot\CSProjTemplates.ps1"
Head "A PowerShell Script for the Migration of C#-based .NET Framework projects to .NET Core 3.1 or .NET 5.0 or .NET 6.0"
Head "Dr. Holger Schwichtenberg, www.IT-Visions.de 2019-2021"
Head "Skript-Version: 0.6.0 (2021-11-22)"
Head "Using .NET SDK Version: $(dotnet --version)"
# ******************************************************
$TFM = "net6.0" # or "net5.0" or "netcoreapp3.1"
$TFMWindows = "net6.0-windows" # or "net5.0" or "netcoreapp3.1"
$defaultNugets = @{
"Microsoft.Windows.Compatibility"="6.0.0" # or: "Microsoft.Windows.Compatibility"="5.0.2" or: "Microsoft.Windows.Compatibility"="3.1.2"
}
#region -------------------------- Register "Migrate this C#-Project to .NET Core" command for .csproj
if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT -ea SilentlyContinue | out-null
$regPath = "HKCR:PROGID\shell\Migrate this C#-Project to .NET Core or .NET 5\"
$progIds = (Get-ItemProperty HKCR:.csproj\OpenWithProgids | gm | where name -Like "VisualStudio*").Name
foreach($progID in $progIds)
{
$classRoot = $regPath -replace "PROGID", $progID
Write-Host "Registering this script in Registry $classRoot ..."
md "$classRoot" -Force | Out-Null
md "$classRoot\command" -Force | Out-Null
$registryPath = "$classRoot\Command"
$Name = "(Default)"
$value = 'powershell.exe -File "' + $PSScriptRoot +"\" + $MyInvocation.MyCommand.Name + '" "%1"'
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
}
success "DONE! Run script as normal user for migrating C# projects to .NET Core\.NET 5!"
Exit-ReturnKey
}
#endregion
# --------------------------------------------------------------------------------
function Migrate-Project($projectfile, $newParentFolderName, $template, $projects, $libs, $nugets)
{
<#
.SYNOPSIS
convert a .NET Framework project to .NET Core\.NET 5\.NET 6
#>
h1 "Converting .NET Framework project to .NET Core\.NET 5\.NET 6: $projectFile"
#region -------------------------- Paths
h2 "Creating paths..."
$filename = [System.IO.Path]::GetFileNameWithoutExtension($projectfile)
$projectFolder = [System.IO.Path]::GetDirectoryName($projectfile)
$folderObj = (get-item $projectFolder)
$parentfolder = $folderObj.Parent.FullName
$newProjectFolder = [System.IO.Path]::Combine($newParentFolderName, $folderObj.Name)
$newProjectName = [System.IO.Path]::GetFileNameWithoutExtension((get-item $projectfile).Name)
$newProjectFilePath = [System.IO.Path]::Combine($newProjectFolder,"$($newProjectName).csproj")
print "Source path: $projectFolder"
print "Source Project file: $projectfile"
print "Target Solution folder: $newParentFolderName"
print "Target path: $newProjectFolder"
print "Target project: $newProjectFilePath"
print "Template: $template"
# Create target Solution folder, if it does not exists
if (-not (test-path $newParentFolderName)) { md $newParentFolderName }
# Remove target project folder, if it DOES exists
Remove-Item $newProjectFolder/* -Recurse -Force -ea SilentlyContinue
md $newProjectFolder -ea SilentlyContinue
#region
#region -------------------------- Getting data from existing project file
h2 "Getting data from existing project file..."
if (-not (test-path $projectfile)) { throw "Project file not found!" }
$rootnamespace = Get-RegExFromFile $projectfile '<RootNamespace>(.*)</RootNamespace>'
if ($rootnamespace -eq $null) { $rootnamespace = $newProjectName }
print "Rootnamespace: $rootnamespace"
if ($projects -eq $null) {
$projects = Get-RegExFromFile $projectfile '<ProjectReference Include="(.*)"'
if ($project -ne $null) { $projects | foreach { print "Projektreference: $_" } }
}
function Get-PackageReferencesFromFile($path)
<#
.SYNOPSIS
Get a tag oder value from a project file
#>
{
[Hashtable] $e = @{ }
$xml = [xml] (Get-Content $path)
$prSet = $xml.SelectNodes("//*[local-name()='PackageReference']")
foreach($pr in $prSet)
{
$name = ($pr.Attributes["Include"]).value
$vers = ($pr.SelectSingleNode("//*[local-name()='Version']")).innerText
#print "$name = $vers"
$e.Add($name, $vers)
}
#print $e.Count
$e
}
$nugetsFromProject += (Get-PackageReferencesFromFile $projectfile )
print $nugetsFromProject.Gettype().fullname
$nugets += $nugetsFromProject
if ($nugets -ne $null) { $nugets.keys | ForEach-Object { print "PackageReference: $_"; } }
$applicationicon = Get-RegExFromFile $projectfile '<ApplicationIcon>(.*)</ApplicationIcon>'
print "applicationicon: $applicationicon"
#endregion
#region -------------------------- Remove Destination Folder"
h2 "Clean Destination Folder $newProjectFolder"
if (test-path $newProjectFolder) {
warning "Removing existing files in $newProjectFolder..."
rd $newProjectFolder -Force -Recurse
}
#endregion
#region -------------------------- Copy Code
h2 "Copy Code $newProjectName to $newProjectFolder"
Copy-Item $projectFolder $newProjectFolder -Recurse -Force
dir $newProjectFolder | out-default
h2 "Make writeable"
Get-ChildItem -Path $newProjectFolder -Recurse -File | ForEach-Object {
$_.IsReadOnly = $false
}
h2 "Remove unused files in $newProjectName..."
if (Test-Path $newProjectFolder\bin) {rd $newProjectFolder\bin -Recurse }
if (Test-Path $newProjectFolder\obj) {rd $newProjectFolder\obj -Recurse }
remove-item $newProjectFolder\*.vspscc
remove-item $newProjectFolder\*.sln
remove-item $newProjectFolder\*.csproj
dir $newProjectFolder -Recurse | Set-ItemProperty -Name IsReadOnly -Value $false -ErrorAction SilentlyContinue | out-default
dir $newProjectFolder | out-default
#endregion
#region -------------------------- Creating new project file
h2 "Creating new project file ($newProjectFilePath )..."
$csproj = $template
$csproj = $csproj.Replace("[DATE]",(get-Date))
$csproj = $csproj.Replace("[rootnamespace]",$rootnamespace)
$csproj = $csproj.Replace("[icon]",$applicationicon)
$csproj = $csproj.Replace("[TFM]",$TFM)
$csproj = $csproj.Replace("[TFMWINDOWS]",$TFMWINDOWS)
$projRef = ""
foreach($r in $projects)
{
print "Project: $r"
$projRef += " " + $ProjRefTemplate.Replace("TODO",$r) + "`n"
}
$csproj = $csproj.Replace("[ProjectReference]",$projRef.TrimEnd())
$assetRef = ""
if (test-path $newProjectFolder\assets)
{
$assets = dir $newProjectFolder\assets
foreach($a in $assets)
{
print "Asset: $a"
$assetRef += " " + $AssetTemplate.Replace("FILE","assets\$($a.name)") + "`n"
}
}
$csproj = $csproj.Replace("[AssetsRef]",$assetRef.TrimEnd())
$nugetref = ""
foreach($n in $nugets.keys)
{
print "Nuget: $n $($nugets[$n])"
$nugetref += " " + $NugetRefTemplate.Replace("NAME",$n).Replace("VERSION",$nugets[$n]) + "`n"
}
$csproj = $csproj.Replace("[NugetReference]",$nugetref.TrimEnd())
$libref = ""
foreach($l in $libs)
{
print "DLL: $l"
$libref += " " + $LibRefTemplate.Replace("TODO",$l)
}
$csproj = $csproj.Replace("[LibReference]",$libref.TrimEnd())
#print $csproj
$csproj | Set-Content $newProjectFilePath -Force
#endregion
#region -------------------------- Build
h2 "Build $newProjectName..."
cd $newProjectFolder
#dotnet restore | out-default
dotnet build | out-default
#endregion
return $newProjectFolder
}
#region ############################ Main
$sourceproject = $args[0] # Get path to .csproj from Script arguments
if ($sourceproject -eq $null) { Write-Error "No path! :-("; Read-Host; exit; }
$projectFolder = [System.IO.Path]::GetDirectoryName($sourceproject)
$projektName = [System.IO.Path]::GetFileName(($sourceproject))
$folderObj = (get-item $projectFolder)
$parentfolder = $folderObj.Parent.FullName
$newParentFolderName = $parentfolder + "#" + $TFM.Replace(".","")
print "Selected Source Folder: $projectFolder"
print "Selected Project: $projektName"
if (test-path ([System.IO.Path]::Combine($projectfolder, "packages.config")) )
{
warning "There is still a packages.config in this project. You should move to <PackageReference>-Tags using Visual Studio. Continue anyway? (Y/N)"
$c = Read-Host
if ($c -ine "y" ) { exit }
}
$template = ""
$c = read-host "Template: C=Console, W=WPF/WinForms, L=Library (DLL), U=Unit Tests Other=exit?"
switch($c.toupper())
{
"W" { $template = $projwpftemplate; }
"C" { $template = $projEXETemplate; }
"U" { $template = $projTestTemplate; }
"L" { $template = $projlibtemplate; }
default { return }
}
print "Target Folder: Press enter to accept the default [$($newParentFolderName)]"
$defaultValue = 'default'
$prompt = Read-Host "Press enter to accept the default [$($newParentFolderName)]"
$newParentFolderName = ($newParentFolderName,$prompt)[[bool]$prompt]
$newProjectFolder = Migrate-Project $sourceproject $newParentFolderName $template $null $null $defaultNugets
success "DONE: $newProjectFolder"
Exit-ReturnKey