1+ # Copyright (c) .NET Foundation. All rights reserved.
2+ # Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+ param ($installPath , $toolsPath , $package , $project )
5+
6+ # #### Supporting Functions #####
7+ function EnsureUserSecretsId {
8+ $id = $null
9+ foreach ($prop in $root.Properties ) {
10+ if ($prop.Name -eq ' UserSecretsId' ) {
11+ $id = $prop.Value
12+ }
13+ }
14+ if ([string ]::IsNullOrEmpty($id )) {
15+ Write-Host (" Generating new UserSecretsId for project " + $project.FullName + " ..." )
16+ $newUserSecretsId = New-Guid
17+ $id = $newUserSecretsId
18+ $root.AddProperty (' UserSecretsId' , $newUserSecretsId.ToString ())
19+ Write-Host (" Set UserSecretsId for project to " + $newUserSecretsId )
20+ }
21+ return $id
22+ }
23+
24+ function EnsureUserSecretsFile {
25+ $datadir = $env: APPDATA
26+ $dirpath = [io.path ]::combine($datadir , " Microsoft" , " UserSecrets" , $userSecretsId )
27+ $envpath = [io.path ]::combine(' $(APPDATA)' , " Microsoft" , " UserSecrets" , $userSecretsId , " secrets.xml" )
28+ if ([string ]::IsNullOrEmpty($datadir ) -or ! (Test-Path $datadir )) {
29+ $datadir = $env: HOME
30+ $dirpath = [io.path ]::combine($datadir , " .microsoft" , " usersecrets" , $userSecretsId )
31+ $envpath = [io.path ]::combine(' ~' , " .microsoft" , " usersecrets" , $userSecretsId , " secrets.xml" )
32+ }
33+ if ([string ]::IsNullOrEmpty($datadir ) -or ! (Test-Path $datadir )) {
34+ return $null , $null
35+ }
36+
37+ $filepath = [io.path ]::combine($dirpath , " secrets.xml" )
38+ $sourcepath = [io.path ]::combine($installPath , " content" , " sample.xml" )
39+
40+
41+ # Don't overwrite an existing secrets file
42+ if (! (Test-Path $filepath )) {
43+ New-Item $dirpath - Type Directory - Force
44+ Copy-Item $sourcepath $filepath - Force
45+ }
46+
47+ return $envpath , $filepath
48+ }
49+
50+ function LinkSecretsFile {
51+ # Make sure there is something to link
52+ if ($secretsFile -eq $null ) {
53+ return
54+ }
55+
56+ # Be smart about the special 'Properties' folder
57+ $appDesignerFolder = $root.Properties | Where-Object {$_.Name -eq ' AppDesignerFolderIsNotHere' } | Select-Object - ExpandProperty Value
58+ if ([string ]::IsNullOrEmpty($appDesignerFolder )) {
59+ $appDesignerFolder = " Properties"
60+ }
61+
62+ # Add secrets.xml as a 'Link'
63+ $propertiesDir = $project.ProjectItems.Item ($appDesignerFolder )
64+ $item = $propertiesDir.ProjectItems.AddFromFile ($expandedSecretsFile )
65+ if (! ($item -eq $null )) {
66+ # If we successfully added the link, update the location to use $(APPDATA) variable instead of an expanded path.
67+ $msbuildItem = $root.Items | Where-Object {$_.Metadata | Where-Object {($_.Name -eq ' Link' ) -and ($_.Value -eq " $appDesignerFolder \secrets.xml" )} } | Select-Object
68+ $msbuildItem.Include = $secretsFile
69+ }
70+ }
71+
72+
73+
74+ # #### Begin Installation #####
75+ Write-Host (' Executing Install.ps1 in UserSecrets package...' )
76+ $project.Save ()
77+ $root = [Microsoft.Build.Construction.ProjectRootElement ]::Open($project.FullName )
78+
79+ # Ensure UserSecretsId exists
80+ $userSecretsId = EnsureUserSecretsId
81+
82+ # Ensure the secrets file exists
83+ $secretsFile , $expandedSecretsFile = EnsureUserSecretsFile
84+
85+ # Ensure the file is linked in AppDesignerFolder, aka 'Properties'
86+ LinkSecretsFile
87+
88+ # Done
89+ $project.Save ()
90+ Write-Host (' Done.' )
0 commit comments