Skip to content

Commit

Permalink
feat: Automation to create a valid copyright, additional analyzer NED… (
Browse files Browse the repository at this point in the history
#20)

* feat: Automation to create a valid copyright, additional analyzer NED0009

* style: Reformatted code

* fix: CodeFactor Apply fixes

---------

Co-authored-by: codefactor-io <support@codefactor.io>
  • Loading branch information
samtrion and code-factor authored Nov 20, 2024
1 parent 09f1316 commit 1b6acd3
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 12 deletions.
20 changes: 20 additions & 0 deletions docs/usage/ned0009.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Project configuration information NED0009

The project file is missing or has an incomplete configuration for the `<CopyrightYearStart />` tag. This tag is essential as it indicates the year when the copyright for the project began. An incomplete or absent `<CopyrightYearStart />` can lead to legal ambiguities regarding ownership and copyright claims.

To resolve this issue, please follow these steps:

- Open the Project File: Locate and open your project file (e.g., .csproj for C# projects).

- Add or Update the `<CopyrightYearStart />` Tag: Ensure that the `<CopyrightYearStart />` tag is present and correctly formatted.

It should look like this:
```xml
<CopyrightYearStart>2021</CopyrightYearStart>
```

- Validate the Configuration: After making changes, validate the project file to ensure there are no syntax errors.

- Rebuild the Project: Once the configuration is corrected, rebuild your project to confirm that the issue is resolved.

If you continue to experience problems, consider checking the documentation for your specific project type or reaching out to your development team for further assistance. Proper configuration of the `<CopyrightYearStart />` tag is essential for specifying the year when the copyright for the project began.
3 changes: 2 additions & 1 deletion src/NetEvolve.Defaults.Analyzer/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ NED0004 | Usage | Warning | The project file is missing or has an incomplete con
NED0005 | Usage | Warning | The project file is missing or has an incomplete configuration for the <PackageProjectUrl /> tag. This tag is vital as it specifies the URL for the project’s homepage or repository, allowing users to easily find more information, documentation, or source code. An incomplete or absent <PackageProjectUrl /> can hinder user engagement and limit the project’s visibility.
NED0006 | Usage | Warning | The project file is missing or has an incomplete configuration for the <RepositoryUrl /> tag. This tag is crucial as it specifies the URL of the project’s source code repository, enabling users and contributors to access the codebase, report issues, and contribute to development. An incomplete or absent <RepositoryUrl /> can limit collaboration and hinder user engagement.
NED0007 | Usage | Warning | The project file is missing or has an incomplete configuration for the <Authors /> tag. This tag is essential as it specifies the authors of the project, providing credit and context for contributions. An incomplete or absent <Authors /> tag can lead to confusion about authorship and may affect collaboration and recognition within the community.
NED0008 | Usage | Warning | The project file is missing or has an incomplete configuration for the <Company /> tag. This tag is important as it specifies the name of the company or organization responsible for the project. An incomplete or absent <Company /> tag can lead to confusion regarding ownership and may affect branding and recognition in the community.
NED0008 | Usage | Warning | The project file is missing or has an incomplete configuration for the <Company /> tag. This tag is important as it specifies the name of the company or organization responsible for the project. An incomplete or absent <Company /> tag can lead to confusion regarding ownership and may affect branding and recognition in the community.
NED0009 | Usage | Info | The project file is missing or has an incomplete configuration for the <CopyrightYearStart /> tag. This tag is essential as it indicates the year when the copyright for the project began. An incomplete or absent <CopyrightYearStart /> can lead to legal ambiguities regarding ownership and copyright claims.
4 changes: 4 additions & 0 deletions src/NetEvolve.Defaults.Analyzer/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace NetEvolve.Defaults.Analyzer.Diagnostics;
namespace NetEvolve.Defaults.Analyzer.Diagnostics;

using System;
using System.Collections.Immutable;
Expand Down Expand Up @@ -43,6 +43,11 @@ internal sealed class IsPackableProjectDiagnosticAnalyzer : DiagnosticAnalyzer
"NED0008",
customTags: WellKnownDiagnosticTags.CompilationEnd
);
internal static readonly DiagnosticDescriptor _ruleNED0009 = Helper.CreateUsageDescriptor(
"NED0009",
DiagnosticSeverity.Info,
customTags: WellKnownDiagnosticTags.CompilationEnd
);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
[
Expand All @@ -54,6 +59,7 @@ internal sealed class IsPackableProjectDiagnosticAnalyzer : DiagnosticAnalyzer
_ruleNED0006,
_ruleNED0007,
_ruleNED0008,
_ruleNED0009,
];

public override void Initialize(AnalysisContext context)
Expand Down Expand Up @@ -89,27 +95,43 @@ public override void Initialize(AnalysisContext context)
AnalyzeIsKeySet(ctx, options, _ruleNED0006, "build_property.repositoryurl");
AnalyzeIsKeySet(ctx, options, _ruleNED0007, "build_property.authors");
AnalyzeIsKeySet(ctx, options, _ruleNED0008, "build_property.company");
AnalyzeIsKeySet(
ctx,
options,
_ruleNED0009,
"build_property.copyrightyearstart",
propertyValue =>
int.TryParse(propertyValue, out var year) && year >= 1900 && year <= 9999
);
});
}

private static void AnalyzeIsKeySet(
CompilationAnalysisContext context,
AnalyzerConfigOptions options,
DiagnosticDescriptor descriptor,
string key
string key,
Func<string, bool>? isValidValue = null
)
{
if (KeyHasNoValidValue(options, key))
if (ValidateKey(options, key, isValidValue))
{
context.ReportDiagnostic(Diagnostic.Create(descriptor, null));
}
}

private static bool KeyHasNoValidValue(AnalyzerConfigOptions options, string key)
private static bool ValidateKey(
AnalyzerConfigOptions options,
string key,
Func<string, bool>? isValidValue
)
{
var isConfigured = options.TryGetValue(key, out var configuredValue);

return !isConfigured || string.IsNullOrWhiteSpace(configuredValue);
return !isConfigured
|| string.IsNullOrWhiteSpace(configuredValue)
|| isValidValue is null
|| !isValidValue.Invoke(configuredValue!);
}

private static bool IsPackable(AnalyzerConfigOptions options) =>
Expand Down
27 changes: 27 additions & 0 deletions src/NetEvolve.Defaults.Analyzer/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/NetEvolve.Defaults.Analyzer/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,13 @@
<data name="NED0008Title" xml:space="preserve">
<value>Configuration Error: &lt;Company /&gt; Tag Missing or Incomplete</value>
</data>
<data name="NED0009Title" xml:space="preserve">
<value>Configuration Information: &lt;CopyrightYearStart /&gt; Tag Missing or Incomplete</value>
</data>
<data name="NED0009Message" xml:space="preserve">
<value>The project file is missing or has an incomplete configuration for the &lt;CopyrightYearStart /&gt; tag.</value>
</data>
<data name="NED0009Description" xml:space="preserve">
<value>The project file is missing or has an incomplete configuration for the &lt;CopyrightYearStart /&gt; tag. This tag is essential as it indicates the year when the copyright for the project began. An incomplete or absent &lt;CopyrightYearStart /&gt; can lead to legal ambiguities regarding ownership and copyright claims.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>

<ItemGroup>
<!-- Define the properties that are visible to the compiler. In this case general properties that are used in the project -->
<CompilerVisibleProperty Include="IsPackable" />
<CompilerVisibleProperty Include="PackageId" />
<CompilerVisibleProperty Include="Title" />
Expand All @@ -10,6 +11,9 @@
<CompilerVisibleProperty Include="RepositoryUrl" />
<CompilerVisibleProperty Include="Company" />
<CompilerVisibleProperty Include="Authors" />

<!-- NetEvolve specific properties -->
<CompilerVisibleProperty Include="CopyrightYearStart" />
</ItemGroup>

</Project>
8 changes: 2 additions & 6 deletions src/NetEvolve.Defaults/build/NetEvolve.Defaults.targets
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<Project>

<PropertyGroup Label="NuGet Audit">
<NuGetAudit Condition="'$(NuGetAudit)' == ''">true</NuGetAudit>
<NuGetAuditMode Condition="'$(NuGetAuditMode)' == ''">all</NuGetAuditMode>
<NuGetAuditLevel Condition="'$(NuGetAuditLevel)' == ''">low</NuGetAuditLevel>
<WarningsAsErrors Condition="$(ContinuousIntegrationBuild) == 'true' OR '$(Configuration)' == 'Release'">(WarningsAsErrors);NU1900;NU1901;NU1902;NU1903;NU1904</WarningsAsErrors>
</PropertyGroup>
<Import Project="$(MSBuildThisFileDirectory)SupportCopyright.targets" Condition="'$(DisableSupportCopyrightTargets)' != 'true'" />
<Import Project="$(MSBuildThisFileDirectory)SupportNuGetAudit.targets" Condition="'$(DisableSupportNuGetAuditTargets)' != 'true'" />

</Project>
12 changes: 12 additions & 0 deletions src/NetEvolve.Defaults/build/SupportCopyright.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project TreatAsLocalProperty="_CopyrightYearStart;_CopyrightInternal">

<PropertyGroup Label="CopyrightAutomation" Condition="'$(Copyright)' == ''">
<_CopyrightYearStart Condition="'$(CopyrightYearStart)' == ''">$([System.DateTime]::UtcNow.Year)</_CopyrightYearStart>
<_CopyrightYearStart Condition="'$(CopyrightYearStart)' != ''">$(CopyrightYearStart)</_CopyrightYearStart>
<_CopyrightInternal>$([System.DateTime]::UtcNow.Year)</_CopyrightInternal>

<Copyright>Copyright @ $(Company) $(_CopyrightYearStart)</Copyright>
<Copyright Condition="'$(_CopyrightInternal)' > '$(_CopyrightYearStart)'">$(Copyright) - $(_CopyrightInternal)</Copyright>
</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions src/NetEvolve.Defaults/build/SupportNuGetAudit.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>

<PropertyGroup Label="NuGet Audit">
<NuGetAudit Condition="'$(NuGetAudit)' == ''">true</NuGetAudit>
<NuGetAuditMode Condition="'$(NuGetAuditMode)' == ''">all</NuGetAuditMode>
<NuGetAuditLevel Condition="'$(NuGetAuditLevel)' == ''">low</NuGetAuditLevel>
<WarningsAsErrors Condition="$(ContinuousIntegrationBuild) == 'true' OR '$(Configuration)' == 'Release'">(WarningsAsErrors);NU1900;NU1901;NU1902;NU1903;NU1904</WarningsAsErrors>
</PropertyGroup>

</Project>

0 comments on commit 1b6acd3

Please sign in to comment.