Skip to content

Commit

Permalink
feat: refactor Sdk, make compatible with .NET 8 and CPM
Browse files Browse the repository at this point in the history
  • Loading branch information
amis92 committed Jan 3, 2024
1 parent eb0ee08 commit 98a618a
Show file tree
Hide file tree
Showing 13 changed files with 600 additions and 212 deletions.
392 changes: 392 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.ionide/
App_Data/
packages/
package/
obj/
bin/
build/
Expand Down
51 changes: 19 additions & 32 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
<Project>

<PropertyGroup Label="Target Platforms" >
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Label="NuGet Properties">
<Authors>Allegro</Authors>
<Company>Allegro</Company>
<Copyright>© Allegro. All rights reserved.</Copyright>
<Description>$(MSBuildProjectName)</Description>
<Tags>Allegro</Tags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
</PropertyGroup>

<PropertyGroup Label="Project Settings" >
<Platforms>AnyCPU</Platforms>
<TargetPlatform>AnyCPU</TargetPlatform>
<ErrorReport>prompt</ErrorReport>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<EnableDefaultNoneItems>false</EnableDefaultNoneItems>
<NoWarn>$(NoWarn);NU5128;SA0001</NoWarn>
<RestoreUseStaticGraphEvaluation>true</RestoreUseStaticGraphEvaluation>
</PropertyGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)$(PackageReadmeFile)" Pack="true" PackagePath="" />
<None Include="$(MSBuildThisFileDirectory)$(PackageLicenseFile)" Pack="true" PackagePath="" />
<!-- <None Include="$(MSBuildThisFileDirectory)$(PackageIconPath)" Pack="true" PackagePath="" Visible="false" /> -->
<!-- <None Include="$(MSBuildThisFileDirectory)CHANGELOG.md" Pack="true" PackagePath=""/> -->
</ItemGroup>

<PropertyGroup Label="Build Output" >
<BaseOutputPath>$(MSBuildThisFileDirectory)\bin\$(MSBuildProjectName)</BaseOutputPath>
<BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<PackageOutputPath>$(MSBuildThisFileDirectory)\nuget</PackageOutputPath>
<DocumentationFileValue>$(MSBuildThisFileDirectory)\bin\$(MSBuildProjectName)\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName).xml</DocumentationFileValue>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup Label="NuGet Properties">
<Authors>Allegro</Authors>
<Company>Allegro</Company>
<Copyright>© Allegro. All rights reserved.</Copyright>
<Description>$(MSBuildProjectName)</Description>
<Tags>Allegro</Tags>
</PropertyGroup>

</Project>
12 changes: 1 addition & 11 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
<Project>
<Sdk Name="Microsoft.Build.CentralPackageVersions" />

<ItemGroup Condition="'$(IsPackable)' == 'true'">
<None Include="Sdk\**"
Pack="true"
PackagePath="Sdk\" />
<None Include="$(PackageIconPath)"
Pack="true"
PackagePath="\"
Visible="false" />
</ItemGroup>
<!-- Empty file prevents accidental inclusion of files from directories above -->
</Project>
72 changes: 32 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

The Allegro .NET SDK provides common and importable project settings, such as build properties, coding styles, analyzers configuration etc.

The SDK is versioned and published on nuget It can be imported into dotnet projects.
The SDK is versioned and published on nuget. It can be imported into dotnet projects.

## Using the SDK

The SDK is meant to be easily importable - only a few initial config lines are required to bring its benefits. The SDK also makes it possible to override any of its default settings.
The SDK is meant to be easily importable - only a few initial config lines are required to bring its benefits. The SDK also makes it possible to override any of its default settings. Version 2+ will work only with .NET SDK v8+.

## Importing

Expand All @@ -27,18 +27,19 @@ It's necessary to include `Allegro.DotnetSdk` with the desired version in `globa
"rollForward": "latestFeature"
},
"msbuild-sdks": {
"Allegro.DotnetSdk": "1.2.0"
"Allegro.DotnetSdk": "2.0.0"
}
}

```

### Directory.Build.props

The `Directory.Build.props` file should be updated in order to actually import the SDK:

```xml
<Project>
<Import Project="Sdk.props" Sdk="Allegro.DotnetSdk" />
<Sdk Name="Allegro.DotnetSdk" />
<PropertyGroup>
<TargetFramework>$(NetCoreVersions)</TargetFramework>
</PropertyGroup>
Expand All @@ -47,60 +48,48 @@ The `Directory.Build.props` file should be updated in order to actually import t
</Project>
```

### Directory.Build.targets

The `Directory.Build.targets` file should be updated in order to actually import the SDK:

```xml
<Project>
<Import Project="Sdk.targets" Sdk="Allegro.DotnetSdk"/>

<!-- other project-specific properties -->
</Project>
```

### Analyzers

The SDK imports and configures several external analyzers - StyleCop, AsyncFixer, Meziantou.

The existing analyzer package reference sections should be removed from `paket.dependencies` and `paket.references`. If they're not removed, the build may fail because of duplicated package references.
## Project settings

## Overriding
Most properties are only set in the SDK if not configured by the project.

The imported properties can be overridden per repo or per project.
To configure the SDK, the following properties can be added into `Directory.Build.props` or `.csproj` files:

## Project settings
- `AllegroDotnetSdkEnableXmlDocAdjustments` (default: true) - enable doc file generation, suppress missing-comments warning (1591) - completely in test projects, as errors otherwise (keep as warning).
- `TreatWarningsAsErrors` (default: true in CI and Rider IDE)
- `UseAllegroDotnetSdkDefaultAnalyzers` (default: true when no CPM) - reference and configure several external analyzers - StyleCop, AsyncFixer, Meziantou.
- `AllegroDotnetSdkEnableGlobalEditorConfig` (default: true) - add `editorconfig.global` analyzer configuration file.
- `AllegroDotnetSdkEnableImplicitUsingsAdjustments` (default: true) - add `System.Collections.Immutable` and remove `Microsoft.Extensions.Logging` and `System.Net.Http` implicit usings.
- `AllegroDotnetSdkEnableAsyncFixer` (default: `UseAllegroDotnetSdkDefaultAnalyzers`) - references AsyncFixer package.
- `AllegroDotnetSdkEnableMeziantou` (default: `UseAllegroDotnetSdkDefaultAnalyzers`) - references Meziantou.Analyzers package.
- `AllegroDotnetSdkEnableStyleCop` (default: `UseAllegroDotnetSdkDefaultAnalyzers`) - references StyleCop.Analyzers package.

In order to override or disable some of the imported components, the behavior changing properties can be added into `Directory.Build.props` or `.csproj` files.
Configure in `Directory.Build.props` - repo wide:

* `Directory.Build.props` - repo wide:
```xml
<Project>
<Import Project="Sdk.props" Sdk="Allegro.DotnetSdk" />
<PropertyGroup>
<TargetFramework>$(NetCoreVersions)</TargetFramework>
</PropertyGroup>

<Sdk Name="Allegro.DotnetSdk" />

<PropertyGroup>
<!-- The line below disables nullable reference types -->
<nullable>disable</nullable>
<!-- The line below disables the StyleCop config -->
<Nullable>disable</Nullable>
<!-- The line below disables the StyleCop -->
<AllegroDotnetSdkEnableStyleCop>false</AllegroDotnetSdkEnableStyleCop>
</PropertyGroup>

<!-- other repo-specific properties -->

</Project>
```

* `.csproj` - project settings:
Configure in `*.csproj` - project settings:

```xml
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- The line below disables implicit usings adjustments -->
<AllegroDotnetSdkEnableImplicitUsingsAdjustments>false</AllegroDotnetSdkEnableImplicitUsingsAdjustments>
</PropertyGroup>

<!-- other project-specific properties -->
</Project>
```

Expand All @@ -117,6 +106,7 @@ More about the analyzer and editor config files can be found in [the docs](https
### Rider

Make sure you have enabled:

- Preferences -> Editor -> Code Style -> Enable StyleCop support (Ruleset files)
- Preferences -> Editor -> Code Style -> Enable EditorConfig support
- Preferences -> Editor -> Inspection Settings -> Read settings from editorconfig, project settings and rule sets
Expand All @@ -125,9 +115,11 @@ Make sure you have enabled:

**Be aware!**
Only some analyzers's warnings can be addressed by auto-format or code cleanup. Some of the warnings are not covered by Rider, and its settings need to be adjusted.
For that, in editor.globalconfig is `# ReSharper properties` section with some already defined settings which align with analyzers. If you find some inconsistency and you find appropriate settings in Rider, which will fix it - please, contribute! :)
For that, in editor.globalconfig is `# ReSharper properties` section with some already defined settings which align with analyzers. If you find some inconsistency and you find appropriate settings in Rider, which will fix it - please, contribute! :)

## License
Copyright 2022 Allegro Group

Copyright Allegro Group

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Expand Down
20 changes: 14 additions & 6 deletions src/Allegro.DotnetSdk/Allegro.DotnetSdk.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.Build.NoTargets">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<BuildOutputTargetFolder>build\</BuildOutputTargetFolder>
<PackageVersion>2.0.0</PackageVersion>
<IsPackable>true</IsPackable>
<DevelopmentDependency>true</DevelopmentDependency>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageType>MSBuildSdk</PackageType>
<Description>Allegro Dotnet SDK</Description>
<PackageTags>MSBuild MSBuildSdk</PackageTags>
<DevelopmentDependency>true</DevelopmentDependency>
<PackageVersion>1.2.2</PackageVersion>
<PackageDescription>Allegro Dotnet SDK</PackageDescription>
<PackageTags>$(Tags) MSBuild MSBuildSdk</PackageTags>
<EnableDefaultNoneItems>false</EnableDefaultNoneItems>
<!-- NU5128: Add lib or ref assemblies for the net6.0 target framework -->
<NoWarn>$(NoWarn);NU5128</NoWarn>
</PropertyGroup>

<ItemGroup>
<None Include="Sdk/**" Pack="true" PackagePath="Sdk/" />
</ItemGroup>

</Project>
100 changes: 21 additions & 79 deletions src/Allegro.DotnetSdk/Sdk/Sdk.props
Original file line number Diff line number Diff line change
@@ -1,85 +1,27 @@
<Project>
<PropertyGroup Label="Target Platforms">
<NetCoreVersions>net6.0</NetCoreVersions>
<NetStandardVersions>netstandard2.0;netstandard2.1</NetStandardVersions>
<LibraryTargetFrameworks>$(NetCoreVersions);$(NetStandardVersions)</LibraryTargetFrameworks>
<ExecutableTargetFrameworks>$(NetCoreVersions)</ExecutableTargetFrameworks>
</PropertyGroup>

<PropertyGroup Label="Project Settings">
<LangVersion>latest</LangVersion>
<AnalysisMode>Recommended</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Documentation can be enforced per-project -->
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<!-- Some devs might want to turn off errors for warning for local development -->
<TreatWarningsAsErrors Condition=" '$(TreatWarningsAsErrors)' != 'false' ">true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup Label="ImplicitUsings additional includes">
<Using Include="System.Collections.Immutable" />
</ItemGroup>
<PropertyGroup>
<!--
Indicate to other targets that Allegro.DotnetSdk is being used.
-->
<UsingAllegroDotnetSdk>true</UsingAllegroDotnetSdk>

<PropertyGroup Label="Build Output">
<RepoRoot Condition="'$(RepoRoot)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildProjectDirectory)', 'global.json'))</RepoRoot>
<BaseOutputPath>$(RepoRoot)\bin\$(MSBuildProjectName)</BaseOutputPath>
<BaseIntermediateOutputPath>$(RepoRoot)\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<PackageOutputPath>$(RepoRoot)\nuget</PackageOutputPath>
</PropertyGroup>

<ItemGroup Label="StyleCop" Condition="'$(AllegroDotnetSdkEnableStyleCop)' != false">
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json"/>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435"> <!-- need to use beta version to get the latest fixes -->
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<!--
Import our custom props just after Directory.Build.props is imported, but before Microsoft.NET.Sdk.props
-->
<CustomAfterDirectoryBuildProps>$(CustomAfterDirectoryBuildProps);$(MSBuildThisFileDirectory)UseAllegroDotnetSdkDefaults.props</CustomAfterDirectoryBuildProps>
<!--
Import our custom targets after Directory.Build.targets is imported
-->
<CustomBeforeDirectoryBuildTargets>$(CustomBeforeDirectoryBuildTargets);$(MSBuildThisFileDirectory)UseAllegroDotnetSdkDefaults.targets</CustomBeforeDirectoryBuildTargets>
</PropertyGroup>

<ItemGroup Label="AsyncFixer" Condition="'$(AllegroDotnetSdkEnableAsyncFixer)' != false">
<PackageReference Include="AsyncFixer" Version="1.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Label="Meziantou" Condition="'$(AllegroDotnetSdkEnableMeziantou)' != false">
<PackageReference Include="Meziantou.Analyzer" Version="1.0.734">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<!--
LEGACY: remove after no more projects reference NetCoreVersions property:
https://sourcegraph.allegrogroup.com/search?q=context:global+repo:AllegroTechies+%22%28NetCoreVersions%29%22&patternType=standard&sm=1&groupBy=repo
-->
<PropertyGroup Label="Target Platforms">
<NetCoreVersions>net6.0</NetCoreVersions>
</PropertyGroup>

<ItemGroup Label="NRT configuration" Condition=" '$(Configuration)' == 'Debug' AND '$(Nullable)' == 'enable' AND '$(AllegroDotnetSdkEnableSmartAnalyzers)' != false ">
<PackageReference Include="SmartAnalyzers.CSharpExtensions.Annotations" Version="4.2.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; compile; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<AssemblyAttribute Include="SmartAnalyzers.CSharpExtensions.Annotations.InitRequiredForNotNullAttribute" />
</ItemGroup>

<ItemGroup Label="Global editor config" Condition="'$(AllegroDotnetSdkDisableGlobalEditorConfig)' != true">
<GlobalAnalyzerConfigFiles Include="$(MSBuildThisFileDirectory)editor.globalconfig" Link="editor.globalconfig" />
</ItemGroup>

<PropertyGroup Label="Properties dedicated for test projects" Condition="$(MSBuildProjectDirectory.Contains(`test`))">
<!-- Exclude analyzer warnings in test projects -->
<!-- CA1707: Identifiers should not contain underscores -->
<NoWarn>$(NoWarn);CA1707</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" !$(MSBuildProjectDirectory.Contains(`test`)) ">
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- SourceLink configuration -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Embed PDBs in .nupkg because Azure Artifacts doesn't support .snupkg yet -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>
$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb
</AllowedOutputExtensionsInPackageBuildOutputFolder>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

</Project>
22 changes: 7 additions & 15 deletions src/Allegro.DotnetSdk/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
<Project>
<!-- Empty file prevents accidental inclusion of files from directories above -->
<Project InitialTargets="AllegroDotnetSdkValidation">

<Target Name="AllegroDotnetSdkValidation">
<Error
Text="Allegro.DotnetSdk requires .NET SDK v8.0.100+ to be used, but $(NETCoreSdkVersion) was found."
Condition="$([MSBuild]::VersionLessThan('$(NETCoreSdkVersion)', '8.0'))" />
</Target>

<!--
According to https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2022#import-order,
Directory.Build.props is imported very early. If the global using System.Net.Http
is added by some file that is imported later, then the Remove in Directory.Build.props
cannot affect the item that does not exist yet. It'd work better
in Directory.Build.targets.
More: https://stackoverflow.com/questions/52866794/why-doesnt-directory-build-props-work-when-building-a-solution-using-visual-stu
-->
<ItemGroup Label="ImplicitUsings additional removes">
<Using Remove="Microsoft.Extensions.Logging" />
<Using Remove="System.Net.Http" />
</ItemGroup>
</Project>
Loading

0 comments on commit 98a618a

Please sign in to comment.