Remove magic ftl file naming.

This commit is contained in:
Paul Chote
2024-10-20 18:48:15 +01:00
committed by Gustas
parent 5a0c8439fc
commit c09d7cbdea
7 changed files with 54 additions and 86 deletions

View File

@@ -30,8 +30,6 @@ namespace OpenRA.Mods.Common.Lint
{
sealed class CheckFluentReferences : ILintPass, ILintMapPass
{
static readonly Regex FilenameRegex = new(@"(?<language>[^\/\\]+)\.ftl$");
void ILintMapPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map)
{
if (map.FluentMessageDefinitions == null)
@@ -43,32 +41,27 @@ namespace OpenRA.Mods.Common.Lint
emitWarning($"Empty key in map ftl files required by {context}");
var mapTranslations = FieldLoader.GetValue<string[]>("value", map.FluentMessageDefinitions.Value);
var allModTranslations = modData.Manifest.Translations;
foreach (var language in GetModLanguages(allModTranslations))
// For maps we don't warn on unused keys. They might be unused on *this* map,
// but the mod or another map may use them and we don't have sight of that.
CheckKeys(allModTranslations.Concat(mapTranslations), map.Open, usedKeys,
_ => false, emitError, emitWarning);
var modFluentBundle = new FluentBundle(modData.Manifest.FluentCulture, allModTranslations, modData.DefaultFileSystem, _ => { });
var mapFluentBundle = new FluentBundle(modData.Manifest.FluentCulture, mapTranslations, map, error => emitError(error.Message));
foreach (var group in usedKeys.KeysWithContext)
{
// Check keys and variables are not missing across all language files.
// But for maps we don't warn on unused keys. They might be unused on *this* map,
// but the mod or another map may use them and we don't have sight of that.
CheckKeys(
allModTranslations.Concat(mapTranslations), map.Open, usedKeys,
language, _ => false, emitError, emitWarning);
var modFluentBundle = new FluentBundle(language, allModTranslations, modData.DefaultFileSystem, _ => { });
var mapFluentBundle = new FluentBundle(language, mapTranslations, map, error => emitError(error.Message));
foreach (var group in usedKeys.KeysWithContext)
if (modFluentBundle.HasMessage(group.Key))
{
if (modFluentBundle.HasMessage(group.Key))
{
if (mapFluentBundle.HasMessage(group.Key))
emitWarning($"Key `{group.Key}` in `{language}` language in map ftl files already exists in mod translations and will not be used.");
}
else if (!mapFluentBundle.HasMessage(group.Key))
{
foreach (var context in group)
emitWarning($"Missing key `{group.Key}` in `{language}` language in map ftl files required by {context}");
}
if (mapFluentBundle.HasMessage(group.Key))
emitWarning($"Key `{group.Key}` in map ftl files already exists in mod translations and will not be used.");
}
else if (!mapFluentBundle.HasMessage(group.Key))
{
foreach (var context in group)
emitWarning($"Missing key `{group.Key}` in map ftl files required by {context}");
}
}
@@ -80,34 +73,30 @@ namespace OpenRA.Mods.Common.Lint
void ILintPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData)
{
Console.WriteLine("Testing Fluent references");
var (usedKeys, testedFields) = GetUsedFluentKeysInMod(modData);
foreach (var context in usedKeys.EmptyKeyContexts)
emitWarning($"Empty key in mod translation files required by {context}");
var allModTranslations = modData.Manifest.Translations.ToArray();
foreach (var language in GetModLanguages(allModTranslations))
CheckModWidgets(modData, usedKeys, testedFields);
// With the fully populated keys, check keys and variables are not missing and not unused across all language files.
var keyWithAttrs = CheckKeys(
allModTranslations, modData.DefaultFileSystem.Open, usedKeys,
file =>
!modData.Manifest.AllowUnusedTranslationsInExternalPackages ||
!modData.DefaultFileSystem.IsExternalFile(file),
emitError, emitWarning);
foreach (var group in usedKeys.KeysWithContext)
{
Console.WriteLine($"Testing language: {language}");
CheckModWidgets(modData, usedKeys, testedFields);
if (keyWithAttrs.Contains(group.Key))
continue;
// With the fully populated keys, check keys and variables are not missing and not unused across all language files.
var keyWithAttrs = CheckKeys(
allModTranslations, modData.DefaultFileSystem.Open, usedKeys,
language,
file =>
!modData.Manifest.AllowUnusedTranslationsInExternalPackages ||
!modData.DefaultFileSystem.IsExternalFile(file),
emitError, emitWarning);
foreach (var group in usedKeys.KeysWithContext)
{
if (keyWithAttrs.Contains(group.Key))
continue;
foreach (var context in group)
emitWarning($"Missing key `{group.Key}` in `{language}` language in mod ftl files required by {context}");
}
foreach (var context in group)
emitWarning($"Missing key `{group.Key}` in mod ftl files required by {context}");
}
// Check if we couldn't test any fields.
@@ -121,14 +110,6 @@ namespace OpenRA.Mods.Common.Lint
$"`{field.ReflectedType.Name}.{field.Name}` - previous warnings may be incorrect");
}
static IEnumerable<string> GetModLanguages(IEnumerable<string> translations)
{
return translations
.Select(filename => FilenameRegex.Match(filename).Groups["language"].Value)
.Distinct()
.OrderBy(l => l);
}
static Keys GetUsedFluentKeysInRuleset(Ruleset rules)
{
var usedKeys = new Keys();
@@ -427,15 +408,11 @@ namespace OpenRA.Mods.Common.Lint
static HashSet<string> CheckKeys(
IEnumerable<string> paths, Func<string, Stream> openFile, Keys usedKeys,
string language, Func<string, bool> checkUnusedKeysForFile,
Action<string> emitError, Action<string> emitWarning)
Func<string, bool> checkUnusedKeysForFile, Action<string> emitError, Action<string> emitWarning)
{
var keyWithAttrs = new HashSet<string>();
foreach (var path in paths)
{
if (!path.EndsWith($"{language}.ftl", StringComparison.Ordinal))
continue;
var stream = openFile(path);
using (var reader = new StreamReader(stream))
{