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
{
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<FileInfo> projs;
HashSet<FileInfo> 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)));
}
}
}