Skip to content

Commit

Permalink
Fixes #233 web.config formatting changes when upgrading.
Browse files Browse the repository at this point in the history
  • Loading branch information
NightOwl888 committed Sep 23, 2013
1 parent ddc493e commit 2882b42
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 19 deletions.
10 changes: 0 additions & 10 deletions nuget/mvcsitemapprovider.web/content/Views/web.config.transform

This file was deleted.

94 changes: 85 additions & 9 deletions nuget/mvcsitemapprovider.web/tools/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ function Add-Or-Update-AppSettings() {
$appSettings.AppendChild($scan)
}

# save the Web.config file
$xml.Save($localPath.Value)
# 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()
}

function Add-Pages-Namespaces() {
Expand Down Expand Up @@ -141,8 +144,74 @@ function Add-Pages-Namespaces() {
$namespaces.AppendChild($html_models)
}

# save the Web.config file
$xml.Save($localPath.Value)
# 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()
}

function Add-Razor-Pages-Namespaces() {
$xml = New-Object xml

$path = [System.IO.Path]::GetDirectoryName($project.FullName)
$web_config_file = "$path\Views\Web.config"

# load Web.config as XML
$xml.Load($web_config_file)

$system_web_webpages_razor = $xml.SelectSingleNode("configuration/system.web.webPages.razor")
if ($system_web_webpages_razor -eq $null) {
$system_web_webpages_razor = $xml.CreateElement("system.web.webPages.razor")
$xml.AppendChild($system_web_webpages_razor)
}

$pages = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages")
if ($pages -eq $null) {
$pages = $xml.CreateElement("pages")

$page_base_type = $xml.CreateAttribute("pageBaseType")
$page_base_type.Value = "System.Web.Mvc.WebViewPage"
$pages.Attributes.Append($page_base_type)

$system_web_webpages_razor.AppendChild($pages)
}

$namespaces = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages/namespaces")
if ($namespaces -eq $null) {
$namespaces = $xml.CreateElement("namespaces")
$pages.AppendChild($namespaces)
}

# add MvcSiteMapProvider.Web.Html if it doesn't already exist
$html = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages/namespaces/add[@namespace='MvcSiteMapProvider.Web.Html']")
if ($html -eq $null) {
$html = $xml.CreateElement("add")

$namespace_html = $xml.CreateAttribute("namespace")
$namespace_html.Value = "MvcSiteMapProvider.Web.Html"
$html.Attributes.Append($namespace_html)

$namespaces.AppendChild($html)
}

# add MvcSiteMapProvider.Web.Html.Models if it doesn't already exist
$html_models = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages/namespaces/add[@namespace='MvcSiteMapProvider.Web.Html.Models']")
if ($html_models -eq $null) {
$html_models = $xml.CreateElement("add")

$namespace_models = $xml.CreateAttribute("namespace")
$namespace_models.Value = "MvcSiteMapProvider.Web.Html.Models"
$html_models.Attributes.Append($namespace_models)

$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()
}

function Update-SiteMap-Element() {
Expand All @@ -168,8 +237,11 @@ function Update-SiteMap-Element() {
}
}

# save the Web.config file
$xml.Save($localPath.Value)
# 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()
}

function Add-MVC4-Config-Sections() {
Expand Down Expand Up @@ -223,8 +295,11 @@ function Add-MVC4-Config-Sections() {
$modules.AppendChild($add)
}

# save the Web.config file
$xml.Save($localPath.Value)
# 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()
}

# Infer which view engine you're using based on the files in your project
Expand All @@ -241,7 +316,8 @@ if ($project.Object.References.Find("System.Web.Mvc").Version -eq "4.0.0.0")
Add-MVC4-Config-Sections
}

# Fixup the web.config file
# Fixup the web.config files
Add-Or-Update-AppSettings
Add-Pages-Namespaces
Add-Razor-Pages-Namespaces
Update-SiteMap-Element
47 changes: 47 additions & 0 deletions nuget/mvcsitemapprovider.web/tools/uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,52 @@ function Remove-Pages-Namespaces() {
$xml.Save($localPath.Value)
}

function Remove-Razor-Pages-Namespaces() {
$xml = New-Object xml

$path = [System.IO.Path]::GetDirectoryName($project.FullName)
$web_config_file = "$path\Views\Web.config"

# load Web.config as XML
$xml.Load($web_config_file)

# remove MvcSiteMapProvider.Web.Html if it exists
$html = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages/namespaces/add[@namespace='MvcSiteMapProvider.Web.Html']")
if ($html -ne $null) {
$html.ParentNode.RemoveChild($html)
}

# remove MvcSiteMapProvider.Web.Html.Models if it exists
$html_models = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages/namespaces/add[@namespace='MvcSiteMapProvider.Web.Html.Models']")
if ($html_models -ne $null) {
$html_models.ParentNode.RemoveChild($html_models)
}

$namespaces = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages/namespaces")
if ($namespaces -ne $null) {
if (($namespaces.HasChildNodes -eq $false) -and ($namespaces.Attributes.Count -eq 0)) {
$namespaces.ParentNode.RemoveChild($namespaces)
}
}

$pages = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages")
if ($pages -ne $null) {
if (($pages.HasChildNodes -eq $false) -and ($pages.Attributes.Count -eq 0)) {
$pages.ParentNode.RemoveChild($pages)
}
}

$system_web_webpages_razor = $xml.SelectSingleNode("configuration/system.web.webPages.razor")
if ($system_web_webpages_razor -ne $null) {
if (($system_web_webpages_razor.HasChildNodes -eq $false) -and ($system_web_webpages_razor.Attributes.Count -eq 0)) {
$system_web_webpages_razor.ParentNode.RemoveChild($system_web_webpages_razor)
}
}

# save the Web.config file
$xml.Save($web_config_file)
}

function Update-SiteMap-Element() {
$xml = New-Object xml

Expand Down Expand Up @@ -162,6 +208,7 @@ if ($project.Object.References.Find("System.Web.Mvc").Version -eq "4.0.0.0")
# Undo the changes made to the config file
Remove-AppSettings
Remove-Pages-Namespaces
Remove-Razor-Pages-Namespaces
Update-SiteMap-Element


Expand Down

0 comments on commit 2882b42

Please sign in to comment.