Private/Cs/WebProj/New-WebApiProject.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
45
46
47
48
49
50
51
Function New-WebApiProject([SolnInfo] $solnInfo, [WebCsprojInfo] $webCsprojInfo, [CsprojInfo] $modelCsprojInfo, [DbInfo] $dbInfo)
{
    Write-Host "### Making WebApi project" $webCsprojInfo.csprojName
    &{dotnet new webapi -n $webCsprojInfo.csprojName -o $webCsprojInfo.csprojDir}
    Confirm-LastExitCode

    # Add proj to soln
    Write-Host "### Add" $webCsprojInfo.csprojName "to solution" $solnInfo.nickName
    &{dotnet sln $solnInfo.solnFile add $webCsprojInfo.csprojFile}
    Confirm-LastExitCode

    # Add nuget packages
    Write-Host "### Add nuget packages to" $webCsprojInfo.csprojName
    &{dotnet add $webCsprojInfo.csprojFile package BoxTurtleCs }
    Confirm-LastExitCode

    # Controllers folder, remove dummy "ValuesController"
    Write-Host "### Remove $($webCsprojInfo.csprojName) scaffolding Controllers/ValuesController.cs"
    if(Test-Path "$($webCsprojInfo.csprojDir)\Controllers\ValuesController.cs") {
        Remove-Item "$($webCsprojInfo.csprojDir)\Controllers\ValuesController.cs"
    }

    # Disable typescript compilation
    Write-Host "### Disable typescript compilation for $($webCsprojInfo.csprojName)"
    $csprojXml = [xml] (type $webCsprojInfo.csprojFile)
    $disableTypescriptXml = $csprojXml.CreateElement("TypeScriptCompiledBlock")
    $disableTypescriptXml.PsBase.InnerText = "true"
    $propertyGroupXml = $csprojXml.SelectSingleNode("Project").SelectSingleNode("PropertyGroup")
    $propertyGroupXml.AppendChild($disableTypescriptXml)

    # Also add this to make Visual Studio happy: <TypeScriptToolsVersion>2.3</TypeScriptToolsVersion>
    # I have no idea what this does or what that verion means, but if we don't do it Visual Studio will ask me to do it.
    $typeScriptToolsVersionXml = $csprojXml.CreateElement("TypeScriptToolsVersion")
    $typeScriptToolsVersionXml.PsBase.InnerText = "2.3"
    $propertyGroupXml.AppendChild($typeScriptToolsVersionXml)

    # Tell Visual Studio to completely ignore angular stuff
    [xml]$ignoreAngularXml = @"
        <ItemGroup>
            <Compile Remove="angular\node_modules\**" />
            <Content Remove="angular\node_modules\**" />
            <EmbeddedResource Remove="angular\node_modules\**" />
            <None Remove="angular\node_modules\**" />
        </ItemGroup>
"@


    
    $csprojXml.Project.AppendChild($csprojXml.ImportNode($ignoreAngularXml.ItemGroup, $true))
    $csprojXml.Save($webCsprojInfo.csprojFile)    

}