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

Commit

Permalink
Merge pull request #90 from AArnott/fix32
Browse files Browse the repository at this point in the history
Evaluate real preprocessor symbols
  • Loading branch information
AArnott authored Sep 6, 2018
2 parents 1845539 + 8912d0e commit 9b7ec97
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<Target Name="GenerateCodeFromAttributes" DependsOnTargets="ResolveReferences" BeforeTargets="CoreCompile;PrepareResources">
<ItemGroup>
<Compile_CodeGenInputs Include="@(Compile)" Condition=" '%(Compile.Generator)' == 'MSBuild:GenerateCodeFromAttributes' " />
<DefineConstantsItems Include="$(DefineConstants)" />
</ItemGroup>
<PropertyGroup>
<GenerateCodeFromAttributesToolPathOverride Condition="'$(GenerateCodeFromAttributesToolPathOverride)' == ''">codegen</GenerateCodeFromAttributesToolPathOverride>
Expand All @@ -13,6 +14,7 @@
<_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>
<_CodeGenToolResponseFileLines>
@(ReferencePath->'-r%0d%0a%(Identity)', '%0d%0a')
@(DefineConstantsItems->'-d%0d%0a%(Identity)', '%0d%0a')
@(GeneratorAssemblySearchPaths->'--generatorSearchPath%0d%0a%(Identity)', '%0d%0a')
--out
$(IntermediateOutputPath)
Expand Down
8 changes: 5 additions & 3 deletions src/CodeGeneration.Roslyn.Tests/DocumentTransformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,20 @@ public void IfElseDirective_OnUsings_InactiveUsingAndDirectives_Dropped()
const string source = @"
using System;
using CodeGeneration.Roslyn.Tests.Generators;
#if SOMETHING
#if SOMETHING_ACTIVE
using System.Linq;
#else
#elif SOMETHING_INACTIVE
using System.Diagnostics;
#else
using System.Never;
#endif
[EmptyPartial]
partial class Empty {}";
const string generated = @"
using System;
using CodeGeneration.Roslyn.Tests.Generators;
using System.Diagnostics;
using System.Linq;
partial class Empty
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ protected static Project CreateProject(params string[] sources)
.WithProjectCompilationOptions(
projectId,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.WithProjectParseOptions(
projectId,
new CSharpParseOptions(preprocessorSymbols: new[] { "SOMETHING_ACTIVE" }))
.AddMetadataReferences(projectId, MetadataReferences);

int count = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/CodeGeneration.Roslyn.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static int Main(string[] args)
{
IReadOnlyList<string> compile = Array.Empty<string>();
IReadOnlyList<string> refs = Array.Empty<string>();
IReadOnlyList<string> preprocessorSymbols = Array.Empty<string>();
IReadOnlyList<string> generatorSearchPaths = Array.Empty<string>();
string generatedCompileItemFile = null;
string outputDirectory = null;
Expand All @@ -21,6 +22,7 @@ static int Main(string[] args)
{
syntax.DefineOption("version", ref version, "Show version of this tool (and exits).");
syntax.DefineOptionList("r|reference", ref refs, "Paths to assemblies being referenced");
syntax.DefineOptionList("d|define", ref preprocessorSymbols, "Preprocessor symbols");
syntax.DefineOptionList("generatorSearchPath", ref generatorSearchPaths, "Paths to folders that may contain generator assemblies");
syntax.DefineOption("out", ref outputDirectory, true, "The directory to write generated source files to");
syntax.DefineOption("projectDir", ref projectDir, true, "The absolute path of the directory where the project file is located");
Expand Down Expand Up @@ -50,6 +52,7 @@ static int Main(string[] args)
ProjectDirectory = projectDir,
Compile = Sanitize(compile),
ReferencePath = Sanitize(refs),
PreprocessorSymbols = preprocessorSymbols,
GeneratorAssemblySearchPaths = Sanitize(generatorSearchPaths),
IntermediateOutputDirectory = outputDirectory,
};
Expand Down
12 changes: 10 additions & 2 deletions src/CodeGeneration.Roslyn/CompilationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class CompilationGenerator
/// </summary>
public IReadOnlyList<string> ReferencePath { get; set; }

/// <summary>
/// Gets or sets a set of preprocessor symbols to define.
/// </summary>
public IEnumerable<string> PreprocessorSymbols { get; set; }

/// <summary>
/// Gets or sets the paths to directories to search for generator assemblies.
/// </summary>
Expand Down Expand Up @@ -309,6 +314,8 @@ private CSharpCompilation CreateCompilation(CancellationToken cancellationToken)
var compilation = CSharpCompilation.Create("codegen")
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.WithReferences(this.ReferencePath.Select(p => MetadataReference.CreateFromFile(p)));
var parseOptions = new CSharpParseOptions(preprocessorSymbols: this.PreprocessorSymbols);

foreach (var sourceFile in this.Compile)
{
using (var stream = File.OpenRead(sourceFile))
Expand All @@ -318,8 +325,9 @@ private CSharpCompilation CreateCompilation(CancellationToken cancellationToken)
compilation = compilation.AddSyntaxTrees(
CSharpSyntaxTree.ParseText(
text,
path: sourceFile,
cancellationToken: cancellationToken));
parseOptions,
sourceFile,
cancellationToken));
}
}

Expand Down

0 comments on commit 9b7ec97

Please sign in to comment.