【调整】Luban.Server的 命令行参数-t 语义调整为额外的模板搜索路径。如果指定,优先搜索此路径,再搜索默认的Templates路径。

main
walon 2021-08-26 17:27:31 +08:00
parent 77fe01dea6
commit 04b51fccc4
3 changed files with 34 additions and 14 deletions

View File

@ -4,6 +4,7 @@ using Luban.Client.Common.Utils;
using Luban.Client.Utils; using Luban.Client.Utils;
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Common.Utils; using Luban.Common.Utils;
using Luban.Job.Common.Utils;
using Luban.Server; using Luban.Server;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -34,7 +35,8 @@ namespace Luban.ClientServer
public string[] WatchDir { get; set; } 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) private static void PrintUsage(string err)
@ -54,7 +56,7 @@ Options:
-l --loglevel <level> log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF -l --loglevel <level> log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF
-c --cachemetafile <file> cache meta file name. default is '.cache.meta' -c --cachemetafile <file> cache meta file name. default is '.cache.meta'
-w --watch <dir> watch data change and regenerate. -w --watch <dir> watch data change and regenerate.
-t --templatedirectory <dir> string templates directory. default is 'Templates' -t --template_search_path <dir> additional template search path
-h --help show usage -h --help show usage
"); ");
} }
@ -122,9 +124,9 @@ Options:
break; break;
} }
case "-t": case "-t":
case "--templatedirectory": case "--template_search_path":
{ {
ops.StringTemplateDir = args[++i]; ops.TemplateSearchPath = args[++i];
break; break;
} }
case "--": case "--":
@ -154,7 +156,11 @@ Options:
private static void StartServer(AllCommandLineOptions 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); GenServer.Ins.Start(options.Port, ProtocolStub.Factories);
@ -206,9 +212,9 @@ Options:
ThreadPool.SetMinThreads(Math.Max(4, processorCount), 5); ThreadPool.SetMinThreads(Math.Max(4, processorCount), 5);
ThreadPool.SetMaxThreads(Math.Max(16, processorCount * 4), 10); 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)) if (string.IsNullOrWhiteSpace(options.Host))
{ {

View File

@ -11,11 +11,24 @@ namespace Luban.Job.Common.Utils
{ {
public static class StringTemplateUtil public static class StringTemplateUtil
{ {
public static string TemplateDir { get; set; } private static List<string> TemplateSearchPaths { get; } = new List<string>();
public static void AddTemplateSearchPath(string path)
{
TemplateSearchPaths.Add(path);
}
public static string GetTemplateString(string templateName) 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<string, Template> s_templates = new(); private static readonly ConcurrentDictionary<string, Template> s_templates = new();

View File

@ -2,6 +2,7 @@ using Bright.Common;
using CommandLine; using CommandLine;
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Common.Utils; using Luban.Common.Utils;
using Luban.Job.Common.Utils;
using Luban.Server.Common; using Luban.Server.Common;
using System; using System;
using System.IO; 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")] [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"; public string LogLevel { get; set; } = "INFO";
[Option('t', "string template directory", Required = false, HelpText = "string template directory.")] [Option('t', "template_search_path", Required = false, HelpText = "additional template search path")]
public string StringTemplateDir { get; set; } public string TemplateSearchPath { get; set; }
} }
private static CommandLineOptions ParseOptions(String[] args) private static CommandLineOptions ParseOptions(String[] args)
@ -48,11 +49,11 @@ namespace Luban.Server
var options = ParseOptions(args); 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)); Luban.Common.Utils.LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.FromString(options.LogLevel));