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

Commit 246ca6a

Browse files
authored
Add GeneratorInConsumerSolution sample (#152)
1 parent 6d800e9 commit 246ca6a

10 files changed

Lines changed: 175 additions & 1 deletion

File tree

azure-pipeline.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ jobs:
4747
- task: VSBuild@1
4848
displayName: Build Visual Studio solution
4949
inputs:
50-
vsVersion: 15.0
50+
vsVersion: '15.0'
51+
solution: src/**/*.sln
5152
msbuildArgs: /t:build,pack /m /v:m /bl:"$(Build.ArtifactStagingDirectory)/build_logs/msbuild.binlog"
5253
platform: $(BuildPlatform)
5354
configuration: $(BuildConfiguration)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<LocalNuGetVersion>*</LocalNuGetVersion>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Consumer", "Sample.Consumer\Sample.Consumer.csproj", "{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Generator", "Sample.Generator\Sample.Generator.csproj", "{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {18247316-A7C2-4598-82BF-F9103F2C6084}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# GeneratorInConsumerSolution
2+
3+
This solution contains two projects: Generator and Consumer.
4+
Generator contains the generator that is used by the Consumer.
5+
6+
The sample generator creates a partial declaration of the
7+
`[GeneratedId]`-annotated type (class/struct) that contains
8+
an additional auto-property:
9+
10+
```csharp
11+
public System.Guid Id { get; } = System.Guid.NewGuid();
12+
```
13+
14+
### Usage Note
15+
16+
Please note that because [`Directory.Build.props`](./Directory.Build.props) by default uses `*` Version, NuGet will resolve that to the latest *stable* version. Most often is not what you want, so please replace that version with the one you want to test.
17+
18+
Also, if you're validating your development with the sample, please remember that after NuGet once restores a version, it'll cache it in the default `packages` folder - to use new Package with the same version string, you'll need to delete cached packages from that folder.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Sample.Generator;
2+
using System;
3+
4+
namespace Sample.Consumer
5+
{
6+
[GeneratedId]
7+
public partial class Class1
8+
{
9+
public void TestId()
10+
{
11+
if (this.Id != Guid.Empty)
12+
{
13+
Console.WriteLine("Generated Id!");
14+
}
15+
}
16+
}
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="CodeGeneration.Roslyn.BuildTime" Version="$(LocalNuGetVersion)" PrivateAssets="all" />
9+
<DotNetCliToolReference Include="dotnet-codegen" Version="$(LocalNuGetVersion)" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<!--
14+
See this issue for information on those additional metadata:
15+
https://github.com/AArnott/CodeGeneration.Roslyn/issues/148
16+
-->
17+
<!--<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj"
18+
ReferenceOutputAssembly="false"
19+
OutputItemType="ResolvedGeneratorReferencePaths"
20+
SkipGetTargetFrameworkProperties="true"
21+
UndefineProperties=";TargetFramework;RuntimeIdentifier"
22+
PrivateAssets="all" />-->
23+
<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj" PrivateAssets="all" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using CodeGeneration.Roslyn;
2+
using System;
3+
4+
namespace Sample.Generator
5+
{
6+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
7+
[CodeGenerationAttribute(typeof(IdGenerator))]
8+
public class GeneratedIdAttribute : Attribute
9+
{
10+
}
11+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using CodeGeneration.Roslyn;
6+
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
10+
namespace Sample.Generator
11+
{
12+
public class IdGenerator : ICodeGenerator
13+
{
14+
public IdGenerator(AttributeData attributeData)
15+
{
16+
}
17+
18+
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken)
19+
{
20+
var partialType = CreatePartialType();
21+
return Task.FromResult(SyntaxFactory.List(partialType));
22+
23+
IEnumerable<MemberDeclarationSyntax> CreatePartialType()
24+
{
25+
var newPartialType =
26+
context.ProcessingNode is ClassDeclarationSyntax classDeclaration
27+
? SyntaxFactory.ClassDeclaration(classDeclaration.Identifier.ValueText)
28+
: context.ProcessingNode is StructDeclarationSyntax structDeclaration
29+
? SyntaxFactory.StructDeclaration(structDeclaration.Identifier.ValueText)
30+
: default(TypeDeclarationSyntax);
31+
if (newPartialType is null)
32+
yield break;
33+
yield return newPartialType
34+
?.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword))
35+
.AddMembers(CreateIdProperty());
36+
}
37+
MemberDeclarationSyntax CreateIdProperty()
38+
{
39+
return SyntaxFactory.ParseMemberDeclaration("public System.Guid Id { get; } = System.Guid.NewGuid();");
40+
}
41+
}
42+
}
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="CodeGeneration.Roslyn" Version="$(LocalNuGetVersion)" />
9+
</ItemGroup>
10+
11+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
5+
<clear />
6+
<add key="local CG.R Debug packages" value="../../bin/Packages/Debug" />
7+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
8+
</packageSources>
9+
</configuration>

0 commit comments

Comments
 (0)