Skip to content

Commit

Permalink
feat: add --verify parameter to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Sep 11, 2024
1 parent 598a892 commit 7254173
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 7 deletions.
8 changes: 5 additions & 3 deletions docs/stacks/agnostic/generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ All configuration of the way that Coalesce interacts with your projects, includi

There are a couple of extra options which are only available as CLI parameters to ``dotnet coalesce``. These options do not affect the behavior of the code generation - only the behavior of the CLI itself.

``--debug`` - When this flag is specified when running ``dotnet coalesce``, Coalesce will wait for a debugger to be attached to its process before starting code generation.

``-v|--verbosity <level>`` - Set the verbosity of the output. Options are ``trace``, ``debug``, ``information``, ``warning``, ``error``, ``critical``, and ``none``.
- `<configFile>` - First positional parameter. Path to the `coalesce.json` configuration file. If not specified, will search upwards from the current folder for a file named `coalesce.json`.
- ``--debug`` - When this flag is specified when running ``dotnet coalesce``, Coalesce will wait for a debugger to be attached to its process before starting code generation.
- `--what-if|-WhatIf` - Runs all code generation, but does not make changes to disk.
- `--verify` - Assert that the code generation does not have any pending changes to its output. Useful in CI builds when combined with `--what-if` to ensure that developers haven't forgotten to run code gen before committing changes.
- ``-v|--verbosity <level>`` - Set the verbosity of the output. Options are ``trace``, ``debug``, ``information``, ``warning``, ``error``, ``critical``, and ``none``.

## Generated Code

Expand Down
4 changes: 3 additions & 1 deletion playground/Coalesce.Web.Vue3/src/models.g.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public Task CleanupAsync(ICollection<string> knownGoodFiles)
{
if (!knownGoodFiles.Any(goodFile => Path.GetFullPath(goodFile) == Path.GetFullPath(file)))
{
Owner.ActionPerformed();
logger.LogWarning(
(DryRun ? " What if: " : "") +
$"Deleting {file} because it was not in the generation outputs.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Task CleanupAsync(ICollection<string> knownGoodFiles)
logger.LogTrace($"Cleaning {this}");
if (File.Exists(TargetPath))
{
Owner.ActionPerformed();
logger.LogWarning(
(DryRun ? " What if: " : "") +
$"Deleting {TargetPath} because it was explicitly flagged for removal."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace IntelliTect.Coalesce.CodeGeneration.Generation
Expand All @@ -26,5 +27,12 @@ public GenerationContext(CoalesceConfiguration config)
public string AreaName => CoalesceConfiguration.Output.AreaName;

public string TypescriptModulePrefix => CoalesceConfiguration.Output.TypescriptModulePrefix;

private int actionCount = 0;
public void ActionPerformed()
{
Interlocked.Increment(ref actionCount);
}
public int ActionsPerformedCount => actionCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public GenerationExecutor(CoalesceConfiguration config, LogLevel logLevel)
public ILogger<GenerationExecutor> Logger { get; private set; }
public ServiceProvider ServiceProvider { get; private set; }

public GenerationContext GenerationContext => ServiceProvider.GetRequiredService<GenerationContext>();

public Task GenerateAsync<TGenerator>()
where TGenerator : IRootGenerator
{
Expand Down Expand Up @@ -85,7 +87,7 @@ public async Task GenerateAsync(Type rootGenerator)


Logger = ServiceProvider.GetRequiredService<ILogger<GenerationExecutor>>();
var genContext = ServiceProvider.GetRequiredService<GenerationContext>();
var genContext = GenerationContext;

Logger.LogInformation("Loading Projects:");
await LoadProjects(Logger, genContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override async Task GenerateAsync()

Uri relPath = new Uri(Environment.CurrentDirectory + Path.DirectorySeparatorChar).MakeRelativeUri(new Uri(outputFile));

this.ActionPerformed();
Logger?.LogInformation(
(DryRun ? " What if: " : "") +
$"{(isRegen ? "Reg" : "G")}enerated: {Uri.UnescapeDataString(relPath.OriginalString)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public abstract class Generator : IGenerator

protected Generator(GeneratorServices services)
{
Services = services;
Logger = services.LoggerFactory.CreateLogger(GetType().Name);
Configure(services.CoalesceConfiguration);
DryRun = services.CoalesceConfiguration.DryRun;
}

protected GeneratorServices Services { get; }
protected ILogger Logger { get; }

public bool DryRun { get; private set; }
Expand Down Expand Up @@ -76,5 +78,7 @@ public virtual void Configure(JObject obj)
}
}
}

public void ActionPerformed() => Services.GenerationContext.ActionPerformed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface IGenerator
Task GenerateAsync();

void Configure(JObject obj);
void ActionPerformed();
}

public interface IGenerator<TModel> : IGenerator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ public abstract class StringBuilderCSharpGenerator<TModel> : StringBuilderFileGe
{
public StringBuilderCSharpGenerator(GeneratorServices services) : base(services)
{
Services = services;
}

public GeneratorServices Services { get; }
public GenerationContext GenerationContext => Services.GenerationContext;

// TODO: Should we remove AreaName entirely? Pretty sure nobody uses it.
Expand Down
10 changes: 10 additions & 0 deletions src/IntelliTect.Coalesce.DotnetTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class Program
Description = "Do not write output files to disk.", LongName = "what-if", ShortName = "WhatIf")]
public bool DryRun { get; }

[Option(CommandOptionType.NoValue,
Description = "Verify that no output changes have been made. Use in CI builds to ensure that codegen has not been forgotten.", LongName = "verify", ShortName = "")]
public bool Verify { get; }

[Argument(0, "config", Description =
"Path to a coalesce.json configuration file that will drive generation. If not specified, it will search in current folder.")]
public string ConfigFile { get; }
Expand Down Expand Up @@ -123,6 +127,12 @@ private async Task<int> OnExecuteAsync(CommandLineApplication app)
return -1;
}

if (Verify && executor.GenerationContext.ActionsPerformedCount > 0)
{
executor.Logger.LogError("Output has uncommitted changes. Run `dotnet coalesce` and commit the changes.");
return -1;
}

return 0;
}

Expand Down

0 comments on commit 7254173

Please sign in to comment.