Skip to content

Commit

Permalink
Merge pull request #32 from dansiegel/autogenerated-message
Browse files Browse the repository at this point in the history
add ability to customize Autogenerated Message
  • Loading branch information
dansiegel authored Feb 19, 2022
2 parents 08664ae + 422dcfc commit b5863ae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/CodeGenHelpers/CodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> _namespaceImports = new List<string>();
private readonly List<string> _assemblyAttributes = new List<string>();
private readonly Queue<IBuilder> _classes = new Queue<IBuilder>();
Expand Down Expand Up @@ -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();
Expand All @@ -96,10 +107,12 @@ private CodeWriter BuildInternal()
var writer = new CodeWriter(IndentStyle);
writer.AppendLine("//------------------------------------------------------------------------------");
writer.AppendLine("// <auto-generated>");
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("// </auto-generated>");
writer.AppendLine("//------------------------------------------------------------------------------");
writer.NewLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This is a Custom Defined Message
// See my Custom Copyright Message
//
// Don't Make Changes to this File!!!
// </auto-generated>
//------------------------------------------------------------------------------

namespace CodeGenHelpers.SampleCode
{
}
30 changes: 30 additions & 0 deletions tests/CodeGenHelpers.Tests/Tests/CodeBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit b5863ae

Please # to comment.