Skip to content

Update template gen to use mustache#5126

Merged
dannyvv merged 6 commits intomicrosoft:masterfrom
dannyvv:pr/Mustache
Jun 6, 2020
Merged

Update template gen to use mustache#5126
dannyvv merged 6 commits intomicrosoft:masterfrom
dannyvv:pr/Mustache

Conversation

@dannyvv
Copy link
Copy Markdown
Member

@dannyvv dannyvv commented Jun 5, 2020

This PR updates the template generation for react-native-windows from regex replace to use the mustache template library.

Benefits

This has the following benefits:

  • We can use conditionals and foreach in the template allowing for better expersivity
  • This expresivity shows that we don't have to construct Xml fragments in Javascript anymore to be inserted by regex replace
  • We don't need to have have a copy and edit for large differences like we had for experimental nuget, it is now a single template for each language.
  • We can now generate a cpp template that use both nuget AND winUI3.

Research

There are several templating scheme's we could have chosen. We had a quick discussion and most favor was for mustache as it Mustashe is a well adtoped, lightweight library dedicated for templating.
Alternatives considered:

  • js template expression: This would have required all files to become js programs that needed to be imported and evaluated. Since a lot of files contain data format or code, we would have likely had quite a few ugly escaping cases.
  • t4: This would have reuquired visual studio to be installed on the users machine and therefore only work on windows. There were also some perf concerns and we would have to write and ship a standalone executable for the msbuild tasks so we can call it from javascript.

Future

In the future we hope this to be integrated with the react-native CLI templating scheme. At the moment there is only a very basic replacement scheme for a set of well defined variables, so at the moment it is not suitable. When we get to that point we'll see if we can leverage that or contribute some improvements in the templating for react native init.

Samples

If you want to run diffs before and after, please see the 'before' and 'after' folders in https://github.com/dannyvv/react-native-windows/tree/demo/MustachTemplatesWithDiff

Quick mustache tutorial:

We only use 4 mustach element: You run mustach via const text = mustache.render(inputText, obj); where obj is a regular javascript object. For example:

{
   name: 'you',
   condition1: true,
   condition2: false,
   myIds: [
    { id: 'abc' },
    { id: 'xyz' }
   ]
}
  • The template Hello {{ name }}! will result in Hello you!.
  • The template Hello {{#condition1}}you!{{/condition1}} will result in Hello you!
  • The template Hello {{^condition2}}you!{{/condition2}} will result in Hello you!
  • The template:
    MyIds:
    {{#myIds}}
    Id: {{ id }}
    {{/myIds}}
    
    will result in:
    MyIds
    Id: abc
    Id: xyz
    
Microsoft Reviewers: Open in CodeFlow

@dannyvv dannyvv requested a review from a team as a code owner June 5, 2020 02:44
@dannyvv
Copy link
Copy Markdown
Member Author

dannyvv commented Jun 5, 2020

</Compile>

@acoates: There is a different between the current generated experimentalnuget and original version. The .filters file in experimentalnuget was missing:

  <ItemGroup>
    <Page Include="MainPage.xaml" />
  </ItemGroup>

I'm assuming that that was just a missed edit, or mixup. Please let me know if it is significant and I'll make it conditional.


Refers to: vnext/local-cli/generator-windows/templates/cs/proj/MyApp.csproj:133 in 5b22f0f. [](commit_id = 5b22f0f, deletion_comment = False)

Comment thread .vscode/settings.json Outdated

if (replacements.useMustache) {
content = mustache.render(content, replacements);
(replacements.regExpPatternsToRemove || []).forEach(regexPattern => {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to handle the // clang-format off and // clang-format on cases with template logic, but I could not get a satisfactory line that clang-format was happy with. It would always mangle the conditional line before the off command and the closing conditional after the on command. It was pretty messy...
I therefore opted for a 1-off removal.

I considered a waterfall model as well where when mustach is on, and a tempaltes fields is set it would set the replacements to that value (else undefined) and it would run the old code. In the end I felt that that code would be a lot harder to read and understand vs the simple foreach and replace slighlty duplicated.

Comment thread vnext/local-cli/generator-windows/templates/cpp/proj/MyApp.sln
Comment thread vnext/local-cli/generator-windows/templates/cpp/src/App.xaml
Comment thread vnext/local-cli/generator-windows/templates/cpp/src/MainPage.idl
Comment thread vnext/local-cli/generator-windows/templates/cpp/src/MainPage.xaml
@asklar
Copy link
Copy Markdown
Member

asklar commented Jun 5, 2020

This PR updates the template generation for react-native-windows from regex replace to use the mustache template library.

@dannyvv can you please add this information to a howto markdown in /docs please?
and also for the codegen pretty please :)

@dannyvv
Copy link
Copy Markdown
Member Author

dannyvv commented Jun 6, 2020

This PR updates the template generation for react-native-windows from regex replace to use the mustache template library.

@dannyvv can you please add this information to a howto markdown in /docs please?
and also for the codegen pretty please :)

Will do.

Copy link
Copy Markdown
Member

@vmoroz vmoroz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@dannyvv dannyvv merged commit 1e39e42 into microsoft:master Jun 6, 2020
@jonthysell jonthysell added the Breaking Change This PR will break existing apps and should be part of the known breaking changes for the release label Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking Change This PR will break existing apps and should be part of the known breaking changes for the release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants