1
+ $ErrorActionPreference = ' Stop'
2
+
3
+ # Whitelisted projects
4
+ $projectWhiteList = (" AD5328" , " 4Relay" , " Ads1115" , " Adxl345" , " Adxl357" , " Ags01db" , " Ahtxx" )
5
+
6
+ # Const packages.config
7
+ $styleCopPackageId = " StyleCop.MSBuild"
8
+ $styleCopPackageVersion = " 6.2.0"
9
+ $styleCopPackageTargetFramework = " netnano1.0"
10
+ $styleCopPackageDevelopmentDependency = " true"
11
+
12
+ # Consts nfProj
13
+ $nfProjXmlNamespace = " http://schemas.microsoft.com/developer/msbuild/2003"
14
+ $styleCopTreatErrorsAsWarningsNodeName = " StyleCopTreatErrorsAsWarnings"
15
+ $styleCopTreatErrorsAsWarningsNodeValue = " false"
16
+ $styleCopImportPackageTargetPath = " packages\StyleCop.MSBuild.$styleCopPackageVersion \build\StyleCop.MSBuild.targets"
17
+
18
+ # Paths
19
+ $styleCopSettingsFileName = " Settings.StyleCop" ;
20
+ $devicesMainFolderPath = " ..\devices"
21
+
22
+ $styleCopSettingsSourceFileContent = Get-Content $styleCopSettingsFileName
23
+ $allDevicesFolders = Get-ChildItem - Path $devicesMainFolderPath - Directory
24
+
25
+ function Test-PSCustomObjectEquality {
26
+ param (
27
+ [Parameter (Mandatory = $true )]
28
+ [PSCustomObject ] $firstObject ,
29
+
30
+ [Parameter (Mandatory = $true )]
31
+ [PSCustomObject ] $secondObject
32
+ )
33
+ -not (Compare-Object $firstObject.PSObject.Properties $secondObject.PSObject.Properties )
34
+ }
35
+
36
+ function SyncStyleCopSettings {
37
+ param (
38
+ [Parameter (Mandatory = $true )]
39
+ [String ] $deviceFolder
40
+ )
41
+ $pathToSettingInCurrentFolder = $deviceFolder + ' \' + $styleCopSettingsFileName
42
+
43
+ if (Test-Path - Path $pathToSettingInCurrentFolder - PathType Leaf) {
44
+ Write-Host " File exists in path " $pathToSettingInCurrentFolder " . Checking file content" - ForegroundColor Green
45
+ $contentOfExistingFile = Get-Content $pathToSettingInCurrentFolder
46
+
47
+ $comapreResult = (Compare-Object $styleCopSettingsSourceFileContent $contentOfExistingFile )
48
+ if ($comapreResult.Count -eq 0 ) {
49
+ Write-Host " Existing file matches source" - ForegroundColor Green
50
+ return
51
+ }
52
+ }
53
+
54
+ Write-Host " Creating/replacing file in " $pathToSettingInCurrentFolder - ForegroundColor Yellow
55
+ Set-Content $pathToSettingInCurrentFolder $styleCopSettingsSourceFileContent
56
+ }
57
+
58
+ function EnsureXmlAttributeExists {
59
+ param (
60
+ [Parameter (Mandatory = $true )]
61
+ [String ] $attributeName ,
62
+
63
+ [Parameter (Mandatory = $true )]
64
+ [String ] $expectedValue ,
65
+
66
+ [Parameter (Mandatory = $true )]
67
+ [System.Xml.XmlElement ] $attributeParentNode
68
+ )
69
+
70
+ $attribute = $attributeParentNode.GetAttribute ($attributeName )
71
+ if (($null -eq $attribute ) -or ($attribute -ne $expectedValue )) {
72
+ $attributeParentNode.SetAttribute ($attributeName , $expectedValue )
73
+ }
74
+ }
75
+
76
+ function UpdatePackagesConfig {
77
+ param (
78
+ [Parameter (Mandatory = $true )]
79
+ [String ] $nfProjFile
80
+ )
81
+
82
+ $dirPath = Split-Path - Path $nfProjFile ;
83
+ $packagesPath = " $dirPath \packages.config"
84
+
85
+ if (! (Test-Path $packagesPath - PathType Leaf)) {
86
+ throw " $packagesPath does not exists"
87
+ }
88
+
89
+ [xml ]$packagesContent = Get-Content $packagesPath
90
+ # check if stylecop ms build exists
91
+ # if not, insert
92
+ $styleCopPackage = $packagesContent.SelectNodes (" packages/package" ) | Where-Object { $_.id -eq $styleCopPackageId }
93
+ if ($null -eq $styleCopPackage ) {
94
+ $styleCopPackage = $packagesContent.CreateElement (" package" )
95
+ EnsureXmlAttributeExists " id" $styleCopPackageId $styleCopPackage
96
+ $notUsed = $packagesContent.packages.AppendChild ($styleCopPackage )
97
+ }
98
+
99
+ EnsureXmlAttributeExists " version" $styleCopPackageVersion $styleCopPackage
100
+ EnsureXmlAttributeExists " targetFramework" $styleCopPackageTargetFramework $styleCopPackage
101
+ EnsureXmlAttributeExists " developmentDependency" $styleCopPackageDevelopmentDependency $styleCopPackage
102
+
103
+ $packagesContent.Save ($packagesPath );
104
+ }
105
+
106
+ function Cleanup {
107
+ param (
108
+ [Parameter (Mandatory = $true )]
109
+ [String ] $nfprojPath
110
+ )
111
+
112
+ # Clean file from "
113
+ # Event if we use " in SetAttribute PS is inserting " instead of "
114
+ # Same for &
115
+ $fileContent = Get-Content $nfprojPath
116
+ $fileContent = $fileContent.Replace (" "" , " `" " )
117
+ $fileContent = $fileContent.Replace (" quot;" , " `" " )
118
+ $fileContent = $fileContent.Replace (" &" , " " )
119
+ $fileContent = $fileContent.Replace (" amp;" , " " )
120
+ Set-Content - Path $nfprojPath - Value $fileContent
121
+ }
122
+
123
+ function EnsureNfProjHasStyleCopSettings {
124
+ param (
125
+ [Parameter (Mandatory = $true )]
126
+ [String ] $deviceFolder
127
+ )
128
+
129
+ $allNfProjFiles = Get-ChildItem - Path $deviceFolder - Recurse - Include * .nfproj;
130
+ foreach ($nfProj in $allNfProjFiles ) {
131
+ # Skip sample projects
132
+ if ($nfProj -like ' *sample*' ) {
133
+ continue
134
+ }
135
+
136
+ Write-Host " Found nfProj: " $nfProj
137
+ [xml ]$nfProjContent = Get-Content $nfProj
138
+ $propertyGroupWithProjectGuid = $nfProjContent.SelectNodes (" /" ).Project.PropertyGroup[1 ];
139
+
140
+ $styleCopErrorsSettingNode = $propertyGroupWithProjectGuid .$styleCopTreatErrorsAsWarningsNodeName
141
+
142
+ if ($null -eq $styleCopErrorsSettingNode ) {
143
+ $styleCopErrorsSettingNode = $nfProjContent.CreateElement ($styleCopTreatErrorsAsWarningsNodeName , $nfProjXmlNamespace )
144
+ # We don't want unnecessery output in console, without assigment node content is displayed in console
145
+ $notUsed = $propertyGroupWithProjectGuid.AppendChild ($styleCopErrorsSettingNode )
146
+ $styleCopErrorsSettingNode.InnerText = $styleCopTreatErrorsAsWarningsNodeValue
147
+ }
148
+ else {
149
+ $propertyGroupWithProjectGuid .$styleCopTreatErrorsAsWarningsNodeName = $styleCopTreatErrorsAsWarningsNodeValue
150
+ }
151
+
152
+
153
+ # Import & Target
154
+ # TODO: Sync settings
155
+ $projectNode = $nfProjContent.SelectNodes (" /" ).Project;
156
+ $importStyleCopProject = $projectNode.Import | Where-Object { $_.Project -eq $styleCopImportPackageTargetPath }
157
+ if ($null -eq $importStyleCopProject ) {
158
+ $importStyleCopProject = $nfProjContent.CreateElement (" Import" , $nfProjXmlNamespace )
159
+ EnsureXmlAttributeExists " Project" $styleCopImportPackageTargetPath $importStyleCopProject
160
+ $notUsed = $projectNode.AppendChild ($importStyleCopProject )
161
+
162
+ # I assume if there is no import node, there is no target also Target
163
+ $targetStyleCop = $nfProjContent.CreateElement (" Target" , $nfProjXmlNamespace )
164
+ EnsureXmlAttributeExists " Name" " EnsureNuGetPackageBuildImports" $targetStyleCop
165
+ EnsureXmlAttributeExists " BeforeTargets" " PrepareForBuild" $targetStyleCop
166
+
167
+ $targetStyleCopPropertyGroup = $nfProjContent.CreateElement (" PropertyGroup" , $nfProjXmlNamespace )
168
+ $notUsed = $targetStyleCop.AppendChild ($targetStyleCopPropertyGroup )
169
+
170
+ $targetStyleCopPropertyGroupErrorText = $nfProjContent.CreateElement (" ErrorText" , $nfProjXmlNamespace )
171
+ $targetStyleCopPropertyGroupErrorText.InnerText = " This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}."
172
+ $notUsed = $targetStyleCopPropertyGroup.AppendChild ($targetStyleCopPropertyGroupErrorText )
173
+
174
+ $targetErrorCondition = $nfProjContent.CreateElement (" Error" , $nfProjXmlNamespace )
175
+ EnsureXmlAttributeExists " Condition" " !Exists('$styleCopImportPackageTargetPath ')" Text="`$ ([System.String]::Format('`$ (ErrorText)', '$styleCopImportPackageTargetPath '))" $targetErrorCondition
176
+ $notUsed = $targetStyleCop.AppendChild ($targetErrorCondition )
177
+ $notUsed = $projectNode.AppendChild ($targetStyleCop )
178
+ }
179
+
180
+ EnsureXmlAttributeExists " Condition" " Exists('$styleCopImportPackageTargetPath ')" $importStyleCopProject
181
+
182
+ $nfProjContent.Save ($nfProj );
183
+ UpdatePackagesConfig $nfProj
184
+ Cleanup $nfProj
185
+ }
186
+ }
187
+
188
+ function isProjectWhitelisted ($deviceName ) {
189
+ # If no project in array, then accept all
190
+ if ($projectWhiteList.Length -eq 0 ){
191
+ return $true
192
+ }
193
+
194
+ if ($deviceName -in $projectWhiteList ) {
195
+ return $true
196
+ }
197
+
198
+ return $false
199
+ }
200
+
201
+ foreach ($deviceFolder in $allDevicesFolders ) {
202
+ if (isProjectWhitelisted $deviceFolder.Name -eq $true ) {
203
+ Write-Host " Checking " $deviceFolder.FullName - ForegroundColor Green
204
+ SyncStyleCopSettings $deviceFolder.FullName
205
+ EnsureNfProjHasStyleCopSettings $deviceFolder.FullName
206
+ }
207
+ }
0 commit comments