TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

Scaffold EF Core models using Handlebars templates.
MIT License
210 stars 53 forks source link

Add ability to specify location of CodeTemplates folder #12

Closed ErikEJ closed 6 years ago

ErikEJ commented 6 years ago

Hi Tony,

I am facing some issue, maybe you can point me to a good place in the code to start debugging:

I am able to invoke the handelbars scaffolder, and drop CodeTemplates, but no replacements takes place after I modify the templates and re-run scaffolding. (I have included the source of both projects for now, until signing gets fixed (if ever))

https://github.com/ErikEJ/SqlCeToolbox/tree/master/src/GUI/POC

https://github.com/ErikEJ/SqlCeToolbox/blob/master/src/GUI/ReverseEngineer20/EFCoreReverseEngineer.cs#L35

tonysneed commented 6 years ago

My library looks for a specific location of the CodeTemplates folder off the project root. If it doesn't find it, it copies the folder to that location and then uses it to generate classes. I'm wondering if it might be coping the files to a location other than the one you modified and then using it.

The place to debug this issue would be the RetrieveTemplateFileContents method in FileSystemTemplateFileService. Here you can see if you're entering the if block or the else block, where it calls OutputFile to copy the CodeTemplates folder from the assembly directory over to the project directory.

tonysneed commented 6 years ago

I was thinking about the signing issue. If the author of Handlebars.net doesn't want to sign his library, I could create a signed version and publish it to NuGet, and then use it in a signed version of my library.

ErikEJ commented 6 years ago

Excellent idea!

ErikEJ commented 6 years ago

Thanks for the pointer!

This is the result of my debugging:

line 45: path = CodeTemplates/CSharpDbContext/Partials\DbImports.hbs

Current dir: C:\Code\SqlCeToolbox\src\GUI\EFCorePowerTools\bin\x86\Debug

line 54 localDirectory: C:\USERS\ERIK\APPDATA\LOCAL\MICROSOFT\VISUALSTUDIO\15.0_C5B80852EXP\EXTENSIONS\ERIKEJ\ EF CORE POWER TOOLS\0.2.0\CodeTemplates/CSharpDbContext/Partials

(This will cause a crash, as the folder does not exist)

And the actual path that contains the files is at: var projectPath = project.Properties.Item("FullPath").Value.ToString();

Being a VS extension, I know the project root path, so maybe it would be helpful to be able to provider a rootPath to the library?

I also excluded the template files from the library source, as I have to provide them myself to get them properly located.

tonysneed commented 6 years ago

So I'll refactor the code to accept a source directory, as well as a target directory, which will take precedence over the defaults. Then you can supply both locations.

ErikEJ commented 6 years ago

I think the WriteCode method already gets a outPutPath in EntityFrameworkCore.Scaffolding.Handlebars\HbsCSharpScaffoldingGenerator.cs, so I guess only the source is needed.

ErikEJ commented 6 years ago

I have hacked my way out of this https://github.com/ErikEJ/SqlCeToolbox/blob/master/src/GUI/POC/EntityFrameworkCore.Scaffolding.Handlebars/FileSystemTemplateFileService.cs#L53 and Handlebars templating is now available in the latest daily build of EF Core Power Tools! https://github.com/ErikEJ/SqlCeToolbox/wiki/EF-Core-Power-Tools Thanks for making this possible!

tonysneed commented 6 years ago

@ErikEJ That's great! I'll go ahead and update my project to add a source for the templates. Then I can publish a signed version of Handlebars.net, so I can sign my project and publish a stable version.

ErikEJ commented 6 years ago

Sound good, nicer to have just nuget packages rather than 3rd party source

ErikEJ commented 6 years ago

Did you do work on this?

tonysneed commented 6 years ago

I can get the strong name done today. It should be straightforward.

ErikEJ commented 6 years ago

The thing is, I need both this and signing, alternatively I have to include source of this project and handlebars - so unless you also provide signed libraries, not worries.

tonysneed commented 6 years ago

I’ll work on the templates source first, then strong naming.

ErikEJ commented 6 years ago

Great, thanks

tonysneed commented 6 years ago

So the way to specify a custom templates root directory is to extend FileSystemTemplateFileService and override RetrieveTemplateFileContents. Then inject the derived class as ITemplateFileService.

I created this repo with a unit test and instructions: https://github.com/tonysneed/Demo.CustomHandlebarsTemplatesDirectory

@ErikEJ Let me know if this makes sense and works for you.

tonysneed commented 6 years ago

@ErikEJ Did the demo repo address your issue?

ErikEJ commented 6 years ago

It did indeed, have just released new Power Tools with 2.1 RC1 reverse engineering and Handlebars support