Skip to content

CLI Tool customization

Jeff Greene edited this page Nov 11, 2022 · 1 revision

The Harmony Core CLI tool has a set of customization points to allow you to change how it behaves in your environment. These customization points take the form of .csx (csharp script) files.

Load customization

During the load process, the cli tool reads the contents of your Harmony.Core.CodeGen.json file. It de-serializes this file into the Solution class that we use internally for all operations around configuration and codegen. If you have custom steps that are required before this solution is loaded you can put a script file at Config\LoadSolution.csx in your solution folder. Below is an example LoadSolution.csx file

class MySolutionLoader
{
	public static async Task<Solution> LoadSolution(string targetJsonFile, string solutionDir, Action<string> logger)
	{
		//var processResult = await Dbl.RunDBR(myWorkingDirectory, cmdline, (logMsg) => {/*log messages are returned here*/});
		logger("Hello from a custom solution loader");
		var loadedSolution = Solution.LoadSolution(targetJsonFile, solutionDir);
		logger("Hello from after the solution was loaded");
		return loadedSolution;
	}
}

return (Func<string, string, Action<string>, Task<Solution>>)MySolutionLoader.LoadSolution;

Generator customization

During code generation, the Harmony Core CLI tool uses its built in generators to determine what templates are generated and what parameters are passed in during that generation process. You can add new generator types or override existing generator types. The builtin generators are named as follows

  • ODataGenerator
  • ModelGenerator
  • TraditionalBridgeGenerator
  • SignalRGenerator
  • WebApiGenerator
  • EFCoreGenerator

You can find all their definitions here

the folder Generators/Enabled will be searched and all *.csx files will be compiled/run. The name of the file (without extension) is what determines the generators name. For example in order to override the ODataGenerator you would create a file called ODataGenerator.csx in the Generators/Enabled folder

The following code is an example generator override for OData that adds a new codegen task for all structures marked for odata support.


public class CustomODataGenerator : ODataGenerator
{
    public override void ApplyDefaults(Solution solution)
    {

    }

    public override List<string> ValidateSolution(Solution solution)
    {
        return new List<string>();
    }

    public override List<CodeGenTask> GenerateTasks(Solution solution)
    {
        var baseTasks = base.GenerateTasks(solution);
        baseTasks.Add(StructureTaskHelper(solution, "Generate controller properties partial classes", solution.ControllersNamespace, solution.ControllersFolder, false, nameof(ODataGenerator), new string[] { "ODataControllerPropertyEndpoints" }, null));
		return baseTasks;
	}
}

new CustomODataGenerator()
Clone this wiki locally