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

Commit 078476a

Browse files
authored
Merge pull request #67 from Pzixel/feature/global_attributes_generation
Assembly attributes support
2 parents b889381 + 7383a10 commit 078476a

File tree

12 files changed

+117
-35
lines changed

12 files changed

+117
-35
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ If you build with `dotnet build`, you need to take a couple extra steps. First,
163163
Second, add this item to an `<ItemGroup>` in the project that will execute the code generator as part of your build:
164164

165165
```xml
166-
<DotNetCliToolReference Include="dotnet-codegen" Version="0.4.11" />
166+
<DotNetCliToolReference Include="dotnet-codegen" Version="0.4.12" />
167167
```
168168

169169
You should adjust the version in the above xml to match the version of this tool you are using.
@@ -187,10 +187,10 @@ to immediately see the effects of your changes on the generated code.
187187
You can also package up your code generator as a NuGet package for others to install
188188
and use. Your NuGet package should include a dependency on the `CodeGeneration.Roslyn.BuildTime`
189189
that matches the version of `CodeGeneration.Roslyn` that you used to produce your generator.
190-
For example, if you used version 0.4.11 of this project, your .nuspec file would include this tag:
190+
For example, if you used version 0.4.12 of this project, your .nuspec file would include this tag:
191191

192192
```xml
193-
<dependency id="CodeGeneration.Roslyn.BuildTime" version="0.4.11" />
193+
<dependency id="CodeGeneration.Roslyn.BuildTime" version="0.4.12" />
194194
```
195195

196196
In addition to this dependency, your NuGet package should include a `build` folder with an
@@ -219,7 +219,7 @@ so that the MSBuild Task can invoke the `dotnet codegen` command line tool:
219219
```xml
220220
<ItemGroup>
221221
<PackageReference Include="YourCodeGenPackage" Version="1.2.3" PrivateAssets="all" />
222-
<DotNetCliToolReference Include="dotnet-codegen" Version="0.4.11" />
222+
<DotNetCliToolReference Include="dotnet-codegen" Version="0.4.12" />
223223
</ItemGroup>
224224
```
225225

src/CodeGeneration.Roslyn.Tasks/GenerateCodeFromAttributes.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class GenerateCodeFromAttributes : ToolTask
2222
[Required]
2323
public ITaskItem[] GeneratorAssemblySearchPaths { get; set; }
2424

25+
[Required]
26+
public string ProjectDirectory { get; set; }
27+
2528
[Required]
2629
public string IntermediateOutputDirectory { get; set; }
2730

@@ -80,6 +83,9 @@ protected override string GenerateResponseFileCommands()
8083
argBuilder.AppendLine("--out");
8184
argBuilder.AppendLine(this.IntermediateOutputDirectory);
8285

86+
argBuilder.AppendLine("--projectDir");
87+
argBuilder.AppendLine(this.ProjectDirectory);
88+
8389
this.generatedCompileItemsFilePath = Path.Combine(this.IntermediateOutputDirectory, Path.GetRandomFileName());
8490
argBuilder.AppendLine("--generatedFilesList");
8591
argBuilder.AppendLine(this.generatedCompileItemsFilePath);

src/CodeGeneration.Roslyn.Tasks/build/CodeGeneration.Roslyn.BuildTime.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Target Name="GenerateCodeFromAttributes" DependsOnTargets="ResolveReferences" BeforeTargets="CoreCompile">
2020
<GenerateCodeFromAttributes
2121
ToolLocationOverride="$(GenerateCodeFromAttributesToolPathOverride)"
22+
ProjectDirectory="$(MSBuildProjectDirectory)"
2223
Compile="@(Compile)"
2324
ReferencePath="@(ReferencePath)"
2425
GeneratorAssemblySearchPaths="@(GeneratorAssemblySearchPaths)"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Andrew Arnott. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
3+
4+
namespace CodeGeneration.Roslyn.Tests.Generators
5+
{
6+
using System;
7+
using System.Diagnostics;
8+
using Validation;
9+
10+
[AttributeUsage(AttributeTargets.Assembly)]
11+
[CodeGenerationAttribute(typeof(DirectoryPathGenerator))]
12+
[Conditional("CodeGeneration")]
13+
public class DirectoryPathAttribute : Attribute
14+
{
15+
}
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Andrew Arnott. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
3+
4+
namespace CodeGeneration.Roslyn.Tests.Generators
5+
{
6+
using System;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.CSharp;
11+
using Microsoft.CodeAnalysis.CSharp.Syntax;
12+
using Validation;
13+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
14+
15+
public class DirectoryPathGenerator : ICodeGenerator
16+
{
17+
public DirectoryPathGenerator(AttributeData attributeData)
18+
{
19+
Requires.NotNull(attributeData, nameof(attributeData));
20+
}
21+
22+
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken)
23+
{
24+
var member = ClassDeclaration("DirectoryPathTest")
25+
.AddMembers(
26+
FieldDeclaration(
27+
VariableDeclaration(
28+
PredefinedType(Token(SyntaxKind.StringKeyword)))
29+
.AddVariables(
30+
VariableDeclarator(Identifier("Path"))
31+
.WithInitializer(
32+
EqualsValueClause(
33+
LiteralExpression(
34+
SyntaxKind.StringLiteralExpression,
35+
Literal(context.ProjectDirectory))))))
36+
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ConstKeyword))));
37+
38+
return Task.FromResult(List<MemberDeclarationSyntax>(new []{member}));
39+
}
40+
}
41+
}

src/CodeGeneration.Roslyn.Tests.Generators/DuplicateWithSuffixGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationCon
3535
var results = SyntaxFactory.List<MemberDeclarationSyntax>();
3636

3737
MemberDeclarationSyntax copy = null;
38-
var applyToClass = context.ProcessingMember as ClassDeclarationSyntax;
38+
var applyToClass = context.ProcessingNode as ClassDeclarationSyntax;
3939
if (applyToClass != null)
4040
{
4141
copy = applyToClass

src/CodeGeneration.Roslyn.Tests.Generators/MultiplySuffixGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationCon
2929
var results = SyntaxFactory.List<MemberDeclarationSyntax>();
3030

3131
MemberDeclarationSyntax copy = null;
32-
var applyToClass = context.ProcessingMember as ClassDeclarationSyntax;
32+
var applyToClass = context.ProcessingNode as ClassDeclarationSyntax;
3333
if (applyToClass != null)
3434
{
3535
var properties = applyToClass.Members.OfType<PropertyDeclarationSyntax>()

src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
33

44
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Text;
85
using CodeGeneration.Roslyn.Tests.Generators;
96
using Xunit;
107

8+
[assembly: DirectoryPath]
9+
1110
public partial class CodeGenerationTests
1211
{
1312
/// <summary>
@@ -21,6 +20,7 @@ public void SimpleGenerationWorks()
2120
var fooB = new CodeGenerationTests.FooB();
2221
var multiplied = new MultipliedBar();
2322
multiplied.ValueSuff1020();
23+
Assert.EndsWith(@"src\CodeGeneration.Roslyn.Tests", DirectoryPathTest.Path, StringComparison.OrdinalIgnoreCase);
2424
}
2525

2626
[DuplicateWithSuffixByName("A")]

src/CodeGeneration.Roslyn.Tool/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ static int Main(string[] args)
1818
IReadOnlyList<string> generatorSearchPaths = Array.Empty<string>();
1919
string generatedCompileItemFile = null;
2020
string outputDirectory = null;
21+
string projectDir = null;
2122
ArgumentSyntax.Parse(args, syntax =>
2223
{
2324
syntax.DefineOptionList("r|reference", ref refs, "Paths to assemblies being referenced");
2425
syntax.DefineOptionList("generatorSearchPath", ref generatorSearchPaths, "Paths to folders that may contain generator assemblies");
2526
syntax.DefineOption("out", ref outputDirectory, true, "The directory to write generated source files to");
27+
syntax.DefineOption("projectDir", ref projectDir, true, "The absolute path of the directory where the project file is located");
2628
syntax.DefineOption("generatedFilesList", ref generatedCompileItemFile, "The path to the file to create with a list of generated source files");
2729
syntax.DefineParameterList("compile", ref compile, "Source files included in compilation");
2830
});
@@ -41,6 +43,7 @@ static int Main(string[] args)
4143

4244
var generator = new CompilationGenerator
4345
{
46+
ProjectDirectory = projectDir,
4447
Compile = compile,
4548
ReferencePath = refs,
4649
GeneratorAssemblySearchPaths = generatorSearchPaths,

src/CodeGeneration.Roslyn/CompilationGenerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class CompilationGenerator
6565
/// </summary>
6666
public IEnumerable<string> EmptyGeneratedFiles => this.emptyGeneratedFiles;
6767

68+
public string ProjectDirectory { get; set; }
69+
6870
public void Generate(IProgress<Diagnostic> progress = null, CancellationToken cancellationToken = default(CancellationToken))
6971
{
7072
Verify.Operation(this.Compile != null, $"{nameof(Compile)} must be set first.");
@@ -104,6 +106,7 @@ public class CompilationGenerator
104106
var generatedSyntaxTree = DocumentTransform.TransformAsync(
105107
compilation,
106108
inputSyntaxTree,
109+
this.ProjectDirectory,
107110
this.LoadAssembly,
108111
progress).GetAwaiter().GetResult();
109112

0 commit comments

Comments
 (0)