Skip to content

Instantly share code, notes, and snippets.

@markushaslinger
Created August 13, 2024 08:41
Show Gist options
  • Save markushaslinger/f788296c1dbc2240653ea57ed223b49b to your computer and use it in GitHub Desktop.
Save markushaslinger/f788296c1dbc2240653ea57ed223b49b to your computer and use it in GitHub Desktop.
dotnet project to solution
$csprojFile = Get-ChildItem -Filter *.csproj -File
if ($csprojFile.Count -eq 0) {
Write-Host "No .csproj file found in the current directory."
exit 1
} elseif ($csprojFile.Count -gt 1) {
Write-Host "More than one .csproj file found. Please ensure there's only one."
exit 1
}
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($csprojFile.Name)
$projectDir = Join-Path -Path (Get-Location) -ChildPath $projectName
New-Item -ItemType Directory -Path $projectDir
$itemsToMove = Get-ChildItem | Where-Object {
if ($_.PSIsContainer) {
# Exclude directories with the name $projectName
$_.Name -ne $projectName
} else {
# Include all files
$_.Name -notmatch "(.editorconfig|.gitignore|Setup-Solution.ps1)"
}
}
foreach ($item in $itemsToMove) {
Move-Item -Path $item.FullName -Destination $projectDir -Force
}
dotnet new sln -n $projectName
$csprojFullPath = Join-Path -Path $projectDir -ChildPath $csprojFile.Name
dotnet sln add $csprojFullPath
Write-Host "Solution setup complete. Project moved to '$projectName' and added to '$projectName.sln'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment