fix script

This commit is contained in:
SinusCosinus
2024-07-04 20:39:48 +02:00
parent 4f70f96e4d
commit 7608b6df1f

View File

@@ -24,68 +24,51 @@ namespace rename_ue_project
/// <exception cref="ArgumentException">if an app parameter was invalid</exception>
static async Task Main(string[] args)
{
if (args.Length < 1) {
if (args.Length < 1)
{
throw new ArgumentException("Please provide the absolute root path to your unreal project");
}
var projectPath = args[0];
if (args.Length < 2) {
if (args.Length < 2)
{
throw new ArgumentException("Please provide the new project name");
}
var projectName = args[1];
if (args.Length < 3) {
if (args.Length < 3)
{
throw new ArgumentException("Please provide a description for your READ.ME (as markdown)");
}
var descLong = args[2];
if (!Directory.Exists(projectPath))
{
throw new IOException($"Directory {projectPath} does not exist");
}
// step 0: validate ue project name
var rgxName = "'^[a-zA-Z0-9_-]+$";
Match m = Regex.Match(projectName, rgxName, RegexOptions.IgnoreCase);
if (m.Success)
{
throw new ArgumentException("Unreal project names can not be longer than 20 chars and must not contain special chars, except - and _");
}
validateNewName(projectName);
// step 1: rename directory
// todo: find the .uproject file. in case there is more than one, throw an exception
if (!Directory.Exists(OLD_PROJECT_NAME))
{
throw new IOException($"Directory {OLD_PROJECT_NAME} does not exist");
}
Directory.Move(
Path.Combine(projectPath, OLD_PROJECT_NAME),
Path.Combine(projectPath, projectName)
);
Console.WriteLine("Renamed project directory");
string oldFile = renameDir(projectPath, projectName);
// step 2: rename project file
File.Move(
Path.Combine(projectPath, projectName, $"{OLD_PROJECT_NAME}{UE_PROJECT_EXTENSION}"),
Path.Combine(projectPath, projectName, $"{projectName}{UE_PROJECT_EXTENSION}")
);
renameUProjectFile(projectPath, projectName, oldFile);
// step 3: replace everything in gitignore
var ignoreContent = await File.ReadAllTextAsync(Path.Combine(projectPath, GITIGNORE));
ignoreContent = ignoreContent.Replace(OLD_PROJECT_NAME, projectName);
Console.WriteLine("Replaced project name in gitignore");
await updateIgnore(projectPath, projectName, oldFile);
// step 4: write read.me in format # projectName + new line + descrLong
var readMe = @$"# {projectName}{Environment.NewLine}{descLong}";
await File.WriteAllTextAsync(Path.Combine(projectPath, README), readMe);
Console.WriteLine("Wrote READ.ME");
await updateReadMe(projectPath, projectName, descLong);
// step 5: replace name in DefaultGame.ini
await updateGameIni(projectPath, projectName);
}
private static async Task updateGameIni(string projectPath, string projectName)
{
var configPath = Path.Combine(projectPath, projectName, CONFIG_FOLDER, DEGAULT_GAME_INI);
if (!File.Exists(configPath))
{
@@ -93,13 +76,67 @@ namespace rename_ue_project
}
var defaultGameIni = await File.ReadAllTextAsync(configPath);
var rgxPrjName = @"(?<=ProjectName=)[^\s]+";
var rgxPrjName = @"(?<=ProjectName=).*";
// Perform the replacement
defaultGameIni = Regex.Replace(defaultGameIni, rgxPrjName, projectName);
await File.WriteAllTextAsync(configPath, defaultGameIni);
Console.WriteLine($"Replaced project name in {DEGAULT_GAME_INI}");
}
private static async Task updateReadMe(string projectPath, string projectName, string descLong)
{
var readMe = @$"# {projectName}{Environment.NewLine}{descLong}";
await File.WriteAllTextAsync(Path.Combine(projectPath, README), readMe);
Console.WriteLine("Wrote READ.ME");
}
private static async Task updateIgnore(string projectPath, string projectName, string oldFile)
{
var ignoreContent = await File.ReadAllTextAsync(Path.Combine(projectPath, GITIGNORE));
ignoreContent = ignoreContent.Replace(Path.GetFileName(oldFile), projectName);
Console.WriteLine("Replaced project name in gitignore");
}
private static void renameUProjectFile(string projectPath, string projectName, string oldFile)
{
File.Move(
Path.Combine(projectPath, projectName, Path.GetFileName(oldFile)),
Path.Combine(projectPath, projectName, $"{projectName}{UE_PROJECT_EXTENSION}")
);
}
private static string renameDir(string projectPath, string projectName)
{
var files = Directory.EnumerateFiles(projectPath, $"*{UE_PROJECT_EXTENSION}", SearchOption.AllDirectories).ToList();
if (files.Count > 1)
{
throw new IOException($"More than one {UE_PROJECT_EXTENSION} file found.");
}
var oldFile = files[0];
var oldDir = Path.GetDirectoryName(oldFile);
if (!Directory.Exists(oldDir))
{
throw new IOException($"Directory {oldDir} does not exist");
}
Directory.Move(
Path.Combine(projectPath, oldDir),
Path.Combine(projectPath, projectName)
);
Console.WriteLine("Renamed project directory");
return oldFile;
}
private static void validateNewName(string projectName)
{
var rgxName = "'^[a-zA-Z0-9_-]+$";
Match m = Regex.Match(projectName, rgxName, RegexOptions.IgnoreCase);
if (m.Success)
{
throw new ArgumentException("Unreal project names can not be longer than 20 chars and must not contain special chars, except - and _");
}
}
}
}