This repository has been archived by the owner on Dec 12, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GeneratorInConsumerSolution sample (#152)
- Loading branch information
Showing
10 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<LocalNuGetVersion>*</LocalNuGetVersion> | ||
</PropertyGroup> | ||
|
||
</Project> |
31 changes: 31 additions & 0 deletions
31
samples/GeneratorInConsumerSolution/GeneratorInConsumerSolution.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26124.0 | ||
MinimumVisualStudioVersion = 15.0.26124.0 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Consumer", "Sample.Consumer\Sample.Consumer.csproj", "{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Generator", "Sample.Generator\Sample.Generator.csproj", "{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {18247316-A7C2-4598-82BF-F9103F2C6084} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# GeneratorInConsumerSolution | ||
|
||
This solution contains two projects: Generator and Consumer. | ||
Generator contains the generator that is used by the Consumer. | ||
|
||
The sample generator creates a partial declaration of the | ||
`[GeneratedId]`-annotated type (class/struct) that contains | ||
an additional auto-property: | ||
|
||
```csharp | ||
public System.Guid Id { get; } = System.Guid.NewGuid(); | ||
``` | ||
|
||
### Usage Note | ||
|
||
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. | ||
|
||
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. |
17 changes: 17 additions & 0 deletions
17
samples/GeneratorInConsumerSolution/Sample.Consumer/Class1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Sample.Generator; | ||
using System; | ||
|
||
namespace Sample.Consumer | ||
{ | ||
[GeneratedId] | ||
public partial class Class1 | ||
{ | ||
public void TestId() | ||
{ | ||
if (this.Id != Guid.Empty) | ||
{ | ||
Console.WriteLine("Generated Id!"); | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
samples/GeneratorInConsumerSolution/Sample.Consumer/Sample.Consumer.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CodeGeneration.Roslyn.BuildTime" Version="$(LocalNuGetVersion)" PrivateAssets="all" /> | ||
<DotNetCliToolReference Include="dotnet-codegen" Version="$(LocalNuGetVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<!-- | ||
See this issue for information on those additional metadata: | ||
https://github.com/AArnott/CodeGeneration.Roslyn/issues/148 | ||
--> | ||
<!--<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj" | ||
ReferenceOutputAssembly="false" | ||
OutputItemType="ResolvedGeneratorReferencePaths" | ||
SkipGetTargetFrameworkProperties="true" | ||
UndefineProperties=";TargetFramework;RuntimeIdentifier" | ||
PrivateAssets="all" />--> | ||
<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
</Project> |
11 changes: 11 additions & 0 deletions
11
samples/GeneratorInConsumerSolution/Sample.Generator/GeneratedIdAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using CodeGeneration.Roslyn; | ||
using System; | ||
|
||
namespace Sample.Generator | ||
{ | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] | ||
[CodeGenerationAttribute(typeof(IdGenerator))] | ||
public class GeneratedIdAttribute : Attribute | ||
{ | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
samples/GeneratorInConsumerSolution/Sample.Generator/IdGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CodeGeneration.Roslyn; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Sample.Generator | ||
{ | ||
public class IdGenerator : ICodeGenerator | ||
{ | ||
public IdGenerator(AttributeData attributeData) | ||
{ | ||
} | ||
|
||
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken) | ||
{ | ||
var partialType = CreatePartialType(); | ||
return Task.FromResult(SyntaxFactory.List(partialType)); | ||
|
||
IEnumerable<MemberDeclarationSyntax> CreatePartialType() | ||
{ | ||
var newPartialType = | ||
context.ProcessingNode is ClassDeclarationSyntax classDeclaration | ||
? SyntaxFactory.ClassDeclaration(classDeclaration.Identifier.ValueText) | ||
: context.ProcessingNode is StructDeclarationSyntax structDeclaration | ||
? SyntaxFactory.StructDeclaration(structDeclaration.Identifier.ValueText) | ||
: default(TypeDeclarationSyntax); | ||
if (newPartialType is null) | ||
yield break; | ||
yield return newPartialType | ||
?.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword)) | ||
.AddMembers(CreateIdProperty()); | ||
} | ||
MemberDeclarationSyntax CreateIdProperty() | ||
{ | ||
return SyntaxFactory.ParseMemberDeclaration("public System.Guid Id { get; } = System.Guid.NewGuid();"); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
samples/GeneratorInConsumerSolution/Sample.Generator/Sample.Generator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CodeGeneration.Roslyn" Version="$(LocalNuGetVersion)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<!--To inherit the global NuGet package sources remove the <clear/> line below --> | ||
<clear /> | ||
<add key="local CG.R Debug packages" value="../../bin/Packages/Debug" /> | ||
<add key="nuget" value="https://api.nuget.org/v3/index.json" /> | ||
</packageSources> | ||
</configuration> |