Skip to content

Commit

Permalink
[new] 新增--customTemplateDir参数,用于指定其他模板搜索路径
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Aug 19, 2023
1 parent 0e2dbc4 commit aa9a03d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/Luban.Core/Tmpl/TemplateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Init()
AddTemplateSearchPath($"{curDir}/Templates", true);
}

public void AddTemplateSearchPath(string templateSearchPath, bool sureExists = false)
public void AddTemplateSearchPath(string templateSearchPath, bool sureExists = false, bool addFirst = false)
{
if (!Directory.Exists(templateSearchPath))
{
Expand All @@ -34,7 +34,14 @@ public void AddTemplateSearchPath(string templateSearchPath, bool sureExists = f
}
return;
}
_templateSearchPaths.Add(templateSearchPath);
if (addFirst)
{
_templateSearchPaths.Insert(0, templateSearchPath);
}
else
{
_templateSearchPaths.Add(templateSearchPath);
}
}

public bool TryGetTemplateString(string templateName, out string result)
Expand Down
15 changes: 14 additions & 1 deletion src/Luban/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Luban.Pipeline;
using Luban.Protobuf.TypeVisitors;
using Luban.Schema.Builtin;
using Luban.Tmpl;
using Luban.Utils;
using NLog;
using System.Reflection;
Expand Down Expand Up @@ -50,6 +51,9 @@ private class CommandOptions
[Option("timeZone", Required = false, HelpText = "time zone")]
public string TimeZone { get; set; }

[Option("customTemplateDir", Required = false, HelpText = "custom template dirs")]
public IEnumerable<string> CustomTemplateDirs { get; set; }

[Option("validationFailAsError", Required = false, HelpText = "validation fail as error")]
public bool ValidationFailAsError { get; set; }

Expand All @@ -69,16 +73,25 @@ private static void Main(string[] args)

var launcher = new SimpleLauncher();
launcher.Start(ParseXargs(opts.Xargs));
AddCustomTemplateDirs(opts.CustomTemplateDirs);

var pipeline = PipelineManager.Ins.CreatePipeline(opts.Pipeline);
pipeline.Run(CreatePipelineArgs(opts));
if (opts.ValidationFailAsError && GenerationContext.Current.AnyValidatorFail)
{
s_logger.Error("have some validation failure. exit code: 1");
s_logger.Error("encounter some validation failure. exit code: 1");
Environment.Exit(1);
}
s_logger.Info("bye~");
}

private static void AddCustomTemplateDirs(IEnumerable<string> dirs)
{
foreach (var dir in dirs)
{
TemplateManager.Ins.AddTemplateSearchPath(dir, true, true);
}
}

private static CommandOptions ParseArgs(string[] args)
{
Expand Down

0 comments on commit aa9a03d

Please sign in to comment.