Skip to content

Commit 610f8a7

Browse files
authored
Merge pull request #51 from cake-contrib/feature/GH-48
(GH-48) test for required/suggested targetFramework-
2 parents b45372e + 8521f14 commit 610f8a7

File tree

13 files changed

+731
-53
lines changed

13 files changed

+731
-53
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
Order: 4
3+
Title: Target Frameworks
4+
---
5+
6+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
7+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
8+
## Table of Contents
9+
10+
- [Goals](#goals)
11+
- [Required / Suggested versions](#required--suggested-versions)
12+
- [Related rules](#related-rules)
13+
- [Usage](#usage)
14+
- [Settings](#settings)
15+
- [Opt-Out](#opt-out)
16+
17+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
18+
19+
## Goals
20+
21+
As .NET Framework < 4.7.2 has issues with running .NET Standard assemblies, and Cake itself can run on .NET Framework 4.6.1 it is suggested to multi-target addins to `netstandard2.0` and `net461` to have the maximum compatibility.
22+
23+
### Required / Suggested versions
24+
25+
Depending on the referenced `Cake.Core`-version different target versions are required and/or suggested.
26+
Missing a required target version will raise [CCG0007](../rules/ccg0007) as an error
27+
while missing a suggested target version will raise [CCG0007](../rules/ccg0007) as a warning.
28+
29+
* Cake.Core <= 0.33.0
30+
* Required: `netstandard2.0`
31+
* Suggested: `net461`
32+
* alternative: `net46`
33+
34+
## Related rules
35+
36+
* [CCG0007](../rules/ccg0007)
37+
38+
## Usage
39+
40+
Using this package automatically enables this guideline.
41+
42+
## Settings
43+
44+
### Opt-Out
45+
46+
It it possible to opt-out of the check for target framework using the following setting:
47+
48+
(*Keep in mind, though that it is not recommended to opt-out of this feature*)
49+
50+
```xml
51+
<ItemGroup>
52+
<CakeContribGuidelinesOmitTargetFramework Include="netstandard2.0" />
53+
<CakeContribGuidelinesOmitTargetFramework Include="net461" />
54+
</ItemGroup>
55+
```

docs/input/rules/ccg0007.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
Order: 7
3+
Title: CCG0007
4+
Description: Missing recommended target
5+
---
6+
7+
> Missing required target: netstandard2.0
8+
9+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
10+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
11+
## Table of Contents
12+
13+
- [Cause](#cause)
14+
- [Description](#description)
15+
- [How to fix violations](#how-to-fix-violations)
16+
- [Related guidelines](#related-guidelines)
17+
18+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
19+
20+
## Cause
21+
22+
This warning is raised, when the addin is not targeted to a recommended target version.
23+
Also, This could be raised as an error, if a required target version is not set.
24+
25+
## Description
26+
27+
Addins should be multi-targeted to `netstandard2.0` and `net461` to have the maximum compatibility.
28+
29+
## How to fix violations
30+
31+
Add the recommended target(s) to the project:
32+
33+
```xml
34+
<PropertyGroup>
35+
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
36+
</PropertyGroup>
37+
```
38+
39+
(Or opt-out of this rule, by setting `CakeContribGuidelinesOmitTargetFramework`)
40+
41+
## Related guidelines
42+
43+
* [Target Frameworks](../guidelines/TargetFramework)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Guidelines/build/CakeContrib.Guidelines.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
<Import Project="$(MSBuildThisFileDirectory)PrivateAssets.targets" />
1010
<Import Project="$(MSBuildThisFileDirectory)RequiredReferences.targets" />
1111
<Import Project="$(MSBuildThisFileDirectory)RequiredFiles.targets" />
12+
<Import Project="$(MSBuildThisFileDirectory)TargetFrameworkVersions.targets" />
1213
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<UsingTask
5+
TaskName="$(CakeContribGuidelinesCustomTasksNamespace).TargetFrameworkVersions"
6+
AssemblyFile="$(CakeContribGuidelinesCustomTasksAssembly)" />
7+
8+
<Target
9+
Name="_CheckTargetFrameworkVersions"
10+
AfterTargets="BeforeBuild"
11+
BeforeTargets="CoreBuild">
12+
<!-- All other rules have some "configuration" here, this rules required/suggested targets are hard-coded in the task. Sadly. -->
13+
14+
<TargetFrameworkVersions
15+
References="@(Reference)"
16+
TargetFramework="$(TargetFramework)"
17+
TargetFrameworks="$(TargetFrameworks)"
18+
Omitted="@(CakeContribGuidelinesOmitTargetFramework)"
19+
ProjectFile="$(MSBuildProjectFullPath)" />
20+
</Target>
21+
</Project>

src/Tasks.IntegrationTests/E2eTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.IO;
3+
34
using CakeContrib.Guidelines.Tasks.IntegrationTests.Fixtures;
5+
46
using FluentAssertions;
7+
58
using Xunit;
69
using Xunit.Abstractions;
710

@@ -191,5 +194,35 @@ public void Missing_file_editorconfig_results_in_CCG0006_warning()
191194
result.WarningLines.Should().Contain(l => l.IndexOf("CCG0006", StringComparison.Ordinal) > -1);
192195
result.WarningLines.Should().Contain(l => l.IndexOf(".editorconfig", StringComparison.Ordinal) > -1);
193196
}
197+
198+
[Fact]
199+
public void Missing_Required_Target_results_in_CCG0007_error()
200+
{
201+
// given
202+
fixture.WithTargetFrameworks("net47");
203+
204+
// when
205+
var result = fixture.Run();
206+
207+
// then
208+
result.IsErrorExitCode.Should().BeTrue();
209+
result.ErrorLines.Should().Contain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
210+
result.ErrorLines.Should().Contain(l => l.IndexOf("netstandard2.0", StringComparison.Ordinal) > -1);
211+
}
212+
213+
[Fact]
214+
public void Missing_Suggested_Target_results_in_CCG0007_warning()
215+
{
216+
// given
217+
fixture.WithTargetFrameworks("netstandard2.0");
218+
219+
// when
220+
var result = fixture.Run();
221+
222+
// then
223+
result.IsErrorExitCode.Should().BeFalse();
224+
result.WarningLines.Should().Contain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
225+
result.WarningLines.Should().Contain(l => l.IndexOf("net461", StringComparison.Ordinal) > -1);
226+
}
194227
}
195228
}

src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
67
using Xunit.Abstractions;
78

89
namespace CakeContrib.Guidelines.Tasks.IntegrationTests.Fixtures
@@ -17,7 +18,8 @@ public class E2eTestFixture : IDisposable
1718
private bool hasStylecopJson = true;
1819
private bool hasStylecopReference = true;
1920
private bool hasEditorConfig = true;
20-
private List<string> customContent = new List<string>();
21+
private readonly List<string> customContent = new List<string>();
22+
private string targetFrameworks = "netstandard2.0;net461";
2123

2224
public E2eTestFixture(string tempFolder, ITestOutputHelper logger)
2325
{
@@ -40,7 +42,7 @@ private string WriteProject()
4042
<Import Project=""{0}"" />
4143
4244
<PropertyGroup>
43-
<TargetFramework>netstandard2.0</TargetFramework>
45+
<TargetFrameworks>{5}</TargetFrameworks>
4446
{2}
4547
</PropertyGroup>
4648
@@ -94,7 +96,8 @@ private string WriteProject()
9496
targets.Item2,
9597
string.Join(Environment.NewLine, properties),
9698
string.Join(Environment.NewLine, items),
97-
string.Join(Environment.NewLine, customContent)));
99+
string.Join(Environment.NewLine, customContent),
100+
targetFrameworks));
98101

99102
return csproj;
100103
}
@@ -134,6 +137,11 @@ internal void WithoutFileEditorconfig()
134137
hasEditorConfig = false;
135138
}
136139

140+
internal void WithTargetFrameworks(string targetFrameworks)
141+
{
142+
this.targetFrameworks = targetFrameworks;
143+
}
144+
137145
private Tuple<string, string> GetTargetsToImport()
138146
{
139147
var codeBase = typeof(E2eTestFixture).Assembly.CodeBase;
Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;</TargetFrameworks>
5-
<TargetFrameworks Condition="'$(OS)'!='Unix'">$(TargetFrameworks);net472</TargetFrameworks>
6-
<AssemblyName>CakeContrib.Guidelines.Tasks.IntegrationTests</AssemblyName>
7-
<RootNamespace>CakeContrib.Guidelines.Tasks.IntegrationTests</RootNamespace>
8-
<IsPackable>false</IsPackable>
9-
</PropertyGroup>
10-
11-
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
12-
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
13-
</PropertyGroup>
14-
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
15-
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
16-
</PropertyGroup>
17-
18-
<ItemGroup>
19-
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
20-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21-
<PrivateAssets>all</PrivateAssets>
22-
</PackageReference>
23-
<PackageReference Include="FluentAssertions" Version="5.10.3" />
24-
<PackageReference Include="FluentAssertions.Analyzers" Version="0.11.4" />
25-
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
26-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
27-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
28-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29-
<PrivateAssets>all</PrivateAssets>
30-
</PackageReference>
31-
<PackageReference Include="Moq" Version="4.14.7" />
32-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
33-
<PackageReference Include="Text.Analyzers" Version="2.6.4">
34-
<PrivateAssets>all</PrivateAssets>
35-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
36-
</PackageReference>
37-
<PackageReference Include="xunit" Version="2.4.1" />
38-
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
39-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
40-
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
41-
<PrivateAssets>all</PrivateAssets>
42-
</PackageReference>
43-
<PackageReference Include="Microsoft.Build" Version="16.7.0" PrivateAssets="All" />
44-
<PackageReference Include="Microsoft.Build.Framework" Version="16.7.0" PrivateAssets="All" />
45-
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.7.0" PrivateAssets="All" />
46-
</ItemGroup>
47-
48-
<ItemGroup>
49-
<ProjectReference Include="..\Tasks\Tasks.csproj" />
50-
</ItemGroup>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;</TargetFrameworks>
5+
<TargetFrameworks Condition="'$(OS)'!='Unix'">$(TargetFrameworks);net472</TargetFrameworks>
6+
<AssemblyName>CakeContrib.Guidelines.Tasks.IntegrationTests</AssemblyName>
7+
<RootNamespace>CakeContrib.Guidelines.Tasks.IntegrationTests</RootNamespace>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
12+
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
13+
</PropertyGroup>
14+
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
15+
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
<PackageReference Include="FluentAssertions" Version="5.10.3" />
24+
<PackageReference Include="FluentAssertions.Analyzers" Version="0.11.4" />
25+
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
26+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
27+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
28+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29+
<PrivateAssets>all</PrivateAssets>
30+
</PackageReference>
31+
<PackageReference Include="Moq" Version="4.14.7" />
32+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
33+
<PackageReference Include="Text.Analyzers" Version="2.6.4">
34+
<PrivateAssets>all</PrivateAssets>
35+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
36+
</PackageReference>
37+
<PackageReference Include="xunit" Version="2.4.1" />
38+
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
39+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
40+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
41+
<PrivateAssets>all</PrivateAssets>
42+
</PackageReference>
43+
<PackageReference Include="Microsoft.Build" Version="16.7.0" PrivateAssets="All" />
44+
<PackageReference Include="Microsoft.Build.Framework" Version="16.7.0" PrivateAssets="All" />
45+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.7.0" PrivateAssets="All" />
46+
</ItemGroup>
47+
48+
<ItemGroup>
49+
<ProjectReference Include="..\Tasks\Tasks.csproj" />
50+
</ItemGroup>
5151
</Project>

0 commit comments

Comments
 (0)