ファイル
manju summonerとClaude Fable 5.1 65867b00d0 resx の生成をソースジェネレーターから MSBuild タスクへ移し、CSV の変更が1回のビルドで反映されるようにした
ソースジェネレーターは CoreCompile 内で動くため、そこで resx を書いても
resx を .resources に変換する GenerateResource には間に合わず、CSV の変更が
2回目のビルドまで埋め込まれなかった。

- YukkuriMovieMaker.Generator.Build(MSBuild タスク)を追加し、CSV から言語別 resx を生成する
- YukkuriMovieMaker.Generator.targets を追加し、AssignTargetPaths の直前でタスクを実行して
  生成した resx を EmbeddedResource に登録する。TaskHostFactory で別プロセス実行にして
  タスク DLL のロックを避ける。CSV・同名 .cs・タスク DLL・プロジェクトファイルを入力にした
  増分判定と、生成済み resx の欠落検知を持つ
- ソースジェネレーターからは resx の書き出しを削除し、プロパティ生成と重複キー診断だけを残す

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-09 00:00:00 +09:00

93 行
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace YukkuriMovieMaker.Generator.Build
{
/// <summary>
/// [AutoGenLocalizer]付きクラスと同名の翻訳CSVから言語別resxを生成するMSBuildタスク。
/// PrepareResourcesより前に実行し、生成したresxをEmbeddedResourceへ加えられるようパスを返す。
/// </summary>
public sealed class GenerateLocalizeResx : Task
{
/// <summary>候補となるCSV(通常は@(AdditionalFiles)の.csv)</summary>
[Required]
public ITaskItem[] CsvFiles { get; set; } = Array.Empty<ITaskItem>();
/// <summary>コンパイル対象ソース(@(Compile))。同名の.csが含まれるCSVだけを対象にする</summary>
[Required]
public ITaskItem[] SourceFiles { get; set; } = Array.Empty<ITaskItem>();
/// <summary>
/// 相対パスの基準となるプロジェクトディレクトリ。
/// タスクホスト(別プロセス)ではカレントディレクトリが保証されないため明示的に受け取る
/// </summary>
[Required]
public string ProjectDirectory { get; set; } = "";
/// <summary>生成対象となった全resx(書き換えなかったものも含む)。プロジェクトディレクトリからの相対パス</summary>
[Output]
public ITaskItem[] GeneratedResxFiles { get; set; } = Array.Empty<ITaskItem>();
public override bool Execute()
{
var compileFullPaths = new HashSet<string>(
SourceFiles.Select(x => ToFullPath(x.ItemSpec)),
StringComparer.OrdinalIgnoreCase);
var outputs = new List<ITaskItem>();
foreach (var item in CsvFiles)
{
var csvPath = ToFullPath(item.ItemSpec);
if (!csvPath.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
continue;
if (!LocalizeResxGenerator.IsLocalizeCsv(csvPath, compileFullPaths))
continue;
try
{
foreach (var resx in LocalizeResxGenerator.Generate(csvPath))
{
if (resx.Updated)
Log.LogMessage(MessageImportance.Normal, "resxを更新しました: {0}", resx.Path);
outputs.Add(new TaskItem(ToRelativePath(resx.Path)));
}
}
catch (Exception ex)
{
Log.LogError(
subcategory: null,
errorCode: "YMMLOC002",
helpKeyword: null,
file: csvPath,
lineNumber: 0, columnNumber: 0, endLineNumber: 0, endColumnNumber: 0,
message: "翻訳CSVからresxを生成できませんでした: {0}", ex.Message);
}
}
GeneratedResxFiles = outputs.ToArray();
return !Log.HasLoggedErrors;
}
string ToFullPath(string path)
{
return Path.GetFullPath(Path.IsPathRooted(path) ? path : Path.Combine(ProjectDirectory, path));
}
/// <summary>
/// 評価時にグロブで拾われたEmbeddedResourceと同じ形(プロジェクト相対・バックスラッシュ)にする。
/// 同一項目の重複登録を避けるためにRemove/Includeで突き合わせられるようにする
/// </summary>
string ToRelativePath(string fullPath)
{
var root = Path.GetFullPath(ProjectDirectory).TrimEnd('\\', '/') + Path.DirectorySeparatorChar;
if (fullPath.StartsWith(root, StringComparison.OrdinalIgnoreCase))
return fullPath.Substring(root.Length).Replace('/', '\\');
return fullPath;
}
}
}