123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- param($installPath, $toolsPath, $package, $project)
- try {
-
- $timestamp = (Get-Date).ToString('yyyyMMddHHmmss')
- $projectName = [IO.Path]::GetFileName($project.ProjectName.Trim([IO.PATH]::DirectorySeparatorChar, [IO.PATH]::AltDirectorySeparatorChar))
- $catalogName = "aspnet-$projectName-$timestamp"
-
- $localdb = "v11.0"
- if ([version]$dte.Version -ge [version]"14.0") {
- $localdb = "MSSQLLocalDB"
- }
- $connectionString ="Data Source=(LocalDb)\$localdb;Initial Catalog=$catalogName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\$catalogName.mdf"
- $connectionStringToken = 'Data Source=(LocalDb)\$localdb;'
- $config = $project.ProjectItems | Where-Object { $_.Name -eq "Web.config" }
- $configPath = ($config.Properties | Where-Object { $_.Name -eq "FullPath" }).Value
-
-
- $xml = New-Object System.Xml.XmlDocument
- $xml.Load($configPath)
-
- function CommentNode($node) {
- if (!$node) {
- return;
- }
-
- $commentNode = $xml.CreateComment($node.OuterXml)
- $parent = $node.ParentNode
- $parent.InsertBefore($commentNode, $node) | Out-Null
- $parent.RemoveChild($node) | Out-Null
- }
-
-
- $node = $xml.SelectSingleNode("/configuration/system.web/membership/providers/add[@type='System.Web.Security.SqlMembershipProvider']")
- $addedNode = $xml.SelectSingleNode("/configuration/system.web/membership/providers/add[@name='DefaultMembershipProvider']")
-
- if ($node) {
-
- $node.Attributes | Where { ! (@('name', 'type', 'connectionStringName') -contains $_.name) } | ForEach {
- $addedNode.SetAttribute($_.name, $_.value)
- }
- }
-
- $oldConnectionNode = $xml.SelectSingleNode("/configuration/connectionStrings/add[@name='$($node.connectionStringName)']")
- CommentNode $node
- CommentNode $oldConnectionNode
-
- $node = $xml.SelectSingleNode("/configuration/system.web/profile/providers/add[@type='System.Web.Profile.SqlProfileProvider']")
- $oldConnectionNode = $xml.SelectSingleNode("/configuration/connectionStrings/add[@name='$($node.connectionStringName)']")
- CommentNode $node
- CommentNode $oldConnectionNode
-
- $node = $xml.SelectSingleNode("/configuration/system.web/roleManager/providers/add[@type='System.Web.Security.SqlRoleProvider']")
- $oldConnectionNode = $xml.SelectSingleNode("/configuration/connectionStrings/add[@name='$($node.connectionStringName)']")
- $connectionStringsToComment += $node.connectionStringName
- CommentNode $node
- CommentNode $oldConnectionNode
-
-
- $connectionStrings = $xml.SelectSingleNode("/configuration/connectionStrings")
- if (!$connectionStrings) {
- $connectionStrings = $xml.CreateElement("connectionStrings")
- $xml.configuration.AppendChild($connectionStrings) | Out-Null
- }
-
- if (!($connectionStrings.SelectNodes("add[@name='DefaultConnection']") | Where { $_.connectionString.StartsWith($connectionStringToken, 'OrdinalIgnoreCase') })) {
-
- $newConnectionNode = $xml.CreateElement("add")
- $newConnectionNode.SetAttribute("name", 'DefaultConnection')
- $newConnectionNode.SetAttribute("providerName", "System.Data.SqlClient")
- $newConnectionNode.SetAttribute("connectionString", $connectionString)
-
- $connectionStrings.AppendChild($newConnectionNode) | Out-Null
- }
-
-
- $xml.Save($configPath)
-
- } catch {
- Write-Error "Unable to update the web.config file at $configPath. Add the following connection string to your config: <add name=`"DefaultConnection`" providerName=`"System.Data.SqlClient`" connectionString=`"$connectionString`" />"
- }
|