Dartk.CSharp.SourceGen.Fsx 0.3.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Dartk.CSharp.SourceGen.Fsx --version 0.3.0
NuGet\Install-Package Dartk.CSharp.SourceGen.Fsx -Version 0.3.0
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="Dartk.CSharp.SourceGen.Fsx" Version="0.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dartk.CSharp.SourceGen.Fsx --version 0.3.0
#r "nuget: Dartk.CSharp.SourceGen.Fsx, 0.3.0"
#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 Dartk.CSharp.SourceGen.Fsx as a Cake Addin
#addin nuget:?package=Dartk.CSharp.SourceGen.Fsx&version=0.3.0

// Install Dartk.CSharp.SourceGen.Fsx as a Cake Tool
#tool nuget:?package=Dartk.CSharp.SourceGen.Fsx&version=0.3.0

CSharp.SourceGen.Fsx

A C# source generator that runs F# scripts.

Prerequisites

Installation

dotnet add package Dartk.CSharp.SourceGen.Fsx

To avoid propagating dependency on the package set the option PrivateAssets="all" in the project file:

<ItemGroup>
    <PackageReference Include="Dartk.CSharp.SourceGen.Fsx" Version="0.3.0" PrivateAssets="All" />
</ItemGroup>

Source generation

Include F# script files with .fsx extension to the project as AdditionalFiles. For example, to execute all scripts in the Scripts folder add this to the project file:

<ItemGroup>
    <AdditionalFiles Include="Scripts/**" />
</ItemGroup>

.fsx script's output will be treated as a source code. Meaning that, the following script:

printf "
public static class HelloWorld
{
    public const string Str = \"Hello, World!\";
}
"

will generate a class:

public static class HelloWorld
{
    public const string Str = "Hello, World!";
}

A complete example is presented below.

Scripts that have file names starting with an underscore will not be executed. But they can be included in other scripts using #load statement. For instance:

  • _script.fsx - will not be executed, can be included in other scripts
  • other-script.fsx - will be executed

If a script references any other files, they should be included to the project as AdditionalFiles as well. Since the generator is incremental, it can only cache and detect changes for "additional" files.

Warning: Microsoft Visual Studio 22 (tested on version 17.4.3 on Windows OS) will call a source generator on every edit of the files that are being cached by the generator. Thus, every character insertion or deletion in a .fsx script will cause the script execution. Therefore, edit those files in an external editor for better performance.

Saving generated files

To save the generated source files set properties EmitCompilerGeneratedFiles and CompilerGeneratedFilesOutputPath in the project file:

<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    
    <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

Example

Create a new console C# project:

dotnet new console -n Example

Install the package Dartk.CSharp.SourceGen.Fsx and set the property PrivateAssets="All" by editing the project file Example.csproj:

<ItemGroup>
    <PackageReference Include="Dartk.CSharp.SourceGen.Fsx" Version="0.3.0" PrivateAssets="All"/>
</ItemGroup>

Create a Scripts folder in the project directory and include files within as AdditionalFiles:

<ItemGroup>
    <AdditionalFiles Include="Scripts/**" />
</ItemGroup>

Add the following file to the Scripts folder:

Number.fsx

#r "nuget: Scriban, 5.4.6"

open Scriban

"
namespace Generated;

public record Number(int Int, string String) {
    {{- i = 0 }}
    {{- for number in numbers }}
    public static readonly Number {{ string.capitalize number }} = new ({{ ++i }}, \"{{ number }}\");
    {{- end }}
}
"
|> fun template -> Template.Parse(template).Render({| numbers = [| "one"; "two"; "three" |] |})
|> printf "%s"

The script above will generate source file Number.g.cs:

// Generated from 'Number.fsx'

namespace Generated;

public record Number(int Int, string String) {
    public static readonly Number One = new (1, "one");
    public static readonly Number Two = new (2, "two");
    public static readonly Number Three = new (3, "three");
}

Now Generated.Number record can be used in your code.

Put this in the Program.cs:

using static System.Console;

WriteLine(Generated.Number.One);
WriteLine(Generated.Number.Two);
WriteLine(Generated.Number.Three);

It will write the following:

Number { Int = 1, String = one }
Number { Int = 2, String = two }
Number { Int = 3, String = three }

See also

There are no supported framework assets in this 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
0.3.0 176 2/16/2023