From d3e1dbf3475e76cc23136021b5214da2f930196a Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Wed, 25 Sep 2013 14:50:00 +0700 Subject: [PATCH] Fixed MvcSiteMapProvider.Web so it will save the web.config files with the original encoding if specified in the XML file. --- .../mvcsitemapprovider.web/tools/install.ps1 | 108 ++++++++---------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/nuget/mvcsitemapprovider.web/tools/install.ps1 b/nuget/mvcsitemapprovider.web/tools/install.ps1 index 74bc70a3..75c80e19 100644 --- a/nuget/mvcsitemapprovider.web/tools/install.ps1 +++ b/nuget/mvcsitemapprovider.web/tools/install.ps1 @@ -32,14 +32,8 @@ function InferPreferredViewEngine() { function Add-Or-Update-AppSettings() { $xml = New-Object xml - # find the Web.config file - $config = $project.ProjectItems | where {$_.Name -eq "Web.config"} - - # find its path on the file system - $localPath = $config.Properties | where {$_.Name -eq "LocalPath"} - - # load Web.config as XML - $xml.Load($localPath.Value) + $web_config_path = Get-Web-Config-Path + $xml.Load($web_config_path) $appSettings = $xml.SelectSingleNode("configuration/appSettings") if ($appSettings -eq $null) { @@ -83,24 +77,14 @@ function Add-Or-Update-AppSettings() { $appSettings.AppendChild($scan) } - # save the Web.config file with formatting - $writer = New-Object System.Xml.XmlTextWriter -ArgumentList @($localPath.Value, $null) - $writer.Formatting = [System.Xml.Formatting]::Indented - $xml.Save($writer) - $writer.Close() + Save-Document-With-Formatting $xml $web_config_path } function Add-Pages-Namespaces() { $xml = New-Object xml - - # find the Web.config file - $config = $project.ProjectItems | where {$_.Name -eq "Web.config"} - - # find its path on the file system - $localPath = $config.Properties | where {$_.Name -eq "LocalPath"} - - # load Web.config as XML - $xml.Load($localPath.Value) + + $web_config_path = Get-Web-Config-Path + $xml.Load($web_config_path) $system_web = $xml.SelectSingleNode("configuration/system.web") if ($system_web -eq $null) { @@ -144,21 +128,17 @@ function Add-Pages-Namespaces() { $namespaces.AppendChild($html_models) } - # save the Web.config file with formatting - $writer = New-Object System.Xml.XmlTextWriter -ArgumentList @($localPath.Value, $null) - $writer.Formatting = [System.Xml.Formatting]::Indented - $xml.Save($writer) - $writer.Close() + Save-Document-With-Formatting $xml $web_config_path } function Add-Razor-Pages-Namespaces() { $xml = New-Object xml $path = [System.IO.Path]::GetDirectoryName($project.FullName) - $web_config_file = "$path\Views\Web.config" + $web_config_path = "$path\Views\Web.config" # load Web.config as XML - $xml.Load($web_config_file) + $xml.Load($web_config_path) $system_web_webpages_razor = $xml.SelectSingleNode("configuration/system.web.webPages.razor") if ($system_web_webpages_razor -eq $null) { @@ -207,24 +187,14 @@ function Add-Razor-Pages-Namespaces() { $namespaces.AppendChild($html_models) } - # save the Views/Web.config file with formatting - $writer = New-Object System.Xml.XmlTextWriter -ArgumentList @($web_config_file, $null) - $writer.Formatting = [System.Xml.Formatting]::Indented - $xml.Save($writer) - $writer.Close() + Save-Document-With-Formatting $xml $web_config_path } function Update-SiteMap-Element() { $xml = New-Object xml - # find the Web.config file - $config = $project.ProjectItems | where {$_.Name -eq "Web.config"} - - # find its path on the file system - $localPath = $config.Properties | where {$_.Name -eq "LocalPath"} - - # load Web.config as XML - $xml.Load($localPath.Value) + $web_config_path = Get-Web-Config-Path + $xml.Load($web_config_path) $siteMap = $xml.SelectSingleNode("configuration/system.web/siteMap") if ($siteMap -ne $null) { @@ -237,24 +207,14 @@ function Update-SiteMap-Element() { } } - # save the Web.config file with formatting - $writer = New-Object System.Xml.XmlTextWriter -ArgumentList @($localPath.Value, $null) - $writer.Formatting = [System.Xml.Formatting]::Indented - $xml.Save($writer) - $writer.Close() + Save-Document-With-Formatting $xml $web_config_path } function Add-MVC4-Config-Sections() { $xml = New-Object xml - - # find the Web.config file - $config = $project.ProjectItems | where {$_.Name -eq "Web.config"} - - # find its path on the file system - $localPath = $config.Properties | where {$_.Name -eq "LocalPath"} - - # load Web.config as XML - $xml.Load($localPath.Value) + + $web_config_path = Get-Web-Config-Path + $xml.Load($web_config_path) # select the node $ws = $xml.SelectSingleNode("configuration/system.webServer") @@ -295,13 +255,45 @@ function Add-MVC4-Config-Sections() { $modules.AppendChild($add) } - # save the Web.config file with formatting - $writer = New-Object System.Xml.XmlTextWriter -ArgumentList @($localPath.Value, $null) + Save-Document-With-Formatting $xml $web_config_path +} + +#Gets the encoding from an open xml document as a System.Text.Encoding type +function Get-Document-Encoding([xml] $xml) { + [string] $encodingStr = "" + if ($xml.FirstChild.NodeType -eq [System.Xml.XmlNodeType]::XmlDeclaration) { + [System.Xml.XmlDeclaration] $declaration = $xml.FirstChild + $encodingStr = $declaration.Encoding + } + if ([string]::IsNullOrEmpty($encodingStr) -eq $false) { + $encoding = $null + Try { + $encoding = [System.Text.Encoding]::GetEncoding($encodingStr) + } + Catch [System.Exception] { + $encoding = $null + } + return $encoding + } else { + return $null + } +} + +function Save-Document-With-Formatting([xml] $xml, [string] $path) { + # save the xml file with formatting and original encoding + $encoding = Get-Document-Encoding $xml + $writer = New-Object System.Xml.XmlTextWriter -ArgumentList @($path, $encoding) $writer.Formatting = [System.Xml.Formatting]::Indented $xml.Save($writer) $writer.Close() } +function Get-Web-Config-Path() { + $path = [System.IO.Path]::GetDirectoryName($project.FullName) + $web_config_path = "$path\Web.config" + return $web_config_path +} + # Infer which view engine you're using based on the files in your project if ([string](InferPreferredViewEngine) -eq 'aspx') { (Get-Project).ProjectItems | ?{ $_.Name -eq "Views" } | %{ $_.ProjectItems | ?{ $_.Name -eq "Shared" } } | %{ $_.ProjectItems | ?{ $_.Name -eq "DisplayTemplates" } } | %{ $_.ProjectItems | ?{ $_.Name -eq "MenuHelperModel.cshtml" -or $_.Name -eq "SiteMapHelperModel.cshtml" -or $_.Name -eq "SiteMapNodeModel.cshtml" -or $_.Name -eq "SiteMapNodeModelList.cshtml" -or $_.Name -eq "SiteMapPathHelperModel.cshtml" -or $_.Name -eq "SiteMapTitleHelperModel.cshtml" -or $_.Name -eq "CanonicalHelperModel.cshtml" -or $_.Name -eq "MetaRobotsHelperModel.cshtml" } } | %{ $_.Delete() }