From 422dcfc33c394426614522fca27d5baaab8be3fd Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Fri, 18 Feb 2022 18:19:09 -0800 Subject: [PATCH] add ability to customize Autogenerated Message --- src/CodeGenHelpers/CodeBuilder.cs | 21 ++++++++++--- .../SampleCode/CustomAutoGeneratedMessage.cs | 12 ++++++++ .../Tests/CodeBuilderTests.cs | 30 +++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/CodeGenHelpers.Tests/SampleCode/CustomAutoGeneratedMessage.cs create mode 100644 tests/CodeGenHelpers.Tests/Tests/CodeBuilderTests.cs diff --git a/src/CodeGenHelpers/CodeBuilder.cs b/src/CodeGenHelpers/CodeBuilder.cs index 19c0f77..8fc016e 100644 --- a/src/CodeGenHelpers/CodeBuilder.cs +++ b/src/CodeGenHelpers/CodeBuilder.cs @@ -8,6 +8,11 @@ namespace CodeGenHelpers { public sealed class CodeBuilder { + private const string DefaultAutoGeneratedMessage = @"This code was generated. + +Changes to this file may cause incorrect behavior and will be lost if +the code is regenerated."; + private string _autoGeneratedMessage; private readonly List _namespaceImports = new List(); private readonly List _assemblyAttributes = new List(); private readonly Queue _classes = new Queue(); @@ -84,6 +89,12 @@ public EnumBuilder AddEnum(string name) return builder; } + public CodeBuilder WithAutoGeneratedMessage(string message) + { + _autoGeneratedMessage = message; + return this; + } + public void Clear() { _namespaceImports.Clear(); @@ -96,10 +107,12 @@ private CodeWriter BuildInternal() var writer = new CodeWriter(IndentStyle); writer.AppendLine("//------------------------------------------------------------------------------"); writer.AppendLine("// "); - writer.AppendLine("// This code was generated."); - writer.AppendLine("//"); - writer.AppendLine("// Changes to this file may cause incorrect behavior and will be lost if"); - writer.AppendLine("// the code is regenerated."); + var autoGeneratedMessage = string.IsNullOrEmpty(_autoGeneratedMessage) ? DefaultAutoGeneratedMessage : _autoGeneratedMessage; + var lines = Regex.Replace(autoGeneratedMessage, @"\r\n", "\n").Split('\n'); + foreach(var line in lines) + { + writer.AppendLine($"// {line}".Trim()); + } writer.AppendLine("// "); writer.AppendLine("//------------------------------------------------------------------------------"); writer.NewLine(); diff --git a/tests/CodeGenHelpers.Tests/SampleCode/CustomAutoGeneratedMessage.cs b/tests/CodeGenHelpers.Tests/SampleCode/CustomAutoGeneratedMessage.cs new file mode 100644 index 0000000..b1f8aae --- /dev/null +++ b/tests/CodeGenHelpers.Tests/SampleCode/CustomAutoGeneratedMessage.cs @@ -0,0 +1,12 @@ +//------------------------------------------------------------------------------ +// +// This is a Custom Defined Message +// See my Custom Copyright Message +// +// Don't Make Changes to this File!!! +// +//------------------------------------------------------------------------------ + +namespace CodeGenHelpers.SampleCode +{ +} diff --git a/tests/CodeGenHelpers.Tests/Tests/CodeBuilderTests.cs b/tests/CodeGenHelpers.Tests/Tests/CodeBuilderTests.cs new file mode 100644 index 0000000..aad1de8 --- /dev/null +++ b/tests/CodeGenHelpers.Tests/Tests/CodeBuilderTests.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +namespace CodeGenHelpers.Tests +{ + public class CodeBuilderTests : TestBase + { + public CodeBuilderTests(ITestOutputHelper testOutput) + : base(testOutput) + { + } + + [Fact] + public void AddsCustomAutoGeneratedMessage() + { + var builder = CodeBuilder.Create(Namespace) + .WithAutoGeneratedMessage(@"This is a Custom Defined Message +See my Custom Copyright Message + +Don't Make Changes to this File!!!"); + + MakeAssertion(builder, "CustomAutoGeneratedMessage"); + } + } +}