Skip to content

Commit

Permalink
Merge pull request #12 from BastianBlokland/feature/header-opt-out
Browse files Browse the repository at this point in the history
Feature/header opt out
  • Loading branch information
BastianBlokland authored Aug 7, 2019
2 parents 5a99c6e + 060d86c commit fb65b5e
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 25 deletions.
4 changes: 2 additions & 2 deletions example/ExpensiveProducts.g.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Generated by: EnumGenerator.Core - 0.1.920.0
// Generated by: EnumGenerator.Core - 3.0.1187.0
// </auto-generated>
//------------------------------------------------------------------------------

using System.CodeDom.Compiler;

[GeneratedCode("EnumGenerator.Core", "0.1.920.0")]
[GeneratedCode("EnumGenerator.Core", "3.0.1187.0")]
public enum ExpensiveProducts
{
Drill = 0,
Expand Down
4 changes: 2 additions & 2 deletions example/Months.g.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Generated by: EnumGenerator.Core - 0.1.920.0
// Generated by: EnumGenerator.Core - 3.0.1187.0
// </auto-generated>
//------------------------------------------------------------------------------

Expand All @@ -11,7 +11,7 @@ namespace Example
/// <summary>
/// Enum with all the months.
/// </summary>
[GeneratedCode("EnumGenerator.Core", "0.1.920.0")]
[GeneratedCode("EnumGenerator.Core", "3.0.1187.0")]
public enum Months
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- Major.Minor adjust manually -->
<PropertyGroup>
<VersionPrefix>3.0</VersionPrefix>
<VersionPrefix>3.1</VersionPrefix>
</PropertyGroup>

<!-- Automatically set suffix info based on environment args (if availble) -->
Expand Down
3 changes: 3 additions & 0 deletions src/EnumGenerator.Cli/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public Application(ILogger<Application> logger)
/// <param name="enumNamespace">
/// Optional namespace to add the generated enum to.
/// </param>
/// <param name="headerMode">Mode to use when adding a header</param>
/// <param name="indentMode">Mode to use when indenting text</param>
/// <param name="indentSize">When indenting with spaces this controls how many</param>
/// <param name="newlineMode">Mode to use when adding newlines to text</param>
Expand All @@ -67,6 +68,7 @@ public int Run(
string entryCommentJPath,
string enumComment,
string enumNamespace,
Core.Exporter.HeaderMode headerMode,
CodeBuilder.IndentMode indentMode,
int indentSize,
CodeBuilder.NewlineMode newlineMode,
Expand Down Expand Up @@ -129,6 +131,7 @@ public int Run(
{
csharp = enumDefinition.ExportCSharp(
enumNamespace,
headerMode,
indentMode,
indentSize,
newlineMode,
Expand Down
1 change: 1 addition & 0 deletions src/EnumGenerator.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static int MainWithOptions(ProgramOptions options)
options.EntryCommentJPath,
options.EnumComment,
options.EnumNamespace,
options.HeaderMode,
options.IndentMode,
options.IndentSize,
options.NewlineMode,
Expand Down
32 changes: 18 additions & 14 deletions src/EnumGenerator.Cli/ProgramOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,59 +26,63 @@ public static IEnumerable<Example> Examples
}

/// <summary>Path to the json-file to generate the enum from</summary>
[Option('i', "input", Required = true, HelpText = "Json file to generate the enum from")]
[Option('i', "input", Required = true, HelpText = "Json file to generate the enum from.")]
public string InputFile { get; set; }

/// <summary>Path where to save the generated enum</summary>
[Option('o', "output", Required = true, HelpText = "Target path for the generated enum")]
[Option('o', "output", Required = true, HelpText = "Target path for the generated enum.")]
public string OutputFile { get; set; }

/// <summary>JPath to the collection to base the enum on</summary>
[Option('c', "collection", Required = false, Default = "[*]", HelpText = "JPath to the collection to base the enum on")]
[Option('c', "collection", Required = false, Default = "[*]", HelpText = "JPath to the collection to base the enum on.")]
public string CollectionJPath { get; set; }

/// <summary>JPath to a name field of a entry in the input file</summary>
[Option('e', "entryname", Required = false, Default = "name", HelpText = "JPath to the name field of a entry in the input file")]
[Option('e', "entryname", Required = false, Default = "name", HelpText = "JPath to the name field of a entry in the input file.")]
public string EntryNameJPath { get; set; }

/// <summary>JPath to a value field of a entry in the input file</summary>
[Option("entryvalue", Required = false, HelpText = "JPath to a value field of a entry in the input file")]
[Option("entryvalue", Required = false, HelpText = "JPath to a value field of a entry in the input file.")]
public string EntryValueJPath { get; set; }

/// <summary>JPath to a comment field of a entry in the input file</summary>
[Option("entrycomment", Required = false, HelpText = "JPath to a comment field of a entry in the input file")]
[Option("entrycomment", Required = false, HelpText = "JPath to a comment field of a entry in the input file.")]
public string EntryCommentJPath { get; set; }

/// <summary>Comment to add to the generated enum</summary>
[Option("comment", Required = false, HelpText = "Comment to add to the generated enum")]
[Option("comment", Required = false, HelpText = "Comment to add to the generated enum.")]
public string EnumComment { get; set; }

/// <summary>Namespace to add the created enum to</summary>
[Option("namespace", Required = false, HelpText = "Namespace to add the created enum to")]
[Option("namespace", Required = false, HelpText = "Namespace to add the created enum to.")]
public string EnumNamespace { get; set; }

/// <summary>Mode to use when adding a header</summary>
[Option("headermode", Required = false, Default = Core.Exporter.HeaderMode.Default, HelpText = "Mode to use when adding a header.")]
public Core.Exporter.HeaderMode HeaderMode { get; set; }

/// <summary>Indentation to use while writing output text</summary>
[Option("indentmode", Required = false, Default = CodeBuilder.IndentMode.Spaces, HelpText = "Indentation to use when writing output text")]
[Option("indentmode", Required = false, Default = CodeBuilder.IndentMode.Spaces, HelpText = "Indentation to use when writing output text.")]
public CodeBuilder.IndentMode IndentMode { get; set; }

/// <summary>When indenting with spaces this controls how many</summary>
[Option("indentsize", Required = false, Default = 4, HelpText = "When indenting with spaces this controls how many")]
[Option("indentsize", Required = false, Default = 4, HelpText = "When indenting with spaces this controls how many.")]
public int IndentSize { get; set; }

/// <summary>Mode to use for ending lines</summary>
[Option("newlinemode", Required = false, Default = CodeBuilder.NewlineMode.Unix, HelpText = "Which newline mode to use when writing output text")]
[Option("newlinemode", Required = false, Default = CodeBuilder.NewlineMode.Unix, HelpText = "Which newline mode to use when writing output text.")]
public CodeBuilder.NewlineMode NewlineMode { get; set; }

/// <summary>Storage type for the exported enum</summary>
[Option("storagetype", Required = false, Default = Core.Exporter.StorageType.Implicit, HelpText = "Underlying storage type for the exported enum")]
[Option("storagetype", Required = false, Default = Core.Exporter.StorageType.Implicit, HelpText = "Underlying storage type for the exported enum.")]
public Core.Exporter.StorageType StorageType { get; set; }

/// <summary>Mode to use when writing curly-brackets</summary>
[Option("curlybracket", Required = false, Default = Core.Exporter.CurlyBracketMode.NewLine, HelpText = "Which curlybracket-mode to use")]
[Option("curlybracket", Required = false, Default = Core.Exporter.CurlyBracketMode.NewLine, HelpText = "Which curlybracket-mode to use.")]
public Core.Exporter.CurlyBracketMode CurlyBracketMode { get; set; }

/// <summary>Switch to enable verbose logging</summary>
[Option('v', "verbose", Required = false, HelpText = "Enable verbose output")]
[Option('v', "verbose", Required = false, HelpText = "Enable verbose output.")]
public bool Verbose { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/EnumGenerator.Cli/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Can be used to generate c# enum files from json files.

Add a reference to the cli-tool to a `ItemGroup` section your of your csproj.
```xml
<DotNetCliToolReference Include="EnumGenerator.Cli" Version="3.0.*" />
<DotNetCliToolReference Include="EnumGenerator.Cli" Version="3.1.*" />
```

## Usage
Expand Down
10 changes: 8 additions & 2 deletions src/EnumGenerator.Core/Exporter/CSharpExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static class CSharpExporter
/// </exception>
/// <param name="enumDefinition">Enum to generate c# source-code for</param>
/// <param name="namespace">Optional namespace to add the enum to</param>
/// <param name="headerMode">Mode to use when adding a header</param>
/// <param name="indentMode">Mode to use for indenting</param>
/// <param name="spaceIndentSize">When indenting with spaces this controls how many</param>
/// <param name="newlineMode">Mode to use for ending lines</param>
Expand All @@ -33,6 +34,7 @@ public static class CSharpExporter
public static string ExportCSharp(
this EnumDefinition enumDefinition,
string @namespace = null,
HeaderMode headerMode = HeaderMode.Default,
CodeBuilder.IndentMode indentMode = CodeBuilder.IndentMode.Spaces,
int spaceIndentSize = 4,
CodeBuilder.NewlineMode newlineMode = CodeBuilder.NewlineMode.Unix,
Expand All @@ -45,8 +47,12 @@ public static string ExportCSharp(
throw new OutOfBoundsValueException(storageType, oobEntry.Value);

var builder = new CodeBuilder(indentMode, spaceIndentSize, newlineMode);
builder.AddHeader();
builder.WriteEndLine();
if (headerMode != HeaderMode.None)
{
builder.AddHeader();
builder.WriteEndLine();
}

builder.WriteLine("using System.CodeDom.Compiler;");
builder.WriteEndLine();

Expand Down
18 changes: 18 additions & 0 deletions src/EnumGenerator.Core/Exporter/HeaderMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace EnumGenerator.Core.Exporter
{
/// <summary>
/// Mode to use when adding a header.
/// </summary>
public enum HeaderMode
{
/// <summary>
/// Add the default header when exporting.
/// </summary>
Default = 0,

/// <summary>
/// Don't add any header when exporting.
/// </summary>
None = 1
}
}
4 changes: 2 additions & 2 deletions src/EnumGenerator.Core/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Can be used for more complex integration into a build pipeline, for simple use-c
There are two ways to add the nuget package:
1. Run:
```bash
dotnet add package EnumGenerator.Core --version '3.0.*'
dotnet add package EnumGenerator.Core --version '3.1.*'
```
2. Add the following to a `ItemGroup` section of your csproj:
```xml
<PackageReference Include="EnumGenerator.Core" Version="3.0.*" />
<PackageReference Include="EnumGenerator.Core" Version="3.1.*" />
```

## Usage
Expand Down
27 changes: 26 additions & 1 deletion src/EnumGenerator.Tests/Exporter/CSharpExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace EnumGenerator.Tests.Builder
{
public sealed class CSharpExporterTests
{
private const string Version = "3.0.0.0";
private const string Version = "3.1.0.0";

[Fact]
public void ThrowsIfExportedWithInvalidNamespace() => Assert.Throws<InvalidNamespaceException>(() =>
Expand Down Expand Up @@ -284,6 +284,31 @@ public enum TestEnum {{
B = 2,
}}
}}
",
actual: export);
}

[Fact]
public void CanBeExportedWithoutHeader()
{
var builder = new EnumBuilder("TestEnum");
builder.PushEntry("A", 1);
builder.PushEntry("B", 2);
var enumDef = builder.Build();

var export = enumDef.ExportCSharp(headerMode: HeaderMode.None);

Assert.Equal(
expected:
$@"using System.CodeDom.Compiler;
[GeneratedCode(""EnumGenerator.Core"", ""{Version}"")]
public enum TestEnum
{{
A = 1,
B = 2,
}}
",
actual: export);
}
Expand Down

0 comments on commit fb65b5e

Please sign in to comment.