This repository has been archived by the owner on Dec 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Give Generators Access to MSBuild Properties (#210)
* Add abiilty to pass msbuild properties to the generator * update readme * revert to item approach * escape semicolons when creating _CodeGenToolResponseFileLines * add sample using TargetFrameworks property to show escaping works * update doc comments in transformation context * add link to sample projects in readme, update changelog Co-authored-by: talenfisher <[email protected]>
- Loading branch information
1 parent
84f9d1e
commit f3a03fc
Showing
19 changed files
with
285 additions
and
3 deletions.
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
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>netcoreapp2.2;netcoreapp3.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CodeGeneration.Roslyn.Attributes" Version="$(LocalNuGetVersion)" PrivateAssets="all" /> | ||
<PackageReference Include="CodeGeneration.Roslyn.Tool" Version="$(LocalNuGetVersion)" PrivateAssets="all" /> | ||
<PackageReference Include="BuildPropsGenerator" Version="1.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</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,27 @@ | ||
using System; | ||
using CodeGeneration.Roslyn; | ||
|
||
namespace BuildPropsConsumer | ||
{ | ||
|
||
[CodeGenerationAttribute("BuildPropsGenerator.FrameworkInfoProviderGenerator, BuildPropsGenerator")] | ||
class FrameworkInfoProviderAttribute : Attribute { } | ||
|
||
[FrameworkInfoProvider] | ||
partial class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var program = new Program(); | ||
var frameworks = program.TargetFrameworks; | ||
var currentFramework = program.CurrentTargetFramework; | ||
|
||
Console.WriteLine("This project is build for the following frameworks: "); | ||
|
||
foreach(var framework in frameworks) { | ||
var message = framework == currentFramework ? $"{framework} (current)" : framework; | ||
Console.WriteLine(message); | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="$(CodeGenerationRoslynPluginSdkPath)Sdk.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PluginRequestedProperty Include="TargetFrameworks" /> | ||
<PluginRequestedProperty Include="TargetFramework" /> | ||
</ItemGroup> | ||
|
||
<Import Project="$(CodeGenerationRoslynPluginSdkPath)Sdk.targets" /> | ||
|
||
</Project> |
54 changes: 54 additions & 0 deletions
54
samples/BuildPropsGenerator/FrameworkInfoProviderGenerator.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,54 @@ | ||
using System; | ||
using System.Linq; | ||
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 BuildPropsGenerator | ||
{ | ||
public class FrameworkInfoProviderGenerator : ICodeGenerator | ||
{ | ||
public FrameworkInfoProviderGenerator(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(CreateTargetFrameworkListProperty(), CreateCurrentTargetFrameworkProperty()); | ||
} | ||
MemberDeclarationSyntax CreateTargetFrameworkListProperty() | ||
{ | ||
var collectionType = "System.Collections.Generic.List<string>"; | ||
var frameworks = context.BuildProperties["TargetFrameworks"]; | ||
var quotedFrameworks = frameworks.Split(";").Select(framework => $"\"{framework}\""); | ||
var commaDelimitedFrameworks = string.Join(',', quotedFrameworks.ToArray()); | ||
|
||
return SyntaxFactory.ParseMemberDeclaration($"public {collectionType} TargetFrameworks {{ get; }} = new {collectionType} {{ {commaDelimitedFrameworks} }};"); | ||
} | ||
MemberDeclarationSyntax CreateCurrentTargetFrameworkProperty() | ||
{ | ||
var framework = context.BuildProperties["TargetFramework"]; | ||
return SyntaxFactory.ParseMemberDeclaration($"public string CurrentTargetFramework {{ get; }} = \"{framework}\";"); | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
#!/usr/bin/env pwsh | ||
|
||
Write-Host "Running in $PSScriptRoot" -ForegroundColor Cyan | ||
Push-Location $PSScriptRoot | ||
try { | ||
Write-Host "dotnet build" -ForegroundColor Green | ||
dotnet build | ||
} | ||
finally { | ||
Pop-Location | ||
} |
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
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
16 changes: 16 additions & 0 deletions
16
src/CodeGeneration.Roslyn.Tests.Generators/AddExampleBuildPropertyAttribute.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,16 @@ | ||
// Copyright (c) Andrew Arnott. All rights reserved. | ||
// Licensed under the MS-PL license. See LICENSE.txt file in the project root for full license information. | ||
|
||
namespace CodeGeneration.Roslyn.Tests.Generators | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using Validation; | ||
|
||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] | ||
[CodeGenerationAttribute(typeof(AddExampleBuildPropertyGenerator))] | ||
[Conditional("CodeGeneration")] | ||
public class AddExampleBuildPropertyAttribute : Attribute | ||
{ | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/CodeGeneration.Roslyn.Tests.Generators/AddExampleBuildPropertyGenerator.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,45 @@ | ||
// Copyright (c) Andrew Arnott. All rights reserved. | ||
// Licensed under the MS-PL license. See LICENSE.txt file in the project root for full license information. | ||
|
||
namespace CodeGeneration.Roslyn.Tests.Generators | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Validation; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
public class AddExampleBuildPropertyGenerator : ICodeGenerator | ||
{ | ||
public AddExampleBuildPropertyGenerator(AttributeData attributeData) | ||
{ | ||
Requires.NotNull(attributeData, nameof(attributeData)); | ||
} | ||
|
||
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken) | ||
{ | ||
var partialClass = GeneratePartialClass(); | ||
return Task.FromResult(SyntaxFactory.List(partialClass)); | ||
|
||
IEnumerable<MemberDeclarationSyntax> GeneratePartialClass() | ||
{ | ||
var classDeclaration = context.ProcessingNode as ClassDeclarationSyntax; | ||
yield return classDeclaration | ||
.AddMembers(CreateExampleBuildProperty()); | ||
} | ||
|
||
MemberDeclarationSyntax CreateExampleBuildProperty() | ||
{ | ||
var value = context.BuildProperties["ExampleBuildProperty"]; | ||
return SyntaxFactory.ParseMemberDeclaration($"public string ExampleBuildProperty {{ get; }} = \"{value}\";"); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.