using KssSmaPlaLib.Encryption; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace KssSmaPlaLib.Tools.ClaraHack.NamespaceAdjust { public static class NamespaceAdjusterUxer { public static bool TuckIntoMain(string[] args) { if (args.Length != 3) return false; if (args[0].ToLower() != "/namespaceadjuster") return false; var projectTopFolderPath = args[1]; var projectName = args[2]; if (NamespaceAdjusterUxer.Try( projectTopFolderPath, projectName, out var updatedCount, out var resultMessage) == false) { Console.WriteLine(resultMessage); } else { Console.WriteLine($"namespace調整完了。[更新ファイル数:{updatedCount}]"); } return true; } public static bool Try( string projectRootPath, string projectName, out int updatedCount, out string resultMessage) { updatedCount = 0; resultMessage = string.Empty; // フォルダバリデ if (!Directory.Exists(projectRootPath)) { resultMessage = $"プロジェクトルートフォルダが存在しません。[プロジェクトルートフォルダ:{projectRootPath}]"; return false; } // 配下全どり var csFiles = Directory.GetFiles( projectRootPath, "*.cs", SearchOption.AllDirectories); foreach (var filePath in csFiles) { // トップ下相対フォルダパス var relativeDir = Path.GetDirectoryName(filePath)?.Substring(projectRootPath.Length).TrimStart(Path.DirectorySeparatorChar); // セパレータ変換('\'→'.') var namespaceSuffix = relativeDir?.Replace(Path.DirectorySeparatorChar, '.'); // トップノード(PJ名)つけてフル化 var fullNamespace = string.IsNullOrEmpty(namespaceSuffix) // PJ名のみ(カレントはトップ下) ? projectName // PJ名+配下 : $"{projectName}.{namespaceSuffix}"; // フル読み var content = File.ReadAllText(filePath); // namespace行の置換 var newContent = Regex.Replace( content, @"namespace\s+[\w\.]+", $"namespace {fullNamespace}"); if (newContent != content) { // 内容変わった時だけ書き込み File.WriteAllText(filePath, newContent); updatedCount++; } } return true; } } }