Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ services.AddHandlebarsScaffolding(options =>
});
```

## Additional Partial Templates

You can add new partial templates to one of the `Partials` folders, then reference with the usual Handlebars syntax. For example, a file named **Comment.hbs** can be referenced from **Class.hbs**.

```hbs
{{{> comment}}}
public class {{class}}
{
```

## Custom Template Data

You may find it useful to add your own custom template data for use in your Handlebars templates. For example, the model namespace is not included by default in the `DbContext` class import statements. To compensate you may wish to add a `models-namespace` template to the **DbImports.hbs** template file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{> imports}}

namespace {{namespace}}
{ {{{> comment}}}
{ // Comment
{{#if class-annotation}}
{{spaces 4}}{{{class-annotation}}}
{{/if}}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CSharpTemplateLanguageService : ITemplateLanguageService
/// Get DbContext template file information.
/// </summary>
/// <returns>Dictionary of templates with file information.</returns>
public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo(ITemplateFileService fileService)
public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo()
{
var result = new Dictionary<string, TemplateFileInfo>
{
Expand All @@ -23,18 +23,48 @@ public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo(ITempla
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextDirectory,
FileName = Constants.DbContextTemplate + Constants.TemplateExtension
}
}
},
{
Constants.DbContextImportTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
FileName = Constants.DbContextImportTemplate + Constants.TemplateExtension
}
},
{
Constants.DbContextCtorTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
FileName = Constants.DbContextCtorTemplate + Constants.TemplateExtension
}
},
{
Constants.DbContextOnConfiguringTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
FileName = Constants.DbContextOnConfiguringTemplate + Constants.TemplateExtension
}
},
{
Constants.DbContextDbSetsTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
FileName = Constants.DbContextDbSetsTemplate + Constants.TemplateExtension
}
},
};

result = fileService.FindAllPartialTemplates(result, Constants.CSharpTemplateDirectories.DbContextPartialsDirectory);
return result;
}

/// <summary>
/// Get Entities template file information.
/// </summary>
/// <returns>Dictionary of templates with file information.</returns>
public Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo(ITemplateFileService fileService)
public Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo()
{
var result = new Dictionary<string, TemplateFileInfo>
{
Expand All @@ -46,9 +76,31 @@ public Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo(ITemplat
FileName = Constants.EntityTypeTemplate + Constants.TemplateExtension
}
},
{
Constants.EntityTypeImportTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
FileName = Constants.EntityTypeImportTemplate + Constants.TemplateExtension
}
},
{
Constants.EntityTypeCtorTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
FileName = Constants.EntityTypeCtorTemplate + Constants.TemplateExtension
}
},
{
Constants.EntityTypePropertyTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
FileName = Constants.EntityTypePropertyTemplate + Constants.TemplateExtension
}
},
};

result = fileService.FindAllPartialTemplates(result, Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>3.8.0</Version>
<Version>3.8.1</Version>
<Authors>Tony Sneed</Authors>
<Company>Tony Sneed</Company>
<Title>Entity Framework Core Scaffolding with Handlebars</Title>
Expand All @@ -12,7 +12,7 @@
<PackageProjectUrl>https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars</PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageTags>scaffolding reverse-engineer entity-framework-core handlebars</PackageTags>
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v3.8.0</PackageReleaseNotes>
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v3.8.1</PackageReleaseNotes>
<LangVersion>latest</LangVersion>
<IncludeSource>true</IncludeSource>
<SignAssembly>true</SignAssembly>
Expand All @@ -38,7 +38,7 @@
</ItemGroup>

<ItemGroup>
<!-- pack tempate files -->
<!-- pack template files -->
<Content Include="CodeTemplates\CSharpDbContext\DbContext.hbs" PackagePath="lib\netstandard2.1\CodeTemplates\CSharpDbContext\" />
<Content Include="CodeTemplates\CSharpDbContext\Partials\DbImports.hbs" PackagePath="lib\netstandard2.1\CodeTemplates\CSharpDbContext\Partials\" />
<Content Include="CodeTemplates\CSharpDbContext\Partials\DbConstructor.hbs" PackagePath="lib\netstandard2.1\CodeTemplates\CSharpDbContext\Partials\" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using EntityFrameworkCore.Scaffolding.Handlebars.Helpers;
using EntityFrameworkCore.Scaffolding.Handlebars.Internal;

namespace EntityFrameworkCore.Scaffolding.Handlebars
Expand Down Expand Up @@ -30,35 +28,6 @@ public virtual string[] InputFiles(params InputFile[] files)
return filePaths.ToArray();
}

/// <summary>
/// Find all files in relative directory
/// </summary>
/// <param name="relativeDirectory">Relative Directory.</param>
/// <returns></returns>
public virtual string[] RetrieveAllFileNames(string relativeDirectory)
{
return Directory.GetFiles(relativeDirectory, $"*{Constants.TemplateExtension}").Select(x => Path.GetFileNameWithoutExtension(x)).ToArray();
}

/// <summary>
/// Finds all partial templates
/// </summary>
/// <param name="result">Dictionary containing template info</param>
/// <param name="relativeDirectory">Relative Directory.</param>
/// <returns></returns>
public virtual Dictionary<string, TemplateFileInfo> FindAllPartialTemplates(Dictionary<string, TemplateFileInfo> result, string relativeDirectory)
{
foreach (var file in RetrieveAllFileNames(relativeDirectory))
{
result.Add(file, new TemplateFileInfo()
{
RelativeDirectory = relativeDirectory,
FileName = file + Constants.TemplateExtension
});
}
return result;
}

/// <summary>
/// Retreive template file contents from the file system.
/// If template is not present, copy it locally.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using EntityFrameworkCore.Scaffolding.Handlebars.Helpers;
using Microsoft.EntityFrameworkCore.Design;
using HandlebarsLib = HandlebarsDotNet.Handlebars;
Expand All @@ -26,7 +27,7 @@ public class HbsDbContextTemplateService : HbsTemplateService, IDbContextTemplat
public HbsDbContextTemplateService(ITemplateFileService fileService,
ITemplateLanguageService languageService) : base(fileService, languageService)
{
DbContextTemplateFiles = LanguageService.GetDbContextTemplateFileInfo(fileService);
DbContextTemplateFiles = LanguageService.GetDbContextTemplateFileInfo();
}

/// <summary>
Expand Down Expand Up @@ -67,14 +68,41 @@ protected virtual Func<object, string> CompileDbContextTemplate(
protected override IDictionary<string, string> GetPartialTemplates(
LanguageOptions language = LanguageOptions.CSharp)
{
var templates = new Dictionary<string, string>();
foreach(var item in DbContextTemplateFiles)
DbContextTemplateFiles.TryGetValue(Constants.DbContextImportTemplate, out TemplateFileInfo importFile);
var importTemplateFile = FileService.RetrieveTemplateFileContents(
importFile.RelativeDirectory, importFile.FileName);

DbContextTemplateFiles.TryGetValue(Constants.DbContextCtorTemplate, out TemplateFileInfo ctorFile);
var ctorTemplateFile = FileService.RetrieveTemplateFileContents(
ctorFile.RelativeDirectory, ctorFile.FileName);

DbContextTemplateFiles.TryGetValue(Constants.DbContextOnConfiguringTemplate, out TemplateFileInfo onConfiguringFile);
var onConfiguringTemplateFile = FileService.RetrieveTemplateFileContents(
ctorFile.RelativeDirectory, onConfiguringFile.FileName);

DbContextTemplateFiles.TryGetValue(Constants.DbContextDbSetsTemplate, out TemplateFileInfo propertyFile);
var propertyTemplateFile = FileService.RetrieveTemplateFileContents(
propertyFile.RelativeDirectory, propertyFile.FileName);

var templates = new Dictionary<string, string>
{
if(item.Value.RelativeDirectory == Constants.CSharpTemplateDirectories.DbContextPartialsDirectory)
{
templates.Add(item.Key, FileService.RetrieveTemplateFileContents(item.Value.RelativeDirectory, item.Value.FileName));
}
}
Constants.DbContextImportTemplate.ToLower(CultureInfo.InvariantCulture),
importTemplateFile
},
{
Constants.DbContextCtorTemplate.ToLower(CultureInfo.InvariantCulture),
ctorTemplateFile
},
{
Constants.DbContextOnConfiguringTemplate.ToLower(CultureInfo.InvariantCulture),
onConfiguringTemplateFile
},
{
Constants.DbContextDbSetsTemplate.ToLower(CultureInfo.InvariantCulture),
propertyTemplateFile
},
};
return templates;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using EntityFrameworkCore.Scaffolding.Handlebars.Helpers;
using Microsoft.EntityFrameworkCore.Design;
using HandlebarsLib = HandlebarsDotNet.Handlebars;
Expand All @@ -26,7 +27,7 @@ public class HbsEntityTypeTemplateService : HbsTemplateService, IEntityTypeTempl
public HbsEntityTypeTemplateService(ITemplateFileService fileService,
ITemplateLanguageService languageService) : base(fileService, languageService)
{
EntitiesTemplateFiles = LanguageService.GetEntitiesTemplateFileInfo(fileService);
EntitiesTemplateFiles = LanguageService.GetEntitiesTemplateFileInfo();
}

/// <summary>
Expand Down Expand Up @@ -67,16 +68,33 @@ protected virtual Func<object, string> CompileEntityTypeTemplate(
protected override IDictionary<string, string> GetPartialTemplates(
LanguageOptions language = LanguageOptions.CSharp)
{

var templates = new Dictionary<string, string>();
foreach (var item in EntitiesTemplateFiles)
EntitiesTemplateFiles.TryGetValue(Constants.EntityTypeCtorTemplate, out TemplateFileInfo ctorFile);
var ctorTemplateFile = FileService.RetrieveTemplateFileContents(
ctorFile.RelativeDirectory, ctorFile.FileName);

EntitiesTemplateFiles.TryGetValue(Constants.EntityTypeImportTemplate, out TemplateFileInfo importFile);
var importTemplateFile = FileService.RetrieveTemplateFileContents(
importFile.RelativeDirectory, importFile.FileName);

EntitiesTemplateFiles.TryGetValue(Constants.EntityTypePropertyTemplate, out TemplateFileInfo propertyFile);
var propertyTemplateFile = FileService.RetrieveTemplateFileContents(
propertyFile.RelativeDirectory, propertyFile.FileName);

var templates = new Dictionary<string, string>
{
if (item.Value.RelativeDirectory == Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory)
{
templates.Add(item.Key, FileService.RetrieveTemplateFileContents(item.Value.RelativeDirectory, item.Value.FileName));
}
}

Constants.EntityTypeCtorTemplate.ToLower(CultureInfo.InvariantCulture),
ctorTemplateFile
},
{
Constants.EntityTypeImportTemplate.ToLower(CultureInfo.InvariantCulture),
importTemplateFile
},
{
Constants.EntityTypePropertyTemplate.ToLower(CultureInfo.InvariantCulture),
propertyTemplateFile
},
};
return templates;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EntityFrameworkCore.Scaffolding.Handlebars.Internal;
using System.Collections.Generic;

namespace EntityFrameworkCore.Scaffolding.Handlebars
{
Expand All @@ -25,20 +24,5 @@ public interface ITemplateFileService : IFileService
/// <returns>File contents.</returns>
string RetrieveTemplateFileContents(string relativeDirectory, string fileName,
string altRelativeDirectory = null);

/// <summary>
/// Retries all files from a relative directory.
/// </summary>
/// <param name="relativeDirectory">Relative directory name.</param>
/// <returns>File names.</returns>
string[] RetrieveAllFileNames(string relativeDirectory);

/// <summary>
/// Finds all partial templates
/// </summary>
/// <param name="result">Dictionary containing template info</param>
/// <param name="relativeDirectory">Relative Directory.</param>
/// <returns></returns>
Dictionary<string, TemplateFileInfo> FindAllPartialTemplates(Dictionary<string, TemplateFileInfo> result, string relativeDirectory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public interface ITemplateLanguageService
/// Get DbContext template file information.
/// </summary>
/// <returns>Dictionary of templates with file information.</returns>
Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo(ITemplateFileService fileService);
Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo();

/// <summary>
/// Get Entities template file information.
/// </summary>
/// <returns>Dictionary of templates with file information.</returns>
Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo(ITemplateFileService fileService);
Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo();
}
}
Loading