@@ -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
3942function 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
205226function SaveConfigFile ($config ) {
0 commit comments