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

Add GeneratorInConsumerSolution sample #152

Merged
merged 6 commits into from Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Consumer", "Sample.Consumer\Sample.Consumer.csproj", "{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Generator", "Sample.Generator\Sample.Generator.csproj", "{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
amis92 marked this conversation as resolved.
Show resolved Hide resolved
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
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}.Debug|x64.ActiveCfg = Debug|Any CPU
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Debug|x64.Build.0 = Debug|Any CPU
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Debug|x86.ActiveCfg = Debug|Any CPU
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Debug|x86.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
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|x64.ActiveCfg = Release|Any CPU
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|x64.Build.0 = Release|Any CPU
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|x86.ActiveCfg = Release|Any CPU
{E47BD7FC-FD4A-44BF-AA67-787CDE3C8A21}.Release|x86.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}.Debug|x64.ActiveCfg = Debug|Any CPU
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Debug|x64.Build.0 = Debug|Any CPU
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Debug|x86.ActiveCfg = Debug|Any CPU
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Debug|x86.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
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|x64.ActiveCfg = Release|Any CPU
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|x64.Build.0 = Release|Any CPU
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|x86.ActiveCfg = Release|Any CPU
{F15F35D6-932C-40BB-9CB9-56EC6F1942AE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Binary file added samples/GeneratorInConsumerSolution/README.md
Binary file not shown.
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" />
amis92 marked this conversation as resolved.
Show resolved Hide resolved
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>