Skip to content

Commit

Permalink
Enable generation of TypeScript entities via Handlebars. (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Sneed authored and ErikEJ committed Jun 29, 2019
1 parent 96a2d56 commit 1a5d48d
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 25 deletions.
22 changes: 17 additions & 5 deletions src/GUI/EFCorePowerTools.Shared/Models/ModelingOptionsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
public class ModelingOptionsModel : INotifyPropertyChanged
{
private bool _installNuGetPackage;
private int _selectedTobeGenerated;
private int _selectedToBeGenerated;
private bool _includeConnectionString;
private bool _useHandelbars;
private int _selectedHandlebarsLanguage;
private bool _replaceId;
private bool _usePluralizer;
private bool _useDatabaseNames;
Expand Down Expand Up @@ -111,6 +112,17 @@ public bool UseHandelbars
}
}

public int SelectedHandlebarsLanguage
{
get => _selectedHandlebarsLanguage;
set
{
if (value == _selectedHandlebarsLanguage) return;
_selectedHandlebarsLanguage = value;
OnPropertyChanged();
}
}

public bool IncludeConnectionString
{
get => _includeConnectionString;
Expand All @@ -122,13 +134,13 @@ public bool IncludeConnectionString
}
}

public int SelectedTobeGenerated
public int SelectedToBeGenerated
{
get => _selectedTobeGenerated;
get => _selectedToBeGenerated;
set
{
if (value == _selectedTobeGenerated) return;
_selectedTobeGenerated = value;
if (value == _selectedToBeGenerated) return;
_selectedToBeGenerated = value;
OnPropertyChanged();
}
}
Expand Down
Binary file modified src/GUI/EFCorePowerTools/CodeTemplates.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IModelingOptionsViewModel : IViewModel
ModelingOptionsModel Model { get; }

IReadOnlyList<string> GenerationModeList { get; }
IReadOnlyList<string> HandlebarsLanguageList { get; }
string Title { get; }
bool MayIncludeConnectionString { get; }

Expand Down
31 changes: 24 additions & 7 deletions src/GUI/EFCorePowerTools/Dialogs/EfCoreModelDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,19 @@
<Setter Property="Margin" Value="0,10,0,0"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
<Style TargetType="ComboBox" x:Key="GenerationMethodComboBoxStyle">
<Style x:Key="GenerationMethodComboBoxStyle"
TargetType="ComboBox">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="IsEditable" Value="False"/>
<Setter Property="MinWidth" Value="200"/>
</Style>
<Style x:Key="HandlebarsLanguageComboBoxStyle"
TargetType="ComboBox">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="IsEditable" Value="False"/>
<Setter Property="MinWidth" Value="105"/>
<Setter Property="Height" Value="23"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Expand Down Expand Up @@ -112,7 +120,7 @@
TabIndex="3"
ItemsSource="{Binding GenerationModeList}"
Style="{StaticResource GenerationMethodComboBoxStyle}"
SelectedIndex="{Binding Model.SelectedTobeGenerated, Mode=TwoWay}" />
SelectedIndex="{Binding Model.SelectedToBeGenerated, Mode=TwoWay}" />

<GroupBox Grid.Row="7"
Header="Naming"
Expand All @@ -139,18 +147,27 @@
Grid.Row="8"
IsChecked="{Binding Model.UseDataAnnotations}"
Style="{StaticResource GenerationOptionsCheckBoxStyle}"/>
<CheckBox TabIndex="11"
Content="Customize code using Handlebars templates (preview)"
Grid.Row="9"
<StackPanel Orientation="Horizontal"
Grid.Row="9">
<CheckBox TabIndex="11"
Name="UseHandlebarsCheckbox"
Content="Customize code using Handlebars templates"
IsChecked="{Binding Model.UseHandelbars}"
Style="{StaticResource GenerationOptionsCheckBoxStyle}"/>
<CheckBox TabIndex="12"
<ComboBox
TabIndex="12"
ItemsSource="{Binding HandlebarsLanguageList}"
Style="{StaticResource HandlebarsLanguageComboBoxStyle}"
SelectedIndex="{Binding Model.SelectedHandlebarsLanguage, Mode=TwoWay}" Margin="10,0,0,0"
IsEnabled="{Binding IsChecked, ElementName=UseHandlebarsCheckbox}" />
</StackPanel>
<CheckBox TabIndex="13"
Content="Include connection string in generated code"
Grid.Row="10"
IsChecked="{Binding Model.IncludeConnectionString}"
IsEnabled="{Binding MayIncludeConnectionString}"
Style="{StaticResource GenerationOptionsCheckBoxStyle}"/>
<CheckBox TabIndex="13"
<CheckBox TabIndex="14"
Content="Install the EF Core provider package in the project"
Grid.Row="11"
IsChecked="{Binding Model.InstallNuGetPackage}"
Expand Down
16 changes: 9 additions & 7 deletions src/GUI/EFCorePowerTools/Handlers/ReverseEngineerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ public async void ReverseEngineerCodeFirst(Project project)
presets.UseDatabaseNames = options.UseDatabaseNames;
presets.UsePluralizer = options.UseInflector;
presets.UseHandelbars = options.UseHandleBars;
presets.SelectedHandlebarsLanguage = options.SelectedHandlebarsLanguage;
presets.ReplaceId = options.IdReplace;
presets.IncludeConnectionString = options.IncludeConnectionString;
presets.ModelName = options.ContextClassName;
presets.Namespace = options.ProjectRootNamespace;
presets.OutputPath = options.OutputPath;
presets.SelectedTobeGenerated = options.SelectedToBeGenerated;
presets.SelectedToBeGenerated = options.SelectedToBeGenerated;
}

var modelDialog = _package.GetView<IModelingOptionsDialog>()
Expand All @@ -189,8 +190,9 @@ public async void ReverseEngineerCodeFirst(Project project)
UseInflector = modelingOptionsResult.Payload.UsePluralizer,
IdReplace = modelingOptionsResult.Payload.ReplaceId,
UseHandleBars = modelingOptionsResult.Payload.UseHandelbars,
SelectedHandlebarsLanguage = modelingOptionsResult.Payload.SelectedHandlebarsLanguage,
IncludeConnectionString = modelingOptionsResult.Payload.IncludeConnectionString,
SelectedToBeGenerated = modelingOptionsResult.Payload.SelectedTobeGenerated,
SelectedToBeGenerated = modelingOptionsResult.Payload.SelectedToBeGenerated,
Dacpac = dacpacPath,
DefaultDacpacSchema = dacpacSchema,
Tables = pickTablesResult.Payload.ToList(),
Expand All @@ -213,25 +215,25 @@ public async void ReverseEngineerCodeFirst(Project project)

var revEngResult = revEng.GenerateFiles(options);

if (modelingOptionsResult.Payload.SelectedTobeGenerated == 0 || modelingOptionsResult.Payload.SelectedTobeGenerated == 2)
if (modelingOptionsResult.Payload.SelectedToBeGenerated == 0 || modelingOptionsResult.Payload.SelectedToBeGenerated == 2)
{
foreach (var filePath in revEngResult.EntityTypeFilePaths)
{
project.ProjectItems.AddFromFile(filePath);
}
if (modelingOptionsResult.Payload.SelectedTobeGenerated == 2)
if (modelingOptionsResult.Payload.SelectedToBeGenerated == 2)
{
if (File.Exists(revEngResult.ContextFilePath)) File.Delete(revEngResult.ContextFilePath);
}
}
if (modelingOptionsResult.Payload.SelectedTobeGenerated == 0 || modelingOptionsResult.Payload.SelectedTobeGenerated == 1)
if (modelingOptionsResult.Payload.SelectedToBeGenerated == 0 || modelingOptionsResult.Payload.SelectedToBeGenerated == 1)
{
project.ProjectItems.AddFromFile(revEngResult.ContextFilePath);
if (!project.IsNetCore() && !isNetStandard)
{
_package.Dte2.ItemOperations.OpenFile(revEngResult.ContextFilePath);
}
if (modelingOptionsResult.Payload.SelectedTobeGenerated == 1)
if (modelingOptionsResult.Payload.SelectedToBeGenerated == 1)
{
foreach (var filePath in revEngResult.EntityTypeFilePaths)
{
Expand All @@ -241,7 +243,7 @@ public async void ReverseEngineerCodeFirst(Project project)
}

var missingProviderPackage = packageResult.Item1 ? null : packageResult.Item2;
if (modelingOptionsResult.Payload.InstallNuGetPackage || modelingOptionsResult.Payload.SelectedTobeGenerated == 2)
if (modelingOptionsResult.Payload.InstallNuGetPackage || modelingOptionsResult.Payload.SelectedToBeGenerated == 2)
{
missingProviderPackage = null;
}
Expand Down
11 changes: 9 additions & 2 deletions src/GUI/EFCorePowerTools/ViewModels/ModelingOptionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ModelingOptionsViewModel : ViewModelBase, IModelingOptionsViewModel

public ModelingOptionsModel Model { get; }
public IReadOnlyList<string> GenerationModeList { get; }
public IReadOnlyList<string> HandlebarsLanguageList { get; }

public string Title
{
Expand Down Expand Up @@ -65,11 +66,16 @@ public ModelingOptionsViewModel(IVisualStudioAccess visualStudioAccess)
"DbContext only",
"EntityTypes only"
};
HandlebarsLanguageList = new[]
{
"C#",
"TypeScript"
};
}

private void Loaded_Executed()
{
Model.SelectedTobeGenerated = 0;
Model.SelectedToBeGenerated = 0;
}

private void Ok_Executed()
Expand Down Expand Up @@ -111,7 +117,8 @@ private void Model_PropertyChanged(object sender, System.ComponentModel.Property
void IModelingOptionsViewModel.ApplyPresets(ModelingOptionsModel presets)
{
Model.InstallNuGetPackage = presets.InstallNuGetPackage;
Model.SelectedTobeGenerated = presets.SelectedTobeGenerated;
Model.SelectedToBeGenerated = presets.SelectedToBeGenerated;
Model.SelectedHandlebarsLanguage = presets.SelectedHandlebarsLanguage;
Model.IncludeConnectionString = presets.IncludeConnectionString;
Model.UseHandelbars = presets.UseHandelbars;
Model.ReplaceId = presets.ReplaceId;
Expand Down
4 changes: 3 additions & 1 deletion src/GUI/ReverseEngineer20/EFCoreReverseEngineer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public EfCoreReverseEngineerResult GenerateFiles(ReverseEngineerOptions reverseE
if (reverseEngineerOptions.UseHandleBars)
{
//TODO Consider being selective based on SelectedToBeGenerated
serviceCollection.AddHandlebarsScaffolding();
var options = Microsoft.EntityFrameworkCore.Design.ReverseEngineerOptions.DbContextAndEntities;
var language = (LanguageOptions)reverseEngineerOptions.SelectedHandlebarsLanguage;
serviceCollection.AddHandlebarsScaffolding(options, language);
serviceCollection.AddSingleton<ITemplateFileService>(provider => new CustomTemplateFileService(reverseEngineerOptions.ProjectPath));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ReverseEngineerOptions
public bool UseInflector { get; set; }
public bool IdReplace { get; set; }
public bool UseHandleBars { get; set; }
public int SelectedHandlebarsLanguage { get; set; }
public bool IncludeConnectionString { get; set; }
public int SelectedToBeGenerated { get; set; }
[IgnoreDataMember]
Expand Down Expand Up @@ -54,6 +55,7 @@ public static ReverseEngineerOptions FromV1(ReverseEngineerOptionsV1 v1)
UseInflector = v1.UseInflector,
IdReplace = v1.IdReplace,
UseHandleBars = v1.UseHandleBars,
SelectedHandlebarsLanguage = 0,
IncludeConnectionString = v1.IncludeConnectionString,
SelectedToBeGenerated = v1.SelectedToBeGenerated,
Dacpac = v1.Dacpac,
Expand Down
7 changes: 5 additions & 2 deletions src/GUI/ReverseEngineer20/ReverseEngineer20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFrameworkCore.Scaffolding.Handlebars, Version=1.7.2.0, Culture=neutral, PublicKeyToken=b10b51e7b9be6a2e, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFrameworkCore.Scaffolding.Handlebars.1.7.2\lib\netstandard2.0\EntityFrameworkCore.Scaffolding.Handlebars.dll</HintPath>
<Reference Include="EntityFrameworkCore.Scaffolding.Handlebars, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b10b51e7b9be6a2e, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFrameworkCore.Scaffolding.Handlebars.2.0.0\lib\netstandard2.0\EntityFrameworkCore.Scaffolding.Handlebars.dll</HintPath>
</Reference>
<Reference Include="EntityFrameworkCore.SqlServerCompact40, Version=2.2.0.0, Culture=neutral, PublicKeyToken=9af395b34ac99006, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFrameworkCore.SqlServerCompact40.Core.2.2.0.7\lib\net461\EntityFrameworkCore.SqlServerCompact40.dll</HintPath>
Expand All @@ -57,6 +57,9 @@
<Reference Include="Handlebars, Version=1.9.5.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
<HintPath>..\packages\Handlebars.Net.1.9.5\lib\net452\Handlebars.dll</HintPath>
</Reference>
<Reference Include="JetBrains.Annotations, Version=2019.1.1.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.2019.1.1\lib\net20\JetBrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Data.Sqlite, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.Sqlite.Core.2.2.0\lib\netstandard2.0\Microsoft.Data.Sqlite.dll</HintPath>
</Reference>
Expand Down
3 changes: 2 additions & 1 deletion src/GUI/ReverseEngineer20/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFrameworkCore.Scaffolding.Handlebars" version="1.7.2" targetFramework="net471" />
<package id="EntityFrameworkCore.Scaffolding.Handlebars" version="2.0.0" targetFramework="net471" />
<package id="EntityFrameworkCore.SqlServerCompact40.Core" version="2.2.0.7" targetFramework="net471" />
<package id="GeoAPI.Core" version="1.7.5" targetFramework="net471" />
<package id="Handlebars.Net" version="1.9.5" targetFramework="net461" />
<package id="JetBrains.Annotations" version="2019.1.1" targetFramework="net471" />
<package id="Microsoft.CSharp" version="4.5.0" targetFramework="net461" />
<package id="Microsoft.Data.Sqlite.Core" version="2.2.0" targetFramework="net461" />
<package id="Microsoft.DotNet.PlatformAbstractions" version="2.1.0" targetFramework="net461" />
Expand Down

0 comments on commit 1a5d48d

Please sign in to comment.