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

Commit

Permalink
Add GeneratorInConsumerSolution sample (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
amis92 committed Jul 15, 2019
1 parent 6d800e9 commit 246ca6a
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 1 deletion.
3 changes: 2 additions & 1 deletion azure-pipeline.yml
Expand Up @@ -47,7 +47,8 @@ jobs:
- task: VSBuild@1
displayName: Build Visual Studio solution
inputs:
vsVersion: 15.0
vsVersion: '15.0'
solution: src/**/*.sln
msbuildArgs: /t:build,pack /m /v:m /bl:"$(Build.ArtifactStagingDirectory)/build_logs/msbuild.binlog"
platform: $(BuildPlatform)
configuration: $(BuildConfiguration)
Expand Down
7 changes: 7 additions & 0 deletions samples/GeneratorInConsumerSolution/Directory.Build.props
@@ -0,0 +1,7 @@
<Project>

<PropertyGroup>
<LocalNuGetVersion>*</LocalNuGetVersion>
</PropertyGroup>

</Project>
@@ -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
18 changes: 18 additions & 0 deletions samples/GeneratorInConsumerSolution/README.md
@@ -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 samples/GeneratorInConsumerSolution/Sample.Consumer/Class1.cs
@@ -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!");
}
}
}
}
@@ -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>
@@ -0,0 +1,11 @@
using CodeGeneration.Roslyn;
using System;

namespace Sample.Generator
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
[CodeGenerationAttribute(typeof(IdGenerator))]
public class GeneratedIdAttribute : Attribute
{
}
}
@@ -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();");
}
}
}
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="CodeGeneration.Roslyn" Version="$(LocalNuGetVersion)" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions samples/GeneratorInConsumerSolution/nuget.config
@@ -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>

0 comments on commit 246ca6a

Please sign in to comment.