-
Notifications
You must be signed in to change notification settings - Fork 218
/
install.ps1
374 lines (299 loc) · 12.8 KB
/
install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# install.ps1
param($rootPath, $toolsPath, $package, $project)
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
function CountSolutionFilesByExtension($extension) {
$path = [System.IO.Path]::GetDirectoryName($project.FullName)
$totalfiles = [System.IO.Directory]::EnumerateFiles("$path", "*." + $extension, [System.IO.SearchOption]::AllDirectories)
[int]$totalcount = ($totalfiles | Measure-Object).Count
[int]$count = $totalcount
# Don't count the DisplayTemplates directory - need to subtract them.
if (($extension -eq "cshtml") -or ($extension -eq "vbhtml")) {
$razorfiles = [System.IO.Directory]::EnumerateFiles("$path\Views\Shared\DisplayTemplates", "*." + $extension)
[int]$razorcount = ($razorfiles | Measure-Object).Count
[int]$count = $totalcount - $razorcount
}
Write-Host "Project has $count $extension extensions"
return $count
}
### Copied from MvcScaffolding
function InferPreferredViewEngine() {
# Assume you want Razor except if you already have some ASPX views and no Razor ones
Write-Host "Checking for .aspx extensions"
if ((CountSolutionFilesByExtension "aspx") -eq 0) { return "razor" }
Write-Host "Checking for razor extensions"
if (((CountSolutionFilesByExtension "cshtml") -gt 0) -or ((CountSolutionFilesByExtension vbhtml) -gt 0)) { return "razor" }
Write-Host "No razor found, using aspx"
return "aspx"
}
function Add-Or-Update-AppSettings() {
$xml = New-Object xml
$web_config_path = Get-Web-Config-Path
$xml.Load($web_config_path)
$conf = $xml.SelectSingleNode("configuration")
if ($conf -eq $null)
{
$conf = $xml.CreateElement("configuration")
$xml.AppendChild($conf)
}
$appSettings = $xml.SelectSingleNode("configuration/appSettings")
if ($appSettings -eq $null) {
$appSettings = $xml.CreateElement("appSettings")
$conf.AppendChild($appSettings)
}
# add or update MvcSiteMapProvider_UseExternalDIContainer
$ext_di = $xml.SelectSingleNode("configuration/appSettings/add[@key='MvcSiteMapProvider_UseExternalDIContainer']")
if ($ext_di -ne $null) {
$ext_di.SetAttribute("value", "false")
} else {
$ext_di = $xml.CreateElement("add")
$key = $xml.CreateAttribute("key")
$key.Value = "MvcSiteMapProvider_UseExternalDIContainer"
$ext_di.Attributes.Append($key)
$value = $xml.CreateAttribute("value")
$value.Value = "false"
$ext_di.Attributes.Append($value)
$appSettings.AppendChild($ext_di)
}
# add or update MvcSiteMapProvider_ScanAssembliesForSiteMapNodes
$scan = $xml.SelectSingleNode("configuration/appSettings/add[@key='MvcSiteMapProvider_ScanAssembliesForSiteMapNodes']")
if ($scan -ne $null) {
$scan.SetAttribute("value", "true")
} else {
$scan = $xml.CreateElement("add")
$key = $xml.CreateAttribute("key")
$key.Value = "MvcSiteMapProvider_ScanAssembliesForSiteMapNodes"
$scan.Attributes.Append($key)
$value = $xml.CreateAttribute("value")
$value.Value = "true"
$scan.Attributes.Append($value)
$appSettings.AppendChild($scan)
}
# add MvcSiteMapProvider_IncludeAssembliesForScan (never update)
$include_scan = $xml.SelectSingleNode("configuration/appSettings/add[@key='MvcSiteMapProvider_IncludeAssembliesForScan']")
if ($include_scan -eq $null) {
$assembly_name = Get-Project-Property-Value "AssemblyName"
$include_scan = $xml.CreateElement("add")
$key = $xml.CreateAttribute("key")
$key.Value = "MvcSiteMapProvider_IncludeAssembliesForScan"
$include_scan.Attributes.Append($key)
$value = $xml.CreateAttribute("value")
$value.Value = "$assembly_name"
$include_scan.Attributes.Append($value)
$appSettings.AppendChild($include_scan)
}
Save-Document-With-Formatting $xml $web_config_path
}
function Add-Pages-Namespaces() {
$xml = New-Object xml
$web_config_path = Get-Web-Config-Path
$xml.Load($web_config_path)
$conf = $xml.SelectSingleNode("configuration")
if ($conf -eq $null)
{
$conf = $xml.CreateElement("configuration")
$xml.AppendChild($conf)
}
$system_web = $xml.SelectSingleNode("configuration/system.web")
if ($system_web -eq $null) {
$system_web = $xml.CreateElement("system.web")
$conf.AppendChild($system_web)
}
$pages = $xml.SelectSingleNode("configuration/system.web/pages")
if ($pages -eq $null) {
$pages = $xml.CreateElement("pages")
$system_web.AppendChild($pages)
}
$namespaces = $xml.SelectSingleNode("configuration/system.web/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/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/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-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_path = "$path\Views\Web.config"
# load Web.config as XML
$xml.Load($web_config_path)
$conf = $xml.SelectSingleNode("configuration")
if ($conf -eq $null)
{
$conf = $xml.CreateElement("configuration")
$xml.AppendChild($conf)
}
$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")
$conf.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-Document-With-Formatting $xml $web_config_path
}
function Update-SiteMap-Element() {
$xml = New-Object xml
$web_config_path = Get-Web-Config-Path
$xml.Load($web_config_path)
$siteMap = $xml.SelectSingleNode("configuration/system.web/siteMap")
if ($siteMap -ne $null) {
if ($xml.SelectSingleNode("configuration/system.web/siteMap[@enabled]") -ne $null) {
$siteMap.SetAttribute("enabled", "false")
} else {
$enabled = $xml.CreateAttribute("enabled")
$enabled.Value = "false"
$siteMap.Attributes.Append($enabled)
}
}
Save-Document-With-Formatting $xml $web_config_path
}
function Add-MVC4-Config-Sections() {
$xml = New-Object xml
$web_config_path = Get-Web-Config-Path
$xml.Load($web_config_path)
$conf = $xml.SelectSingleNode("configuration")
if ($conf -eq $null)
{
$conf = $xml.CreateElement("configuration")
$xml.AppendChild($conf)
}
$ws = $xml.SelectSingleNode("configuration/system.webServer")
if ($ws -eq $null) {
$ws = $xml.CreateElement("system.webServer")
$conf.AppendChild($ws)
}
$modules = $xml.SelectSingleNode("configuration/system.webServer/modules")
if ($modules -eq $null) {
$modules = $xml.CreateElement("modules")
$ws.AppendChild($modules)
}
$remove = $xml.SelectSingleNode("configuration/system.webServer/modules/remove[@name='UrlRoutingModule-4.0']")
if ($remove -eq $null) {
$remove = $xml.CreateElement("remove")
$name = $xml.CreateAttribute("name")
$name.Value = "UrlRoutingModule-4.0"
$remove.Attributes.Append($name)
$modules.AppendChild($remove)
}
$add = $xml.SelectSingleNode("configuration/system.webServer/modules/add[@name='UrlRoutingModule-4.0']")
if ($add -eq $null) {
$add = $xml.CreateElement("add")
$name = $xml.CreateAttribute("name")
$name.Value = "UrlRoutingModule-4.0"
$add.Attributes.Append($name)
$type = $xml.CreateAttribute("type")
$type.Value = "System.Web.Routing.UrlRoutingModule"
$add.Attributes.Append($type)
$modules.AppendChild($add)
}
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
}
function Get-Project-Property-Value([string] $property_name) {
$ms_build_project = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($Project.FullName) | Select-Object -First 1
foreach ($property in $ms_build_project.Properties) {
if ($property.Name -eq $property_name) {
$value = $property.EvaluatedValue
break
}
}
return $value
}
# 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() }
} else {
(Get-Project).ProjectItems | ?{ $_.Name -eq "Views" } | %{ $_.ProjectItems | ?{ $_.Name -eq "Shared" } } | %{ $_.ProjectItems | ?{ $_.Name -eq "DisplayTemplates" } } | %{ $_.ProjectItems | ?{ $_.Name -eq "MenuHelperModel.ascx" -or $_.Name -eq "SiteMapHelperModel.ascx" -or $_.Name -eq "SiteMapNodeModel.ascx" -or $_.Name -eq "SiteMapNodeModelList.ascx" -or $_.Name -eq "SiteMapPathHelperModel.ascx" -or $_.Name -eq "SiteMapTitleHelperModel.ascx" -or $_.Name -eq "CanonicalHelperModel.ascx" -or $_.Name -eq "MetaRobotsHelperModel.ascx" } } | %{ $_.Delete() }
}
# If MVC 4 or higher, install web.config section to fix 404 not found on sitemap.xml (#124)
$mvc_version = $project.Object.References.Find("System.Web.Mvc").Version
Write-Host "MVC Version: $mvc_version"
if ($mvc_version -notmatch '^[123]\.' -or [string]::IsNullOrEmpty($mvc_version))
{
Write-Host "Installing config sections for MVC >= 4"
Add-MVC4-Config-Sections
}
# Fixup the web.config files
Add-Or-Update-AppSettings
Add-Pages-Namespaces
Add-Razor-Pages-Namespaces
Update-SiteMap-Element