From bef6dbec45c604b2a302d3fed9206dcec1dbc649 Mon Sep 17 00:00:00 2001 From: femsci Date: Mon, 6 May 2024 12:48:28 +0200 Subject: [PATCH] >.> --- src/Nyanbyte.DotnetTools.Rename/Program.cs | 66 ++++++++++++++++++---- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/src/Nyanbyte.DotnetTools.Rename/Program.cs b/src/Nyanbyte.DotnetTools.Rename/Program.cs index 931efd9..1d73b00 100644 --- a/src/Nyanbyte.DotnetTools.Rename/Program.cs +++ b/src/Nyanbyte.DotnetTools.Rename/Program.cs @@ -6,36 +6,78 @@ namespace Nyanbyte.DotnetTools.Rename; public class Program { + private static string destName = default!; private static void Main(string[] args) { - DirectoryInfo dir = new(Environment.CurrentDirectory); + DirectoryInfo dir = new("/tmp/test"); + + destName = args[0]; DirectoryInfo? parent = dir; - List projs; + HashSet projs = new(); + + Console.WriteLine($"{dir.Name} -> {destName}"); do { - var files = parent.GetFiles().Where(f => f.Extension == "sln" || f.Extension == "csproj"); - projs = files.ToList(); - } while ((parent = dir.Parent) != null); + var files = parent.GetFiles().Where(f => f.Extension == ".sln" || f.Extension == ".csproj"); + projs.UnionWith(files); + } while ((parent = parent.Parent) != null); - if (projs is null) + Console.WriteLine($"P: {projs?.Count}"); + + if (projs == null) { return; } - FileInfo? slnFile = projs.SingleOrDefault(f => f.Extension == "sln"); - if (slnFile is not null) + FileInfo? slnFile = projs.SingleOrDefault(f => f.Extension == ".sln"); + Console.WriteLine($"SLN: {slnFile == null}"); + if (slnFile != null) { SolutionFile sln = SolutionFile.Parse(slnFile.FullName); - projs = sln.ProjectsInOrder.Select(p => new FileInfo(p.AbsolutePath)).ToList(); + projs = sln.ProjectsInOrder.Select(p => new FileInfo(p.AbsolutePath)).ToHashSet(); } foreach (var proj in projs) { - ProjectInstance pi = ProjectInstance.FromFile(proj.FullName, new()); - string projDir = Path.GetDirectoryName(pi.FullPath)!; - throw new NotImplementedException(); + Console.WriteLine($"Ren {proj.Name}"); + RenameProject(proj); } } + + private static void RenameProject(FileInfo projectFile) + { + string projectDir = projectFile.DirectoryName!; + string oldName = Path.GetFileNameWithoutExtension(projectDir); + + projectFile.MoveTo($"{Path.Combine(projectFile.Directory!.FullName, destName)}.csproj"); + + foreach (var path in Directory.EnumerateFiles(projectDir, "**")) + { + FileInfo file = new(path); + Task.Run(async () => + { + using StreamReader reader = File.OpenText(path); + string? line; + using MemoryStream mem = new(); + StreamWriter writer = new(mem); + while ((line = await reader.ReadLineAsync()) != null) + { + line = line.Replace(oldName, destName); + await writer.WriteLineAsync(line); + } + reader.Close(); + using var fos = File.OpenWrite(path); + await mem.CopyToAsync(fos); + }); + + + Console.WriteLine($"Path: {path}, dir: {Path.GetDirectoryName(path)}, fn: {file.Name.Replace(oldName, destName)}"); + + if (file.Name.Contains(oldName)) + File.Move(path, Path.Combine(Path.GetDirectoryName(path)!, file.Name.Replace(oldName, destName))); + } + + } }