Skip to content

Commit 2978603

Browse files
Ellerbachtorbacz
andauthored
Add StyleCop linter, script to help in adoption and 17 cleaned bindings (#515)
* first attempt * adding settings.stylecop to the solutions * Create sync script for all devices which (#484) ***NO_CI*** * New Rules + fixes for 5 more projects (#503) * StyleCop fixes - 17 projects (#506) * 4 project style cop fixes * Fix 13 projects. StyleCop rules. * Adjust style cop settings for existing projects. * fixing YX5300 * fiwing weatherhelper * more fixes * fixing axp192 * axp192 fix comment * axp192 fixing intellisense comment, adjusting path Co-authored-by: Marcin Torba <[email protected]>
1 parent 5ee1c92 commit 2978603

File tree

231 files changed

+5386
-1788
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

231 files changed

+5386
-1788
lines changed

StyleCop/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Description
2+
3+
This script was created to sync Stylecop settings across all projects in pointed path. It's iterating through all folders looking for .nfproj files.
4+
If any found, then update it configuration to use Stylecop with settings from this folder.
5+
6+
## How to use
7+
8+
Run this script from power shell console.
9+
10+
```shell
11+
.\syncSettingsOverEveryDevice.ps1
12+
13+
## Configuration
14+
All configurable options are available at the top of script as variables. You can change few parameters inside the script.
15+
16+
$projectWhiteList = ("AD5328", "4Relay") - **Project folders which should be considered when running script. Leave empty for all.**
17+
18+
$styleCopPackageVersion = "6.2.0" **- version of StyleCop.MSBuild package**
19+
20+
$styleCopTreatErrorsAsWarningsNodeValue = "false" **- should stylecop errors be treated as compilation warnings or errors**
21+
22+
$styleCopSettingsFileName = "Settings.StyleCop" **- path to main Settings.StyleCop file which is used to sync settings for all projects**
23+
24+
$devicesMainFolderPath = "..\devices"

StyleCop/Settings.StyleCop

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<StyleCopSettings Version="105">
2+
<Analyzers>
3+
<Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
4+
<Rules>
5+
<Rule Name="FileHeaderMustContainFileName">
6+
<RuleSettings>
7+
<BooleanProperty Name="Enabled">False</BooleanProperty>
8+
</RuleSettings>
9+
</Rule>
10+
<Rule Name="FileHeaderMustHaveValidCompanyText">
11+
<RuleSettings>
12+
<BooleanProperty Name="Enabled">False</BooleanProperty>
13+
</RuleSettings>
14+
</Rule>
15+
<Rule Name="FileHeaderFileNameDocumentationMustMatchTypeName">
16+
<RuleSettings>
17+
<BooleanProperty Name="Enabled">False</BooleanProperty>
18+
</RuleSettings>
19+
</Rule>
20+
<Rule Name="PropertyDocumentationMustHaveValueText">
21+
<RuleSettings>
22+
<BooleanProperty Name="Enabled">True</BooleanProperty>
23+
</RuleSettings>
24+
</Rule>
25+
<Rule Name="DocumentationTextMustBeginWithACapitalLetter">
26+
<RuleSettings>
27+
<BooleanProperty Name="Enabled">True</BooleanProperty>
28+
</RuleSettings>
29+
</Rule>
30+
<Rule Name="DocumentationTextMustEndWithAPeriod">
31+
<RuleSettings>
32+
<BooleanProperty Name="Enabled">True</BooleanProperty>
33+
</RuleSettings>
34+
</Rule>
35+
<Rule Name="FileHeaderMustShowCopyright">
36+
<RuleSettings>
37+
<BooleanProperty Name="Enabled">False</BooleanProperty>
38+
</RuleSettings>
39+
</Rule>
40+
<Rule Name="FileHeaderMustHaveCopyrightText">
41+
<RuleSettings>
42+
<BooleanProperty Name="Enabled">False</BooleanProperty>
43+
</RuleSettings>
44+
</Rule>
45+
<Rule Name="ElementDocumentationMustBeSpelledCorrectly">
46+
<RuleSettings>
47+
<BooleanProperty Name="Enabled">False</BooleanProperty>
48+
</RuleSettings>
49+
</Rule>
50+
<Rule Name="DocumentationTextMustContainWhitespace">
51+
<RuleSettings>
52+
<BooleanProperty Name="Enabled">False</BooleanProperty>
53+
</RuleSettings>
54+
</Rule>
55+
</Rules>
56+
<AnalyzerSettings>
57+
<BooleanProperty Name="IgnorePrivates">True</BooleanProperty>
58+
<BooleanProperty Name="IgnoreInternals">True</BooleanProperty>
59+
</AnalyzerSettings>
60+
</Analyzer>
61+
<Analyzer AnalyzerId="StyleCop.CSharp.ReadabilityRules">
62+
<Rules>
63+
<Rule Name="PrefixLocalCallsWithThis">
64+
<RuleSettings>
65+
<BooleanProperty Name="Enabled">False</BooleanProperty>
66+
</RuleSettings>
67+
</Rule>
68+
<Rule Name="PrefixCallsCorrectly">
69+
<RuleSettings>
70+
<BooleanProperty Name="Enabled">False</BooleanProperty>
71+
</RuleSettings>
72+
</Rule>
73+
</Rules>
74+
<AnalyzerSettings />
75+
</Analyzer>
76+
<Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
77+
<Rules>
78+
<Rule Name="UsingDirectivesMustBePlacedWithinNamespace">
79+
<RuleSettings>
80+
<BooleanProperty Name="Enabled">False</BooleanProperty>
81+
</RuleSettings>
82+
</Rule>
83+
<Rule Name="ElementsMustAppearInTheCorrectOrder">
84+
<RuleSettings>
85+
<BooleanProperty Name="Enabled">False</BooleanProperty>
86+
</RuleSettings>
87+
</Rule>
88+
<Rule Name="ElementsMustBeOrderedByAccess">
89+
<RuleSettings>
90+
<BooleanProperty Name="Enabled">False</BooleanProperty>
91+
</RuleSettings>
92+
</Rule>
93+
</Rules>
94+
<AnalyzerSettings />
95+
</Analyzer>
96+
<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
97+
<Rules>
98+
<Rule Name="FieldNamesMustNotBeginWithUnderscore">
99+
<RuleSettings>
100+
<BooleanProperty Name="Enabled">False</BooleanProperty>
101+
</RuleSettings>
102+
</Rule>
103+
<Rule Name="FieldNamesMustNotUseHungarianNotation">
104+
<RuleSettings>
105+
<BooleanProperty Name="Enabled">False</BooleanProperty>
106+
</RuleSettings>
107+
</Rule>
108+
</Rules>
109+
<AnalyzerSettings />
110+
</Analyzer>
111+
</Analyzers>
112+
</StyleCopSettings>
+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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 &quot;
113+
#Event if we use " in SetAttribute PS is inserting &quot; instead of "
114+
#Same for &amp;
115+
$fileContent = Get-Content $nfprojPath
116+
$fileContent = $fileContent.Replace("&quot;", "`"")
117+
$fileContent = $fileContent.Replace("quot;", "`"")
118+
$fileContent = $fileContent.Replace("&amp;", "")
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')&quot; Text=&quot;`$([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

Comments
 (0)