Skip to content

Commit b9484f1

Browse files
author
Jeff Treuting
committed
PoserShell script to clean duplicate entries w/ nuget
When NuGet does an update, if you have changed the config file entries from what they are when initially installed (for example, changing the connection string for a EF5 repository, or changing the timeout value for the TimeoutCachingStrategy) then NuGet adds a new entry at the bottom of the list. The problem is that the name is the key and then the project errors out because you can't have duplicate keys in a section. So the Install.ps1 script checks the web.config and/or the app.config file to see if there are duplciate entries and if so removes the last ones since they are the dups that were just entered. Fixes #63
1 parent 79b120e commit b9484f1

6 files changed

Lines changed: 334 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Install.ps1
2+
param($installPath, $toolsPath, $package, $project)
3+
4+
Function Clean-Dups-In-Config-File ($localPath, $parentNodePath, $nodePath)
5+
{
6+
# load config as XML
7+
$xml.Load($localPath)
8+
9+
# select parent node to call the RemoveChild on later
10+
$parentNode = $xml.SelectSingleNode($parentNodePath)
11+
12+
# select the nodes
13+
$nodes = $xml.SelectNodes($parentNodePath + "/" + $nodePath)
14+
15+
if ($nodes.Count -gt 0)
16+
{
17+
# if there are multiple repository names with same key, then the NuGet update added the additional ones and they will be last
18+
# so loop through those nodes, excluding the first one, and remove them
19+
$i = 1;
20+
while ($i -lt $nodes.Count)
21+
{
22+
$parentNode.RemoveChild($nodes.Item($i))
23+
$i++
24+
}
25+
26+
# save the config file
27+
$xml.Save($localPath)
28+
}
29+
30+
}
31+
32+
$xml = New-Object xml
33+
34+
# find the Web.config file if there is one
35+
$config = $project.ProjectItems | where {$_.Name -eq "Web.config" }
36+
if ($config -ne $null)
37+
{
38+
# find its path on the file system
39+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
40+
41+
# clean duplicate entries
42+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/repositories" "repository[@name='cacheRepository']"
43+
}
44+
45+
# find the App.config file if there is one
46+
$config = $project.ProjectItems | where {$_.Name -eq "App.config" }
47+
if ($config -ne $null)
48+
{
49+
# find its path on the file system
50+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
51+
52+
# clean duplicate entries
53+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/repositories" "repository[@name='cacheRepository']"
54+
}
55+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Install.ps1
2+
param($installPath, $toolsPath, $package, $project)
3+
4+
Function Clean-Dups-In-Config-File ($localPath, $parentNodePath, $nodePath)
5+
{
6+
# load config as XML
7+
$xml.Load($localPath)
8+
9+
# select parent node to call the RemoveChild on later
10+
$parentNode = $xml.SelectSingleNode($parentNodePath)
11+
12+
# select the nodes
13+
$nodes = $xml.SelectNodes($parentNodePath + "/" + $nodePath)
14+
15+
if ($nodes.Count -gt 0)
16+
{
17+
# if there are multiple repository names with the same key, then the NuGet update added the additional ones and they will be last
18+
# so loop through those nodes, excluding the first one, and remove them
19+
$i = 1;
20+
while ($i -lt $nodes.Count)
21+
{
22+
$parentNode.RemoveChild($nodes.Item($i))
23+
$i++
24+
}
25+
26+
# save the config file
27+
$xml.Save($localPath)
28+
}
29+
30+
}
31+
32+
$xml = New-Object xml
33+
34+
# find the Web.config file if there is one
35+
$config = $project.ProjectItems | where {$_.Name -eq "Web.config" }
36+
if ($config -ne $null)
37+
{
38+
# find its path on the file system
39+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
40+
41+
# clean duplicate entries
42+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingProviders" "cachingProvider[@name='memCachedProvider']"
43+
}
44+
45+
# find the App.config file if there is one
46+
$config = $project.ProjectItems | where {$_.Name -eq "App.config" }
47+
if ($config -ne $null)
48+
{
49+
# find its path on the file system
50+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
51+
52+
# clean duplicate entries
53+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingProviders" "cachingProvider[@name='memCachedProvider']"
54+
}
55+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Install.ps1
2+
param($installPath, $toolsPath, $package, $project)
3+
4+
Function Clean-Dups-In-Config-File ($localPath, $parentNodePath, $nodePath)
5+
{
6+
# load config as XML
7+
$xml.Load($localPath)
8+
9+
# select parent node to call the RemoveChild on later
10+
$parentNode = $xml.SelectSingleNode($parentNodePath)
11+
12+
# select the nodes
13+
$nodes = $xml.SelectNodes($parentNodePath + "/" + $nodePath)
14+
15+
if ($nodes.Count -gt 0)
16+
{
17+
# if there are multiple repository names with the same key, then the NuGet update added the additional ones and they will be last
18+
# so loop through those nodes, excluding the first one, and remove them
19+
$i = 1;
20+
while ($i -lt $nodes.Count)
21+
{
22+
$parentNode.RemoveChild($nodes.Item($i))
23+
$i++
24+
}
25+
26+
# save the config file
27+
$xml.Save($localPath)
28+
}
29+
30+
}
31+
32+
$xml = New-Object xml
33+
34+
# find the Web.config file if there is one
35+
$config = $project.ProjectItems | where {$_.Name -eq "Web.config" }
36+
if ($config -ne $null)
37+
{
38+
# find its path on the file system
39+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
40+
41+
# clean duplicate entries
42+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingProviders" "cachingProvider[@name='redisProvider']"
43+
}
44+
45+
# find the App.config file if there is one
46+
$config = $project.ProjectItems | where {$_.Name -eq "App.config" }
47+
if ($config -ne $null)
48+
{
49+
# find its path on the file system
50+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
51+
52+
# clean duplicate entries
53+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingProviders" "cachingProvider[@name='redisProvider']"
54+
}
55+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Install.ps1
2+
param($installPath, $toolsPath, $package, $project)
3+
4+
Function Clean-Dups-In-Config-File ($localPath, $parentNodePath, $nodePath)
5+
{
6+
# load config as XML
7+
$xml.Load($localPath)
8+
9+
# select parent node to call the RemoveChild on later
10+
$parentNode = $xml.SelectSingleNode($parentNodePath)
11+
12+
# select the nodes
13+
$nodes = $xml.SelectNodes($parentNodePath + "/" + $nodePath)
14+
15+
if ($nodes.Count -gt 0)
16+
{
17+
# if there are multiple repository names with the same key, then the NuGet update added the additional ones and they will be last
18+
# so loop through those nodes, excluding the first one, and remove them
19+
$i = 1;
20+
while ($i -lt $nodes.Count)
21+
{
22+
$parentNode.RemoveChild($nodes.Item($i))
23+
$i++
24+
}
25+
26+
# save the config file
27+
$xml.Save($localPath)
28+
}
29+
30+
}
31+
32+
$xml = New-Object xml
33+
34+
# find the Web.config file if there is one
35+
$config = $project.ProjectItems | where {$_.Name -eq "Web.config" }
36+
if ($config -ne $null)
37+
{
38+
# find its path on the file system
39+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
40+
41+
# clean duplicate entries
42+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/repositories" "repository[@name='ef5Repository']"
43+
}
44+
45+
# find the App.config file if there is one
46+
$config = $project.ProjectItems | where {$_.Name -eq "App.config" }
47+
if ($config -ne $null)
48+
{
49+
# find its path on the file system
50+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
51+
52+
# clean duplicate entries
53+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/repositories" "repository[@name='ef5Repository']"
54+
}
55+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Install.ps1
2+
param($installPath, $toolsPath, $package, $project)
3+
4+
Function Clean-Dups-In-Config-File ($localPath, $parentNodePath, $nodePath)
5+
{
6+
# load config as XML
7+
$xml.Load($localPath)
8+
9+
# select parent node to call the RemoveChild on later
10+
$parentNode = $xml.SelectSingleNode($parentNodePath)
11+
12+
# select the nodes
13+
$nodes = $xml.SelectNodes($parentNodePath + "/" + $nodePath)
14+
15+
if ($nodes.Count -gt 0)
16+
{
17+
# if there are multiple repository names with same key, then the NuGet update added the additional ones and they will be last
18+
# so loop through those nodes, excluding the first one, and remove them
19+
$i = 1;
20+
while ($i -lt $nodes.Count)
21+
{
22+
$parentNode.RemoveChild($nodes.Item($i))
23+
$i++
24+
}
25+
26+
# save the config file
27+
$xml.Save($localPath)
28+
}
29+
30+
}
31+
32+
$xml = New-Object xml
33+
34+
# find the Web.config file if there is one
35+
$config = $project.ProjectItems | where {$_.Name -eq "Web.config" }
36+
if ($config -ne $null)
37+
{
38+
# find its path on the file system
39+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
40+
41+
# clean duplicate entries
42+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/repositories" "repository[@name='inMemoryRepository']"
43+
}
44+
45+
# find the App.config file if there is one
46+
$config = $project.ProjectItems | where {$_.Name -eq "App.config" }
47+
if ($config -ne $null)
48+
{
49+
# find its path on the file system
50+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
51+
52+
# clean duplicate entries
53+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/repositories" "repository[@name='inMemoryRepository']"
54+
}
55+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Install.ps1
2+
param($installPath, $toolsPath, $package, $project)
3+
4+
Function Clean-Dups-In-Config-File ($localPath, $parentNodePath, $nodePath)
5+
{
6+
# load config as XML
7+
$xml.Load($localPath)
8+
9+
# select parent node to call the RemoveChild on later
10+
$parentNode = $xml.SelectSingleNode($parentNodePath)
11+
12+
# select the nodes
13+
$nodes = $xml.SelectNodes($parentNodePath + "/" + $nodePath)
14+
15+
if ($nodes.Count -gt 0)
16+
{
17+
# if there are multiple repository names with same key, then the NuGet update added the additional ones and they will be last
18+
# so loop through those nodes, excluding the first one, and remove them
19+
$i = 1;
20+
while ($i -lt $nodes.Count)
21+
{
22+
$parentNode.RemoveChild($nodes.Item($i))
23+
$i++
24+
}
25+
26+
# save the config file
27+
$xml.Save($localPath)
28+
}
29+
30+
}
31+
32+
$xml = New-Object xml
33+
34+
# find the Web.config file if there is one
35+
$config = $project.ProjectItems | where {$_.Name -eq "Web.config" }
36+
if ($config -ne $null)
37+
{
38+
# find its path on the file system
39+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
40+
41+
# clean duplicate entries
42+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingStrategies" "cachingStrategy[@name='standardCachingStrategy']"
43+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingStrategies" "cachingStrategy[@name='timeoutCachingStrategy']"
44+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingStrategies" "cachingStrategy[@name='noCaching']"
45+
}
46+
47+
# find the App.config file if there is one
48+
$config = $project.ProjectItems | where {$_.Name -eq "App.config" }
49+
if ($config -ne $null)
50+
{
51+
# find its path on the file system
52+
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
53+
54+
# clean duplicate entries
55+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingStrategies" "cachingStrategy[@name='standardCachingStrategy']"
56+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingStrategies" "cachingStrategy[@name='timeoutCachingStrategy']"
57+
Clean-Dups-In-Config-File $localPath.Value "configuration/sharpRepository/cachingStrategies" "cachingStrategy[@name='noCaching']"
58+
}
59+

0 commit comments

Comments
 (0)