Created
July 14, 2018 20:47
-
-
Save jamescrowley/dce76ae799006c237f4e8fcf07dd57e4 to your computer and use it in GitHub Desktop.
Migrate to new CS Project format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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