Skip to content

Hello World Plugin

Jeff Campbell edited this page Jun 14, 2021 · 6 revisions

Overview

With Genesis v2, code generation plugins are written as classes in .Net Core assemblies that are loaded by the Genesis.CLI application. This tutorial walks through how to create a new code generation plugin step by step.

Steps

  • Create a new .Net Core Assembly Project targeting 3.1.X.

  • Add the Genesis.Plugin Nuget package to your project.

  • Create a new class ExampleCodeGeneratorPlugin. Add the following file contents below.
using Genesis.Plugin;

namespace HelloWorldPlugin
{
    /// <summary>
    /// An example code generation plugin that creates a single output class file.
    /// </summary>
    public class ExampleCodeGeneratorPlugin : ICodeGenerator
    {
        /// <inheritdoc />
        public string Name => nameof(ExamplePlugin);

        /// <inheritdoc />
        public int Priority => 0;

        /// <inheritdoc />
        public bool RunInDryMode => true;

        private string EXAMPLE_FILE_CONTENTS =
@"
public class ExamplePluginOutputClass { }
";

        /// <inheritdoc />
        public CodeGenFile[] Generate(CodeGeneratorData[] data)
        {
            return new CodeGenFile[]
            {
                new CodeGenFile(
                    "ExamplePluginOutputClass.cs",
                    EXAMPLE_FILE_CONTENTS,
                    Name)
            };
        }
    }
}
  • Build the project and copy the contents of the bin folder to a new folder in the Plugins folder of your GenesisCLI installation.
  • Add the new plugin to your genesis config by either:
    • If using Unity, you can update your GenesisSettings asset to include your new config by selecting the Auto Import button on the inspector OR.
    • Generate a new Genesis config by opening the command line to the GenesisCLI installation folder and entering dotnet genesis.cli.dll config create OR.
    • Opening an existing genesis config and add the full type name (including namespace) of your HelloWorldPlugin to the value entry of the key-value-pairs with keys Genesis.PreProcessors and Genesis.AllPreProcessors.

Redacted Example Json

{
  "keyValuePairs": [
    {
      "key": "Genesis.CodeGenerators",
      "value": "ExampleCodeGeneratorPlugin"
    },
    {
      "key": "Genesis.CodeGenerators",
      "value": "ExampleCodeGeneratorPlugin"
    }
  ]
}

Done! From this point on when running code generation your new ExampleCodeGeneratorPlugin should also execute.

Clone this wiki locally