Skip to content

Commit

Permalink
Fixed MvcSiteMapProvider.Web so it will save the web.config files wit…
Browse files Browse the repository at this point in the history
…h the original encoding if specified in the XML file.
  • Loading branch information
NightOwl888 committed Sep 25, 2013
1 parent b7481b4 commit e627d0a
Showing 1 changed file with 50 additions and 58 deletions.
108 changes: 50 additions & 58 deletions nuget/mvcsitemapprovider.web/tools/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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() }
Expand Down

0 comments on commit e627d0a

Please sign in to comment.