Skip to content

Commit

Permalink
Validation works.
Browse files Browse the repository at this point in the history
  • Loading branch information
MKadaner committed Oct 8, 2023
1 parent af40c87 commit e5f903b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 30 deletions.
15 changes: 11 additions & 4 deletions misc/build-checks/LngChecker/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Immutable;
using System.Reflection.Emit;
using System.Text.RegularExpressions;

namespace LngChecker
Expand All @@ -17,7 +19,12 @@ public static IEnumerable<string> UnIndent(this IEnumerable<string> lines)
}
}

public static IEnumerable<(List<string> Comments, string Label)> ToLangHppLabels(
public static IEnumerable<string> Indent(this IEnumerable<string> lines)
{
return lines.Select(l => l.Length == 0 ? l : $"\t{l}");
}

public static IEnumerable<(ImmutableList<string> comments, string label)> ToLangHppLabels(
this IEnumerable<string> lines)
{
var comments = new List<string>();
Expand All @@ -27,7 +34,7 @@ public static IEnumerable<string> UnIndent(this IEnumerable<string> lines)
var match = GlobalConstants.LangHppLabelPattern.Match(line);
if (match.Success)
{
yield return (comments, match.Groups["label"].Value);
yield return (comments.ToImmutableList(), match.Groups["label"].Value);
comments = new List<string>();
}
else
Expand All @@ -37,14 +44,14 @@ public static IEnumerable<string> UnIndent(this IEnumerable<string> lines)
}
}

public static IEnumerable<(string Label, bool NeedTranslation, string Value)> ToLngLabels(
public static IEnumerable<(string label, bool needTranslation, string value)> ToLngLabels(
this IEnumerable<string> lines)
{
var needTranslation = false;

foreach (var line in lines)
{
if (line == GlobalConstants.NeedTranslationPattern)
if (line == GlobalConstants.NeedTranslation)
{
needTranslation = true;
continue;
Expand Down
15 changes: 9 additions & 6 deletions misc/build-checks/LngChecker/GlobalConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ namespace LngChecker
{
internal static class GlobalConstants
{
public static readonly string FarDir = Path.Combine(GeneratedConstants.FarManagerRootDir, "far");
public static readonly string LangHpp = Path.Combine(FarDir, "lang.hpp");
public static readonly string FarEngLng = Path.Combine(FarDir, "FarEng.lng.m4");
public const string LangHpp = "lang.hpp";
public const string FarEngLng = "FarEng.lng.m4";
public const string FarLngMask = "Far???.lng.m4";
public static readonly string FarDir = Path.GetFullPath(Path.Combine(GeneratedConstants.FarManagerRootDir, "far"));
public static readonly string LangHppPath = Path.Combine(FarDir, LangHpp);
public static readonly string FarEngLngPath = Path.Combine(FarDir, FarEngLng);

public static readonly Regex LangHppLabelPattern = new(@"^(?<label>M[\w\d]+),$", _RegexOptions);

Expand All @@ -19,10 +22,10 @@ internal static class GlobalConstants
@"",
};

public static readonly Regex LngFileHeaderLanguagePattern = new(@"^\.Language=(?<language>\w+),(?<comment>\w+)$", _RegexOptions);
public static readonly Regex LngFileLabelValuePattern = new(@"^(?<label>M[\w\d]+)=(?<value>"".+"")$", _RegexOptions);
public static readonly Regex LngFileHeaderLanguagePattern = new(@"^\.Language=(?<language>\w+),(?<comment>[\p{L}\s()]+)$", _RegexOptions);
public static readonly Regex LngFileLabelValuePattern = new(@"^(?<label>M[\w\d]+)=(?<value>"".*"")$", _RegexOptions);

public const string NeedTranslationPattern = "// need translation:";
public const string NeedTranslation = "// need translation:";

private const RegexOptions _RegexOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture;
}
Expand Down
29 changes: 15 additions & 14 deletions misc/build-checks/LngChecker/LangHpp.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Immutable;

namespace LngChecker
{
internal class LangHpp
Expand All @@ -9,30 +11,29 @@ public LangHpp(string path)
.SkipWhile(l => l != "{")
.Skip(1)
.TakeWhile(l => l != "};")
.ToArray();
_labels = _labelLines
.ToImmutableList();
Labels = _labelLines
.UnIndent()
.ToLangHppLabels()
.ToList();
.ToImmutableList();

SanityCheck();
Validate();
}

public List<(List<string> Comments, string Label)> Labels => _labels;

private void SanityCheck()
private void Validate()
{
if (!_labelLines.SequenceEqual(
_labels
.SelectMany(l => l.Comments.Append($"{l.Label},"))
.Select(l => l.Length == 0 ? l : $"\t{l}")))
if (!_labelLines.SequenceEqual(Generate()))
{
throw new Exception($"Failed sanity check on file {_path}");
throw new Exception($"Failed validation on file {_path}");
}
}

private IEnumerable<string> Generate() =>
Labels.SelectMany(l => l.comments.Append($"{l.label},")).Indent();

public ImmutableList<(ImmutableList<string> comments, string label)> Labels { get; }

private readonly string _path;
private readonly string[] _labelLines;
private readonly List<(List<string> Comments, string Label)> _labels;
private readonly ImmutableList<string> _labelLines;
}
}
9 changes: 8 additions & 1 deletion misc/build-checks/LngChecker/LngChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ public class Program
{
public static int Main(string[] parameters)
{
var langHpp = new LangHpp(GlobalConstants.LangHpp);
var langHpp = new LangHpp(GlobalConstants.LangHppPath);
var farEngLng = new LngFile(GlobalConstants.FarEngLngPath);
farEngLng.Validate(langHpp.Labels);

foreach (var farLngFile in Directory.EnumerateFiles(GlobalConstants.FarDir, GlobalConstants.FarLngMask, SearchOption.TopDirectoryOnly))
{
new LngFile(farLngFile).Validate(langHpp.Labels);
}

return 0;
}
Expand Down
42 changes: 37 additions & 5 deletions misc/build-checks/LngChecker/LngFile.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
using System.Collections.Immutable;
using System.Text;

namespace LngChecker
{
internal class LngFile
{
public LngFile(string path)
{
_path = path;
_allLines = File.ReadAllLines(_path);
_allLines = File.ReadAllLines(_path).ToImmutableList();
(_language, _comment) = ParseHeader();
_labels = _allLines
Labels = _allLines
.Skip(GlobalConstants.LngFileHeaderFormat.Length)
.ToLngLabels()
.ToDictionary(l => l.Label, l => (l.NeedTranslation, l.Value));
.ToImmutableDictionary(l => l.label, l => (l.needTranslation, l.value));
}

public void Validate(IEnumerable<(ImmutableList<string> comments, string label)> labels)
{
if (!_allLines.SequenceEqual(Generate(labels)))
{
throw new Exception($"Failed validation on file {_path}");
}
}

public void WriteBack(IEnumerable<(ImmutableList<string> comments, string label)> labels)
{
File.WriteAllLines(_path, Generate(labels), Encoding.UTF8);
}

private (string language, string comment) ParseHeader()
Expand All @@ -21,10 +37,26 @@ public LngFile(string path)
throw new Exception($"Cannot parse language from file {_path}");
}

private IEnumerable<string> Generate(IEnumerable<(ImmutableList<string> comments, string label)> labels)
{
return GlobalConstants.LngFileHeaderFormat
.Select(l => string.Format(l, _language, _comment))
.Concat(labels.SelectMany(l => l.comments.Concat(GetLabelStrings(l.label))));
}

private IEnumerable<string> GetLabelStrings(string label)
{
if (!Labels.TryGetValue(label, out var value)) yield break;

if (value.needTranslation) yield return GlobalConstants.NeedTranslation;
yield return $"{label}={value.value}";
}

public ImmutableDictionary<string, (bool needTranslation, string value)> Labels { get; }

private readonly string _path;
private readonly string[] _allLines;
private readonly ImmutableList<string> _allLines;
private readonly string _language;
private readonly string _comment;
private readonly Dictionary<string, (bool NeedTranslation, string Value)> _labels;
}
}

0 comments on commit e5f903b

Please sign in to comment.