Compare commits

..

No commits in common. "feature/rename" and "nya" have entirely different histories.

View file

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