Skip to content

Commit

Permalink
Add smart library packing sample
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed May 17, 2024
1 parent 94cbfa5 commit 5746907
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 0 deletions.
16 changes: 16 additions & 0 deletions samples/LibAnalyzerTasks/Analyzer/Analyzer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!-- Combined with the launchSettings.json, allows debugging analyzers/generators -->
<IsRoslynComponent>true</IsRoslynComponent>
<!-- Target "newer" Roslyn with incremental generators support -->
<PackFolder>analyzers/dotnet/roslyn4.0</PackFolder>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NuGetizer" Version="1.2.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" Pack="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"SponsorableLib": {
"commandName": "DebugRoslynComponent",
"targetProject": "..\\Tests\\Tests.csproj"
}
}
}
18 changes: 18 additions & 0 deletions samples/LibAnalyzerTasks/Analyzer/SampleGenerator.cs
Original file line number Diff line number Diff line change
@@ -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<Diagnostic> reportDiagnostic)
{
reportDiagnostic(Diagnostic.Create("LIB001", "Compiler", "Hi", DiagnosticSeverity.Warning, DiagnosticSeverity.Warning, true, 1));
}
}
22 changes: 22 additions & 0 deletions samples/LibAnalyzerTasks/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project>

<PropertyGroup>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>

<!-- Roslyn caches analyzers aggressively so we are better off using a very dynamic version number
for local builds where a quick devloop is key. We bump version every 10 seconds -->
<VersionPrefix>42.42.$([System.Math]::Floor($([MSBuild]::Divide($([System.DateTime]::Now.TimeOfDay.TotalSeconds), 10))))</VersionPrefix>

<PackOnBuild>true</PackOnBuild>
<PackageOutputPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)bin'))</PackageOutputPath>
<PublishRepositoryUrl>true</PublishRepositoryUrl>

<RestoreSources>https://pkg.kzu.app/index.json;https://api.nuget.org/v3/index.json</RestoreSources>
<RestoreSources Condition="Exists('$(PackageOutputPath)')">$(PackageOutputPath);$(RestoreSources)</RestoreSources>

<!-- Avoid deleting older packed versions to avoid rebuild errors (since package is in same solution, which is uncommon) -->
<EnablePackCleanup>false</EnablePackCleanup>
</PropertyGroup>

</Project>
51 changes: 51 additions & 0 deletions samples/LibAnalyzerTasks/LibAnalyzerTasks.sln
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions samples/LibAnalyzerTasks/Library/Library.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>LibAnalyzerTasks</PackageId>
<Description>Sample library which includes analyzers, targets and tasks</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NuGetizer" Version="1.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Analyzer\Analyzer.csproj" ReferenceOutputAssembly="false" OutputType="Analyzer" />
<ProjectReference Include="..\Tasks\Tasks.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions samples/LibAnalyzerTasks/Library/MyLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Library;

public class MyLibrary
{

}
8 changes: 8 additions & 0 deletions samples/LibAnalyzerTasks/Library/readme.md
Original file line number Diff line number Diff line change
@@ -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`

8 changes: 8 additions & 0 deletions samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<!-- This makes sure builds are never broken from sponsorship warnings, even if WarningsAsErrors is used -->
<WarningsNotAsErrors>$(WarningsNotAsErrors);LIB001;LIB002;LIB003;LIB004;LIB005</WarningsNotAsErrors>
</PropertyGroup>

</Project>
13 changes: 13 additions & 0 deletions samples/LibAnalyzerTasks/Tasks/LibAnalyzerTasks.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<!-- This showcases how to use a package-provided file as diagnostics linked content -->
<SponsorableLibSponsorable>$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\readme.md))</SponsorableLibSponsorable>
</PropertyGroup>

<ItemGroup>
<!-- This makes it visible to the generator -->
<CompilerVisibleProperty Include="SponsorableLibSponsorable" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions samples/LibAnalyzerTasks/Tasks/Tasks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackFolder>buildTransitive</PackFolder>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.9.20" />
<PackageReference Include="NuGetizer" Version="1.2.1" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions samples/LibAnalyzerTasks/Tests/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Library;
using Xunit;

namespace Tests;

public class Sample
{
[Fact]
public void Test()
{
var api = new MyLibrary();
}
}
20 changes: 20 additions & 0 deletions samples/LibAnalyzerTasks/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.8.0" />

<!-- NOTE: requires packing the Library project first -->
<PackageReference Include="LibAnalyzerTasks" Version="42.42.*" Condition="Exists('$(PackageOutputPath)')" />
</ItemGroup>

<ItemGroup>
<!-- NOTE: requires packing and restoring first. Removes compile to avoid initial build error -->
<Compile Remove="Sample.cs" Condition="!Exists('$(PackageOutputPath)')" />
</ItemGroup>

</Project>

0 comments on commit 5746907

Please sign in to comment.