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