Skip to content

Commit

Permalink
Merge pull request #106 from Kentico/feature/refactoring-models
Browse files Browse the repository at this point in the history
generating partial classes is now enabled by default (best practice)
  • Loading branch information
petrsvihlik authored Sep 24, 2020
2 parents 52adbc3 + 4279b6f commit 9921af8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ See the [list of all RIDs](https://docs.microsoft.com/en-us/dotnet/articles/core
| `-p` | `--projectid` | True | `null` | A GUID that can be found in [Kentico Kontent](https://app.kontent.ai) -> API keys -> Project ID |
| `-n` | `--namespace` | False | `KenticoKontentModels` | A name of the [C# namespace](https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx) |
| `-o` | `--outputdir` | False | `\.` | An output folder path |
| `-g` | `--generatepartials` | False | `false` | Generates partial classes for customization. Partial classes are the best practice for customization so the recommended value is `true`. |
| `-g` | `--generatepartials` | False | `true` | Generates partial classes for customization. Partial classes are the best practice for customization so the recommended value is `true`. |
| `-t` | `--withtypeprovider` | False | `true` | Indicates whether the `CustomTypeProvider` class should be generated (see [Customizing the strong-type binding logic](https://github.com/Kentico/delivery-sdk-net/wiki/Working-with-Strongly-Typed-Models-(aka-Code-First-Approach)#customizing-the-strong-type-binding-logic) for more info) |
| `-s` | `--structuredmodel` | False | `false` | Generates `IRichTextContent` instead of `string` for rich-text elements. This enables utilizing [structured rich-text rendering](https://github.com/Kentico/delivery-sdk-net/wiki/Structured-Rich-text-rendering) |
| `-f` | `--filenamesuffix` | False | `null` | Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs) |
Expand Down
5 changes: 0 additions & 5 deletions src/Kentico.Kontent.ModelGenerator.Core/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public CodeGenerator(IOptions<CodeGeneratorOptions> options, IDeliveryClient del
_options = options.Value;
_client = deliveryClient;
_outputProvider = outputProvider;

if (_options.GeneratePartials && string.IsNullOrEmpty(_options.FileNameSuffix))
{
_options.FileNameSuffix = "Generated";
}
}

public async Task<int> RunAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ namespace Kentico.Kontent.ModelGenerator.Core.Configuration
{
public class CodeGeneratorOptions
{
public static bool DefaultGeneratePartials = false;
public static bool DefaultGeneratePartials = true;
public static bool DefaultWithTypeProvider = true;
public static bool DefaultStructuredModel = false;
public static bool DefaultContentManagementApi = false;
public static string DefaultFileNameSuffix = "Generated";

/// <summary>
/// Delivery Client configuration.
Expand All @@ -27,7 +28,7 @@ public class CodeGeneratorOptions
/// <summary>
/// Optionally add suffix to the generated files
/// </summary>
public string FileNameSuffix { get; set; }
public string FileNameSuffix { get; set; } = DefaultFileNameSuffix;

/// <summary>
/// Optionally generate partial classes for user customization
Expand Down
11 changes: 6 additions & 5 deletions test/Kentico.Kontent.ModelGenerator.Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Kentico.Kontent.ModelGenerator.Core;
using Kentico.Kontent.ModelGenerator.Core.Configuration;
using Xunit;
using System.Linq;

namespace Kentico.Kontent.ModelGenerator.Tests
{
Expand Down Expand Up @@ -85,10 +86,9 @@ public async Task IntegrationTest(bool cmApi)

Assert.True(Directory.GetFiles(Path.GetFullPath(TEMP_DIR)).Length > 10);

foreach (var filepath in Directory.EnumerateFiles(Path.GetFullPath(TEMP_DIR)))
{
Assert.DoesNotContain(".Generated.cs", Path.GetFileName(filepath));
}
Assert.NotEmpty(Directory.EnumerateFiles(Path.GetFullPath(TEMP_DIR), "*.Generated.cs"));
Assert.NotEmpty(Directory.EnumerateFiles(Path.GetFullPath(TEMP_DIR)).Where(p=> !p.Contains("*.Generated.cs")));
Assert.NotEmpty(Directory.EnumerateFiles(Path.GetFullPath(TEMP_DIR), "*TypeProvider.cs"));

// Cleanup
Directory.Delete(TEMP_DIR, true);
Expand All @@ -104,14 +104,15 @@ public async Task IntegrationTestWithGeneratedSuffix(bool cmApi)
.Respond("application/json", await File.ReadAllTextAsync(Path.Combine(AppContext.BaseDirectory, "Fixtures/types.json")));
var httpClient = mockHttp.ToHttpClient();

const string transformFilename = "Generated";
const string transformFilename = "CustomSuffix";

var mockOptions = new Mock<IOptions<CodeGeneratorOptions>>();
mockOptions.Setup(x => x.Value).Returns(new CodeGeneratorOptions
{
DeliveryOptions = new DeliveryOptions { ProjectId = PROJECT_ID },
Namespace = "CustomNamespace",
OutputDir = TEMP_DIR,
GeneratePartials = false,
FileNameSuffix = transformFilename,
ContentManagementApi = cmApi
});
Expand Down

0 comments on commit 9921af8

Please sign in to comment.