Private/BoxTurtleClasses.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<############################################################################
 # Store metadata about a Visual Studio solution.
 # By convention, the solution has a nickname like "MyThing"
 # and the solution is stored in "MyThingSolution\MyThingSolution.sln"
 #
 # $nickName User supplied abbreviation for this whole solution
 # $solnDir Directory where .sln file lives
 # $solnFile Fully qualified path to .sln file
 # $solnName Name of *.sln file without path or .sln suffix
 ############################################################################>

class SolnInfo
{
    [string]$nickName
    [string]$solnDir
    [string]$solnFile
    [string]$solnName
    [WebCsprojInfo]$webCsprojInfo
    [ModelCsprojInfo]$modelCsprojInfo
    [CsprojInfo]$bizCsprojInfo
    [DbInfo]$dbInfo
    [string]$rootDir
    [string]$confFile
    $tableInfos

    SolnInfo() {
    }

    SolnInfo($nickName, $outputDir) {
        $this.solnName = "${nickName}Solution"
        $this.solnDir = $outputDir + "\" + $this.solnName
        $this.nickName = $nickName
        $this.solnFile = $this.solnDir + "\" + $this.solnName + ".sln"

        $this.rootDir = $outputDir
        $this.confFile = "$($this.rootDir)\boxTurtle.conf.json"
        $this.tableInfos = $null
    }

    
    [SolnInfo] static Load() {

        # Get boxturtle.conf.json from this directory tree
        [bool]$foundIt = $false
        [SolnInfo]$solnInfo = $null
        [string]$boxTurtleConfJson = ""
        [string]$boxTurlteConfJson = ""

        # Check dir, parent dir, grand-parent, etc. recursively
        [string]$myDir = (Get-Location)
        while($myDir.length -gt 3 <# because "c:\" is 3 characters#>) {
            # Make absolutely sure path ends in slash
            $myDir = $myDir.TrimEnd('\') + '\'

            $boxTurtleConfJson = "$($myDir)boxturtle.conf.json"
            #Write-Host "### Looking up for $boxTurtleConfJson"
            if(Test-Path $boxTurtleConfJson) {
                $foundIt = $true
                break
            } else {
                $parent = (Get-Item $myDir).Parent
                if($parent -eq $null) {
                    break 
                } else {
                    $myDir = [string]($parent.FullName)
                }
            }
        }

        # Try child directories. If one and only one subdir has it, use that
        [int]$subdirsWithConf = 0
        [string]$latestMatch = ""
        if($foundIt -eq $false) {
            $subdirs = get-childitem | where-object {$_.Psiscontainer -eq "True"} | select-object FullName
            foreach($subdir in $subdirs) {
                $myDir = $subdir.FullName
                $myDir = $myDir.TrimEnd('\') + '\'
                $temp = "$($myDir)boxTurtle.conf.json"
                #Write-Host "### Looking down for $temp in myDir=$($myDir)###"
                if(Test-Path $temp) {
                    $subdirsWithConf++
                    $latestMatch = $temp
                }
            }
            if($subdirsWithConf -eq 1) {
                $foundIt = $true
                $boxTurtleConfJson = $latestMatch
            }
        }

        if($foundIt -eq $false) {
            throw "Unable to find a unique boxturtle.conf.json in current directory '" + (Get-Location) + "' or lower"
        } else {
            Write-Host "### Using config $boxTurtleConfJson"
            [string]$jsonStr = Get-Content -raw $boxTurtleConfJson 
            $solnInfo = [SolnInfo](ConvertFrom-Json $jsonStr)
        }

        # Force this to be loaded explicitly when needed
        $solnInfo.tableInfos = $null

        return $solnInfo
    }


    SaveConf() {
        # Don't save table metadata, we'll reload each time to get it fresh
        $this.tableInfos = $null

        $this | ConvertTo-Json | Out-File $this.confFile
    }

    [System.Collections.Hashtable]GetTableInfos() {
        if($this.tableInfos -eq $null) {
            $this.tableInfos = New-PsModel $this.dbInfo
        }
        return $this.tableInfos
    }

    [CsprojInfo]GetProjInfoByName([string]$projName) {
        if(($this.webCsprojInfo -ne $null) -and ($this.webCsprojInfo.csprojName -eq $projName)) {
            return $this.webCsprojInfo
        } elseif(($this.modelCsprojInfo -ne $null) -and ($this.modelCsprojInfo.csprojName -eq $projName)) {
            return $this.modelCsprojInfo
        } elseif(($this.bizCsprojInfo -ne $null) -and ($this.bizCsprojInfo.csprojName -eq $projName)) {
            return $this.bizCsprojInfo
        } else {
            throw "cannot find project named '$projName'"
        }
    }
}

<############################################################################
 # Store metadata about a Visual Studio project.
 # By convention, it has a name csprojName like "MyProject"
 # and the project will be $solnInfo.solnDir\MyProject\MyProject.csproj
 #
 # $csprojName Name of project without path or .csproj suffix
 # $csprojDir Directory where .csproj file lives
 # $csprojFile Fully qualified path to .csproj file
 # $namespace Default C# namespace to use for this project
 ############################################################################>

class CsprojInfo
{
    [string]$csprojName
    [string]$csprojDir
    [string]$csprojFile
    [string]$namespace

    CsprojInfo() {
    }

    CsprojInfo([SolnInfo] $solnInfo, [string]$csprojName) {
        $this.csprojName = $csprojName
        $this.csprojDir = "{0}\{1}" -f $solnInfo.solnDir, $csprojName
        $this.csprojFile = "{0}\{1}.csproj" -f $this.csprojDir, $this.csprojName
        $this.namespace = $csprojName
    }
}

<############################################################################
 # Subclass of CsprojInfo, but for ASP DotNet Core websites with angular.
 #
 # $angularDir Directory where angular is stored, by convention just inside C# web project
 # $angularModelDir Directory where angular model TypeScripts are stored, by convention "$($webcsprojInfo.csprojDir)\ClientApp\app\model"
 # $angularServiceDir Directory where angular Service TypeScripts are stored, by convention "$($webcsprojInfo.csprojDir)\ClientApp\app\Service"
 # $angularComponentDir Directory where angular Component TypeScripts are stored, by convention "$($webcsprojInfo.csprojDir)\ClientApp\app\Component"
 ############################################################################>

class WebCsprojInfo : CsprojInfo
{
    [string]$angularDir
    [string]$angularAppDir
    [string]$angularModelDir
    [string]$angularServiceDir
    [string]$angularComponentDir
    [string]$appModuleFile
    [string]$appRoutingFile
    [string]$angularStyle
    [string]$styleCss

    WebCsprojInfo() {
    }

    WebCsprojInfo([SolnInfo] $solnInfo, [string]$csprojName, [string]$angularStyle):base($solnInfo, $csprojName) { 
        $this.angularStyle = $angularStyle
        if($angularStyle -eq 'ANGULAR_IO') {
            # It's Angular.io style against MS Web API
            $this.angularDir = "$($this.csprojDir)\angular"
            $this.angularAppDir = "$($this.csprojDir)\angular\src\app"
            $this.appModuleFile = "$($this.angularAppDir)\app.module.ts"
            $this.appRoutingFile = "$($this.angularAppDir)\app-routing.module.ts"
            $this.styleCss = "$($this.angularDir)/src/styles.css"
        } else {
            # It's microsoft "dotnet new angular" style against MS MVC
            $this.angularDir = "$($this.csprojDir)"
            $this.angularAppDir = "$($this.csprojDir)\ClientApp\app"
            $this.appModuleFile = "$($this.angularAppDir)\app.module.shared.ts"
            $this.appRoutingFile = "$($this.angularAppDir)\app.module.shared.ts"
            $this.styleCss = "$($this.angularDir)/src/styles.css"
        }
        
        $this.angularModelDir = "$($this.angularAppDir)\model"
        $this.angularServiceDir = "$($this.angularAppDir)\service"
        $this.angularComponentDir = "$($this.angularAppDir)\component"
    }
}


<############################################################################
 # Subclass of CsprojInfo, but for ASP DotNet Core entity framework projects.
 #
 ############################################################################>

class ModelCsprojInfo : CsprojInfo
{
    [string]$efContextName
    [string]$efWithViewContextName
    [string]$contextName
    [string]$efNamespace
    [string]$efWithViewNamespace

    ModelCsprojInfo() {
    }

    ModelCsprojInfo([SolnInfo] $solnInfo, [string]$csprojName):base($solnInfo, $csprojName) { 
        $this.efContextName = "$($solnInfo.nickName)Context"
        $this.efWithViewContextName = "$($solnInfo.nickName)WithViewContext"
        $this.contextName = $this.efWithViewContextName
        $this.efNamespace = "$($this.namespace).Model"
        $this.efWithViewNamespace = "$($this.namespace).ModelWithView"
    }
}

<############################################################################
 # Database connection info
 #
 # $dbServer Database server name
 # $db Database name
 # $connStr C# database connection string
 ############################################################################>

class DbInfo
{
    [string]$dbServer
    [string]$db
    [string]$connStr
    
    DbInfo() {
    }

    DbInfo([SolnInfo]$solnInfo, $dbServer, $db) {
        $this.dbServer = $dbServer
        $this.db = $solnInfo.nickName 
        $this.connStr = "Server=${dbServer};Database=${db};Trusted_Connection=True" 
    }
}

<############################################################################
 # Metadata about database column used to generate angular ORM stuff and some C# stuff
 #
 # $columnCapitalCamel Column name in capitalized format like "ClientFirstName"
 # $columnLowerCamel Column name in not-capitalized format like "clientFirstName"
 # $dataType Column data type in SQL Server syntax
 # $isNullable True if column is nullable
 # $maxLength Max char length in database
 # $caption Column name as a caption "Client First Name"
 ############################################################################>

class ColumnInfo
{
    [string]$columnCapitalCamel
    [string]$columnLowerCamel
    [string]$dataType
    [bool]$isNullable
    [bool]$isPrimaryKey
    [int]$maxLength
    [string]$caption

    ColumnInfo([string]$column, [string]$dataType, [bool]$isNullable, [int]$maxLength, [bool]$isPrimarykey) {
        $this.columnCapitalCamel = (ConvertTo-CapitalCamelCase $column)
        $this.caption = (ConvertTo-TitleCase $column)
        $this.columnLowerCamel = (ConvertTo-LowerCamelCase $column)
        $this.dataType = $dataType
        $this.isNullable = $isNullable
        $this.maxLength = $maxLength
        $this.isPrimaryKey = $isPrimaryKey
    }
}

<############################################################################
 # Metadata about database table used to generate angular ORM stuff and some C# stuff
 #
 # $tablePretty Table name like "Customer Account"
 # $tableCapitalCamel Table name like "CustomerAccount"
 # $tableLowerCamel Table name like "customerAccount"
 # $tableLowerKebab Table name like "customer-account"
 # $columnInfos Array of ColumnInfos describing columns
 ############################################################################>

class TableInfo
{
    [string]$tablePretty
    [string]$tableCapitalCamel
    [string]$tableLowerCamel
    [string]$tableLowerKebab
    [ColumnInfo[]]$columnInfos
    [bool]$isView

    TableInfo([string]$entity, [ColumnInfo[]]$columnInfos, [bool]$isView) {
        $this.tableCapitalCamel = (ConvertTo-CapitalCamelCase $entity)
        $this.tableLowerCamel = (ConvertTo-LowerCamelCase $entity)
        $this.tableLowerKebab = (ConvertTo-KebabCase $entity)
        $this.tablePretty = (ConvertTo-TitleCase $entity)
        $this.columnInfos = $columnInfos
        $this.isView = $isView
    }
}