Skip to content

Commit

Permalink
Allow constants to request a root area different than Constants
Browse files Browse the repository at this point in the history
This opens up other scenarios for specific constants we don't want
behind a Constants propery (i.e ThisAssembly.Git, see #69).

A small refactoring of the targets with a new PrepareConstants
will also make it easier for extenders to run before we do our
conversion to AdditionalFiles, to keep the extended items clean
even when they need to run before/after certain targets.
  • Loading branch information
kzu committed Jan 25, 2023
1 parent 3633673 commit 5892aff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/ThisAssembly.Constants/ConstantsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Value", out var value);
x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Comment", out var comment);
return (name: Path.GetFileName(x.Left.Path), value: value!, comment: string.IsNullOrWhiteSpace(comment) ? null : comment);
x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Root", out var root);
return (
name: Path.GetFileName(x.Left.Path),
value: value!,
comment: string.IsNullOrWhiteSpace(comment) ? null : comment,
root: string.IsNullOrWhiteSpace(root) ? "Constants" : root!);
})
.Combine(context.CompilationProvider.Select((p, _) => p.Language));

Expand All @@ -34,14 +39,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

}

void GenerateConstant(SourceProductionContext spc, ((string name, string value, string? comment), string language) arg2)
void GenerateConstant(SourceProductionContext spc, ((string name, string value, string? comment, string root), string language) args)
{
var ((name, value, comment), language) = arg2;
var ((name, value, comment, root), language) = args;

var root = Area.Load(new List<Constant> { new Constant(name, value, comment), });
var rootArea = Area.Load(new List<Constant> { new Constant(name, value, comment), }, root);
var file = language.Replace("#", "Sharp") + ".sbntxt";
var template = Template.Parse(EmbeddedResource.GetContent(file), file);
var output = template.Render(new Model(root), member => member.Name);
var output = template.Render(new Model(rootArea), member => member.Name);

// Apply formatting since indenting isn't that nice in Scriban when rendering nested
// structures via functions.
Expand All @@ -61,7 +66,6 @@ void GenerateConstant(SourceProductionContext spc, ((string name, string value,
//}

spc.AddSource($"{name}.g.cs", SourceText.From(output, Encoding.UTF8));

}
}
}
19 changes: 13 additions & 6 deletions src/ThisAssembly.Constants/ThisAssembly.Constants.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="SourceItemType" />
<CompilerVisibleItemMetadata Include="Constant" MetadataName="Comment" />
<CompilerVisibleItemMetadata Include="Constant" MetadataName="Value" />
<CompilerVisibleItemMetadata Include="Constant" MetadataName="Root" />
</ItemGroup>

<ItemDefinitionGroup>
Expand All @@ -17,9 +18,8 @@
</FileConstant>
</ItemDefinitionGroup>

<Target Name="_InjectConstantAdditionalFiles"
BeforeTargets="PrepareForBuild;CompileDesignTime;GenerateMSBuildEditorConfigFileShouldRun"
DependsOnTargets="PrepareResourceNames">
<!-- Allows extenders to run before this target to add their @(Constant) and @(FileConstant) items -->
<Target Name="PrepareConstants">
<ItemGroup>
<FileConstant Condition="!$([System.IO.Path]::IsPathRooted('%(RelativeDir)')) OR '%(Link)' != ''">
<AreaPath Condition="!$([System.IO.Path]::IsPathRooted('%(RelativeDir)'))">%(RelativeDir)%(Filename)</AreaPath>
Expand All @@ -31,10 +31,17 @@
<Area>$([MSBuild]::ValueOrDefault('%(AreaPath)', '').Replace('\', '.').Replace('/', '.'))</Area>
<Value>%(AreaPath)%(FileExtension)</Value>
</FileConstant>
<Constant Include="@(FileConstant -> '%(Area)')"
Condition="'%(FileConstant.Area)' != '' AND '%(FileConstant.Value)' != ''"
Value="%(FileConstant.Value)"
<Constant Include="@(FileConstant -> '%(Area)')"
Condition="'%(FileConstant.Area)' != '' AND '%(FileConstant.Value)' != ''"
Value="%(FileConstant.Value)"
Comment="%(FileConstant.Comment)" />
</ItemGroup>
</Target>

<Target Name="_InjectConstantAdditionalFiles"
BeforeTargets="PrepareForBuild;CompileDesignTime;GenerateMSBuildEditorConfigFileShouldRun"
DependsOnTargets="PrepareResourceNames;PrepareConstants">
<ItemGroup>
<AdditionalFiles Include="@(Constant)" SourceItemType="Constant" />
</ItemGroup>
</Target>
Expand Down

0 comments on commit 5892aff

Please sign in to comment.