Skip to content

Add template path configuration parameters #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
78 changes: 41 additions & 37 deletions src/AbpHelper.Core/AbpHelper.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\common.props" />
<Import Project="..\..\common.props" />

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>EasyAbp.AbpHelper.Core</PackageId>
<AssemblyName>EasyAbp.AbpHelper.Core</AssemblyName>
<RootNamespace>EasyAbp.AbpHelper.Core</RootNamespace>
<LangVersion>10.0</LangVersion>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>EasyAbp.AbpHelper.Core</PackageId>
<AssemblyName>EasyAbp.AbpHelper.Core</AssemblyName>
<RootNamespace>EasyAbp.AbpHelper.Core</RootNamespace>
<LangVersion>10.0</LangVersion>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elsa" Version="1.2.2.29" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="7.0.0" />
<PackageReference Include="Scriban" Version="5.5.2" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20371.2" />
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Core" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Http" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.VirtualFileSystem" Version="$(AbpVersion)" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="NuGet.Protocol" Version="6.2.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Elsa" Version="1.2.2.29" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="7.0.0" />
<PackageReference Include="Scriban" Version="5.5.2" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20371.2" />
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Core" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Http" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.VirtualFileSystem" Version="$(AbpVersion)" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="NuGet.Protocol" Version="6.2.2" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Templates\**\*.*" />
<Content Remove="Templates\**\*.*" />
<EmbeddedResource Include="Templates\**\*.*" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Templates\**\*.*" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Fody" Version="6.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Update="Templates\**\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Update="Fody" Version="6.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions src/AbpHelper.Core/Commands/CommandOptionsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using EasyAbp.AbpHelper.Core.Attributes;

namespace EasyAbp.AbpHelper.Core.Commands
Expand All @@ -14,5 +15,20 @@ public abstract class CommandOptionsBase

[Option("exclude", Description = "Exclude directories when searching files, arguments can contain a combination of valid literal path and wildcard (* and ?) characters. Use double asterisk(**) to search all directories. Example: --exclude *Folder1 Folder2/Folder* **/*Folder? **/*Folder*")]
public virtual string[] Exclude { get; set; } = Array.Empty<string>();

[Option("templates-path", Description = "Config templates path, Built-in templates are used by default")]
public string? TemplatesPath { set { _templatesPath = value; } }

private string? _templatesPath = string.Empty;

public string TemplatesPathCombine(string subPath)
{
if (_templatesPath.IsNullOrWhiteSpace())
{
return Path.Combine(new[] { AppDomain.CurrentDomain.BaseDirectory, "Templates", subPath });
}

return Path.Combine(_templatesPath!, subPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override IActivityBuilder ConfigureBuild(ControllerCommandOption optio
step =>
{
step.VariableName = VariableNames.TemplateDirectory;
step.ValueExpression = new LiteralExpression<string>("/Templates/Controller");
step.ValueExpression = new LiteralExpression<string>(option.TemplatesPathCombine("Controller"));
})
.Then<IfElse>(
step => step.ConditionExpression = new JavaScriptExpression<bool>($"{OptionVariableName}.{nameof(ControllerCommandOption.SkipBuild)}"),
Expand Down
2 changes: 1 addition & 1 deletion src/AbpHelper.Core/Commands/Generate/Crud/CrudCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override IActivityBuilder ConfigureBuild(CrudCommandOption option, IAc
step =>
{
step.VariableName = VariableNames.TemplateDirectory;
step.ValueExpression = new LiteralExpression<string>("/Templates/Crud");
step.ValueExpression = new LiteralExpression<string>(option.TemplatesPathCombine("Crud"));
})
.Then<FileFinderStep>(
step => { step.SearchFileName = new LiteralExpression(entityFileName); })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override IActivityBuilder ConfigureBuild(LocalizationCommandOption opt
step =>
{
step.VariableName = VariableNames.TemplateDirectory;
step.ValueExpression = new LiteralExpression<string>("/Templates/Localization");
step.ValueExpression = new LiteralExpression<string>(option.TemplatesPathCombine("Localization"));
})
.Then<SetModelVariableStep>()
/* Add localization */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override IActivityBuilder ConfigureBuild(MethodsCommandOption option,
step =>
{
step.VariableName = VariableNames.TemplateDirectory;
step.ValueExpression = new LiteralExpression<string>("/Templates/Methods");
step.ValueExpression = new LiteralExpression<string>(option.TemplatesPathCombine("Methods"));
})
.Then<FileFinderStep>(
step =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override IActivityBuilder ConfigureBuild(ServiceCommandOption option,
step =>
{
step.VariableName = VariableNames.TemplateDirectory;
step.ValueExpression = new LiteralExpression<string>("/Templates/Service");
step.ValueExpression = new LiteralExpression<string>(option.TemplatesPathCombine("Service"));
})
.Then<SetModelVariableStep>()
.Then<GroupGenerationStep>(
Expand Down
12 changes: 6 additions & 6 deletions src/AbpHelper.Core/Commands/Module/Add/AddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ public AddCommand([NotNull] IServiceProvider serviceProvider) : base(serviceProv
protected override IActivityBuilder ConfigureBuild(AddCommandOption option, IActivityBuilder activityBuilder)
{
var moduleIdToCustomsMapping = typeof(ModuleCommandOption).GetProperties()
.Where(prop => prop.PropertyType == typeof(bool) && (bool) prop.GetValue(option)!)
.Where(prop => prop.PropertyType == typeof(bool) && (bool)prop.GetValue(option)!)
.Select(prop => _packageProjectMap[prop.Name.ToKebabCase()])
.ToDictionary(x => x, x => new List<string>(new[] {$"{x}:{x}"}));
.ToDictionary(x => x, x => new List<string>(new[] { $"{x}:{x}" }));

if (!option.Custom.IsNullOrEmpty())
{
foreach (var customPart in option.Custom.Split(','))
{
var moduleId = customPart.Substring(0, customPart.IndexOf(':'));

if (!moduleIdToCustomsMapping.ContainsKey(moduleId))
{
moduleIdToCustomsMapping.Add(moduleId, new List<string>());
}

moduleIdToCustomsMapping[moduleId].Add(customPart);
}
}
Expand All @@ -74,7 +74,7 @@ protected override IActivityBuilder ConfigureBuild(AddCommandOption option, IAct
step =>
{
step.VariableName = VariableNames.TemplateDirectory;
step.ValueExpression = new LiteralExpression<string>("/Templates/Module");
step.ValueExpression = new LiteralExpression<string>(option.TemplatesPathCombine("Module"));
})
.Then<SetVariable>(
step =>
Expand Down
10 changes: 5 additions & 5 deletions src/AbpHelper.Core/Commands/Module/Remove/RemoveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public RemoveCommand([NotNull] IServiceProvider serviceProvider) : base(serviceP
protected override IActivityBuilder ConfigureBuild(RemoveCommandOption option, IActivityBuilder activityBuilder)
{
var moduleNameToAppProjectNameMapping = typeof(ModuleCommandOption).GetProperties()
.Where(prop => prop.PropertyType == typeof(bool) && (bool) prop.GetValue(option)!)
.Where(prop => prop.PropertyType == typeof(bool) && (bool)prop.GetValue(option)!)
.Select(prop => _packageProjectMap[prop.Name.ToKebabCase()])
.ToDictionary(x => x, x => x);

if (!option.Custom.IsNullOrEmpty())
{
foreach (var customPart in option.Custom.Split(','))
Expand All @@ -61,15 +61,15 @@ protected override IActivityBuilder ConfigureBuild(RemoveCommandOption option, I
moduleNameToAppProjectNameMapping.Add(s[0], s[1]);
}
}

string cdOption = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? " /d" : "";

return base.ConfigureBuild(option, activityBuilder)
.Then<SetVariable>(
step =>
{
step.VariableName = VariableNames.TemplateDirectory;
step.ValueExpression = new LiteralExpression<string>("/Templates/Module");
step.ValueExpression = new LiteralExpression<string>(option.TemplatesPathCombine("Module"));
})
.Then<SetVariable>(
step =>
Expand Down Expand Up @@ -137,7 +137,7 @@ protected override IActivityBuilder ConfigureBuild(RemoveCommandOption option, I
.Then<DependsOnStep>(step =>
{
step.Action = new LiteralExpression<DependsOnStep.ActionType>(((int)DependsOnStep.ActionType.Remove).ToString());
})
})
.Then<FileModifierStep>()
.Then<IfElse>(
step => step.ConditionExpression = new JavaScriptExpression<bool>("TargetAppProjectName == 'EntityFrameworkCore'"),
Expand Down
15 changes: 12 additions & 3 deletions src/AbpHelper.Core/Generator/TextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ public string GenerateByTemplateName(string templateDirectory, string templateNa
{
return GenerateByTemplateName(templateDirectory, templateName, model, out _);
}

public string GenerateByTemplateName(string templateDirectory, string templateName, object model, out TemplateContext context)
{
string path = Path.Combine(templateDirectory, templateName).NormalizePath();
var templateFile = _virtualFileProvider.GetFileInfo(path);
var templateText = templateFile.ReadAsString();

var templateText = "";
if (path.Contains(":"))
{
templateText = File.ReadAllText(path);
}
else
{
var templateFile = _virtualFileProvider.GetFileInfo(path);
templateText = templateFile.ReadAsString();
}
return GenerateByTemplateText(templateText, model, out context);
}

Expand Down