Skip to content

Commit

Permalink
Lots of hydration bug fixes after smoke testing install/reinstall.
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenMolloy committed May 2, 2020
1 parent 516bceb commit 47077c8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

<!-- If compilers tag is absent -->
<system.codedom>
<compilers xdt:Transform="InsertIfMissing">
</compilers>
<compilers xdt:Transform="InsertIfMissing" />
</system.codedom>

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

<!-- Inserting the new compiler -->
<system.codedom>
<compilers>
<compiler extension=".cs" xdt:Transform="Insert" />
</compilers>
</system.codedom>

<!-- If a .vb compiler is already present, the existing entry needs to be removed before inserting the new entry -->
<system.codedom>
<compilers>
Expand All @@ -38,10 +30,4 @@
</compilers>
</system.codedom>

<!-- Inserting the new compiler -->
<system.codedom>
<compilers>
<compiler extension=".vb" xdt:Transform="Insert" />
</compilers>
</system.codedom>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@
</appSettings>
<appSettings xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)">
</appSettings>

<system.codedom>
<compilers>
<compiler
extension=".cs"
xdt:Transform="Remove"
xdt:Locator="Match(extension)" />
</compilers>
</system.codedom>
<system.codedom>
<compilers>
<compiler
extension=".vb"
xdt:Transform="Remove"
xdt:Locator="Match(extension)" />
</compilers>
</system.codedom>
<system.codedom>
<compilers xdt:Transform="Remove" xdt:Locator="Condition(count(child::*) = 0)" />
</system.codedom>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ function InstallCodeDomProvider($providerDescription) {
$config = ReadConfigFile
$rehydratedCount = RehydrateOldDeclarations $config $providerDescription
$updatedCount = UpdateDeclarations $config $providerDescription
if ($updatedCount -le 0) { AddDefaultDeclaration $config $providerDescription }

##### Add the default provider if it wasn't rehydrated above
$defaultProvider = $config.xml.configuration["system.codedom"].compilers.compiler | where { $_.extension -eq $providerDescription.FileExtension }
if ($defaultProvider -eq $null) { AddDefaultDeclaration $config $providerDescription }
SaveConfigFile $config
}

function UninstallCodeDomProvider($providerType) {
##### Dehydrate config declarations #####
$config = ReadConfigFile
DehydrateDeclarations $config $providerType
DehydrateDeclarations $config $providerType | Out-Null
SaveConfigFile $config
}

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

if ([io.file]::Exists($tempFile)) {
$xml = (Select-Xml -Path "$tempFile" -XPath /).Node
$xml.PreserveWhitespace = $true
} else {
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.AppendChild($xml.CreateElement("driedDeclarations"))
$dd = $xml.CreateElement("driedDeclarations")
$xml.AppendChild($dd) | Out-Null
}

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

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

# Save the dehydrated declarations
$tmpFolder = Split-Path $tempFile
md -Force $tmpFolder
$xml.Save($tempFile)
md -Force $tmpFolder | Out-Null
$xml.Save($tempFile) | Out-Null
return $count
}

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

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

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

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

# Make dried record removal permanent
$xml.Save($tempFile)
$xml.Save($tempFile) | Out-Null

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

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

# Do the same thing for default providerOptions if not already present
foreach ($p in $providerDescription.Parameters | where { ($_.IsRequired -eq $true) and ($_.IsProviderOption -eq $true)}) {
if ($provider.($p.Name) -eq $null) {
foreach ($p in $providerDescription.Parameters | where { ($_.IsRequired -eq $true) -and ($_.IsProviderOption -eq $true)}) {
$existing = $provider.providerOption | where { $_.name -eq $p.Name }
if ($existing -eq $null) {
if ($p.DefaultValue -eq $null) {
Write-Host "Failed to add providerOption to '$($provider.name)' codeDom provider: '$($p.Name)' is required, but does not have a default value."
return
}
$po = $config.xml.CreateElement("providerOption")
$po.SetAttribute("name", $p.Name)
$po.SetAttribute("value", $p.DefaultValue)
$provider.AppendChild($po)
AppendChildNode $po $provider 4
}
}
}
Expand All @@ -178,7 +186,7 @@ function AddDefaultDeclaration($config, $providerDescription) {
$po = $config.xml.CreateElement("providerOption")
$po.SetAttribute("name", $p.Name)
$po.SetAttribute("value", $p.DefaultValue)
$dd.AppendChild($po)
AppendChildNode $po $dd 4
} else {
$dd.SetAttribute($p.Name, $p.DefaultValue)
}
Expand All @@ -188,18 +196,31 @@ function AddDefaultDeclaration($config, $providerDescription) {
# type last
$dd.SetAttribute("type", "$($providerDescription.TypeName), $($providerDescription.Assembly), Version=$($providerDescription.Version), Culture=neutral, PublicKeyToken=31bf3856ad364e35")

AppendProviderNode $dd $config.xml.configuration["system.codedom"].compilers
AppendChildNode $dd $config.xml.configuration["system.codedom"]["compilers"]
}

function AppendProviderNode($provider, $parent) {
function AppendChildNode($provider, $parent, $indentLevel = 3) {
$lastSibling = $parent.ChildNodes | where { $_ -isnot [System.Xml.XmlWhitespace] } | select -Last 1
if ($lastSibling -ne $null) {
$wsBefore = $lastSibling.PreviousSibling | where { $_ -is [System.Xml.XmlWhitespace] }
$parent.InsertAfter($provider, $lastSibling)
if ($wsBefore -ne $null) { $parent.InsertAfter($wsBefore.Clone(), $lastSibling) | Out-Null }
# If not the first child, then copy the whitespace convention of the existing child
$ws = "";
$prev = $lastSibling.PreviousSibling | where { $_ -is [System.Xml.XmlWhitespace] }
while ($prev -ne $null) {
$ws = $prev.data + $ws
$prev = $prev.PreviousSibling | where { $_ -is [System.Xml.XmlWhitespace] }
}
$parent.InsertAfter($provider, $lastSibling) | Out-Null
if ($ws.length -gt 0) { $parent.InsertAfter($parent.OwnerDocument.CreateWhitespace($ws), $lastSibling) | Out-Null }
return
}
$parent.AppendChild($provider)

# Add on a new line with indents. Make sure there is no existing whitespace mucking this up.
foreach ($exws in $parent.ChildNodes | where { $_ -is [System.Xml.XmlWhitespace] }) { $parent.RemoveChild($exws) }
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace("`r`n")) | Out-Null
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace(" " * $indentLevel)) | Out-Null
$parent.AppendChild($provider) | Out-Null
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace("`r`n")) | Out-Null
$parent.AppendChild($parent.OwnerDocument.CreateWhitespace(" " * ($indentLevel - 1))) | Out-Null
}

function SaveConfigFile($config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ $shouldUseRoslyn45 = $projectTargetFramework -match '4.5'
$binDirectory = Join-Path $projectRoot 'bin'

#
# Some things vary depending on which framework version you target. Set the defaults first
# so the variables are scoped for the entire script. If you target an older framework,
# (4.5-4.7.1) then we need to change some of these.
# Some things vary depending on which framework version you target. If you target an
# older framework, (4.5-4.7.1) then we need to change some of these.
#
$compilerVersion = $package.Version
if($package.Versions -ne $null) { $compilerVersion = @($package.Versions)[0] }
$packageDirectory = Split-Path $installPath
$compilerPackageFolderName = $package.Id + "." + $compilerVersion
$compilerPackageDirectory = Join-Path $packageDirectory $compilerPackageFolderName
$compilerPackageToolsDirectory = Join-Path $compilerPackageDirectory 'tools\roslyn472'
$csLanguageVersion = 'default'
$vbLanguageVersion = 'default'
Expand All @@ -34,7 +38,7 @@ if ($projectTargetFramework -match '^4\.5')
$csLanguageVersion = '6'
$vbLanguageVersion = '14'
}
else if (($projectTargetFramework -match '^4\.6') or ($projectTargetFramework -match '^4\.7$') or ($projectTargetFramework -match '^4\.7\.[01]'))
elseif (($projectTargetFramework -match '^4\.6') -or ($projectTargetFramework -match '^4\.7$') -or ($projectTargetFramework -match '^4\.7\.[01]'))
{
$compilerPackageToolsDirectory = Join-Path $compilerPackageDirectory 'tools\roslyn46'
$csLanguageVersion = 'default'
Expand All @@ -48,12 +52,14 @@ else if (($projectTargetFramework -match '^4\.6') or ($projectTargetFramework -m
. "$PSScriptRoot\common.ps1"
$csCodeDomProvider = [CodeDomProviderDescription]@{
TypeName="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider";
AssemblyRef="Microsoft.CodeDom.Providers.DotNetCompilerPlatform";
Assembly="Microsoft.CodeDom.Providers.DotNetCompilerPlatform";
Version="3.4.0.0";
FileExtension=".cs";
Parameters=@(
[CompilerParameterDescription]@{ Name="language"; DefaultValue="c#;cs;csharp"; IsRequired=$true; IsProviderOption=$false },
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$false; IsProviderOption=$false },
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$true; IsProviderOption=$false },
[CompilerParameterDescription]@{ Name="goofyParam"; DefaultValue="foobaz"; IsRequired=$true; IsProviderOption=$true },
[CompilerParameterDescription]@{ Name="verySeriousParam"; DefaultValue="synergy"; IsRequired=$true; IsProviderOption=$true },
[CompilerParameterDescription]@{ Name="compilerOptions"; DefaultValue="/langversion:" + $csLanguageVersion + " /nowarn:1659;1699;1701;612;618"; IsRequired=$false; IsProviderOption=$false });
}
InstallCodeDomProvider $csCodeDomProvider
Expand All @@ -64,7 +70,7 @@ $vbCodeDomProvider = [CodeDomProviderDescription]@{
FileExtension=".vb";
Parameters=@(
[CompilerParameterDescription]@{ Name="language"; DefaultValue="vb;vbs;visualbasic;vbscript"; IsRequired=$true; IsProviderOption=$false },
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$false; IsProviderOption=$false },
[CompilerParameterDescription]@{ Name="warningLevel"; DefaultValue="4"; IsRequired=$true; IsProviderOption=$false },
[CompilerParameterDescription]@{ Name="compilerOptions"; DefaultValue="/langversion:" + $vbLanguageVersion + " /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"; IsRequired=$false; IsProviderOption=$false });
}
InstallCodeDomProvider $vbCodeDomProvider
Expand All @@ -80,19 +86,7 @@ Copy-Item $libDirectory\* $binDirectory -force | Out-Null
# the applicaiton's bin folder.
# For Web Applicaiton project, this is done in csproj.
if ($project.Type -eq 'Web Site') {
$packageDirectory = Split-Path $installPath

if($package.Versions -eq $null)
{
$compilerVersion = $package.Version
}
else
{
$compilerVersion = @($package.Versions)[0]
}

$compilerPackageFolderName = $package.Id + "." + $compilerVersion
$compilerPackageDirectory = Join-Path $packageDirectory $compilerPackageFolderName
if ((Get-Item $compilerPackageDirectory) -isnot [System.IO.DirectoryInfo])
{
Write-Host "The install.ps1 cannot find the installation location of package $compilerPackageName, or the pakcage is not installed correctly."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ param($installPath, $toolsPath, $package, $project)
# First save the code dom compiler declarations off to a temp file so they can be restored
# in the event that this is a package upgrade scenario.
. "$PSScriptRoot\common.ps1"
CommonUninstall "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
CommonUninstall "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
UninstallCodeDomProvider "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
UninstallCodeDomProvider "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform"


# Then remove the compiler bits from the bin directory
Expand Down

0 comments on commit 47077c8

Please sign in to comment.