From 5746907b3fc84565cb74005bce632f5e177aff5e Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Fri, 17 May 2024 15:00:00 -0300 Subject: [PATCH] Add smart library packing sample --- .../LibAnalyzerTasks/Analyzer/Analyzer.csproj | 16 ++++++ .../Analyzer/Properties/launchSettings.json | 8 +++ .../Analyzer/SampleGenerator.cs | 18 +++++++ .../LibAnalyzerTasks/Directory.Build.props | 22 ++++++++ samples/LibAnalyzerTasks/LibAnalyzerTasks.sln | 51 +++++++++++++++++++ .../LibAnalyzerTasks/Library/Library.csproj | 18 +++++++ samples/LibAnalyzerTasks/Library/MyLibrary.cs | 6 +++ samples/LibAnalyzerTasks/Library/readme.md | 8 +++ .../Tasks/LibAnalyzerTasks.props | 8 +++ .../Tasks/LibAnalyzerTasks.targets | 13 +++++ samples/LibAnalyzerTasks/Tasks/Tasks.csproj | 13 +++++ samples/LibAnalyzerTasks/Tests/Sample.cs | 14 +++++ samples/LibAnalyzerTasks/Tests/Tests.csproj | 20 ++++++++ 13 files changed, 215 insertions(+) create mode 100644 samples/LibAnalyzerTasks/Analyzer/Analyzer.csproj create mode 100644 samples/LibAnalyzerTasks/Analyzer/Properties/launchSettings.json create mode 100644 samples/LibAnalyzerTasks/Analyzer/SampleGenerator.cs create mode 100644 samples/LibAnalyzerTasks/Directory.Build.props create mode 100644 samples/LibAnalyzerTasks/LibAnalyzerTasks.sln create mode 100644 samples/LibAnalyzerTasks/Library/Library.csproj create mode 100644 samples/LibAnalyzerTasks/Library/MyLibrary.cs create mode 100644 samples/LibAnalyzerTasks/Library/readme.md create mode 100644 samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.props create mode 100644 samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.targets create mode 100644 samples/LibAnalyzerTasks/Tasks/Tasks.csproj create mode 100644 samples/LibAnalyzerTasks/Tests/Sample.cs create mode 100644 samples/LibAnalyzerTasks/Tests/Tests.csproj diff --git a/samples/LibAnalyzerTasks/Analyzer/Analyzer.csproj b/samples/LibAnalyzerTasks/Analyzer/Analyzer.csproj new file mode 100644 index 00000000..a426461a --- /dev/null +++ b/samples/LibAnalyzerTasks/Analyzer/Analyzer.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + + true + + analyzers/dotnet/roslyn4.0 + + + + + + + + \ No newline at end of file diff --git a/samples/LibAnalyzerTasks/Analyzer/Properties/launchSettings.json b/samples/LibAnalyzerTasks/Analyzer/Properties/launchSettings.json new file mode 100644 index 00000000..777d6ebb --- /dev/null +++ b/samples/LibAnalyzerTasks/Analyzer/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "SponsorableLib": { + "commandName": "DebugRoslynComponent", + "targetProject": "..\\Tests\\Tests.csproj" + } + } +} \ No newline at end of file diff --git a/samples/LibAnalyzerTasks/Analyzer/SampleGenerator.cs b/samples/LibAnalyzerTasks/Analyzer/SampleGenerator.cs new file mode 100644 index 00000000..89f429fa --- /dev/null +++ b/samples/LibAnalyzerTasks/Analyzer/SampleGenerator.cs @@ -0,0 +1,18 @@ +using System; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace Analyzer; + +[Generator(LanguageNames.CSharp)] +public class SampleGenerator : IIncrementalGenerator +{ + public void Initialize(IncrementalGeneratorInitializationContext context) + => context.RegisterSourceOutput(context.AnalyzerConfigOptionsProvider, + (c, t) => CheckAndReport(t.GlobalOptions, c.ReportDiagnostic)); + + void CheckAndReport(AnalyzerConfigOptions globalOptions, Action reportDiagnostic) + { + reportDiagnostic(Diagnostic.Create("LIB001", "Compiler", "Hi", DiagnosticSeverity.Warning, DiagnosticSeverity.Warning, true, 1)); + } +} diff --git a/samples/LibAnalyzerTasks/Directory.Build.props b/samples/LibAnalyzerTasks/Directory.Build.props new file mode 100644 index 00000000..a1642b80 --- /dev/null +++ b/samples/LibAnalyzerTasks/Directory.Build.props @@ -0,0 +1,22 @@ + + + + false + latest + + + 42.42.$([System.Math]::Floor($([MSBuild]::Divide($([System.DateTime]::Now.TimeOfDay.TotalSeconds), 10)))) + + true + $([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)bin')) + true + + https://pkg.kzu.app/index.json;https://api.nuget.org/v3/index.json + $(PackageOutputPath);$(RestoreSources) + + + false + + + diff --git a/samples/LibAnalyzerTasks/LibAnalyzerTasks.sln b/samples/LibAnalyzerTasks/LibAnalyzerTasks.sln new file mode 100644 index 00000000..a1d761dc --- /dev/null +++ b/samples/LibAnalyzerTasks/LibAnalyzerTasks.sln @@ -0,0 +1,51 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33312.197 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A78BAC35-43DD-428F-BDD2-402C1C6D010E}" + ProjectSection(SolutionItems) = preProject + Directory.Build.props = Directory.Build.props + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Analyzer", "Analyzer\Analyzer.csproj", "{C39F78D0-F46A-4576-8E76-F2B18AE9B361}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tasks", "Tasks\Tasks.csproj", "{E3584F00-8260-459D-9BC6-D1FDA2AD25F7}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Library", "Library\Library.csproj", "{D163293D-D567-4D2A-8305-C2044CC3D4CC}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{07E10270-FC47-4A61-AFDE-AF9F649B627E}" + ProjectSection(ProjectDependencies) = postProject + {D163293D-D567-4D2A-8305-C2044CC3D4CC} = {D163293D-D567-4D2A-8305-C2044CC3D4CC} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C39F78D0-F46A-4576-8E76-F2B18AE9B361}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C39F78D0-F46A-4576-8E76-F2B18AE9B361}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C39F78D0-F46A-4576-8E76-F2B18AE9B361}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C39F78D0-F46A-4576-8E76-F2B18AE9B361}.Release|Any CPU.Build.0 = Release|Any CPU + {E3584F00-8260-459D-9BC6-D1FDA2AD25F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E3584F00-8260-459D-9BC6-D1FDA2AD25F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E3584F00-8260-459D-9BC6-D1FDA2AD25F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E3584F00-8260-459D-9BC6-D1FDA2AD25F7}.Release|Any CPU.Build.0 = Release|Any CPU + {D163293D-D567-4D2A-8305-C2044CC3D4CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D163293D-D567-4D2A-8305-C2044CC3D4CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D163293D-D567-4D2A-8305-C2044CC3D4CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D163293D-D567-4D2A-8305-C2044CC3D4CC}.Release|Any CPU.Build.0 = Release|Any CPU + {07E10270-FC47-4A61-AFDE-AF9F649B627E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07E10270-FC47-4A61-AFDE-AF9F649B627E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07E10270-FC47-4A61-AFDE-AF9F649B627E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07E10270-FC47-4A61-AFDE-AF9F649B627E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6D936DB6-E5FF-47AB-B7EE-B0464C4D052A} + EndGlobalSection +EndGlobal diff --git a/samples/LibAnalyzerTasks/Library/Library.csproj b/samples/LibAnalyzerTasks/Library/Library.csproj new file mode 100644 index 00000000..48357644 --- /dev/null +++ b/samples/LibAnalyzerTasks/Library/Library.csproj @@ -0,0 +1,18 @@ + + + + netstandard2.0 + LibAnalyzerTasks + Sample library which includes analyzers, targets and tasks + + + + + + + + + + + + diff --git a/samples/LibAnalyzerTasks/Library/MyLibrary.cs b/samples/LibAnalyzerTasks/Library/MyLibrary.cs new file mode 100644 index 00000000..df05d41e --- /dev/null +++ b/samples/LibAnalyzerTasks/Library/MyLibrary.cs @@ -0,0 +1,6 @@ +namespace Library; + +public class MyLibrary +{ + +} diff --git a/samples/LibAnalyzerTasks/Library/readme.md b/samples/LibAnalyzerTasks/Library/readme.md new file mode 100644 index 00000000..e60c2134 --- /dev/null +++ b/samples/LibAnalyzerTasks/Library/readme.md @@ -0,0 +1,8 @@ +# Showcases smart library packing + +Packs and consumes a library containing: + +1. Library.csproj: main API for the package packed to `lib/netstandard2.0` +1. Analyzer.csproj: sample analyzer/generator lib packed to `analyzers/dotnet/roslyn4.0` to showcase Roslyn 4.0+ API requirement +1. Tasks.csproj: containing sample targets and tasks packed to `buildTransitive` + diff --git a/samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.props b/samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.props new file mode 100644 index 00000000..59350f22 --- /dev/null +++ b/samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.props @@ -0,0 +1,8 @@ + + + + + $(WarningsNotAsErrors);LIB001;LIB002;LIB003;LIB004;LIB005 + + + \ No newline at end of file diff --git a/samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.targets b/samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.targets new file mode 100644 index 00000000..8ce01493 --- /dev/null +++ b/samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.targets @@ -0,0 +1,13 @@ + + + + + $([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\readme.md)) + + + + + + + + \ No newline at end of file diff --git a/samples/LibAnalyzerTasks/Tasks/Tasks.csproj b/samples/LibAnalyzerTasks/Tasks/Tasks.csproj new file mode 100644 index 00000000..9c0ed2c0 --- /dev/null +++ b/samples/LibAnalyzerTasks/Tasks/Tasks.csproj @@ -0,0 +1,13 @@ + + + + netstandard2.0 + buildTransitive + + + + + + + + diff --git a/samples/LibAnalyzerTasks/Tests/Sample.cs b/samples/LibAnalyzerTasks/Tests/Sample.cs new file mode 100644 index 00000000..0f13ffb7 --- /dev/null +++ b/samples/LibAnalyzerTasks/Tests/Sample.cs @@ -0,0 +1,14 @@ +using System; +using Library; +using Xunit; + +namespace Tests; + +public class Sample +{ + [Fact] + public void Test() + { + var api = new MyLibrary(); + } +} \ No newline at end of file diff --git a/samples/LibAnalyzerTasks/Tests/Tests.csproj b/samples/LibAnalyzerTasks/Tests/Tests.csproj new file mode 100644 index 00000000..07824dea --- /dev/null +++ b/samples/LibAnalyzerTasks/Tests/Tests.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + + + + + + + + + + + + + + + + \ No newline at end of file