Skip to content

Commit

Permalink
Make project constants a special case of Constants
Browse files Browse the repository at this point in the history
Just like metadata, git and assembly info. This completes the unification of all packages, so that the specific ones just make configuring things in MSBuild easier by providing automatic conversions to @(Constant) items.
  • Loading branch information
kzu committed Sep 13, 2024
1 parent 54486ed commit 9427a92
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 164 deletions.
31 changes: 24 additions & 7 deletions src/ThisAssembly.Constants/ConstantsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Scriban;
using static Devlooped.Sponsors.SponsorLink;
using Resources = Devlooped.Sponsors.Resources;

namespace ThisAssembly;

Expand All @@ -29,11 +31,26 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (value != null && value.StartsWith("|") && value.EndsWith("|"))
value = value[1..^1].Replace('|', ';');

return (
name: Path.GetFileName(x.Left.Path),
value: value ?? "",
comment: string.IsNullOrWhiteSpace(comment) ? null : comment,
root: string.IsNullOrWhiteSpace(root) ? "Constants" : root!);
var name = Path.GetFileName(x.Left.Path);
if (string.IsNullOrEmpty(root))
{
root = "Constants";
}
else if (root == ".")
{
var parts = name.Split(['.'], 2);
if (parts.Length == 2)
{
// root should be the first part up to the first dot of name
// and name should be the rest
// note we only do this if there's an actual dot, otherwise, we
// just leave the root's default of Constants
root = parts[0];
name = parts[1];
}
}

return (name, value: value ?? "", comment: string.IsNullOrWhiteSpace(comment) ? null : comment, root!);
});

// Read the ThisAssemblyNamespace property or default to null
Expand Down Expand Up @@ -62,7 +79,7 @@ void GenerateConstant(SourceProductionContext spc, ((string name, string value,
return;
}

var rootArea = Area.Load(new List<Constant> { new Constant(name, value, comment), }, root);
var rootArea = Area.Load([new(name, value, comment),], root);
// For now, we only support C# though
var file = parse.Language.Replace("#", "Sharp") + ".sbntxt";
var template = Template.Parse(EmbeddedResource.GetContent(file), file);
Expand All @@ -89,6 +106,6 @@ void GenerateConstant(SourceProductionContext spc, ((string name, string value,
// .ToString();
//}

spc.AddSource($"{name}.g.cs", SourceText.From(output, Encoding.UTF8));
spc.AddSource($"{root}.{name}.g.cs", SourceText.From(output, Encoding.UTF8));
}
}
2 changes: 1 addition & 1 deletion src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
BeforeTargets="PrepareConstants"
DependsOnTargets="GetAssemblyAttributes">
<ItemGroup>
<Constant Include="%(AssemblyMetadata.Identity)" Value="%(AssemblyMetadata.Value)" Comment="%(AssemblyMetadata.Comment)" Root="Metadata" />
<Constant Include="@(AssemblyMetadata -> 'Metadata.%(Identity)')" Value="%(AssemblyMetadata.Value)" Comment="%(AssemblyMetadata.Comment)" Root="." />
</ItemGroup>
</Target>

Expand Down
40 changes: 0 additions & 40 deletions src/ThisAssembly.Project/CSharp.sbntxt

This file was deleted.

18 changes: 0 additions & 18 deletions src/ThisAssembly.Project/Model.cs

This file was deleted.

59 changes: 0 additions & 59 deletions src/ThisAssembly.Project/ProjectPropertyGenerator.cs

This file was deleted.

16 changes: 5 additions & 11 deletions src/ThisAssembly.Project/ThisAssembly.Project.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@
</InjectThisAssemblyProjectDependsOn>
</PropertyGroup>

<Target Name="PrepareProjectProperties" />

<Target Name="InjectThisAssemblyProject" DependsOnTargets="$(InjectThisAssemblyProjectDependsOn);PrepareProjectProperties"
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun">
<Target Name="PrepareProjectProperties" BeforeTargets="PrepareConstants">
<ItemGroup>
<ProjectPropertyDistinct Include="@(ProjectProperty -> Distinct())" />
</ItemGroup>
<PropertyGroup>
<ThisAssemblyProject>@(ProjectPropertyDistinct, '|')</ThisAssemblyProject>
</PropertyGroup>
<ItemGroup Condition="'$(ThisAssemblyProject)' != ''">
<CompilerVisibleProperty Include="@(ProjectPropertyDistinct)" />
<CompilerVisibleProperty Include="ThisAssemblyProject" />
<ItemGroup>
<!-- Collect requested properties, and eval their value -->
<Constant Include="@(ProjectPropertyDistinct -> 'Project.%(Identity)')" Value="$(%(ProjectPropertyDistinct.Identity))" Root="." />
</ItemGroup>
</Target>

</Project>
28 changes: 0 additions & 28 deletions src/ThisAssembly.Project/Visual Basic.sbntxt

This file was deleted.

0 comments on commit 9427a92

Please sign in to comment.