Skip to content

Commit

Permalink
[change] 如果未设置pathValidator.rootDir选项,自动禁用path校验并且打印警告日志
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Sep 17, 2023
1 parent 82833a3 commit 4ca3caa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
31 changes: 22 additions & 9 deletions src/Luban.Core/GenerationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@ public class GenerationContext

public static LubanConfig GlobalConf { get; set; }

public DefAssembly Assembly { get; }
public DefAssembly Assembly { get; private set; }

public RawTarget Target => Assembly.Target;

public List<string> IncludeTags { get; }
public List<string> IncludeTags { get; private set; }

public List<string> ExcludeTags { get; }
public List<string> ExcludeTags { get; private set; }

private readonly ConcurrentDictionary<string, TableDataInfo> _recordsByTables = new();

public string TopModule => Target.TopModule;

public List<DefTable> Tables => Assembly.GetAllTables();

private List<DefTypeBase> ExportTypes { get; }
private List<DefTypeBase> ExportTypes { get; set; }

public List<DefTable> ExportTables { get; }
public List<DefTable> ExportTables { get; private set; }

public List<DefBean> ExportBeans { get; }
public List<DefBean> ExportBeans { get; private set; }

public List<DefEnum> ExportEnums { get; }
public List<DefEnum> ExportEnums { get; private set; }

public TimeZoneInfo TimeZone { get; }
public TimeZoneInfo TimeZone { get; private set; }

private readonly Dictionary<string, object> _uniqueObjects = new();

Expand All @@ -70,9 +70,13 @@ public void LoadDatas()
s_logger.Info("load datas end");
}

public GenerationContext(GenerationContextBuilder builder)
public GenerationContext()
{
Current = this;
}

public void Init(GenerationContextBuilder builder)
{
Assembly = builder.Assembly;
IncludeTags = builder.IncludeTags;
ExcludeTags = builder.ExcludeTags;
Expand Down Expand Up @@ -196,6 +200,15 @@ public object GetUniqueObject(string key)
}
}

public object TryGetUniqueObject(string key)
{
lock (this)
{
_uniqueObjects.TryGetValue(key, out var obj);
return obj;
}
}

public object GetOrAddUniqueObject(string key, Func<object> factory)
{
lock (this)
Expand Down
3 changes: 2 additions & 1 deletion src/Luban.Core/Pipeline/DefaultPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected void LoadSchema()
protected void PrepareGenerationContext()
{
s_logger.Debug("prepare generation context");
_genCtx = new GenerationContext();
_defAssembly = new DefAssembly(_rawAssembly, _args.Target, _args.OutputTables);

var generationCtxBuilder = new GenerationContextBuilder
Expand All @@ -61,7 +62,7 @@ protected void PrepareGenerationContext()
ExcludeTags = _args.ExcludeTags,
TimeZone = _args.TimeZone,
};
_genCtx = new GenerationContext(generationCtxBuilder);
_genCtx.Init(generationCtxBuilder);
}

protected void LoadDatas()
Expand Down
19 changes: 16 additions & 3 deletions src/Luban.DataValidator.Builtin/Path/PathValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ namespace Luban.DataValidator.Builtin.Path;
public class PathValidator : DataValidatorBase
{
private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger();

private readonly string _rootDir;

private string _rawPattern;

private IPathPattern _pathPattern;

public PathValidator()
{
if (!EnvManager.Current.TryGetOption(BuiltinOptionNames.PathValidatorFamily, BuiltinOptionNames.PathValidatorRootDir, false, out _rootDir))
{
string key = $"{BuiltinOptionNames.PathValidatorFamily}.{BuiltinOptionNames.PathValidatorRootDir}";
if (GenerationContext.Current.GetOrAddUniqueObject(key, () => this) == this)
{
s_logger.Warn("don't set option '-x {0}=<rootValidationDir>', path validation is disabled", key);
}
}
}

public override void Compile(DefField field, TType type)
Expand Down Expand Up @@ -89,15 +99,18 @@ public override void Compile(DefField field, TType type)

public override void Validate(DataValidatorContext ctx, TType type, DType data)
{
if (string.IsNullOrEmpty(_rootDir))
{
return;
}

string value = ((DString)data).Value;
if (value == "" && _pathPattern.EmptyAble)
{
return;
}

string rootDir = EnvManager.Current.GetOption(BuiltinOptionNames.PathValidatorFamily, BuiltinOptionNames.PathValidatorRootDir, false);

if (!_pathPattern.ExistPath(rootDir, value))
if (!_pathPattern.ExistPath(_rootDir, value))
{
s_logger.Error("{}:{} (来自文件:{}) 找不到对应文件", RecordPath, value, Source);
GenerationContext.Current.LogValidatorFail(this);
Expand Down
2 changes: 1 addition & 1 deletion src/Luban.L10N/DefaultTextProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Load()
if (!EnvManager.Current.TryGetOption(BuiltinOptionNames.L10NFamily, BuiltinOptionNames.TextProviderFile, true,
out string textProviderFile))
{
s_logger.Error("not found option: '-x {0}.{1}=<textProviderFile>', text validation is disabled", BuiltinOptionNames.L10NFamily, BuiltinOptionNames.TextProviderFile);
s_logger.Warn("not found option: '-x {0}.{1}=<textProviderFile>', text validation is disabled", BuiltinOptionNames.L10NFamily, BuiltinOptionNames.TextProviderFile);
_enableTextValidation = false;
return;
}
Expand Down

0 comments on commit 4ca3caa

Please sign in to comment.