-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathSynchronize-Files-Used-By-VisualBasic-Templates.ps1
80 lines (67 loc) · 3.53 KB
/
Synchronize-Files-Used-By-VisualBasic-Templates.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
# Overwrite all XAML files in VB directories with files from comparable C# folders.
# XAML files should be the same for both C# & VB so this means changes can be made for C# and then easily copied for the VB templates.
# This is necessary because the way template folders are copied and filtered means that, for example, the VB templates can't point to files in the C# folders.
# This script finds all interested files in VB folders and then copies the equivalent file from the CS version
Get-ChildItem ..\templates\* -Recurse -include *.xaml, *.resw, *.md, *.png, Package.appxmanifest | where { $_.FullName -Match "._VB\\" -and ($_.FullName -notmatch "\\templates\\Uwp\\SC\\") } | % { $cs = $_.FullName -replace "._VB\\", "\\"; Copy-Item $cs $_.FullName }
# This script handles project file postactions that add 3rd party references
Get-ChildItem ..\templates\* -Recurse -include _postaction.vbproj | where { $_.FullName -notmatch "\\templates\\Uwp\\SC\\" } | % { $cs = $_.FullName -replace "._VB\\", "\\"; $cs = $cs -replace ".vbproj", ".csproj"; Copy-Item $cs $_.FullName }
# Formats JSON in a nicer format than the built-in ConvertTo-Json does.
# This is based on code from https://github.com/PowerShell/PowerShell/issues/2736 and will be built into PS6.0
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
$indent = 0;
($json -Split '\n' |
% {
if ($_ -match '[\}\]]') {
# This line contains ] or }, decrement the indentation level
$indent--
}
$line = (' ' * $indent * 4) + $_.TrimStart().Replace(': ', ': ')
if ($_ -match '[\{\[]') {
# This line contains [ or {, increment the indentation level
$indent++
}
# Regex ensures that output isn't encoded in the powershell unicode style. (We want '&', not '\u0026')
[regex]::Unescape($line)
}) -Join "`n"
}
# This script updates all the localized string values in VB template files from their C# equivalents.
# This is needed as the files can't just be copied as they have VB specific content.
Get-ChildItem ..\templates\* -Recurse -include *template.json | where { $_.FullName -Match "VB\\" -and ($_.FullName -notmatch "\\templates\\Uwp\\SC\\") } | % {
$vbFile = $_.FullName;
$csFile = $_.FullName -replace "._VB\\", "\\";
Try
{
$vbJson = (Get-Content $vbFile) -join "`n" | ConvertFrom-Json;
}
Catch
{
Write-Output $vbFile;
}
$csJson = (Get-Content $csFile) -join "`n" | ConvertFrom-Json;
$vbEquivIdentity = $vbJson.identity -replace ".VB", "";
$somethingWasChanged = $false;
if ($vbEquivIdentity -eq $csJson.identity)
{
# Not every template contains "name" or "description" properties so only look at those that do.
if (($vbJson.PSobject.Properties.name -match "name") -and ($csJson.PSobject.Properties.name -match "name"))
{
if ($vbJson.name -ne $csJson.name -and $vbJson.tags.'ts.type' -ne "composition")
{
$vbJson.name = $csJson.name
$somethingWasChanged = $true;
}
}
if (($vbJson.PSobject.Properties.name -match "description") -and ($csJson.PSobject.Properties.name -match "description"))
{
if ($vbJson.description -ne $csJson.description)
{
$vbJson.description = $csJson.description
$somethingWasChanged = $true;
}
}
if ($somethingWasChanged)
{
$vbJson | ConvertTo-Json | Format-Json | Out-File $vbFile -Encoding utf8;
}
}
}