Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Migrate to new CS Project format
function Extract-ProjectReferences ($projectFile) {
[regex]::matches((Get-Content $projectFile), '<ProjectReference Include=\"([^"]*)\"') | ForEach-Object { $_.Groups[1].Value }
}
function Extract-NuGetReferences ($nugetFile) {
[regex]::matches((Get-Content $nugetFile), '<package id=\"([^"]*)\" version=\"([^"]*)\"') | ForEach-Object { @{ Name = $_.Groups[1].Value; Version = $_.Groups[2].Value } }
}
function Get-UpgradedProjectFile ($projectFile) {
$nugetReferences = Extract-NuGetReferences ($projectFile.DirectoryName + "\packages.config")
$nugetReferencesSection = $nugetReferences | ForEach-Object { ' <PackageReference Include="' + $_.Name + '" Version="' + $_.Version + '" />' } | Out-String
$projectReferences = Extract-ProjectReferences $projectFile
$projectReferenceSection = $projectReferences | ForEach-Object { ' <ProjectReference Include="' + $_ + '" />' } | Out-String
return '<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
' + $projectReferenceSection + '
</ItemGroup>
<ItemGroup>
' + $nugetReferencesSection + '
</ItemGroup>
</Project>'
}
function Upgrade-AllProjects {
$projectFiles = get-childitem "*.csproj" -Recurse
foreach ($projectFile in $projectFiles) {
Write-Host "Generating new project file for $projectFile"
$newProjectFile = Get-UpgradedProjectFile $projectFile
Rename-Item $projectFile ($projectfile.FullName + ".old") -Force
Set-Content -Path $projectFile -Value $newProjectFile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.