Podimo.ConstEmbed by Podimo
Nuget / site data
Details
Info
Name: Podimo.ConstEmbed
A source generator that turns additional files into file constants in a generated namespace. This is an alternative approach to embedding files in C# source manually, or loading them manually as embedded resources via reflection. With ConstEmbed, you will never have to see a runtime error because you mistyped the name, as the constants are evaluated at compile-time.
Author: Podimo
NuGet: https://www.nuget.org/packages/Podimo.ConstEmbed/
You can find more details at https://github.com/podimo/Podimo.ConstEmbed
Original Readme
Podimo.ConstEmbed
This project is a Source Generator which generates constant strings from files at compile-time.
Using
We use project files to control the generation of constants. You can see how these are used in Podimo.ExampleConsoleApp.
License
See LICENSE-APACHE, LICENSE-MIT.
About
File content transformed to constants
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- createDB.sql
This is the CSharp Project that references Podimo.ConstEmbed
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Podimo.ConstEmbed" Version="1.0.2" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<PropertyGroup>
<!-- The namespace under which we generate the constants. -->
<ConstEmbedNamespace>MyAppNamespace</ConstEmbedNamespace>
<!-- The visibility of the classes in which the constants are declared. -->
<ConstEmbedVisibility>public</ConstEmbedVisibility>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="sql/*.sql" ConstEmbed="SQL" />
</ItemGroup>
<ItemGroup>
<None Remove="sql\createDB.sql" />
</ItemGroup>
</Project>
This is the use of Podimo.ConstEmbed in Program.cs
Console.WriteLine(MyAppNamespace.SQL.createDB);
This is the use of Podimo.ConstEmbed in createDB.sql
create database Andrei;
GO;
use Andrei;
GO;
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- SQL.createDB.g.cs
namespace MyAppNamespace
{
public static partial class SQL
{
public const string createDB = @"create database Andrei;
GO;
use Andrei;
GO;
";
}
}
Usefull
Download Example (.NET C# )
Share Podimo.ConstEmbed
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Podimo.ConstEmbed