Skip to content

Add template path configuration #202

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"culture": "zh-Hans",
"texts": {
"Welcome": "欢迎使用",
Expand All @@ -17,6 +17,11 @@
"SolutionDirectoryPath": "解决方案路径",
"SolutionType:Application": "应用",
"SolutionType:Module": "模块",
"Template": "模板方案",
"OpenTemplate": "打开模板方案",
"ManageTemplates": "管理模板方案",
"TemplateDisplayName": "名称",
"TemplateDirectoryPath": "模板方案路径",
"Use": "使用",
"Open": "打开",
"Current": "当前",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"culture": "zh-Hant",
"texts": {
"Welcome": "歡迎使用",
Expand All @@ -17,6 +17,11 @@
"SolutionDirectoryPath": "解決方案路徑",
"SolutionType:Application": "應用",
"SolutionType:Module": "模塊",
"Template": "範本方案",
"OpenTemplate": "打開範本方案",
"ManageTemplates": "管理範本方案",
"TemplateDisplayName": "名稱",
"TemplateDirectoryPath": "範本方案路徑",
"Use": "使用",
"Open": "打開",
"NoDataAvailable": "無可用數據",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ public class InputDtoWithDirectory
[NotNull]
public virtual string Directory { get; set; }

[CanBeNull]
public virtual string TemplatePath { get; set; }

protected InputDtoWithDirectory()
{

}

public InputDtoWithDirectory([NotNull] string directory)
{
Directory = directory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;

namespace EasyAbp.AbpHelper.Gui.Templates.Dtos
{
public class TemplateDto
{
[Required]
public string DisplayName { get; set; }

public string DirectoryPath { get; set; }

public bool Equals([CanBeNull] TemplateDto other)
{
if (other == null)
{
return false;
}

return DirectoryPath == other.DirectoryPath;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyAbp.AbpHelper.Gui.Templates.Dtos;
using JetBrains.Annotations;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace EasyAbp.AbpHelper.Gui.Templates
{
public interface ITemplateAppService : IApplicationService
{
Task<ListResultDto<TemplateDto>> GetListAsync();

Task<TemplateDto> UseAsync(TemplateDto input);

Task DeleteAsync(TemplateDto input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyAbp.AbpHelper.Gui.Templates.Dtos;

namespace EasyAbp.AbpHelper.Gui.Templates
{
public interface IRecentlyTemplatesManager
{
Task<List<TemplateDto>> GetListAsync();

Task UpdateListAsync(List<TemplateDto> input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.AbpHelper.Gui.Templates.Dtos;
using Newtonsoft.Json;
using Volo.Abp.DependencyInjection;

namespace EasyAbp.AbpHelper.Gui.Templates
{
public class RecentlyTemplatesManager : IRecentlyTemplatesManager, ITransientDependency
{
private static readonly string PersonalDirectoryPath =
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

private static readonly string SettingsDirectoryPath = Path.Combine(PersonalDirectoryPath, ".abphelper-gui");

private static readonly string FilePath = Path.Combine(SettingsDirectoryPath, "RecentlyTemplates.json");

public async Task<List<TemplateDto>> GetListAsync()
{
if (!File.Exists(FilePath))
{
Directory.CreateDirectory(SettingsDirectoryPath);

await File.WriteAllTextAsync(FilePath, JsonConvert.SerializeObject(new List<TemplateDto>()));
}

var result = JsonConvert.DeserializeObject<List<TemplateDto>>(await File.ReadAllTextAsync(FilePath));

if (!result.Any(x => x.DisplayName == "Default"))
{
result.Add(new TemplateDto
{
DisplayName = "Default",
DirectoryPath = string.Empty
});
}

return result;
}

public async Task UpdateListAsync(List<TemplateDto> input)
{
input ??= new List<TemplateDto>();

await File.WriteAllTextAsync(FilePath, JsonConvert.SerializeObject(input));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.AbpHelper.Core.Services;
using EasyAbp.AbpHelper.Gui.Templates.Dtos;
using JetBrains.Annotations;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace EasyAbp.AbpHelper.Gui.Templates
{
public class TemplateAppService : ApplicationService, ITemplateAppService
{
private const byte RecentlyTemplatesMaxCount = 10;

private readonly IRecentlyTemplatesManager _manager;

public TemplateAppService(
IRecentlyTemplatesManager manager)
{
_manager = manager;
}

public virtual async Task<ListResultDto<TemplateDto>> GetListAsync()
{
return new(await _manager.GetListAsync());
}

public virtual async Task<TemplateDto> UseAsync(TemplateDto input)
{
Check.NotNullOrWhiteSpace(input.DisplayName, nameof(input.DisplayName));

var list = await _manager.GetListAsync();

var template = FindTemplate(list, input);

if (template == null)
{
template = input;

list.AddFirst(template);
}
else
{
list.MoveItem((x) => x == template, 0);
}

if (!await IsTemplateDirectoryValidAsync(template))
{
list.RemoveAt(0);

await UpdateRecentlyTemplateListAsync(list);

throw new BusinessException("Gui:InvalidTemplateDirectoryPath");
}

await UpdateRecentlyTemplateListAsync(list);

return input;
}

protected virtual async Task UpdateRecentlyTemplateListAsync(List<TemplateDto> list)
{
if (list.Count > RecentlyTemplatesMaxCount)
{
list = list.GetRange(0, RecentlyTemplatesMaxCount);
}

await _manager.UpdateListAsync(list);
}

protected virtual Task<bool> IsTemplateDirectoryValidAsync(TemplateDto template)
{
return Task.FromResult(string.IsNullOrWhiteSpace(template.DirectoryPath) || Directory.Exists(template.DirectoryPath));
}

protected virtual TemplateDto FindTemplate(IEnumerable<TemplateDto> templates, TemplateDto target)
{
return templates.FirstOrDefault(x => x.DirectoryPath == target.DirectoryPath);
}

public virtual async Task DeleteAsync(TemplateDto input)
{
var list = await _manager.GetListAsync();

list.Remove(FindTemplate(list, input));

await UpdateRecentlyTemplateListAsync(list);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ public abstract class ExecutableComponentBaseWithCurrentSolution : ExecutableCom
{
[Inject]
private ICurrentSolution CurrentSolution { get; set; }


[Inject]
private ICurrentTemplate CurrentTemplate { get; set; }

protected override void OnInitialized()
{
CurrentSolution.OnChangeAsync += CurrentSolutionChangedAsync;
CurrentTemplate.OnChangeAsync += CurrentTemplateChangedAsync;
}

public void Dispose()
{
CurrentSolution.OnChangeAsync -= CurrentSolutionChangedAsync;
CurrentTemplate.OnChangeAsync -= CurrentTemplateChangedAsync;
}

protected virtual async Task CurrentSolutionChangedAsync()
Expand All @@ -28,5 +33,14 @@ protected virtual async Task CurrentSolutionChangedAsync()
}

protected abstract Task OnCurrentSolutionChangedAsync();

protected virtual async Task CurrentTemplateChangedAsync()
{
await OnCurrentTemplateChangedAsync();

StateHasChanged();
}

protected abstract Task OnCurrentTemplateChangedAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,42 @@ namespace EasyAbp.AbpHelper.Gui.Blazor.Pages.Shared
{
[Inject]
protected ICurrentSolution CurrentSolution { get; set; }


[Inject]
protected ICurrentTemplate CurrentTemplate { get; set; }

protected TInput Input { get; set; } = new();

protected override Task OnInitializedAsync()
{
SetDirectoryToCurrentSolutionPath();

SetDirectoryToCurrentTemplatePath();

return base.OnInitializedAsync();
}

protected override Task OnCurrentSolutionChangedAsync()
{
SetDirectoryToCurrentSolutionPath();

return Task.CompletedTask;
}

protected virtual void SetDirectoryToCurrentSolutionPath()
{
Input.Directory = CurrentSolution.Value?.DirectoryPath ?? string.Empty;
}

protected override Task OnCurrentTemplateChangedAsync()
{
SetDirectoryToCurrentTemplatePath();

return Task.CompletedTask;
}

protected virtual void SetDirectoryToCurrentTemplatePath()
{
Input.TemplatePath = CurrentTemplate.Value?.DirectoryPath ?? string.Empty;
}
}
}
Loading