-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-table-scheme.ps1
143 lines (121 loc) · 4.68 KB
/
generate-table-scheme.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
$destinationFile = ".\README.md"
########## Substitution function #############
function Substitute-Table {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline = $true)]
[string] $original,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $identifier,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $table
)
$text = ""
[bool] $skip = $false
foreach ($line in $original.Split("`r`n")) {
$i+=1
if($line -eq "<!-- END $identifier TABLE -->") {
$skip = $false
Write-Host "`t`tPrevious content skipped between lines $j and $i"
}
if (-not $skip) {
$text += $line + "`r`n"
}
if ($line -eq "<!-- START $identifier TABLE -->"){
$skip = $true
$text += $table
Write-Host "`t`tNew content inserted after line $i"
$j = $i+1
}
}
return $text
}
########### Create a markdown table ##########
function Write-Table {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[object[]]$mappers,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string[]] $keys
)
Write-Host "`t$($mappers.Count) mappers to transform in markdown table"
$items = @()
$columns = @{}
foreach($mapper in $mappers) {
$dbName = "![$($mapper.Database)]"
$dbName += "(https://img.shields.io/badge/"
$dbName += [URI]::EscapeUriString($mapper.Database)
$dbName += "-" + $mapper.MainColor -replace '#', ''
$dbName += "?logo=$($mapper.Slug)"
$dbName += "&logoColor=$($mapper.SecondaryColor -replace '#', '')"
$dbName += "&style=flat-square"
$dbName += ")"
foreach ($prop in $mapper.PSObject.Properties)
{
if (-not($keys -Contains $prop)) {
$mapper.PSObject.Properties.Remove($prop)
}
}
$item = $mapper
$item.Database = $dbName
$item.Aliases = $mapper.Aliases -join ', '
$items += $item
$item.PSObject.Properties | %{
if((-not $columns.ContainsKey($_.Name) -or $columns[$_.Name] -lt $_.Value.ToString().Length) -and ($null -ne $_.Value)) {
$columns[$_.Name] = $_.Value.ToString().Length
}
}
Write-Host "`t`t$($mapper.Class)"
}
foreach($key in $($columns.Keys)) {
$columns[$key] = [Math]::Max($columns[$key], $key.Length)
}
$table=""
$header = @()
foreach($key in $keys) {
$header += ('{0,-' + $columns[$key] + '}') -f ($key -creplace '([A-Z])', ' $1').Trim()
}
$table += '|' + ($header -join ' | ') + "|`r`n"
$separator = @()
foreach($key in $keys) {
$separator += '-' * $columns[$key]
}
$table += '|' + ($separator -join ' | ') + "|`r`n"
foreach($item in $items) {
$values = @()
foreach($key in $keys) {
$values += ('{0,-' + $columns[$key] + '}') -f ($item.($key)).Replace("|", "\|")
}
$table += '|' + ($values -join ' | ') + "|`r`n"
}
Write-Host "`tCreated markdown table with $($table.Split("`r`n").GetUpperBound(0)) lines and a width of $($table.Split("`r`n")[0].Length) chars"
return $table
}
########### MAIN ##########
Write-Host "Creating new version of $destinationFile for markdown tables ..."
$elapsed = Measure-Command -Expression {
$text = (Get-Content -Path $destinationFile) -join "`r`n"
$table = Get-Content -Path ".\Docs\_data\natives.json" `
| ConvertFrom-Json -NoEnumerate `
| Write-Table -Keys @("Database", "Aliases", "ProviderInvariantName")
$text = $text | Substitute-Table -Identifier "ADONET" -Table $table
$table = Get-Content -Path ".\Docs\_data\adomd.json" `
| ConvertFrom-Json -NoEnumerate `
| Write-Table -Keys @("Database", "Aliases", "ProviderInvariantName")
$text = $text | Substitute-Table -Identifier "ADOMD" -Table $table
$table = Get-Content -Path ".\Docs\_data\odbc.json" `
| ConvertFrom-Json -NoEnumerate `
| Write-Table -Keys @("Database", "Aliases", "NamePattern")
$text = $text | Substitute-Table -Identifier "ODBC" -Table $table
$table = Get-Content -Path ".\Docs\_data\oledb.json" `
| ConvertFrom-Json -NoEnumerate `
| Write-Table -Keys @("Database", "Aliases", "NamePattern")
$text = $text | Substitute-Table -Identifier "OLEDB" -Table $table
$text | Out-File -Path $destinationFile
}
Write-Host "New version of $destinationFile created in $($elapsed.TotalSeconds) seconds."