Public/New-Solution.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<#
.SYNOPSIS
    Assuming Visual Studio 2017.3 installed, will create a new C# solution in current
    or specified directory.
.DESCRIPTION
    Will attempt to delete existing directory or halt if deletion fails.
.PARAMETER name
    Name of the overall solution in upper camel case like "Zoo" or "ClassAction". Don't use "Solution" or "sln" as part of the name.
.PARAMETER directory
    Defaults to current directory. Will create a solution folder inside this directory which is name parameter plus "Solution"
.EXAMPLE
    C:\PS> New-Solution Zoo c:\code
    Creates a valid but empty Visual Studio solution c:\code\Zoo\ZooSolution\ZooSolution.sln
.NOTES
    Author: Brian Woelfel
    Date: 2017/09/07
#>

Function New-Solution() {
    param ([Parameter(Mandatory=$true)][string]$name, [Alias("dir")][string]$directory = "")

    if($directory -eq "") {
        $directory = (Get-Location)
        $directory = "$($directory)\$name"
    }

    [SolnInfo] $solnInfo = [SolnInfo]::new($name, $directory)

    if(Test-Path $solnInfo.solnDir) {
        Write-Host "### Remove old root directory $solnInfo.solnDir"
        Remove-Item -Recurse -Force $solnInfo.solnDir
    }

    if(-not(Test-Path $solnInfo.solnDir)) {
        Write-Host "### Make root directory $solnInfo.solnDir"
        New-Item $solnInfo.solnDir -ItemType directory
    }

    Write-Host "### Make new solution " $solnInfo.solnName " at " $solnInfo.solnDir
    
    &{dotnet new sln -n $solnInfo.solnName -o $solnInfo.solnDir}
    Confirm-LastExitCode
    
    $solnInfo.SaveConf()
}