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

Commit 9b7ec97

Browse files
authoredSep 6, 2018
Merge pull request #90 from AArnott/fix32
Evaluate real preprocessor symbols
2 parents 1845539 + 8912d0e commit 9b7ec97

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed
 

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

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<Target Name="GenerateCodeFromAttributes" DependsOnTargets="ResolveReferences" BeforeTargets="CoreCompile;PrepareResources">
55
<ItemGroup>
66
<Compile_CodeGenInputs Include="@(Compile)" Condition=" '%(Compile.Generator)' == 'MSBuild:GenerateCodeFromAttributes' " />
7+
<DefineConstantsItems Include="$(DefineConstants)" />
78
</ItemGroup>
89
<PropertyGroup>
910
<GenerateCodeFromAttributesToolPathOverride Condition="'$(GenerateCodeFromAttributesToolPathOverride)' == ''">codegen</GenerateCodeFromAttributesToolPathOverride>
@@ -13,6 +14,7 @@
1314
<_CodeGenToolWarningText>dotnet-codegen: Failed to generate the list of generated files. The tool didn't run succesfully. Please check https://github.com/AArnott/CodeGeneration.Roslyn for usage instructions.</_CodeGenToolWarningText>
1415
<_CodeGenToolResponseFileLines>
1516
@(ReferencePath->'-r%0d%0a%(Identity)', '%0d%0a')
17+
@(DefineConstantsItems->'-d%0d%0a%(Identity)', '%0d%0a')
1618
@(GeneratorAssemblySearchPaths->'--generatorSearchPath%0d%0a%(Identity)', '%0d%0a')
1719
--out
1820
$(IntermediateOutputPath)

‎src/CodeGeneration.Roslyn.Tests/DocumentTransformTests.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,20 @@ public void IfElseDirective_OnUsings_InactiveUsingAndDirectives_Dropped()
117117
const string source = @"
118118
using System;
119119
using CodeGeneration.Roslyn.Tests.Generators;
120-
#if SOMETHING
120+
#if SOMETHING_ACTIVE
121121
using System.Linq;
122-
#else
122+
#elif SOMETHING_INACTIVE
123123
using System.Diagnostics;
124+
#else
125+
using System.Never;
124126
#endif
125127
126128
[EmptyPartial]
127129
partial class Empty {}";
128130
const string generated = @"
129131
using System;
130132
using CodeGeneration.Roslyn.Tests.Generators;
131-
using System.Diagnostics;
133+
using System.Linq;
132134
133135
partial class Empty
134136
{

‎src/CodeGeneration.Roslyn.Tests/Helpers/CompilationTestsBase.cs

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ protected static Project CreateProject(params string[] sources)
8585
.WithProjectCompilationOptions(
8686
projectId,
8787
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
88+
.WithProjectParseOptions(
89+
projectId,
90+
new CSharpParseOptions(preprocessorSymbols: new[] { "SOMETHING_ACTIVE" }))
8891
.AddMetadataReferences(projectId, MetadataReferences);
8992

9093
int count = 0;

‎src/CodeGeneration.Roslyn.Tool/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static int Main(string[] args)
1212
{
1313
IReadOnlyList<string> compile = Array.Empty<string>();
1414
IReadOnlyList<string> refs = Array.Empty<string>();
15+
IReadOnlyList<string> preprocessorSymbols = Array.Empty<string>();
1516
IReadOnlyList<string> generatorSearchPaths = Array.Empty<string>();
1617
string generatedCompileItemFile = null;
1718
string outputDirectory = null;
@@ -21,6 +22,7 @@ static int Main(string[] args)
2122
{
2223
syntax.DefineOption("version", ref version, "Show version of this tool (and exits).");
2324
syntax.DefineOptionList("r|reference", ref refs, "Paths to assemblies being referenced");
25+
syntax.DefineOptionList("d|define", ref preprocessorSymbols, "Preprocessor symbols");
2426
syntax.DefineOptionList("generatorSearchPath", ref generatorSearchPaths, "Paths to folders that may contain generator assemblies");
2527
syntax.DefineOption("out", ref outputDirectory, true, "The directory to write generated source files to");
2628
syntax.DefineOption("projectDir", ref projectDir, true, "The absolute path of the directory where the project file is located");
@@ -50,6 +52,7 @@ static int Main(string[] args)
5052
ProjectDirectory = projectDir,
5153
Compile = Sanitize(compile),
5254
ReferencePath = Sanitize(refs),
55+
PreprocessorSymbols = preprocessorSymbols,
5356
GeneratorAssemblySearchPaths = Sanitize(generatorSearchPaths),
5457
IntermediateOutputDirectory = outputDirectory,
5558
};

‎src/CodeGeneration.Roslyn/CompilationGenerator.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class CompilationGenerator
4646
/// </summary>
4747
public IReadOnlyList<string> ReferencePath { get; set; }
4848

49+
/// <summary>
50+
/// Gets or sets a set of preprocessor symbols to define.
51+
/// </summary>
52+
public IEnumerable<string> PreprocessorSymbols { get; set; }
53+
4954
/// <summary>
5055
/// Gets or sets the paths to directories to search for generator assemblies.
5156
/// </summary>
@@ -309,6 +314,8 @@ private CSharpCompilation CreateCompilation(CancellationToken cancellationToken)
309314
var compilation = CSharpCompilation.Create("codegen")
310315
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
311316
.WithReferences(this.ReferencePath.Select(p => MetadataReference.CreateFromFile(p)));
317+
var parseOptions = new CSharpParseOptions(preprocessorSymbols: this.PreprocessorSymbols);
318+
312319
foreach (var sourceFile in this.Compile)
313320
{
314321
using (var stream = File.OpenRead(sourceFile))
@@ -318,8 +325,9 @@ private CSharpCompilation CreateCompilation(CancellationToken cancellationToken)
318325
compilation = compilation.AddSyntaxTrees(
319326
CSharpSyntaxTree.ParseText(
320327
text,
321-
path: sourceFile,
322-
cancellationToken: cancellationToken));
328+
parseOptions,
329+
sourceFile,
330+
cancellationToken));
323331
}
324332
}
325333

0 commit comments

Comments
 (0)
Failed to load comments.