RSCG – NFH.FileEmbed

RSCG – NFH.FileEmbed
 
 

name NFH.FileEmbed
nuget https://www.nuget.org/packages/NFH.FileEmbed/
link https://github.com/SirCxyrtyx/FileEmbed
author Natalie Howes

Generating embedded files

 

This is how you can use NFH.FileEmbed .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  
  <ItemGroup>
    <EmbeddedResource Include="TestData\Countries.txt" />
  </ItemGroup>

  
  <ItemGroup>
    <PackageReference Include="NFH.FileEmbed" Version="0.1.0-beta" />
  </ItemGroup>

  <PropertyGroup>
		<AdditionalFileItemNames>$(AdditionalFileItemNames);EmbeddedResource</AdditionalFileItemNames>
	</PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<!--<ItemGroup>
		<CompilerVisibleProperty Include="FileEmbed_MaxEmbedSize" />
	</ItemGroup>
	<PropertyGroup>
		<FileEmbed_MaxEmbedSize>SIZE_IN_BYTES_GOES_HERE</FileEmbed_MaxEmbedSize>
	</PropertyGroup>-->
</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using EmbedDemo;
using System.Text;

Console.WriteLine("Hello, World!");

var data= TestData.Countries();

Console.WriteLine(Encoding.UTF8.GetString(data) );


using FileEmbed;

namespace EmbedDemo;

public static partial class TestData
{
    [FileEmbed(@"TestData\Countries.txt")]
    public static partial ReadOnlySpan<byte> Countries();
}



USA
Germany
France
Romania
Italy


 

The code that is generated is

namespace EmbedDemo
{
	partial class TestData
	{
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("FileEmbed", "0.1.0.0")]
		public static partial global::System.ReadOnlySpan<byte> Countries() => "USA\r\nGermany\r\nFrance\r\nRomania\r\nItaly\r\n"u8;
	}
}

namespace FileEmbed
{
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("FileEmbed", "0.1.0.0")]
    [global::System.Diagnostics.Conditional("FILE_EMBED_KEEP_SOURCE_GENERATOR_ATTRIBUTE")]
    [global::System.AttributeUsage(global::System.AttributeTargets.Method, Inherited = false)]
    internal class FileEmbedAttribute : global::System.Attribute
    {
        public string FilePath { get; }

        public long Offset { get; }

        public int Length { get; }

        public FileEmbedAttribute(string filePath) : this(filePath, 0, -1) { }

        public FileEmbedAttribute(string filePath, long offset) : this(filePath, offset, -1) { }

        public FileEmbedAttribute(string filePath, long offset, int length)
        {
            FilePath = filePath;
            Offset = offset;
            Length = length;
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/NFH.FileEmbed


Posted

in

, ,

by

Tags: