Skip to content

Instantly share code, notes, and snippets.

@jamescrowley
Created July 14, 2018 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescrowley/dce76ae799006c237f4e8fcf07dd57e4 to your computer and use it in GitHub Desktop.
Save jamescrowley/dce76ae799006c237f4e8fcf07dd57e4 to your computer and use it in GitHub Desktop.
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