1
+ # Copyright 2016 Exceptionless
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+
8
+ function find_config ($project ) {
9
+ $configPath = $null
10
+
11
+ if ($project -eq $null -or $project.ProjectItems -eq $null ) {
12
+ return $configPath
13
+ }
14
+
15
+ try {
16
+ $config = $project.ProjectItems.Item (" Web.config" )
17
+ } catch { }
18
+
19
+ if ($config -eq $null ) {
20
+ try {
21
+ $config = $project.ProjectItems.Item (" App.config" )
22
+ } catch { }
23
+ }
24
+
25
+ if ($config -ne $null ) {
26
+ try {
27
+ $configPath = $config.Properties.Item (" LocalPath" ).Value
28
+ } catch { }
29
+ }
30
+
31
+ return $configPath
32
+ }
33
+
34
+ function update_config ($configPath , $platform ) {
35
+ [xml ] $configXml = gc $configPath
36
+ $shouldSave = $false
37
+
38
+ if ($configXml -ne $null ) {
39
+ $configSection = $configXml.SelectSingleNode (" configuration/configSections/section[@name='exceptionless']" )
40
+ if ($configSection -eq $null ) {
41
+ $parentNode = $configXml.SelectSingleNode (" configuration/configSections" )
42
+ if ($parentNode -eq $null ) {
43
+ if ($configXml.SelectSingleNode (" configuration" ).ChildNodes.Count -eq 0 ) {
44
+ $parentNode = $configXml.SelectSingleNode (" configuration" ).AppendChild($configXml.CreateElement (' configSections' ))
45
+ } else {
46
+ $parentNode = $configXml.SelectSingleNode (" configuration" ).InsertBefore($configXml.CreateElement (' configSections' ), $configXml.SelectSingleNode (" configuration" ).ChildNodes[0 ])
47
+ }
48
+ }
49
+ $configSection = $configXml.CreateElement (' section' )
50
+ $configSection.SetAttribute (' name' , ' exceptionless' )
51
+ $configSection.SetAttribute (' type' , ' Exceptionless.ExceptionlessSection, Exceptionless.Signed' )
52
+
53
+ $parentNode.AppendChild ($configSection )
54
+ $shouldSave = $true
55
+ }
56
+
57
+ $exceptionlessConfig = $configXml.SelectSingleNode (" configuration/exceptionless" )
58
+ if ($exceptionlessConfig -eq $null ) {
59
+ $exceptionlessNode = $configXml.CreateElement (' exceptionless' )
60
+ $exceptionlessNode.SetAttribute (' apiKey' , ' API_KEY_HERE' )
61
+
62
+ $target = $configXml.configuration [' system.web' ]
63
+ $configXml.SelectSingleNode (" configuration" ).InsertBefore($exceptionlessNode , $target )
64
+ $shouldSave = $true
65
+ } else {
66
+ if ($exceptionlessConfig.HasAttribute (' queuePath' )) {
67
+ $exceptionlessConfig.SetAttribute (' storagePath' , $exceptionlessConfig.GetAttribute (' queuePath' ))
68
+ $exceptionlessConfig.RemoveAttribute (' queuePath' )
69
+ $shouldSave = $true
70
+ }
71
+
72
+ $extendedData = $exceptionlessConfig.SelectSingleNode (' extendedData' )
73
+ if ($extendedData -ne $null ) {
74
+ $data = $configXml.CreateElement (' data' )
75
+ $data.InnerXML = $extendedData.InnerXML
76
+ $exceptionlessConfig.AppendChild ($data )
77
+ $exceptionlessConfig.RemoveChild ($extendedData )
78
+
79
+ $shouldSave = $true
80
+ }
81
+ }
82
+
83
+ if ($platform -ne $null -and $platform -ne ' WebApi' ) {
84
+ $webServerModule = $configXml.SelectSingleNode (" configuration/system.webServer/modules/add[@name='ExceptionlessModule']" )
85
+ if ($webServerModule -eq $null ) {
86
+ $parentNode = $configXml.SelectSingleNode (" configuration/system.webServer" )
87
+ if ($parentNode -eq $null ) {
88
+ $parentNode = $configXml.SelectSingleNode (" configuration" ).AppendChild($configXml.CreateElement (' system.webServer' ))
89
+ }
90
+ $parentNode = $configXml.SelectSingleNode (" configuration/system.webServer/modules" )
91
+ if ($parentNode -eq $null ) {
92
+ $parentNode = $configXml.configuration [' system.webServer' ].AppendChild($configXml.CreateElement (' modules' ))
93
+ }
94
+ $webServerModule = $configXml.CreateElement (' add' )
95
+ $webServerModule.SetAttribute (' name' , ' ExceptionlessModule' )
96
+ $webServerModule.SetAttribute (' type' , ' Exceptionless.' + $platform + ' .ExceptionlessModule, Exceptionless.' + $platform )
97
+
98
+ $parentNode.AppendChild ($webServerModule )
99
+ $shouldSave = $true
100
+ }
101
+ }
102
+
103
+ if ($shouldSave -eq $true ) {
104
+ $configXml.Save ($configPath )
105
+ }
106
+ }
107
+ }
108
+
109
+ function remove_config ($configPath , $platform ) {
110
+ [xml ] $configXml = gc $configPath
111
+ $shouldSave = $false
112
+
113
+ if ($configXml -ne $null ) {
114
+ $configSection = $configXml.SelectSingleNode (" configuration/configSections/section[@name='exceptionless']" )
115
+ if ($configSection -ne $null ) {
116
+ [Void ]$configSection.ParentNode.RemoveChild ($configSection )
117
+ $shouldSave = $true
118
+ }
119
+
120
+ $configSection = $configXml.SelectSingleNode (" configuration/exceptionless" )
121
+ if ($configSection -ne $null ) {
122
+ if ($configSection.HasAttribute (" apiKey" ) -and ($configSection.GetAttribute (" apiKey" ) -ne ' API_KEY_HERE' ) -and ($configSection.GetAttribute (" apiKey" ).length -gt 10 )) {
123
+ " Exceptionless API Key has been configured and will not be removed."
124
+ } else {
125
+ [Void ]$configSection.ParentNode.RemoveChild ($configSection )
126
+ $shouldSave = $true
127
+ }
128
+ }
129
+
130
+ if ($platform -ne $null -and $platform -ne ' WebApi' ) {
131
+ $webModule = $configXml.SelectSingleNode (" configuration/system.web/httpModules/add[@name='ExceptionlessModule']" )
132
+ if ($webModule -ne $null ) {
133
+ [Void ]$webModule.ParentNode.RemoveChild ($webModule )
134
+ $shouldSave = $true
135
+ }
136
+
137
+ $webServerModule = $configXml.SelectSingleNode (" configuration/system.webServer/modules/add[@name='ExceptionlessModule']" )
138
+ if ($webServerModule -ne $null ) {
139
+ [Void ]$webServerModule.ParentNode.RemoveChild ($webServerModule )
140
+ $shouldSave = $true
141
+ }
142
+ }
143
+
144
+
145
+ if ($shouldSave -eq $true ) {
146
+ $configXml.Save ($configPath )
147
+ }
148
+ }
149
+ }
150
+
151
+ function add_attribute ($sourceFile ) {
152
+ if (! (test-path $sourceFile )) {
153
+ return
154
+ }
155
+
156
+ $input = get-content $sourceFile
157
+ $isUpdated = $input | Select-String " Exceptionless" - quiet
158
+
159
+ if ($isUpdated ) {
160
+ return
161
+ }
162
+
163
+ # seems to insure a linefeed
164
+ " " | Out-file $sourceFile - append
165
+ if ([string ]$sourceFile.EndsWith (' .vb' )) {
166
+ ' <assembly: Exceptionless.Configuration.Exceptionless("API_KEY_HERE")>' | Out-File $sourceFile - append
167
+ } else {
168
+ ' [assembly: Exceptionless.Configuration.Exceptionless("API_KEY_HERE")]' | Out-File $sourceFile - append
169
+ }
170
+ }
0 commit comments