forked from CommunityToolkit/Labs-Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscoverSamples.ps1
106 lines (85 loc) · 4.65 KB
/
DiscoverSamples.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
Param (
[Parameter(HelpMessage = "Disables suppressing changes to the ./.vscode/launch.json file in git, allowing changes to be committed.")]
[switch]$allowGitChanges = $false
)
$templatedSampleProjectReferencesDefinitionMarker = "[TemplatedSampleProjectReferences]"
$sampleRefsPropsTemplatePath = "$PSScriptRoot/common/Labs.SampleRefs.props.template";
$generatedSampleRefsPropsPath = "$PSScriptRoot/common/Labs.SampleRefs.props";
function CreateVsCodeLaunchConfigJson {
param (
[string]$projectName
)
return "{
`"name`": `"$projectName`",
`"type`": `"coreclr`",
`"request`": `"launch`",
`"program`": `"dotnet`",
`"args`": [
`"run`",
`"build`",
`"/r`",
`"/p:UnoSourceGeneratorUseGenerationHost=true`",
`"/p:UnoSourceGeneratorUseGenerationController=false`",
`"/p:UnoRemoteControlPort=443`",
`"--project=`$`{workspaceFolder`}/labs/$projectName/samples/$projectName.Wasm/$projectName.Wasm.csproj`",
`"-p:TargetFrameworks=netstandard2.0`",
`"-p:TargetFramework=net5.0`"
],
`"presentation`": {
`"group`": `"2`"
},
`"cwd`": `"`$`{workspaceFolder`}/labs/$projectName/samples/$projectName.Wasm`"
}";
}
# Execute ProjectReference generation for all heads
$sampleRefsPropsTemplate = Get-Content -Path $sampleRefsPropsTemplatePath;
Write-Output "Loaded sample ProjectReference template from $sampleRefsPropsTemplatePath";
# Add sample projects
foreach ($sampleProjectPath in Get-ChildItem -Recurse -Path "$PSScriptRoot/labs/*/samples/*.Samples/*.Samples.csproj") {
$relativePath = Resolve-Path -Relative -Path $sampleProjectPath;
$relativePath = $relativePath.TrimStart('.\');
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($relativePath);
Write-Host "Adding $projectName to project references";
$projectReferenceDefinition = "<ProjectReference Include=`"`$(RepositoryDirectory)$relativePath`" />";
$sampleRefsPropsTemplate = $sampleRefsPropsTemplate -replace [regex]::escape($templatedSampleProjectReferencesDefinitionMarker), ($templatedSampleProjectReferencesDefinitionMarker + "
" + $projectReferenceDefinition);
}
# Add library projects
foreach ($sampleProjectPath in Get-ChildItem -Recurse -Path "$PSScriptRoot/labs/*/src/*.csproj") {
$relativePath = Resolve-Path -Relative -Path $sampleProjectPath;
$relativePath = $relativePath.TrimStart('.\');
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($relativePath);
Write-Host "Adding $projectName to project references";
$projectReferenceDefinition = "<ProjectReference Include=`"`$(RepositoryDirectory)$relativePath`" />";
$sampleRefsPropsTemplate = $sampleRefsPropsTemplate -replace [regex]::escape($templatedSampleProjectReferencesDefinitionMarker), ($templatedSampleProjectReferencesDefinitionMarker + "
" + $projectReferenceDefinition);
}
$sampleRefsPropsTemplate = $sampleRefsPropsTemplate -replace [regex]::escape($templatedSampleProjectReferencesDefinitionMarker), "";
# Save
Set-Content -Path $generatedSampleRefsPropsPath -Value $sampleRefsPropsTemplate;
Write-Output "Sample project references generated at $generatedSampleRefsPropsPath";
$launchConfigJson = Get-Content -Path "$PSScriptRoot/.vscode/launch.json";
$launchConfig = $launchConfigJson | ConvertFrom-Json;
# Remove all non-generated configurations
$originalConfigurations = $launchConfig.configurations;
$launchConfig.configurations = @();
$launchConfig.configurations += $originalConfigurations[0];
$launchConfig.configurations += $originalConfigurations[1];
foreach ($wasmProjectPath in Get-ChildItem -Recurse -Path "$PSScriptRoot/labs/*/samples/*.Wasm/*.Wasm.csproj") {
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($wasmProjectPath) -Replace ".Wasm", "";
Write-Host "Generating VSCode launch config for $projectName";
$configJson = CreateVsCodeLaunchConfigJson $projectName;
$config = $configJson | ConvertFrom-Json;
$launchConfig.configurations += $config;
}
if ($allowGitChanges.IsPresent) {
Write-Warning "Changes to the default launch.json in Labs can now be committed. Run this command again without the -allowGitChanges flag to disable committing further changes.";
git update-index --no-assume-unchanged ./.vscode/launch.json
}
else {
Write-Output "Changes to the default launch.json in Labs are now suppressed. To switch branches, run git reset --hard with a clean working tree. Include the -allowGitChanges flag to enable committing changes.";
git update-index --assume-unchanged $PSScriptRoot/.vscode/launch.json
}
# Save
Set-Content -Path "$PSScriptRoot/.vscode/launch.json" -Value ($launchConfig | ConvertTo-Json -Depth 9);
Write-Output "Saved VSCode launch configs to $PSScriptRoot/.vscode/launch.json";