A collection of helpful source generators.
- ConstantFromJsonGenerator.cs: A source generator that creates a constant from a JSON file.
- LibManResourceVersionGenerator.cs: Automatically creates constants with the
LibMan_prefix from libraries entries in the libman.json file
You can watch a demo video of the project here.
For general details about and on using the Helpful Libraries see the root Readme.
-
Add a JSON file to your project.
-
Set the
Build Actionof the JSON file toAdditionalFilesfor example:<ItemGroup> <AdditionalFiles Include="package.json" /> </ItemGroup>
-
Add reference to both the Source Generator and the Attributes project (which contains the marker attributes like
[ConstantFromJson]) and make sure to include the project as analyzer:<ProjectReference Include="..\Lombiq.HelpfulLibraries.Attributes\Lombiq.HelpfulLibraries.Attributes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" /> <ProjectReference Include="..\Lombiq.HelpfulLibraries.SourceGenerators\Lombiq.HelpfulLibraries.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
-
In the samples you can also see the snippet below, while not strictly necessary for the source generator to function, it suppresses a warning that happens in Visual Studio when first cloning the project. If you do decide to include this part make sure you update the relative paths to the correct location of the projects.
<PropertyGroup> <SourceGeneratorLocation>$(SolutionDir)src\Libraries\Lombiq.HelpfulLibraries\Lombiq.HelpfulLibraries.SourceGenerators\bin\Debug\netstandard2.0\Lombiq.HelpfulLibraries.SourceGenerators.dll</SourceGeneratorLocation> <SourceGeneratorLocation Condition=" '$(Configuration)' != 'Debug' ">$(SolutionDir)src\Libraries\Lombiq.HelpfulLibraries\Lombiq.HelpfulLibraries.SourceGenerators\bin\Release\netstandard2.0\Lombiq.HelpfulLibraries.SourceGenerators.dll</SourceGeneratorLocation> </PropertyGroup> <Target Name="CustomBeforeCompile" BeforeTargets="Compile"> <MSBuild Condition="!Exists('$(SourceGeneratorLocation)')" Projects="..\Lombiq.HelpfulLibraries.SourceGenerators\Lombiq.HelpfulLibraries.SourceGenerators.csproj" /> </Target>
-
Wherever you want to use the JSON file, make sure to use a
partial class.
-
Follow the steps above.
-
Add the
[ConstantFromJsonGenerator]attribute to yourpartialclass. Where the first parameter is the name of the constant and the second parameter is the path to the JSON file, the last parameter is the name or 'key' for the value we are looking for.[ConstantFromJson("GulpVersion", "package.json", "gulp")] public partial class YourClass { }
-
Run a build and the constant will be generated.
-
Use the constant in your code, full example:
using System; using Generators; namespace Lombiq.HelpfulLibraries.SourceGenerators.Sample; [ConstantFromJson("GulpUglifyVersion", "package.json", "gulp-uglify")] [ConstantFromJson("GulpVersion", "package.json", "gulp")] public partial class Examples { // Show usage of the generated constants public void LogVersions() { Console.WriteLine(GulpUglifyVersion); Console.WriteLine(GulpVersion); } }
This source generator is for LibraryManager users. It allows you to access the versions of the libraries defined in your libman.json file as constants in your code. This way, if your code needs the dependency versions (like it is the case when defining resource manifests in Orchard Core), you can avoid hardcoding them and instead refer to the generated constants, ensuring consistency and easier maintenance.
-
Follow the general steps above.
-
Add the
[LibManVersions]attribute to yourpartialclass. -
Run a build and an individual constant will be generated for each entry in the
librariesarray of your libman.json file. Each array item is turned into a separate constant using the value of itslibraryproperty. The part after the@becomes the value and the part before it becomes the constant's name using theLibManVersions.{sanitized}formula. Heresanitizedis the name where all non-alphanumeric characters are treated as word boundaries when converting into PascalCase and removed to comply with C# variable naming rules. -
Use the constant in your code, full example:
using System; using Generators; namespace Lombiq.HelpfulLibraries.SourceGenerators.Sample; [LibManVersions] public partial class Examples { // Show usage of the generated constants public void LogVersions() { Console.WriteLine(LibManVersions.ChartJs); // Outputs "4.5.1". Derived from "chart.js@4.5.1". Console.WriteLine(LibManVersions.ChartjsPluginAnnotation); // Outputs "3.1.0". Derived from "chartjs-plugin-annotation@3.1.0". } }