From 04b51fccc4bf74e95ccbb8cde0dfe55bbd505aea Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 26 Aug 2021 17:27:31 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91Luban.Serve?= =?UTF-8?q?r=E7=9A=84=20=E5=91=BD=E4=BB=A4=E8=A1=8C=E5=8F=82=E6=95=B0-t=20?= =?UTF-8?q?=E8=AF=AD=E4=B9=89=E8=B0=83=E6=95=B4=E4=B8=BA=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E7=9A=84=E6=A8=A1=E6=9D=BF=E6=90=9C=E7=B4=A2=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E3=80=82=E5=A6=82=E6=9E=9C=E6=8C=87=E5=AE=9A=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=85=88=E6=90=9C=E7=B4=A2=E6=AD=A4=E8=B7=AF=E5=BE=84=EF=BC=8C?= =?UTF-8?q?=E5=86=8D=E6=90=9C=E7=B4=A2=E9=BB=98=E8=AE=A4=E7=9A=84Templates?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.ClientServer/Program.cs | 20 ++++++++++++------- .../Source/Utils/StringTemplateUtil.cs | 17 ++++++++++++++-- src/Luban.Server/Source/Program.cs | 11 +++++----- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Luban.ClientServer/Program.cs b/src/Luban.ClientServer/Program.cs index ef24075..467548c 100644 --- a/src/Luban.ClientServer/Program.cs +++ b/src/Luban.ClientServer/Program.cs @@ -4,6 +4,7 @@ using Luban.Client.Common.Utils; using Luban.Client.Utils; using Luban.Common.Protos; using Luban.Common.Utils; +using Luban.Job.Common.Utils; using Luban.Server; using System; using System.Collections.Generic; @@ -34,7 +35,8 @@ namespace Luban.ClientServer public string[] WatchDir { get; set; } - public string StringTemplateDir { get; set; } + [Option('t', "template_search_path", Required = false, HelpText = "string template search path.")] + public string TemplateSearchPath { get; set; } } private static void PrintUsage(string err) @@ -54,7 +56,7 @@ Options: -l --loglevel log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF -c --cachemetafile cache meta file name. default is '.cache.meta' -w --watch watch data change and regenerate. - -t --templatedirectory string templates directory. default is 'Templates' + -t --template_search_path additional template search path -h --help show usage "); } @@ -122,9 +124,9 @@ Options: break; } case "-t": - case "--templatedirectory": + case "--template_search_path": { - ops.StringTemplateDir = args[++i]; + ops.TemplateSearchPath = args[++i]; break; } case "--": @@ -154,7 +156,11 @@ Options: private static void StartServer(AllCommandLineOptions options) { - Job.Common.Utils.StringTemplateUtil.TemplateDir = options.StringTemplateDir; + if (!string.IsNullOrEmpty(options.TemplateSearchPath)) + { + StringTemplateUtil.AddTemplateSearchPath(options.TemplateSearchPath); + } + StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates")); GenServer.Ins.Start(options.Port, ProtocolStub.Factories); @@ -206,9 +212,9 @@ Options: ThreadPool.SetMinThreads(Math.Max(4, processorCount), 5); ThreadPool.SetMaxThreads(Math.Max(16, processorCount * 4), 10); - if (string.IsNullOrEmpty(options.StringTemplateDir)) + if (string.IsNullOrEmpty(options.TemplateSearchPath)) { - options.StringTemplateDir = FileUtil.GetPathRelateApplicationDirectory("Templates"); + options.TemplateSearchPath = FileUtil.GetPathRelateApplicationDirectory("Templates"); } if (string.IsNullOrWhiteSpace(options.Host)) { diff --git a/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs b/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs index 9d8f44c..6235303 100644 --- a/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs +++ b/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs @@ -11,11 +11,24 @@ namespace Luban.Job.Common.Utils { public static class StringTemplateUtil { - public static string TemplateDir { get; set; } + private static List TemplateSearchPaths { get; } = new List(); + + public static void AddTemplateSearchPath(string path) + { + TemplateSearchPaths.Add(path); + } public static string GetTemplateString(string templateName) { - return File.ReadAllText($"{TemplateDir}/{templateName}.tpl", Encoding.UTF8); + foreach (var searchPath in TemplateSearchPaths) + { + var fullPath = $"{searchPath}/{templateName}.tpl"; + if (File.Exists(fullPath)) + { + return File.ReadAllText(fullPath, Encoding.UTF8); + } + } + throw new FileNotFoundException($"can't find {templateName}.tpl in paths:{string.Join(';', TemplateSearchPaths)}"); } private static readonly ConcurrentDictionary s_templates = new(); diff --git a/src/Luban.Server/Source/Program.cs b/src/Luban.Server/Source/Program.cs index 94187e5..1690f4c 100644 --- a/src/Luban.Server/Source/Program.cs +++ b/src/Luban.Server/Source/Program.cs @@ -2,6 +2,7 @@ using Bright.Common; using CommandLine; using Luban.Common.Protos; using Luban.Common.Utils; +using Luban.Job.Common.Utils; using Luban.Server.Common; using System; using System.IO; @@ -20,8 +21,8 @@ namespace Luban.Server [Option('l', "loglevel", Required = false, HelpText = "log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF")] public string LogLevel { get; set; } = "INFO"; - [Option('t', "string template directory", Required = false, HelpText = "string template directory.")] - public string StringTemplateDir { get; set; } + [Option('t', "template_search_path", Required = false, HelpText = "additional template search path")] + public string TemplateSearchPath { get; set; } } private static CommandLineOptions ParseOptions(String[] args) @@ -48,11 +49,11 @@ namespace Luban.Server var options = ParseOptions(args); - if (string.IsNullOrEmpty(options.StringTemplateDir)) + if (!string.IsNullOrEmpty(options.TemplateSearchPath)) { - options.StringTemplateDir = FileUtil.GetPathRelateApplicationDirectory("Templates"); + StringTemplateUtil.AddTemplateSearchPath(options.TemplateSearchPath); } - Job.Common.Utils.StringTemplateUtil.TemplateDir = options.StringTemplateDir; + StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates")); Luban.Common.Utils.LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.FromString(options.LogLevel));