Skip to content

Commit 47077c8

Browse files
committed
Lots of hydration bug fixes after smoke testing install/reinstall.
1 parent 516bceb commit 47077c8

5 files changed

Lines changed: 60 additions & 76 deletions

File tree

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/config.install.xdt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
<!-- If compilers tag is absent -->
99
<system.codedom>
10-
<compilers xdt:Transform="InsertIfMissing">
11-
</compilers>
10+
<compilers xdt:Transform="InsertIfMissing" />
1211
</system.codedom>
1312

1413
<!-- If a .cs compiler is already present, the existing entry needs to be removed before inserting the new entry -->
@@ -21,13 +20,6 @@
2120
</compilers>
2221
</system.codedom>
2322

24-
<!-- Inserting the new compiler -->
25-
<system.codedom>
26-
<compilers>
27-
<compiler extension=".cs" xdt:Transform="Insert" />
28-
</compilers>
29-
</system.codedom>
30-
3123
<!-- If a .vb compiler is already present, the existing entry needs to be removed before inserting the new entry -->
3224
<system.codedom>
3325
<compilers>
@@ -38,10 +30,4 @@
3830
</compilers>
3931
</system.codedom>
4032

41-
<!-- Inserting the new compiler -->
42-
<system.codedom>
43-
<compilers>
44-
<compiler extension=".vb" xdt:Transform="Insert" />
45-
</compilers>
46-
</system.codedom>
4733
</configuration>

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/config.uninstall.xdt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,6 @@
55
</appSettings>
66
<appSettings xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)">
77
</appSettings>
8-
9-
<system.codedom>
10-
<compilers>
11-
<compiler
12-
extension=".cs"
13-
xdt:Transform="Remove"
14-
xdt:Locator="Match(extension)" />
15-
</compilers>
16-
</system.codedom>
17-
<system.codedom>
18-
<compilers>
19-
<compiler
20-
extension=".vb"
21-
xdt:Transform="Remove"
22-
xdt:Locator="Match(extension)" />
23-
</compilers>
24-
</system.codedom>
258
<system.codedom>
269
<compilers xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)" />
2710
</system.codedom>

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/tools/common.ps1

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ function InstallCodeDomProvider($providerDescription) {
3232
$config = ReadConfigFile
3333
$rehydratedCount = RehydrateOldDeclarations $config $providerDescription
3434
$updatedCount = UpdateDeclarations $config $providerDescription
35-
if ($updatedCount -le 0) { AddDefaultDeclaration $config $providerDescription }
35+
36+
##### Add the default provider if it wasn't rehydrated above
37+
$defaultProvider = $config.xml.configuration["system.codedom"].compilers.compiler | where { $_.extension -eq $providerDescription.FileExtension }
38+
if ($defaultProvider -eq $null) { AddDefaultDeclaration $config $providerDescription }
3639
SaveConfigFile $config
3740
}
3841

3942
function UninstallCodeDomProvider($providerType) {
4043
##### Dehydrate config declarations #####
4144
$config = ReadConfigFile
42-
DehydrateDeclarations $config $providerType
45+
DehydrateDeclarations $config $providerType | Out-Null
4346
SaveConfigFile $config
4447
}
4548

@@ -72,25 +75,27 @@ function DehydrateDeclarations($config, $typeName) {
7275

7376
if ([io.file]::Exists($tempFile)) {
7477
$xml = (Select-Xml -Path "$tempFile" -XPath /).Node
78+
$xml.PreserveWhitespace = $true
7579
} else {
7680
$xml = New-Object System.Xml.XmlDocument
7781
$xml.PreserveWhitespace = $true
78-
$xml.AppendChild($xml.CreateElement("driedDeclarations"))
82+
$dd = $xml.CreateElement("driedDeclarations")
83+
$xml.AppendChild($dd) | Out-Null
7984
}
8085

8186
foreach ($rec in $config.xml.configuration["system.codedom"].compilers.compiler | where { IsSameType $_.type $typeName }) {
8287
# Remove records from config.
83-
$config.xml.configuration["system.codedom"].compilers.RemoveChild($rec)
88+
$config.xml.configuration["system.codedom"].compilers.RemoveChild($rec) | Out-Null
8489

8590
# Add the record to the temp stash. Don't worry about duplicates.
86-
AppendProviderNode $xml.ImportNode($rec, $true) $xml.DocumentElement
91+
AppendChildNode $xml.ImportNode($rec, $true) $xml.DocumentElement
8792
$count++
8893
}
8994

9095
# Save the dehydrated declarations
9196
$tmpFolder = Split-Path $tempFile
92-
md -Force $tmpFolder
93-
$xml.Save($tempFile)
97+
md -Force $tmpFolder | Out-Null
98+
$xml.Save($tempFile) | Out-Null
9499
return $count
95100
}
96101

@@ -102,21 +107,21 @@ function RehydrateOldDeclarations($config, $providerDescription) {
102107
$xml = (Select-Xml -Path "$tempFile" -XPath /).Node
103108
$xml.PreserveWhitespace = $true
104109

105-
foreach($rec in $xml.driedDeclarations.add | where { IsSameType $_.type ($providerDescription.TypeName + "," + $providerDescription.Assembly) }) {
110+
foreach($rec in $xml.driedDeclarations.compiler | where { IsSameType $_.type ($providerDescription.TypeName + "," + $providerDescription.Assembly) }) {
106111
# Remove records that match type, even if we don't end up rehydrating them.
107-
$xml.driedDeclarations.RemoveChild($rec)
112+
$xml.driedDeclarations.RemoveChild($rec) | Out-Null
108113

109114
# Skip if an existing record of the same file extension already exists.
110115
$existingRecord = $config.xml.configuration["system.codedom"].compilers.compiler | where { $_.extension -eq $rec.extension }
111116
if ($existingRecord -ne $null) { continue }
112117

113118
# Bring the record back to life
114-
AppendProviderNode $config.xml.ImportNode($rec, $true) $config.xml.configuration["system.codedom"].compilers
119+
AppendChildNode $config.xml.ImportNode($rec, $true) $config.xml.configuration["system.codedom"]["compilers"]
115120
$count++
116121
}
117122

118123
# Make dried record removal permanent
119-
$xml.Save($tempFile)
124+
$xml.Save($tempFile) | Out-Null
120125

121126
return $count
122127
}
@@ -132,27 +137,30 @@ function UpdateDeclarations($config, $providerDescription) {
132137
$provider.type = "$($providerDescription.TypeName), $($providerDescription.Assembly), Version=$($providerDescription.Version), Culture=neutral, PublicKeyToken=31bf3856ad364e35"
133138

134139
# Add default attributes if they are required and not already present
135-
foreach ($p in $providerDescription.Parameters | where { ($_.IsRequired -eq $true) and ($_.IsProviderOption -eq $false) }) {
140+
foreach ($p in $providerDescription.Parameters | where { ($_.IsRequired -eq $true) -and ($_.IsProviderOption -eq $false) }) {
136141
if ($provider.($p.Name) -eq $null) {
137142
if ($p.DefaultValue -eq $null) {
138143
Write-Host "Failed to add parameter to '$($provider.name)' codeDom provider: '$($p.Name)' is required, but does not have a default value."
139144
return
140145
}
141-
$provider.SetAttribute($p.Name, $p.DefaultValue)
146+
$attr = $config.xml.CreateAttribute($p.Name)
147+
$attr.Value = $p.DefaultValue
148+
$provider.Attributes.InsertBefore($attr, $provider.Attributes["type"]) | Out-Null
142149
}
143150
}
144151

145152
# Do the same thing for default providerOptions if not already present
146-
foreach ($p in $providerDescription.Parameters | where { ($_.IsRequired -eq $true) and ($_.IsProviderOption -eq $true)}) {
147-
if ($provider.($p.Name) -eq $null) {
153+
foreach ($p in $providerDescription.Parameters | where { ($_.IsRequired -eq $true) -and ($_.IsProviderOption -eq $true)}) {
154+
$existing = $provider.providerOption | where { $_.name -eq $p.Name }
155+
if ($existing -eq $null) {
148156
if ($p.DefaultValue -eq $null) {
149157
Write-Host "Failed to add providerOption to '$($provider.name)' codeDom provider: '$($p.Name)' is required, but does not have a default value."
150158
return
151159
}
152160
$po = $config.xml.CreateElement("providerOption")
153161
$po.SetAttribute("name", $p.Name)
154162
$po.SetAttribute("value", $p.DefaultValue)
155-
$provider.AppendChild($po)
163+
AppendChildNode $po $provider 4
156164
}
157165
}
158166
}
@@ -178,7 +186,7 @@ function AddDefaultDeclaration($config, $providerDescription) {
178186
$po = $config.xml.CreateElement("providerOption")
179187
$po.SetAttribute("name", $p.Name)
180188
$po.SetAttribute("value", $p.DefaultValue)
181-
$dd.AppendChild($po)
189+
AppendChildNode $po $dd 4
182190
} else {
183191
$dd.SetAttribute($p.Name, $p.DefaultValue)
184192
}
@@ -188,18 +196,31 @@ function AddDefaultDeclaration($config, $providerDescription) {
188196
# type last
189197
$dd.SetAttribute("type", "$($providerDescription.TypeName), $($providerDescription.Assembly), Version=$($providerDescription.Version), Culture=neutral, PublicKeyToken=31bf3856ad364e35")
190198

191-
AppendProviderNode $dd $config.xml.configuration["system.codedom"].compilers
199+
AppendChildNode $dd $config.xml.configuration["system.codedom"]["compilers"]
192200
}
193201

194-
function AppendProviderNode($provider, $parent) {
202+
function AppendChildNode($provider, $parent, $indentLevel = 3) {
195203
$lastSibling = $parent.ChildNodes | where { $_ -isnot [System.Xml.XmlWhitespace] } | select -Last 1
196204
if ($lastSibling -ne $null) {
197-
$wsBefore = $lastSibling.PreviousSibling | where { $_ -is [System.Xml.XmlWhitespace] }
198-
$parent.InsertAfter($provider, $lastSibling)
199-
if ($wsBefore -ne $null) { $parent.InsertAfter($wsBefore.Clone(), $lastSibling) | Out-Null }
205+
# If not the first child, then copy the whitespace convention of the existing child
206+
$ws = "";
207+
$prev = $lastSibling.PreviousSibling | where { $_ -is [System.Xml.XmlWhitespace] }
208+
while ($prev -ne $null) {
209+
$ws = $prev.data + $ws
210+
$prev = $prev.PreviousSibling | where { $_ -is [System.Xml.XmlWhitespace] }
211+
}
212+
$parent.InsertAfter($provider, $lastSibling) | Out-Null
213+
if ($ws.length -gt 0) { $parent.InsertAfter($parent.OwnerDocument.CreateWhitespace($ws), $lastSibling) | Out-Null }
200214
return
201215
}
202-
$parent.AppendChild($provider)
216+
217+
# Add on a new line with indents. Make sure there is no existing whitespace mucking this up.
218+
foreach ($exws in $parent.ChildNodes | where { $_ -is [System.Xml.XmlWhitespace] }) { $parent.RemoveChild($exws) }
219+
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace("`r`n")) | Out-Null
220+
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace(" " * $indentLevel)) | Out-Null
221+
$parent.AppendChild($provider) | Out-Null
222+
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace("`r`n")) | Out-Null
223+
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace(" " * ($indentLevel - 1))) | Out-Null
203224
}
204225

205226
function SaveConfigFile($config) {

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/tools/install.ps1

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ $shouldUseRoslyn45 = $projectTargetFramework -match '4.5'
2121
$binDirectory = Join-Path $projectRoot 'bin'
2222

2323
#
24-
# Some things vary depending on which framework version you target. Set the defaults first
25-
# so the variables are scoped for the entire script. If you target an older framework,
26-
# (4.5-4.7.1) then we need to change some of these.
24+
# Some things vary depending on which framework version you target. If you target an
25+
# older framework, (4.5-4.7.1) then we need to change some of these.
2726
#
27+
$compilerVersion = $package.Version
28+
if($package.Versions -ne $null) { $compilerVersion = @($package.Versions)[0] }
29+
$packageDirectory = Split-Path $installPath
30+
$compilerPackageFolderName = $package.Id + "." + $compilerVersion
31+
$compilerPackageDirectory = Join-Path $packageDirectory $compilerPackageFolderName
2832
$compilerPackageToolsDirectory = Join-Path $compilerPackageDirectory 'tools\roslyn472'
2933
$csLanguageVersion = 'default'
3034
$vbLanguageVersion = 'default'
@@ -34,7 +38,7 @@ if ($projectTargetFramework -match '^4\.5')
3438
$csLanguageVersion = '6'
3539
$vbLanguageVersion = '14'
3640
}
37-
else if (($projectTargetFramework -match '^4\.6') or ($projectTargetFramework -match '^4\.7$') or ($projectTargetFramework -match '^4\.7\.[01]'))
41+
elseif (($projectTargetFramework -match '^4\.6') -or ($projectTargetFramework -match '^4\.7$') -or ($projectTargetFramework -match '^4\.7\.[01]'))
3842
{
3943
$compilerPackageToolsDirectory = Join-Path $compilerPackageDirectory 'tools\roslyn46'
4044
$csLanguageVersion = 'default'
@@ -48,12 +52,14 @@ else if (($projectTargetFramework -match '^4\.6') or ($projectTargetFramework -m
4852
. "$PSScriptRoot\common.ps1"
4953
$csCodeDomProvider = [CodeDomProviderDescription]@{
5054
TypeName="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider";
51-
AssemblyRef="Microsoft.CodeDom.Providers.DotNetCompilerPlatform";
55+
Assembly="Microsoft.CodeDom.Providers.DotNetCompilerPlatform";
5256
Version="3.4.0.0";
5357
FileExtension=".cs";
5458
Parameters=@(
5559
[CompilerParameterDescription]@{ Name="language"; DefaultValue="c#;cs;csharp"; IsRequired=$true; IsProviderOption=$false },
56-
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$false; IsProviderOption=$false },
60+
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$true; IsProviderOption=$false },
61+
[CompilerParameterDescription]@{ Name="goofyParam"; DefaultValue="foobaz"; IsRequired=$true; IsProviderOption=$true },
62+
[CompilerParameterDescription]@{ Name="verySeriousParam"; DefaultValue="synergy"; IsRequired=$true; IsProviderOption=$true },
5763
[CompilerParameterDescription]@{ Name="compilerOptions"; DefaultValue="/langversion:" + $csLanguageVersion + " /nowarn:1659;1699;1701;612;618"; IsRequired=$false; IsProviderOption=$false });
5864
}
5965
InstallCodeDomProvider $csCodeDomProvider
@@ -64,7 +70,7 @@ $vbCodeDomProvider = [CodeDomProviderDescription]@{
6470
FileExtension=".vb";
6571
Parameters=@(
6672
[CompilerParameterDescription]@{ Name="language"; DefaultValue="vb;vbs;visualbasic;vbscript"; IsRequired=$true; IsProviderOption=$false },
67-
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$false; IsProviderOption=$false },
73+
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$true; IsProviderOption=$false },
6874
[CompilerParameterDescription]@{ Name="compilerOptions"; DefaultValue="/langversion:" + $vbLanguageVersion + " /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"; IsRequired=$false; IsProviderOption=$false });
6975
}
7076
InstallCodeDomProvider $vbCodeDomProvider
@@ -80,19 +86,7 @@ Copy-Item $libDirectory\* $binDirectory -force | Out-Null
8086
# the applicaiton's bin folder.
8187
# For Web Applicaiton project, this is done in csproj.
8288
if ($project.Type -eq 'Web Site') {
83-
$packageDirectory = Split-Path $installPath
84-
85-
if($package.Versions -eq $null)
86-
{
87-
$compilerVersion = $package.Version
88-
}
89-
else
90-
{
91-
$compilerVersion = @($package.Versions)[0]
92-
}
9389

94-
$compilerPackageFolderName = $package.Id + "." + $compilerVersion
95-
$compilerPackageDirectory = Join-Path $packageDirectory $compilerPackageFolderName
9690
if ((Get-Item $compilerPackageDirectory) -isnot [System.IO.DirectoryInfo])
9791
{
9892
Write-Host "The install.ps1 cannot find the installation location of package $compilerPackageName, or the pakcage is not installed correctly."

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/tools/uninstall.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ param($installPath, $toolsPath, $package, $project)
1212
# First save the code dom compiler declarations off to a temp file so they can be restored
1313
# in the event that this is a package upgrade scenario.
1414
. "$PSScriptRoot\common.ps1"
15-
CommonUninstall "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
16-
CommonUninstall "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
15+
UninstallCodeDomProvider "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
16+
UninstallCodeDomProvider "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
1717

1818

1919
# Then remove the compiler bits from the bin directory

0 commit comments

Comments
 (0)