Skip to content

Commit 6e4ba03

Browse files
author
Bart Koelman
authored
Coding guidelines cibuild (#962)
* Added inspectcode/cleanupcode scripts and cibuild validation * Auto-formatted codebase using Resharper * Updated documentation to match with new style * Updated contributing guidelines
1 parent b45d1fa commit 6e4ba03

File tree

661 files changed

+12043
-8880
lines changed

Some content is hidden

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

661 files changed

+12043
-8880
lines changed

.config/dotnet-tools.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"jetbrains.resharper.globaltools": {
6+
"version": "2020.3.3",
7+
"commands": [
8+
"jb"
9+
]
10+
},
11+
"regitlint": {
12+
"version": "2.1.3",
13+
"commands": [
14+
"regitlint"
15+
]
16+
}
17+
}
18+
}

.editorconfig

+112-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,123 @@
1-
# EditorConfig is awesome: http://EditorConfig.org
2-
3-
# top-most EditorConfig file
1+
# Remove the line below if you want to inherit .editorconfig settings from higher directories
42
root = true
53

64
[*]
7-
end_of_line = lf
8-
insert_final_newline = true
95
indent_style = space
106
indent_size = 4
117
charset = utf-8
8+
trim_trailing_whitespace = true
9+
end_of_line = lf
10+
insert_final_newline = true
1211

13-
[*.{csproj,props}]
12+
[*.{csproj,json}]
1413
indent_size = 2
1514

16-
[*.{cs,vb}]
17-
dotnet_naming_rule.private_members_with_underscore.symbols = private_fields
18-
dotnet_naming_rule.private_members_with_underscore.style = prefix_underscore
19-
dotnet_naming_rule.private_members_with_underscore.severity = suggestion
15+
[*.{cs}]
16+
#### .NET Coding Conventions ####
17+
18+
# Organize usings
19+
dotnet_sort_system_directives_first = true
20+
21+
# this. preferences
22+
dotnet_style_qualification_for_field = false:suggestion
23+
dotnet_style_qualification_for_property = false:suggestion
24+
dotnet_style_qualification_for_method = false:suggestion
25+
dotnet_style_qualification_for_event = false:suggestion
26+
27+
# Language keywords vs BCL types preferences
28+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
29+
dotnet_style_predefined_type_for_member_access = true:suggestion
30+
31+
# Modifier preferences
32+
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion
33+
csharp_style_pattern_local_over_anonymous_function = false:silent
34+
35+
# Expression-level preferences
36+
dotnet_style_operator_placement_when_wrapping = end_of_line
37+
dotnet_style_prefer_auto_properties = true:suggestion
38+
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
39+
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
40+
41+
# Parameter preferences
42+
dotnet_code_quality_unused_parameters = non_public:suggestion
43+
44+
# Expression-bodied members
45+
csharp_style_expression_bodied_accessors = true:suggestion
46+
csharp_style_expression_bodied_constructors = false:suggestion
47+
csharp_style_expression_bodied_indexers = true:suggestion
48+
csharp_style_expression_bodied_lambdas = true:suggestion
49+
csharp_style_expression_bodied_local_functions = false:suggestion
50+
csharp_style_expression_bodied_methods = false:suggestion
51+
csharp_style_expression_bodied_operators = false:suggestion
52+
csharp_style_expression_bodied_properties = true:suggestion
53+
54+
# Code-block preferences
55+
csharp_prefer_braces = true:suggestion
56+
57+
# Expression-level preferences
58+
csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion
59+
60+
# 'using' directive preferences
61+
csharp_using_directive_placement = outside_namespace:suggestion
62+
63+
64+
#### C# Formatting Rules ####
65+
66+
# Indentation preferences
67+
csharp_indent_case_contents_when_block = false
68+
69+
# Wrapping preferences
70+
csharp_preserve_single_line_statements = false
71+
72+
73+
#### Naming styles ####
74+
75+
dotnet_diagnostic.IDE1006.severity = warning
76+
77+
# Naming rules
78+
dotnet_naming_rule.private_const_fields_should_be_pascal_case.symbols = private_const_fields
79+
dotnet_naming_rule.private_const_fields_should_be_pascal_case.style = pascal_case
80+
dotnet_naming_rule.private_const_fields_should_be_pascal_case.severity = warning
81+
82+
dotnet_naming_rule.private_static_readonly_fields_should_be_pascal_case.symbols = private_static_readonly_fields
83+
dotnet_naming_rule.private_static_readonly_fields_should_be_pascal_case.style = pascal_case
84+
dotnet_naming_rule.private_static_readonly_fields_should_be_pascal_case.severity = warning
85+
86+
dotnet_naming_rule.private_static_or_readonly_fields_should_start_with_underscore.symbols = private_static_or_readonly_fields
87+
dotnet_naming_rule.private_static_or_readonly_fields_should_start_with_underscore.style = camel_case_prefix_with_underscore
88+
dotnet_naming_rule.private_static_or_readonly_fields_should_start_with_underscore.severity = warning
89+
90+
dotnet_naming_rule.locals_and_parameters_should_be_camel_case.symbols = locals_and_parameters
91+
dotnet_naming_rule.locals_and_parameters_should_be_camel_case.style = camel_case
92+
dotnet_naming_rule.locals_and_parameters_should_be_camel_case.severity = warning
93+
94+
dotnet_naming_rule.types_and_members_should_be_pascal_case.symbols = types_and_members
95+
dotnet_naming_rule.types_and_members_should_be_pascal_case.style = pascal_case
96+
dotnet_naming_rule.types_and_members_should_be_pascal_case.severity = warning
97+
98+
# Symbol specifications
99+
dotnet_naming_symbols.private_const_fields.applicable_kinds = field
100+
dotnet_naming_symbols.private_const_fields.applicable_accessibilities = private
101+
dotnet_naming_symbols.private_const_fields.required_modifiers = const
102+
103+
dotnet_naming_symbols.private_static_readonly_fields.applicable_kinds = field
104+
dotnet_naming_symbols.private_static_readonly_fields.applicable_accessibilities = private
105+
dotnet_naming_symbols.private_static_readonly_fields.required_modifiers = static,readonly
106+
107+
dotnet_naming_symbols.private_static_or_readonly_fields.applicable_kinds = field
108+
dotnet_naming_symbols.private_static_or_readonly_fields.applicable_accessibilities = private
109+
dotnet_naming_symbols.private_static_or_readonly_fields.required_modifiers = static readonly
110+
111+
dotnet_naming_symbols.locals_and_parameters.applicable_kinds = local,parameter
112+
dotnet_naming_symbols.locals_and_parameters.applicable_accessibilities = *
113+
114+
dotnet_naming_symbols.types_and_members.applicable_kinds = *
115+
dotnet_naming_symbols.types_and_members.applicable_accessibilities = *
116+
117+
# Naming styles
118+
dotnet_naming_style.pascal_case.capitalization = pascal_case
20119

21-
dotnet_naming_symbols.private_fields.applicable_kinds = field
22-
dotnet_naming_symbols.private_fields.applicable_accessibilities = private
120+
dotnet_naming_style.camel_case_prefix_with_underscore.required_prefix = _
121+
dotnet_naming_style.camel_case_prefix_with_underscore.capitalization = camel_case
23122

24-
dotnet_naming_style.prefix_underscore.capitalization = camel_case
25-
dotnet_naming_style.prefix_underscore.required_prefix = _
123+
dotnet_naming_style.camel_case.capitalization = camel_case

.github/CONTRIBUTING.MD

-41
This file was deleted.

.github/CONTRIBUTING.md

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# I don't want to read this whole thing I just have a question!!!
2+
3+
> Note: Please don't file an issue to ask a question.
4+
5+
You'll get faster results by using our official [Gitter channel](https://gitter.im/json-api-dotnet-core/Lobby) or [StackOverflow](https://stackoverflow.com/search?q=jsonapidotnetcore) where the community chimes in with helpful advice if you have questions.
6+
7+
# How can I contribute?
8+
9+
## Reporting bugs
10+
11+
This section guides you through submitting a bug report.
12+
Following these guidelines helps maintainers and the community understand your report, reproduce the behavior and find related reports.
13+
14+
Before creating bug reports:
15+
- Perform a search to see if the problem has already been reported. If it has and the issue is still open, add a comment to the existing issue instead of opening a new one. If you find a Closed issue that seems like it is the same thing that you're experiencing, open a new issue and include a link to the original issue in the body of your new one.
16+
- Clone the source and run the project locally. You might be able to find the cause of the problem and fix things yourself. Most importantly, check if you can reproduce the problem in the latest version of the master branch.
17+
18+
When you are creating a bug report, please include as many details as possible.
19+
Fill out the issue template, the information it asks for helps us resolve issues faster.
20+
21+
### How do I submit a (good) bug report?
22+
23+
Bugs are tracked as [GitHub issues](https://github.com/json-api-dotnet/JsonApiDotNetCore/issues). Create an issue and provide the following information by filling in the template.
24+
Explain the problem and include additional details to help maintainers reproduce the problem:
25+
26+
- **Use a clear and descriptive title** for the issue to identify the problem.
27+
- **Describe the exact steps which reproduce the problem** in as many details as possible. When listing steps, don't just say what you did, but explain how you did it.
28+
- **Provide specific examples to demonstrate the steps.** Include links to files or GitHub projects, or copy/pasteable snippets, which you use in those examples. If you're providing snippets in the issue, use [Markdown code blocks](https://docs.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks).
29+
- **Describe the behavior you observed after following the steps** and point out what exactly is the problem with that behavior. Explain which behavior you expected to see instead and why.
30+
- **If you're reporting a crash**, include the full exception stack trace.
31+
32+
## Suggesting enhancements
33+
34+
This section guides you through submitting an enhancement suggestion, including completely new features and minor improvements to existing functionality. Following these guidelines helps maintainers and the community understand your suggestion and find related suggestions.
35+
36+
Before creating enhancement suggestions:
37+
- Check the [documentation](https://www.jsonapi.net/usage/resources/index.html) and [integration tests](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/master/test/JsonApiDotNetCoreExampleTests/IntegrationTests) for existing features. You might discover the enhancement is already available.
38+
- Perform a search to see if the feature has already been reported. If it has and the issue is still open, add a comment to the existing issue instead of opening a new one.
39+
40+
When you are creating an enhancement suggestion, please include as many details as possible. Fill in the template, including the steps that you imagine you would take if the feature you're requesting existed.
41+
42+
- **Use a clear and descriptive title** for the issue to identify the suggestion.
43+
- **Provide a step-by-step description of the suggested enhancement** in as many details as possible.
44+
- **Provide specific examples to demonstrate the usage.** Include copy/pasteable snippets which you use in those examples, as [Markdown code blocks](https://docs.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks).
45+
- **Describe the current behavior and explain which behavior you expected to see instead** and why.
46+
- **Explain why this enhancement would be useful** to most users and isn't something that can or should be implemented in your API project directly.
47+
- **Verify that your enhancement does not conflict** with the [JSON:API specification](https://jsonapi.org/).
48+
49+
## Your first code contribution
50+
51+
Unsure where to begin contributing? You can start by looking through these [beginner](https://github.com/json-api-dotnet/JsonApiDotNetCore/labels/good%20first%20issue) and [help-wanted](https://github.com/json-api-dotnet/JsonApiDotNetCore/labels/help%20wanted) issues.
52+
53+
## Pull requests
54+
55+
Please follow these steps to have your contribution considered by the maintainers:
56+
57+
- **The worst thing in the world is opening a PR that gets rejected** after you've put a lot of effort in it. So for any non-trivial changes, open an issue first to discuss your approach and ensure it fits the product vision.
58+
- Follow all instructions in the template. Don't forget to add tests and update documentation.
59+
- After you submit your pull request, verify that all status checks are passing. In release builds, all compiler warnings are treated as errors, so you should address them before push.
60+
61+
We use [CSharpGuidelines](https://csharpcodingguidelines.com/) as our coding standard (with a few minor exceptions). Coding style is validated during PR build, where we inject an extra settings layer that promotes various suggestions to warning level. This ensures a high-quality codebase without interfering too much when editing code.
62+
You can run the following [PowerShell scripts](https://github.com/PowerShell/PowerShell/releases) locally:
63+
- `inspectcode.ps1`: Scans the code for style violations and opens the result in your web browser.
64+
- `cleanupcode.ps1` Reformats the entire codebase to match with our configured style.
65+
66+
Code inspection violations can be addressed in several ways, depending on the situation:
67+
- Types that are reported to be never instantiated (because the IoC container creates them dynamically) should be decorated with `[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]`.
68+
- Exposed models that contain members never being read/written/assigned to should be decorated with `[UsedImplicitly(ImplicitUseTargetFlags.Members)]`.
69+
- Types that are part of our public API surface can be decorated with `[PublicAPI]`. This suppresses messages such as "type can be marked sealed/internal", "virtual member is never overridden", "member is never used" etc.
70+
- Incorrect violations can be [suppressed](https://www.jetbrains.com/help/resharper/Code_Analysis__Code_Inspections.html#ids-of-code-inspections) using a code comment.
71+
72+
In few cases, the automatic reformat decreases the readability of code. For example, when calling a Fluent API using chained method calls. This can be prevented using [formatter directives](https://www.jetbrains.com/help/resharper/Enforcing_Code_Formatting_Rules.html#configure):
73+
74+
```c#
75+
public sealed class AppDbContext : DbContext
76+
{
77+
protected override void OnModelCreating(ModelBuilder builder)
78+
{
79+
// @formatter:wrap_chained_method_calls chop_always
80+
81+
builder.Entity<MusicTrack>()
82+
.HasOne(musicTrack => musicTrack.Lyric)
83+
.WithOne(lyric => lyric.Track)
84+
.HasForeignKey<MusicTrack>();
85+
86+
// @formatter:wrap_chained_method_calls restore
87+
}
88+
}
89+
```
90+
91+
## Backporting and hotfixes (for maintainers)
92+
93+
- Checkout the version you want to apply the feature on top of and create a new branch to release the new version:
94+
```
95+
git checkout tags/v2.5.1 -b release/2.5.2
96+
```
97+
- Cherrypick the merge commit: `git cherry-pick {git commit SHA}`
98+
- Bump the package version in the csproj
99+
- Make any other compatibility, documentation or tooling related changes
100+
- Push the branch to origin and verify the build
101+
- Once the build is verified, create a GitHub release, tagging the release branch
102+
- Open a PR back to master with any other additions

.github/PULL_REQUEST_TEMPLATE.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Closes #{ISSUE_NUMBER}
44

55
#### QUALITY CHECKLIST
66
- [ ] Changes implemented in code
7+
- [ ] Complies with our [contributing guidelines](./.github/CONTRIBUTING.md)
78
- [ ] Adapted tests
89
- [ ] Documentation updated
910
- [ ] Created issue to update [Templates](https://github.com/json-api-dotnet/Templates/issues/new): {ISSUE_NUMBER}

Build.ps1

+50-1
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,64 @@ function CheckLastExitCode {
1717
}
1818
}
1919

20+
function RunInspectCode {
21+
$outputPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'jetbrains-inspectcode-results.xml')
22+
dotnet jb inspectcode JsonApiDotNetCore.sln --output="$outputPath" --properties:Configuration=Release --severity=WARNING --verbosity=WARN -dsl=GlobalAll -dsl=SolutionPersonal -dsl=ProjectPersonal
23+
CheckLastExitCode
24+
25+
[xml]$xml = Get-Content "$outputPath"
26+
if ($xml.report.Issues -and $xml.report.Issues.Project) {
27+
foreach ($project in $xml.report.Issues.Project) {
28+
if ($project.Issue.Count -gt 0) {
29+
$project.ForEach({
30+
Write-Output "`nProject $($project.Name)"
31+
$failed = $true
32+
33+
$_.Issue.ForEach({
34+
$issueType = $xml.report.IssueTypes.SelectSingleNode("IssueType[@Id='$($_.TypeId)']")
35+
$severity = $_.Severity ?? $issueType.Severity
36+
37+
Write-Output "[$severity] $($_.File):$($_.Line) $($_.Message)"
38+
})
39+
})
40+
}
41+
}
42+
43+
if ($failed) {
44+
throw "One or more projects failed code inspection.";
45+
}
46+
}
47+
}
48+
49+
function RunCleanupCode {
50+
# When running in cibuild for a pull request, this reformats only the files changed in the PR and fails if the reformat produces changes.
51+
52+
if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) {
53+
Write-Output "Running code cleanup in cibuild for pull request"
54+
55+
$sourceCommitHash = $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT
56+
$targetCommitHash = git rev-parse "$env:APPVEYOR_REPO_BRANCH"
57+
58+
Write-Output "Source commit hash = $sourceCommitHash"
59+
Write-Output "Target commit hash = $targetCommitHash"
60+
61+
dotnet regitlint -s JsonApiDotNetCore.sln --print-command --jb --profile --jb --profile='\"JADNC Full Cleanup\"' --jb --properties:Configuration=Release --jb --verbosity=WARN -f commits -a $sourceCommitHash -b $targetCommitHash --fail-on-diff --print-diff
62+
CheckLastExitCode
63+
}
64+
}
65+
2066
$revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
2167
$revision = "{0:D4}" -f [convert]::ToInt32($revision, 10)
2268

23-
dotnet restore
69+
dotnet tool restore
2470
CheckLastExitCode
2571

2672
dotnet build -c Release
2773
CheckLastExitCode
2874

75+
RunInspectCode
76+
RunCleanupCode
77+
2978
dotnet test -c Release --no-build
3079
CheckLastExitCode
3180

0 commit comments

Comments
 (0)