Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 0c2c8bb

Browse files
authored
feat: Sdk and Tool usage validation (#209)
* feat: Plugin.Sdk inserts check into Plugin targets This checks whether CodeGeneration.Roslyn.Tool targets are imported, and raises a warning when they're not. * feat: Inform/warn on Tool used with older packages
1 parent c5fe1b7 commit 0c2c8bb

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

src/CodeGeneration.Roslyn.Plugin.Sdk/build/BuildPluginPackage.targets

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@
5959
</Project>
6060
]]>
6161
</PackagePropsContent>
62+
<PackageTargetsContent>
63+
<![CDATA[
64+
<?xml version="1.0" encoding="utf-8" ?>
65+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
66+
<Target Name="CheckCgrToolUsed" BeforeTargets="Build">
67+
<Warning Condition=" '%24(UsingCodeGenerationRoslynToolTargets)' != 'true' " Text="CodeGeneration.Roslyn.Tool build targets weren't detected. CG.R Plugins (generators) won't be run without importing targets from the CodeGeneration.Roslyn.Tool package (v$(PackageVersion))." />
68+
</Target>
69+
</Project>
70+
]]>
71+
</PackageTargetsContent>
6272
<PackagePropsPath>$(IntermediateOutputPath)$(PackageId).props</PackagePropsPath>
73+
<PackageTargetsPath>$(IntermediateOutputPath)$(PackageId).targets</PackageTargetsPath>
6374
</PropertyGroup>
6475
<WriteLinesToFile File="$(PackagePropsPath)" Lines="$(PackagePropsContent)" Overwrite="true" />
76+
<WriteLinesToFile File="$(PackageTargetsPath)" Lines="$(PackageTargetsContent)" Overwrite="true" />
6577
<ItemGroup>
66-
<TfmSpecificPackageFile Include="$(PackagePropsPath)" Pack="true" PackagePath="build/" BuildAction="None" />
67-
<FileWrites Include="$(PackagePropsPath)" />
78+
<TfmSpecificPackageFile Include="$(PackagePropsPath);$(PackageTargetsPath)" Pack="true" PackagePath="build/" BuildAction="None" />
79+
<FileWrites Include="$(PackagePropsPath);$(PackageTargetsPath)" />
6880
</ItemGroup>
6981
</Target>
7082

src/CodeGeneration.Roslyn.PluginMetapackage.Sdk/build/CodeGeneration.Roslyn.PluginMetapackage.Sdk.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
Condition=" '%(Identity)' == 'CodeGeneration.Roslyn.Tool' " />
102102
</ItemGroup>
103103
<Warning
104-
Condition=" @(_CgrToolRef->WithMetadataValue('Version', '$(CodeGenerationRoslynPluginMetapackageSdkVersion)')->WithMetadataValue('PrivateAssets', 'none')->Count()) == 0 "
104+
Condition=" '@(_CgrToolRef->WithMetadataValue('Version', '$(CodeGenerationRoslynPluginMetapackageSdkVersion)')->WithMetadataValue('PrivateAssets', 'none'))' == '' "
105105
Code="CGR2001"
106106
Text="$(_CheckCgrToolReferenceExistsWarning.Trim())" />
107107
</Target>

src/CodeGeneration.Roslyn.Tool/build/CodeGeneration.Roslyn.Tool.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<PropertyGroup>
5+
<!--
6+
Announce this Tool's build artifacts are being used.
7+
Plugins can validate whether the Tool is available to use on this property.
8+
-->
9+
<UsingCodeGenerationRoslynToolTargets>true</UsingCodeGenerationRoslynToolTargets>
10+
</PropertyGroup>
311

412
<ItemDefinitionGroup>
513
<Compile>

src/CodeGeneration.Roslyn.Tool/build/CodeGeneration.Roslyn.Tool.targets

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@
4040
Code="CGR1002"
4141
Text="Using GeneratorAssemblySearchPaths is deprecated, please use CodeGenerationRoslynPlugin ItemGroup. See $(CgrUrl) for more info. Suppress this warning by adding CGR1002 to MSBuildWarningsAsMessages PropertyGroup."
4242
Condition=" '@(GeneratorAssemblySearchPaths)' != '' " />
43+
<!-- Warning for when the BuildTime package is referenced. -->
44+
<Warning
45+
Code="CGR1003"
46+
Condition=" '@(PackageReference->AnyHaveMetadataValue('Identity', 'CodeGeneration.Roslyn.BuildTime'))' == 'true' "
47+
Text="Using both CodeGeneration.Roslyn.Tool and CodeGeneration.Roslyn.BuildTime is unsupported. Please either remove reference to the BuildTime package, or add an explicit reference and mark it as ExcludeAssets=&quot;all&quot;. See $(CgrUrl) for more info." />
48+
<!-- Inform the user there are no generators so the Tool won't be run. -->
49+
<Message
50+
Condition=" '@(CodeGenerationRoslynPlugin)' != '' "
51+
Text="There are no registered Plugins (generators) for the CodeGeneration.Roslyn.Tool to run. The tool won't run."
52+
Importance="high" />
53+
<!-- Inform the user DotNetCliToolReference is deprecated and unnecessary since v0.7. -->
54+
<Message
55+
Condition=" '@(DotNetCliToolReference->AnyHaveMetadataValue('Identity', 'dotnet-codegen'))' == 'true' "
56+
Text="DotNetCliToolReference of 'dotnet-codegen' is deprecated and can be removed since CodeGeneration.Roslyn v0.7."
57+
Importance="high" />
4358
<PropertyGroup>
4459
<CodeGenerationRoslynToolPath
4560
Condition="'$(GenerateCodeFromAttributesToolPathOverride)' != ''">$(GenerateCodeFromAttributesToolPathOverride)</CodeGenerationRoslynToolPath>
@@ -101,10 +116,13 @@
101116
and just read the existing file in ReadGenerateCodeFromAttributesResults target.
102117
-->
103118
<Target Name="GenerateCodeFromAttributesCore"
104-
Condition=" '@(_CodeGenToolInputs_Compile)' != '' AND '$(_CodeGenToolVersionExitCode)' == '0' "
119+
Condition=" '@(_CodeGenToolInputs_Compile)' != '' AND '$(_CodeGenToolVersionExitCode)' == '0' AND '@(CodeGenerationRoslynPlugin)' != '' "
105120
Inputs="$(_CodeGenToolResponseFileFullPath);@(CodeGenerationRoslynPlugin);@(_CodeGenToolInputs_Compile)"
106121
Outputs="$(_CodeGenToolGeneratedFileListFullPath)">
107-
<!--Run the tool and raise an error when failed-->
122+
<!--
123+
Run the tool and raise an error when failed:
124+
dotnet "path/to/CodeGeneration.Roslyn.Tool" "@path/to/responseFile.rsp"
125+
-->
108126
<Exec
109127
Command="dotnet &quot;$(CodeGenerationRoslynToolPath)&quot; &quot;%40$(_CodeGenToolResponseFileFullPath)&quot;"
110128
StandardOutputImportance="normal" />

0 commit comments

Comments
 (0)