PodNet.EmbeddedTexts 1.0.1

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package PodNet.EmbeddedTexts --version 1.0.1                
NuGet\Install-Package PodNet.EmbeddedTexts -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PodNet.EmbeddedTexts" Version="1.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PodNet.EmbeddedTexts --version 1.0.1                
#r "nuget: PodNet.EmbeddedTexts, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PodNet.EmbeddedTexts as a Cake Addin
#addin nuget:?package=PodNet.EmbeddedTexts&version=1.0.1

// Install PodNet.EmbeddedTexts as a Cake Tool
#tool nuget:?package=PodNet.EmbeddedTexts&version=1.0.1                

PodNet.EmbeddedTexts Nuget

A simple C# incremental code generator that allows for efficiently embedding text file content into your code.

Usage

  1. Add the PodNet.EmbeddedTexts NuGet package to your .NET project.
  2. Add some text files (can be code files, can be markdown, plain text etc.) to your project you want the contents of to be available to you at compile time. Mark the files as having a build action of AdditionalFile. The most straightforward way to do this is by editing the .csproj project file and adding a pattern in an <ItemGroup>:
    <ItemGroup>
      <AdditionalFiles Include="Files/**" />
    </ItemGroup>
    
    You can do the above for individual files as well if you so choose. In this case you can even use the Visual Studio Properties Window (<kbd>F4</kbd> by default, while a file is being selected in Solution Explorer) and set the Build action property of the file(s) to "C# analyzer additional file". Once you do, Visual Studio will edit your .csproj file accordingly.
  3. Once you set this up, you'll immediately find a public static string Content { get; } property generated on a class in a namespace according to the structure of your project, which returns the string content of the file. So, if your root namespace for the project is MyProject, and you have an AdditionalFiles element in your project's Files/MyFile.txt file with content file contents, you'll be able to call:
    Console.WriteLine(Files.MyFile_txt.Content); // Writes: "file contents"
    

Additional configuration

The generator is on by default for every AdditionalFiles text file you have in your project, and will include the file contents in your compilation. If this is not your desired use-case, you have a few options.

Additionally, you can configure the name of the generated namespace and class for every piece of content.

[!NOTE]
It's imporant to mention that the below is standard configuration practice for MSBuild items. The MSBuild project files (like .csproj or Directory.build.props) define items in the children of ItemGroup elements. The AdditionalFiles element can be used by all modern source generators and are not specific to PodNet.EmbeddedTexts. One quirk of MSBuild items is that they can be Included individually or by globbing patterns, but the execution and parsing of these files is (mostly) sequential, so if you have any files Included by any patterns, you then have to Update them instead of Include-ing again.

Make the generator opt-in

Set the MSBuild property PodNetAutoEmbedAdditionalTexts to false to disable automatic generation for all AdditionalFiles. Then, for each file you wish to embed in the compilation, add a PodNet_EmbedText="true" attribute its AdditionalFiles item.

<Project>
  
  <PropertyGroup>
    
    <PodNetAutoEmbedAdditionalTexts>false</PodNetAutoEmbedAdditionalTexts>
  </PropertyGroup>
  <ItemGroup>
    
    <AdditionalFiles Include="Files/**" />

    
    <AdditionalFiles Update="Files/Embed/**" PodNet_EmbedText="true" />

    
    <AdditionalFiles Update="Files/Embed/.gitkeep" PodNet_EmbedText="false" />
  </ItemGroup>
</Project>

This is useful if you have any source generators enabled that work on text files you wish to not include in the compilation.

[!WARNING] Remember that including all text files in the compilation will essentially make your assemblies/executables larger by about the size of the file. The generated static class and property won't be loaded into memory until first being referenced, but this can incur a performance hit when embedding larger files.

Opt-out of generation for files or folders

Whether you have the generator automatically generating or not, you can explicitly opt-out of generation by setting PodNet_EmbedText="false".

<Project>
  
  <ItemGroup>
    
    <AdditionalFiles Include="Files/NoEmbed/**" PodNet_EmbedText="false" />
  </ItemGroup>
</Project>

Configuring the generated class name and namespace

You can set the PodNet_EmbedTextNamespace and PodNet_EmbedTextClassName properties on the items to override the default namespace and class name.

<Project>
  
  <ItemGroup>
    
    <AdditionalFiles Include="Files/My File.txt" PodNet_EmbedTextNamespace="OtherNamespace" PodNet_EmbedTextClassName="MyFileTXT" />
  </ItemGroup>
</Project>

The above results in: OtherNamespace.MyFileTXT.Content being the property that holds the file content.

Advanced parameterization

Don't be shy to use MSBuild properties, well-known metadata and such to configure the generator.

<AdditionalFiles Include="@(Compile)" PodNet_EmbedTextNamespace="$(RootNamespace).CompiledFiles" PodNet_EmbedTextClassName="%(Filename)" />

The above includes all .cs files (and other files that are at that point included in the compilation) into the source itself, in the MyProject.CompiledFiles namespace, with the class name being that of the filename without the extension.

Contributing and Support

This project is intended to be widely usable, but no warranties are provided. If you want to contact us, feel free to do so in the repo's [Discussions], at our website at podnet.hu, or find us anywhere from LinkedIn and Patreon to Meetup, YouTube or X.

Any kinds of contributions from issues to PRs and open discussions are welcome!

Don't forget to give us a ⭐ if you like this repo (it's free to give kudos!) or share it on socials, but we're not averse to offering you some benefits at our 🍻 Patreon 🍻 either, if you're so inclined!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.3 101 9/11/2024
1.2.2 111 9/10/2024
1.2.1 125 8/7/2024
1.2.0 105 7/18/2024
1.1.1 87 7/17/2024
1.0.1 102 7/15/2024
1.0.0 84 7/12/2024