Skip to content

Commit 1905881

Browse files
committed
Merge branch 'alpha'
2 parents 564948c + 8438508 commit 1905881

File tree

37 files changed

+3110
-0
lines changed

37 files changed

+3110
-0
lines changed

.gitignore

Lines changed: 484 additions & 0 deletions
Large diffs are not rendered by default.

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Changelog
2+
---
3+
4+
## v0.0.1-alpha02 (2024-09-09)
5+
6+
### Features
7+
8+
* Add `[Conditional]` attribute to restrict output of the `EmbeddedResourcePropertiesAttribute`
9+
10+
## v0.0.1-alpha (2024-09-09)
11+
12+
### Features
13+
14+
* Add support for overriding `ReadEmbeddedResourceValue` partial method

Directory.Build.props

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project>
2+
3+
<Import Project="version.props" />
4+
5+
<PropertyGroup>
6+
<Authors>Stephen Denne</Authors>
7+
<Copyright>Copyright © Stephen Denne 2024</Copyright>
8+
<CheckEolTargetFramework>false</CheckEolTargetFramework>
9+
</PropertyGroup>
10+
11+
<PropertyGroup>
12+
<NoPackageAnalysis>true</NoPackageAnalysis>
13+
<PackageOutputPath>$(MSBuildThisFileDirectory)\artifacts</PackageOutputPath>
14+
</PropertyGroup>
15+
16+
<Import Project="releasenotes.props" />
17+
18+
</Project>

EmbeddedResourceProperty.sln

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35222.181
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EmbeddedResourceProperty", "EmbeddedResourceProperty\EmbeddedResourceProperty.csproj", "{0CA5D352-B689-44D2-A8C2-61FD522FE84D}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EmbeddedResourcePropertyGenerator", "EmbeddedResourcePropertyGenerator\EmbeddedResourcePropertyGenerator.csproj", "{E236FDDB-F896-4BFD-AD97-54E6600902E1}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EmbeddedResourcePropertyGenerator.Tests", "EmbeddedResourcePropertyGenerator.Tests\EmbeddedResourcePropertyGenerator.Tests.csproj", "{5B687AED-BC94-489C-87EF-46E9C859BA74}"
11+
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{916AEDA2-766A-47A8-BA0D-31DBA403E706}"
13+
ProjectSection(SolutionItems) = preProject
14+
CHANGELOG.md = CHANGELOG.md
15+
Directory.Build.props = Directory.Build.props
16+
releasenotes.props = releasenotes.props
17+
version.props = version.props
18+
EndProjectSection
19+
EndProject
20+
Global
21+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
22+
Debug|Any CPU = Debug|Any CPU
23+
Release|Any CPU = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
26+
{0CA5D352-B689-44D2-A8C2-61FD522FE84D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{0CA5D352-B689-44D2-A8C2-61FD522FE84D}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{0CA5D352-B689-44D2-A8C2-61FD522FE84D}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{0CA5D352-B689-44D2-A8C2-61FD522FE84D}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{E236FDDB-F896-4BFD-AD97-54E6600902E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{E236FDDB-F896-4BFD-AD97-54E6600902E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{E236FDDB-F896-4BFD-AD97-54E6600902E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{E236FDDB-F896-4BFD-AD97-54E6600902E1}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{5B687AED-BC94-489C-87EF-46E9C859BA74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{5B687AED-BC94-489C-87EF-46E9C859BA74}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{5B687AED-BC94-489C-87EF-46E9C859BA74}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{5B687AED-BC94-489C-87EF-46E9C859BA74}.Release|Any CPU.Build.0 = Release|Any CPU
38+
EndGlobalSection
39+
GlobalSection(SolutionProperties) = preSolution
40+
HideSolutionNode = FALSE
41+
EndGlobalSection
42+
GlobalSection(ExtensibilityGlobals) = postSolution
43+
SolutionGuid = {5D61F622-E912-46F9-8E45-A72ECADD0382}
44+
EndGlobalSection
45+
EndGlobal

EmbeddedResourceProperty/Class1.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace EmbeddedResourceProperty
2+
{
3+
public class Class1
4+
{
5+
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.2">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
18+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
23+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
24+
<PackageReference Include="Verify.SourceGenerators" Version="2.4.0" />
25+
<PackageReference Include="Verify.Xunit" Version="26.3.0" />
26+
<PackageReference Include="xunit" Version="2.9.0" />
27+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
28+
<PrivateAssets>all</PrivateAssets>
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
</PackageReference>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<Using Include="Xunit" />
35+
</ItemGroup>
36+
37+
<ItemGroup>
38+
<ProjectReference Include="..\EmbeddedResourcePropertyGenerator\EmbeddedResourcePropertyGenerator.csproj" />
39+
</ItemGroup>
40+
41+
<ItemGroup>
42+
<None Update="GeneratorSnapshotTests.AlternateFolderSelectedCorrectly#Query.g.verified.cs">
43+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
44+
</None>
45+
<None Update="GeneratorSnapshotTests.AlternateFolderSelectedCorrectly#_EmbeddedResourcePropertiesAttribute.g.verified.cs">
46+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
47+
</None>
48+
<None Update="GeneratorSnapshotTests.AlternateFolderSelectedCorrectly#_EmbeddedResourceReader.g.verified.cs">
49+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
50+
</None>
51+
<None Update="GeneratorSnapshotTests.GeneratesEmbeddedResourcePropertiesCorrectly#Queries.g.verified.cs">
52+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
53+
</None>
54+
<None Update="GeneratorSnapshotTests.GeneratesEmbeddedResourcePropertiesCorrectly#_EmbeddedResourcePropertiesAttribute.g.verified.cs">
55+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
56+
</None>
57+
<None Update="GeneratorSnapshotTests.GeneratesEmbeddedResourcePropertiesCorrectly#_EmbeddedResourceReader.g.verified.cs">
58+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
59+
</None>
60+
<None Update="GeneratorSnapshotTests.GeneratesEmbeddedResourcePropertiesWithNoProperties#_EmbeddedResourcePropertiesAttribute.g.verified.cs">
61+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
62+
</None>
63+
<None Update="GeneratorSnapshotTests.GeneratesEmbeddedResourcePropertiesWithStrangeNamesCorrectly#_EmbeddedResourcePropertiesAttribute.g.verified.cs">
64+
<DependentUpon>GeneratorSnapshotTests.cs</DependentUpon>
65+
</None>
66+
</ItemGroup>
67+
68+
</Project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//HintName: Query.g.cs
2+
//------------------------------------------------------------------------------
3+
// <auto-generated>
4+
// This code was generated by the Datacute.EmbeddedResourcePropertyGenerator.
5+
// </auto-generated>
6+
//------------------------------------------------------------------------------
7+
8+
#nullable enable
9+
10+
file static class ReadingMethods
11+
{
12+
public static string ReadEmbeddedResource(string resourceName)
13+
{
14+
return global::Datacute.EmbeddedResourcePropertyGenerator.EmbeddedResourceReader.Read(typeof(Query), resourceName);
15+
}
16+
}
17+
file static class BackingFields
18+
{
19+
public static string? Example;
20+
}
21+
file static class ResourceNames
22+
{
23+
public const string Example = "EmbeddedResourcePropertyGenerator.Tests.Queries.example.sql";
24+
}
25+
/// <summary>
26+
/// This class's properties are generated from project files meeting the criteria:
27+
/// <list type="bullet">
28+
/// <item>
29+
/// <description>they are both an <c>EmbeddedResource</c> and an <c>AdditionalFile</c></description>
30+
/// </item>
31+
/// <item>
32+
/// <description>they are in the project folder <c>Queries</c></description>
33+
/// </item>
34+
/// <item>
35+
/// <description>they have the extension <c>.sql</c></description>
36+
/// </item>
37+
/// </list>
38+
/// </summary>
39+
public static partial class Query
40+
{
41+
static partial void ReadEmbeddedResourceValue(ref string? backingField, string resourceName, string propertyName);
42+
static partial void AlterEmbeddedResourceReturnValue(ref string value, string resourceName, string propertyName);
43+
44+
/// <summary>Text value of the Embedded Resource: example.sql</summary>
45+
/// <value>
46+
/// <code>
47+
/// Example sql content
48+
/// </code>
49+
/// </value>
50+
/// <remarks>
51+
/// The value is read from the embedded resource on first access.
52+
/// </remarks>
53+
public static string Example
54+
{
55+
get
56+
{
57+
ReadEmbeddedResourceValue(ref BackingFields.Example, ResourceNames.Example, "Example");
58+
var value = BackingFields.Example ??= ReadingMethods.ReadEmbeddedResource(ResourceNames.Example);
59+
AlterEmbeddedResourceReturnValue(ref value, ResourceNames.Example, "Example");
60+
return value;
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//HintName: _EmbeddedResourcePropertiesAttribute.g.cs
2+
//------------------------------------------------------------------------------
3+
// <auto-generated>
4+
// This code was generated by the Datacute.EmbeddedResourcePropertyGenerator.
5+
// </auto-generated>
6+
//------------------------------------------------------------------------------
7+
8+
#nullable enable
9+
10+
namespace Datacute.EmbeddedResourcePropertyGenerator;
11+
12+
/// <summary>
13+
/// Use a source generator to add properties to this class for each embedded resource file
14+
/// with a filename matching the given <see cref="Extension"/>
15+
/// found in the given <see cref="Path"/>.
16+
/// <para>
17+
/// If the path starts with "/" it is relative to the project root,
18+
/// otherwise it is relative to the folder containing the class with this attribute.
19+
/// If the path is not specified, the class name is used.
20+
/// </para>
21+
/// </summary>
22+
/// <remarks>
23+
/// Source generators only have access to <c>AdditionalFiles</c>. All <c>EmbeddedResource</c> files can be automatically included as <c>AdditionalFiles</c> by including the following line in the project file:
24+
/// <code>
25+
/// &lt;ItemGroup&gt;
26+
/// &lt;AdditionalFileItemNames&gt;$(AdditionalFileItemNames);EmbeddedResource&lt;/AdditionalFileItemNames&gt;
27+
/// &lt;/ItemGroup&gt;
28+
/// </code>
29+
/// </remarks>
30+
[global::System.Diagnostics.Conditional("DATACUTE_EMBEDDEDRESOURCEPROPERTIES_USAGES")]
31+
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
32+
internal sealed class EmbeddedResourcePropertiesAttribute : global::System.Attribute
33+
{
34+
/// <value>The filename extension of the embedded resource files
35+
/// to include as properties, defaulting to ".txt".</value>
36+
public string Extension { get; private set; }
37+
38+
/// <value>The path of the directory of embedded resource files
39+
/// to include as properties.</value>
40+
/// <remarks>
41+
/// If the path starts with "/" it is treated as relative to the project root,
42+
/// otherwise it is relative to the folder containing the class with this attribute.
43+
/// If the path is not specified, the class name is used.
44+
/// </remarks>
45+
public string? Path { get; private set; }
46+
47+
public EmbeddedResourcePropertiesAttribute(string extension = ".txt", string? path = null)
48+
{
49+
Extension = extension;
50+
Path = path;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//HintName: _EmbeddedResourceReader.g.cs
2+
//------------------------------------------------------------------------------
3+
// <auto-generated>
4+
// This code was generated by the Datacute.EmbeddedResourcePropertyGenerator.
5+
// </auto-generated>
6+
//------------------------------------------------------------------------------
7+
8+
#nullable enable
9+
10+
namespace Datacute.EmbeddedResourcePropertyGenerator;
11+
12+
public static class EmbeddedResourceReader
13+
{
14+
public static string Read(Type containingType, string resourceName)
15+
{
16+
var assembly = containingType.Assembly;
17+
using var stream = assembly.GetManifestResourceStream(resourceName)!;
18+
using var streamReader = new global::System.IO.StreamReader(stream, global::System.Text.Encoding.UTF8);
19+
var resourceText = streamReader.ReadToEnd();
20+
return resourceText;
21+
}
22+
}

0 commit comments

Comments
 (0)