Private/Cs/WebProj/Edit-WebProject.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<############################################################################
 # Given a Visual Studio ASP DotNet Core Angular website, add an existing
 # Entity Framework project and additional database support code.
 #
 ############################################################################>

Function Edit-WebProjectAddDb([SolnInfo] $solInfo, [WebCsprojInfo] $webCsprojInfo, [CsprojInfo] $modelCsprojInfo, [DbInfo] $dbInfo) 
{
    Write-Host "### Update web project with model"
    Edit-ProjectAddReferencePrivate $webCsprojInfo.csprojFile `
        $modelCsprojInfo.csprojFile

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

    # Get the Startup.cs file, add code to read in the connection string
    [string]$startupCsFile = "$($webCsprojInfo.csprojDir)\Startup.cs"
    
    # Tell Startup.cs to initialize database connection from
    # connection string in app*Settings.json

    Write-Host "### Update web Startup.cs with 'using' statements for ef"
    Edit-CsAddUsing $startupCsFile "Microsoft.EntityFrameworkCore"
    Write-Host "### Update web Startup.cs with 'using' statements for $($solnInfo.nickName) Model"
    Edit-CsAddUsing $startupCsFile "$($modelCsprojInfo.namespace).Model"
    Edit-CsAddUsing $startupCsFile "$($modelCsprojInfo.namespace).ModelWithView"

    Write-Host "### Update web Startup.cs to configure entity framework with connection string from appSettings*.json"
    AppendCodeAfterTwoMatchingLines $startupCsFile "public void ConfigureServices" "{" @"
            // Set up EF from connection string
            string connection = Configuration.GetSection("ConnectionStrings").GetValue<string>("$($dbInfo.db)");
            services.AddDbContext <$($modelCsprojInfo.contextName)> (options => options.UseSqlServer(connection));
"@
 "services.AddDbContext"

    Write-Host "### Add connection string to appSettings.Development.json"
    [string]$appSettingsDevelopmentJsonFile = "$($webCsprojInfo.csprojDir)\appSettings.Development.json"
    Edit-WebProjectAddConnStr $appSettingsDevelopmentJsonFile $dbInfo.db $dbInfo.connStr

    # Note for MICROSOFT style, dotnet code generator does standard MVC stuff to wire up pages, but for ANGULAR_IO
    # we have to tell Web API how to serve index.html
    if($webCsprojInfo.angularStyle -eq 'ANGULAR_IO') {
        Write-Host "### Update web Startup.cs to serve index.html"
        AppendCodeAfterTwoMatchingLines $startupCsFile "public void Configure\(IApplicationBuilder app, IHostingEnvironment env\)" "{" @"
                app.Use(async (context, next) =>
                {
                    await next();
                    if (context.Response.StatusCode == 404 &&
                       !System.IO.Path.HasExtension(context.Request.Path.Value) &&
                       !context.Request.Path.Value.StartsWith("/api/"))
                    {
                        context.Request.Path = "/index.html";
                        await next();
                    }
                });
                app.UseMvcWithDefaultRoute();
                app.UseDefaultFiles();
                app.UseStaticFiles();
"@
 "context.Request.Path = "
    }




}

<############################################################################
 # Add database connection string to supplied appSettings.Development.json file name
 # in the correct JSON syntax
 ############################################################################>

Function Edit-WebProjectAddConnStr([string] $jsonFile, [string] $db, [string] $connStr) {
    [string]$fileContents = (Get-Content -raw $jsonFile)
    if(-not ($fileContents -match "ConnectionStrings")) {
        $fileAsJson = (Get-Content -raw $jsonFile | ConvertFrom-Json)
        [string]$newValueAsStr = @"
{
    "$db": "$connStr"
}
"@

        $newValueAsJson = ConvertFrom-Json $newValueAsStr
        $fileAsJson | add-Member -Name "ConnectionStrings" -value $newValueAsJson -MemberType NoteProperty
        $fileAsJson | ConvertTo-Json | Out-FileUtf8NoBom $jsonFile
    }
}