forked from microsoft/microsoft-ui-xaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateNewControlInProject.ps1
84 lines (68 loc) · 2.61 KB
/
GenerateNewControlInProject.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
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$controlName,
[string]$projectName
)
function FindAndReplaceInFile
{
Param($file, $regex, $replace)
$content = Get-Content $file -Raw
[regex]$pattern = $regex
$content = $pattern.replace($content, $replace, 1)
Set-Content -Path $file -Value $content
}
function AddAttribute
{
Param([xml]$xml, $element, $attrName, $attrValue)
$attribute = $xml.CreateAttribute($attrName)
$attribute.Value = $attrValue;
$element.Attributes.Append($attribute)
}
$toolsDir = Split-Path -Path $MyInvocation.MyCommand.Path;
$muxControlsDir = Split-Path $toolsDir -Parent
$controlDir = $muxControlsDir + "\dev\$projectName"
# TODO: check if project directory exists
Copy-Item "$toolsDir\GenerateNewControlProjectFiles\NEWCONTROL.cpp" "$controlDir\$controlName.cpp"
Copy-Item "$toolsDir\GenerateNewControlProjectFiles\NEWCONTROL.h" "$controlDir\$controlName.h"
# Replace NEWCONTROL with $controlName in file contents
$files = "$controlDir\$controlName.cpp", "$controlDir\$controlName.h"
foreach ($file in $files)
{
$file
(Get-Content $file) |
Foreach-Object { $_ -replace "NEWCONTROL", $controlName } |
Set-Content $file
}
# TODO: Add to idl file
# Add project to ProjectName.vcxitems
$controlProject = $controlDir + "\$projectName.vcxitems";
[xml]$xml = Get-Content $controlProject
foreach ($group in $xml.Project.ItemGroup)
{
if ($group.ChildNodes.item(0).Name.Equals("ClInclude"))
{
$include = $xml.CreateElement("ClInclude", $xml.Project.NamespaceURI);
AddAttribute $xml $include "Include" "`$(MSBuildThisFileDirectory)$controlName.h"
$group.AppendChild($include);
}
elseif ($group.ChildNodes.item(0).Name.Equals("ClCompile"))
{
$include = $xml.CreateElement("ClCompile", $xml.Project.NamespaceURI);
AddAttribute $xml $include "Include" "`$(MSBuildThisFileDirectory)$controlName.cpp"
$group.AppendChild($include);
$include = $xml.CreateElement("ClCompile", $xml.Project.NamespaceURI);
AddAttribute $xml $include "Include" "`$(MSBuildThisFileDirectory)..\Generated\$controlName.properties.cpp"
$group.AppendChild($include);
}
}
$xml.Save($controlProject)
# Add header file to XamlMetadataProviderGenerated.tt
FindAndReplaceInFile ($muxControlsDir + "\dev\dll\XamlMetadataProviderGenerated.tt") "#endif" @"
#include "$controlName.h"
#endif
"@
# Add new profiler id to RuntimeProfiler.h
FindAndReplaceInFile ($muxControlsDir + "\dev\Telemetry\RuntimeProfiler.h") "(\s*ProfId_Size, .*\s*})" @"
ProfId_$controlName,`$1
"@