This commit is contained in:
femsci 2024-05-06 12:48:28 +02:00
parent f6198b1355
commit bef6dbec45
Signed by: femsci
GPG key ID: 08F7911F0E650C67

View file

@ -6,36 +6,78 @@ namespace Nyanbyte.DotnetTools.Rename;
public class Program public class Program
{ {
private static string destName = default!;
private static void Main(string[] args) private static void Main(string[] args)
{ {
DirectoryInfo dir = new(Environment.CurrentDirectory); DirectoryInfo dir = new("/tmp/test");
destName = args[0];
DirectoryInfo? parent = dir; DirectoryInfo? parent = dir;
List<FileInfo> projs; HashSet<FileInfo> projs = new();
Console.WriteLine($"{dir.Name} -> {destName}");
do do
{ {
var files = parent.GetFiles().Where(f => f.Extension == "sln" || f.Extension == "csproj"); var files = parent.GetFiles().Where(f => f.Extension == ".sln" || f.Extension == ".csproj");
projs = files.ToList(); projs.UnionWith(files);
} while ((parent = dir.Parent) != null); } while ((parent = parent.Parent) != null);
if (projs is null) Console.WriteLine($"P: {projs?.Count}");
if (projs == null)
{ {
return; return;
} }
FileInfo? slnFile = projs.SingleOrDefault(f => f.Extension == "sln"); FileInfo? slnFile = projs.SingleOrDefault(f => f.Extension == ".sln");
if (slnFile is not null) Console.WriteLine($"SLN: {slnFile == null}");
if (slnFile != null)
{ {
SolutionFile sln = SolutionFile.Parse(slnFile.FullName); 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) foreach (var proj in projs)
{ {
ProjectInstance pi = ProjectInstance.FromFile(proj.FullName, new()); Console.WriteLine($"Ren {proj.Name}");
string projDir = Path.GetDirectoryName(pi.FullPath)!; RenameProject(proj);
throw new NotImplementedException();
} }
} }
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)));
}
}
} }