Skip to content

Commit 81f099b

Browse files
committed
style: add stylecop / resharper for code checks. (#46)
this adds the possiblity to have a similar code structure across various collarborators.
1 parent 436a5cf commit 81f099b

37 files changed

+446
-280
lines changed

DotnetOperatorSdk.sln

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{95F3A6DD-B421-441D-B263-1B34A1465FF5}"
4+
ProjectSection(SolutionItems) = preProject
5+
README.md = README.md
6+
EndProjectSection
47
EndProject
58
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubeOps", "src\KubeOps\KubeOps.csproj", "{D7AB6CB9-94B6-4FEB-B7D8-D8AA793BD2A4}"
69
EndProject
@@ -16,6 +19,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubeOps.TestOperator", "tes
1619
EndProject
1720
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubeOps.TestOperator.Test", "tests\KubeOps.TestOperator.Test\KubeOps.TestOperator.Test.csproj", "{B374D7E4-E9BA-47F8-B1A4-440DECD376E4}"
1821
EndProject
22+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{EF94C48A-1D2A-459B-B397-A130463FF52D}"
23+
ProjectSection(SolutionItems) = preProject
24+
config\CodeAnalysis.targets = config\CodeAnalysis.targets
25+
config\Common.targets = config\Common.targets
26+
config\project.ruleset = config\project.ruleset
27+
config\stylecop.json = config\stylecop.json
28+
.gitignore = .gitignore
29+
.releaserc.json = .releaserc.json
30+
EndProjectSection
31+
EndProject
1932
Global
2033
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2134
Debug|Any CPU = Debug|Any CPU

DotnetOperatorSdk.sln.DotSettings

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<wpf:ResourceDictionary xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xml:space="preserve">
3-
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
4-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
5-
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_CONSTRUCTOR_INITIALIZER_ON_SAME_LINE/@EntryValue">False</s:Boolean>
6-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
7-
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_TYPE_CONSTRAINTS_ON_SAME_LINE/@EntryValue">False</s:Boolean>
8-
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_WHILE_ON_NEW_LINE/@EntryValue">True</s:Boolean>
9-
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
10-
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_DECLARATION_LPAR/@EntryValue">True</s:Boolean>
11-
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_INVOCATION_LPAR/@EntryValue">True</s:Boolean>
12-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARGUMENTS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
13-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARRAY_INITIALIZER_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
14-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_BINARY_EXPRESSIONS/@EntryValue">CHOP_IF_LONG</s:String>
15-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_METHOD_CALLS/@EntryValue">CHOP_IF_LONG</s:String>
16-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_EXTENDS_LIST_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
17-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_MULTIPLE_TYPE_PARAMEER_CONSTRAINTS_STYLE/@EntryValue">CHOP_ALWAYS</s:String>
18-
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_PARAMETERS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
19-
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
20-
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
21-
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
22-
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/TRAILING_COMMA_IN_MULTILINE_LISTS/@EntryValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
5+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
6+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_CONSTRUCTOR_INITIALIZER_ON_SAME_LINE/@EntryValue">False</s:Boolean>
7+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
8+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_TYPE_CONSTRAINTS_ON_SAME_LINE/@EntryValue">False</s:Boolean>
9+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_WHILE_ON_NEW_LINE/@EntryValue">True</s:Boolean>
10+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
11+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
12+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_DECLARATION_LPAR/@EntryValue">True</s:Boolean>
13+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_INVOCATION_LPAR/@EntryValue">True</s:Boolean>
14+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARGUMENTS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
15+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARRAY_INITIALIZER_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
16+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_BINARY_EXPRESSIONS/@EntryValue">CHOP_IF_LONG</s:String>
17+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_METHOD_CALLS/@EntryValue">CHOP_IF_LONG</s:String>
18+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_EXTENDS_LIST_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
19+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_MULTIPLE_TYPE_PARAMEER_CONSTRAINTS_STYLE/@EntryValue">CHOP_ALWAYS</s:String>
20+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_PARAMETERS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
21+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
22+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
23+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
24+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
2325
</wpf:ResourceDictionary>

config/CodeAnalysis.targets

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project>
2+
3+
<ItemGroup>
4+
<PackageReference Include="Roslynator.Analyzers" Version="3.0.0-rc" PrivateAssets="All"/>
5+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205" PrivateAssets="All"/>
6+
<AdditionalFiles Include="$(MSBuildThisFileDirectory)/stylecop.json"/>
7+
</ItemGroup>
8+
9+
<PropertyGroup>
10+
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)/project.ruleset</CodeAnalysisRuleSet>
11+
<TreatWarningsAsErrors Condition="'$(Configuration)' == 'Release'">true</TreatWarningsAsErrors>
12+
</PropertyGroup>
13+
14+
</Project>

config/Common.targets

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<LangVersion>8</LangVersion>
7+
<Authors>Christoph Bühler</Authors>
8+
<Company>smartive AG</Company>
9+
<Owner>cbuehler</Owner>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
14+
<_Parameter1>$(MSBuildProjectName).Test</_Parameter1>
15+
</AssemblyAttribute>
16+
</ItemGroup>
17+
18+
</Project>

config/project.ruleset

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RuleSet Name="Project Ruleset" Description="Default ruleset for dotnet core projects" ToolsVersion="15.0">
3+
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
4+
<Rule Id="SA0001" Action="None"/>
5+
<Rule Id="SA1600" Action="None"/>
6+
7+
<Rule Id="CS1591" Action="None"/>
8+
9+
<Rule Id="SA1008" Action="Info"/>
10+
<Rule Id="SA1009" Action="Info"/>
11+
12+
<Rule Id="SA1101" Action="None"/>
13+
<Rule Id="SA1106" Action="Info"/>
14+
<Rule Id="SA1118" Action="None"/>
15+
<Rule Id="SX1101" Action="Info"/>
16+
17+
<Rule Id="SA1309" Action="None"/>
18+
<Rule Id="SX1309" Action="Warning"/>
19+
<Rule Id="SA1313" Action="Info"/>
20+
21+
<Rule Id="SA1501" Action="Info"/>
22+
<Rule Id="SA1503" Action="Info"/>
23+
24+
<Rule Id="SA1623" Action="None"/>
25+
<Rule Id="SA1633" Action="None"/>
26+
<Rule Id="SA1642" Action="None"/>
27+
<Rule Id="SA1643" Action="None"/>
28+
<Rule Id="SA1649" Action="None"/>
29+
</Rules>
30+
<Rules AnalyzerId="Roslynator.CSharp.Analyzers" RuleNamespace="Roslynator.CSharp.Analyzers">
31+
<Rule Id="RCS1029" Action="None"/>
32+
<Rule Id="RCS1090" Action="None"/>
33+
</Rules>
34+
</RuleSet>

config/stylecop.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
3+
"settings": {
4+
"orderingRules": {
5+
"usingDirectivesPlacement": "outsideNamespace"
6+
},
7+
"layoutRules": {
8+
"newlineAtEndOfFile": "require"
9+
}
10+
}
11+
}

src/KubeOps/KubeOps.csproj

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

3+
<Import Project="..\..\config\Common.targets" />
4+
<Import Project="..\..\config\CodeAnalysis.targets" />
5+
36
<PropertyGroup>
47
<OutputType>exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
6-
<Nullable>enable</Nullable>
7-
<LangVersion>8</LangVersion>
8-
<Authors>Christoph Bühler</Authors>
9-
<Company>smartive AG</Company>
10-
<Owner>cbuehler</Owner>
118
</PropertyGroup>
129

1310
<PropertyGroup>

src/KubeOps/Operator/Caching/ResourceCache.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public TEntity Upsert(TEntity resource, out CacheComparisonResult result)
4848
return resource;
4949
}
5050

51+
public void Remove(TEntity resource) => Remove(resource.Metadata.Uid);
52+
53+
public void Clear()
54+
{
55+
_cache.Clear();
56+
_metrics.CachedItemsSize.Set(_cache.Count);
57+
_metrics.CachedItemsSummary.Observe(_cache.Count);
58+
}
59+
5160
private CacheComparisonResult CompareCache(TEntity resource)
5261
{
5362
if (!Exists(resource))
@@ -75,15 +84,6 @@ private CacheComparisonResult CompareCache(TEntity resource)
7584
return CacheComparisonResult.Modified;
7685
}
7786

78-
public void Remove(TEntity resource) => Remove(resource.Metadata.Uid);
79-
80-
public void Clear()
81-
{
82-
_cache.Clear();
83-
_metrics.CachedItemsSize.Set(_cache.Count);
84-
_metrics.CachedItemsSummary.Observe(_cache.Count);
85-
}
86-
8787
private bool Exists(TEntity resource) => _cache.ContainsKey(resource.Metadata.Uid);
8888

8989
private bool Exists(string id) => _cache.ContainsKey(id);

src/KubeOps/Operator/Client/KubernetesClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ public Task<Watcher<TResource>> Watch<TResource>(
228228
crd.Group,
229229
crd.Version,
230230
crd.Plural,
231-
timeoutSeconds: (int) timeout.TotalSeconds,
231+
timeoutSeconds: (int)timeout.TotalSeconds,
232232
watch: true,
233233
cancellationToken: cancellationToken)
234234
: ApiClient.ListNamespacedCustomObjectWithHttpMessagesAsync(
235235
crd.Group,
236236
crd.Version,
237237
@namespace,
238238
crd.Plural,
239-
timeoutSeconds: (int) timeout.TotalSeconds,
239+
timeoutSeconds: (int)timeout.TotalSeconds,
240240
watch: true,
241241
cancellationToken: cancellationToken);
242242

src/KubeOps/Operator/Commands/Generators/CrdGenerator.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public CrdGenerator(EntitySerializer serializer)
2727
[Option("--use-old-crds", Description = "Defines that the old crd definitions (V1Beta1) should be used.")]
2828
public bool UseOldCrds { get; set; }
2929

30+
public static IEnumerable<V1CustomResourceDefinition> GenerateCrds(Assembly? assembly = null)
31+
{
32+
assembly ??= Assembly.GetEntryAssembly();
33+
if (assembly == null)
34+
{
35+
throw new Exception("No Entry Assembly found.");
36+
}
37+
38+
return GetTypesWithAttribute<KubernetesEntityAttribute>(assembly)
39+
.Where(type => !type.GetCustomAttributes<IgnoreEntityAttribute>().Any())
40+
.Select(EntityToCrdExtensions.CreateCrd);
41+
}
42+
3043
public async Task<int> OnExecuteAsync(CommandLineApplication app)
3144
{
3245
var crds = GenerateCrds().ToList();
@@ -76,19 +89,6 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
7689
return ExitCodes.Success;
7790
}
7891

79-
public static IEnumerable<V1CustomResourceDefinition> GenerateCrds(Assembly? assembly = null)
80-
{
81-
assembly ??= Assembly.GetEntryAssembly();
82-
if (assembly == null)
83-
{
84-
throw new Exception("No Entry Assembly found.");
85-
}
86-
87-
return GetTypesWithAttribute<KubernetesEntityAttribute>(assembly)
88-
.Where(type => !type.GetCustomAttributes<IgnoreEntityAttribute>().Any())
89-
.Select(EntityToCrdExtensions.CreateCrd);
90-
}
91-
9292
private static IEnumerable<Type> GetTypesWithAttribute<TAttribute>(Assembly assembly)
9393
where TAttribute : Attribute =>
9494
assembly.GetTypes().Where(type => type.GetCustomAttributes<TAttribute>().Any());

0 commit comments

Comments
 (0)