Created
February 27, 2024 16:02
-
-
Save peteraritchie/881223f18437bc5e4b59d14a2d251bde to your computer and use it in GitHub Desktop.
A work in progress for a couple (maybe more) classes to wrap some `dotnet new` commands.
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
<# e.g.: | |
. .\visual-studio-classes.ps1 | |
$csproj = [Project]::CreateClassLib(".\Solution\ClassLib\ClassLib.csproj"); | |
$csproj.AddClass('MyClass'); | |
#> | |
class Project { | |
[string]$Folder; | |
Project() { | |
$this.Init((Get-Location).Path, "csproj"); | |
} | |
Project($folder) { | |
$this.Init($folder, "csproj"); | |
} | |
Project($folder, $projectType) { | |
$this.Init($folder, $projectType); | |
} | |
[void] AddClass([string]$className) { | |
$path = "$($this.Folder)\*.$($this.projectType)"; | |
Write-host "AddClass: $path"; | |
if (!(Test-Path -Path $path)){ | |
Write-Error "no project file exists in folder '$($this.Folder)'"; | |
} else { | |
dotnet new class -n "$className" -o "$($this.Folder)"; | |
} | |
} | |
[void] AddInterface([string]$interfaceName) { | |
if (!(Test-Path -Path "$($this.Folder)\*.$($this.projectType)")){ | |
Write-Error "no project file exists in folder '$($this.Folder)'"; | |
} else { | |
Write-Host "dotnet new interface -n `"$interfaceName`" -o `"$($this.Folder)`""; | |
} | |
} | |
[void] AddProjectReference([string]$path){ | |
dotnet add $this.Folder reference $path; | |
} | |
[void] AddProjectReference([Project]$project){ | |
dotnet add $this.Folder reference $project.Folder; | |
} | |
[void] AddPackageReference([string]$packageIdentifier){ | |
# dotnet add $this.Folder package $packageIdentifier; | |
} | |
static [Project] CreateClassLib([string]$path) { | |
$extension = (split-path -path $path -extension).Substring(1); | |
$dir = (split-path -path $path); | |
$file = (split-path -path $path -LeafBase); | |
if(!(Test-Path -Path $path)) { | |
dotnet new classlib -n $file -o $dir --framework 'net8.0'; | |
if(Test-Path "$dir\Class1.cs") { Remove-Item "$dir\Class1.cs"; } | |
} | |
$p = [Project]::new($dir, $extension); | |
return $p; | |
} | |
static [Project] CreateXunit([string]$path) { | |
$extension = (split-path -path $path -extension).Substring(1); | |
$dir = (split-path -path $path); | |
$file = (split-path -path $path -LeafBase); | |
if(!(Test-Path -Path $path)) { | |
dotnet new xunit -n $file -o $dir --framework 'net8.0'; | |
if(Test-Path "$dir\UnitTest1.cs") { Remove-Item "$dir\UnitTest1.cs"; } | |
} | |
$p = [Project]::new((Split-Path -Path $path), $extension); | |
return $p; | |
} | |
hidden [void] Init($folder, $projectType) { | |
$this.Folder = $folder; | |
$this.projectType = $projectType; | |
} | |
hidden [string]$projectType; | |
} | |
<# eg: | |
. .\visual-studio-classes.ps1 | |
$sln = [Solution]::Create("Accounting"); | |
$csproj = $s.NewClassLibraryProject("Accounting\BookKeeping\BookKeeping.csproj"); | |
#> | |
class Solution { | |
[string]$Folder; | |
Solution($path){ | |
$this.Folder = $path; | |
} | |
# dotnet new gitignore -o $solutionName >NUL 2>&1 || $(throw 'error creating .gitignore'); | |
# dotnet new buildprops -o "$solutionName" >NUL 2>&1 || $(throw 'error creating buildprops'); | |
# NewClassLibraryProject | |
[Project] NewClassLibraryProject([string] $libraryName) { | |
$project = [Project]::CreateClassLib((Join-Path -Path $this.Folder -ChildPath $libraryName)); | |
$this.AddProject($project); | |
return $project; | |
} | |
# NewTestProject | |
[Project] NewTestProject([string] $libraryName) { | |
$project = [Project]::CreateXunit((Join-Path -Path $this.Folder -ChildPath $libraryName)); | |
$this.AddProject($project); | |
return $project; | |
} | |
# AddProject | |
[void] AddProject([Project] $project) { | |
dotnet sln "$($this.Folder)" add "$($project.Folder)" >NUL 2>&1 || $(throw 'error adding to solution'); | |
} | |
static [Solution] Create([string]$path) { | |
$extension = split-path -path $path -Extension; | |
$dir = (split-path -path $path); | |
$filename = (split-path -path $path -Leaf); | |
$file = (split-path -path $path -LeafBase); | |
if(!($extension)) { | |
$filename = "$filename.sln" | |
$dir = $path; | |
} | |
$fullPath = "$dir\$filename"; | |
if(!(Test-Path -Path $fullPath -PathType Leaf)) { | |
if(!($dir)) { $dir = '.'; } | |
Write-Information "Should create $filename in directory $dir"; | |
dotnet new solution -o $dir -n $file | |
} else { | |
Write-Information "Using existing solution $fullPath"; | |
} | |
return [Solution]::new($dir); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment