From 99f04faa3971b3f10241492de807b8af11744ce5 Mon Sep 17 00:00:00 2001 From: Mark Hajek Date: Thu, 13 Oct 2022 10:06:52 +0100 Subject: [PATCH 1/7] generalized the adjusting of relative import project directives in project files - now not only the paket.restore reference will be updated, but others as well --- .../CommonExtensionsTests.cs | 18 ++++++++ ModernRonin.ProjectRenamer/Application.cs | 42 +++++++++---------- .../CommonExtensions.cs | 8 ++++ 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs index 2a2f819..82198c3 100644 --- a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs +++ b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs @@ -36,6 +36,24 @@ public void IsDirectorySeparator_returns_true_for_windows_separator() '\\'.IsDirectorySeparator().Should().Be(IsWindows); } + [Test] + public void MoveRelativePath() + { + if (IsWindows) + { + @"..\..\.paket.restore".MoveRelativePath(@"c:\projects\mine", @"c:\github\projects\first") + .Should() + .Be(@"..\..\..\.paket.restore"); + } + else + { + @"..\..\.paket.restore" + .MoveRelativePath("/users/mine/projects", "/users/mine/github/projects/first") + .Should() + .Be(@"..\..\..\.paket.restore"); + } + } + [Test] public void Repeat_returns_the_string_repeated_the_passed_number_of_times() => "alpha".Repeat(3).Should().Be("alphaalphaalpha"); diff --git a/ModernRonin.ProjectRenamer/Application.cs b/ModernRonin.ProjectRenamer/Application.cs index 1212cd2..bb37ae3 100644 --- a/ModernRonin.ProjectRenamer/Application.cs +++ b/ModernRonin.ProjectRenamer/Application.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Xml.Linq; using Microsoft.Build.Construction; using MoreLinq.Extensions; @@ -82,7 +83,7 @@ public void Run() removeFromSolution(); removeOldReferences(); gitMove(); - updatePaketReference(); + updateImportProjects(); addNewReferences(); addToSolution(); updatePaket(); @@ -120,7 +121,8 @@ bool hasReferenceToOldProject(string p) => IEnumerable getReferencedProjects(string project) { - var relativeReferences = _dotnet.GetReferencedProjects(project).Select(p => p = Path.Combine(p.Split('\\'))); + var relativeReferences = _dotnet.GetReferencedProjects(project) + .Select(p => p = Path.Combine(p.Split('\\'))); var baseDirectory = Path.GetFullPath(Path.GetDirectoryName(project)); return relativeReferences.Select(r => r.ToAbsolutePath(baseDirectory)); } @@ -146,8 +148,8 @@ void build() _dotnet.BuildSolution(() => { if (_input.AskUser( - "dotnet build returned an error or warning - do you want to rollback all changes?") - ) + "dotnet build returned an error or warning - do you want to rollback all changes?") + ) { _git.RollbackAllChanges(); _runtime.Abort(); @@ -161,25 +163,17 @@ void updatePaket() if (isPaketUsed && !_configuration.DontRunPaketInstall) _dotnet.PaketInstall(); } - void updatePaketReference() + void updateImportProjects() { - if (!isPaketUsed) return; - const string restoreTargets = @"\.paket\Paket.Restore.targets"; - var nesting = Path.GetFullPath(newProjectPath).Count(CommonExtensions.IsDirectorySeparator) - - CurrentDirectoryAbsolute.Count(CommonExtensions.IsDirectorySeparator) - 1; - var paketPath = @"..\".Repeat(nesting)[..^1] + restoreTargets; - var lines = File.ReadAllLines(newProjectPath).Select(fixup); - File.WriteAllLines(newProjectPath, lines); - - string fixup(string line) => - isPaketReference(line) ? $"" : line; - - bool isPaketReference(string line) + var project = XDocument.Load(newProjectPath).Root; + project!.Elements().Where(e => e.Name == "Import").ForEach(updateProject); + project.Save(newProjectPath); + + void updateProject(XElement import) { - var trimmed = line.Trim(); - if (!trimmed.StartsWith(" (false, null, null), - _ when project.ParentProjectGuid == null => (true, Path.Combine(project.AbsolutePath.Split('\\')), null), + _ when project.ParentProjectGuid == null => (true, + Path.Combine(project.AbsolutePath.Split('\\')), null), _ => (true, Path.Combine(project.AbsolutePath.Split('\\')), path(solution.ProjectsByGuid[project.ParentProjectGuid])) }; @@ -231,7 +226,8 @@ string[] allProjects() return all.Except(excluded).ToArray(); - string[] filesIn(string directory) => _filesystem.FindProjectFiles(directory, true, _configuration.ProjectFileExtension); + string[] filesIn(string directory) => + _filesystem.FindProjectFiles(directory, true, _configuration.ProjectFileExtension); } } diff --git a/ModernRonin.ProjectRenamer/CommonExtensions.cs b/ModernRonin.ProjectRenamer/CommonExtensions.cs index 5f3ff52..b297b07 100644 --- a/ModernRonin.ProjectRenamer/CommonExtensions.cs +++ b/ModernRonin.ProjectRenamer/CommonExtensions.cs @@ -13,6 +13,14 @@ public static class CommonExtensions public static bool IsDirectorySeparator(this char self) => self == Path.DirectorySeparatorChar || self == Path.AltDirectorySeparatorChar; + public static string MoveRelativePath(this string self, + string oldBaseDirectory, + string newBaseDirectory) + { + var absolute = self.ToAbsolutePath(oldBaseDirectory); + return absolute.ToRelativePath(newBaseDirectory); + } + public static string Repeat(this string self, int count) { var result = new StringBuilder(self.Length * count); From dfb37344a93ed6c271e7da89b2ab96f97ab8c7d7 Mon Sep 17 00:00:00 2001 From: Mark Hajek Date: Thu, 13 Oct 2022 10:09:55 +0100 Subject: [PATCH 2/7] bumped version, fixes #38 --- ModernRonin.ProjectRenamer/release.history | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ModernRonin.ProjectRenamer/release.history b/ModernRonin.ProjectRenamer/release.history index 2bf7999..0455977 100644 --- a/ModernRonin.ProjectRenamer/release.history +++ b/ModernRonin.ProjectRenamer/release.history @@ -1,7 +1,9 @@  - 2.2.0 + 2.2.1 +2.2.1: +* bugfix: all Import Project directives with relative paths will be correctly adjusted now 2.2.0: * feature: tool can be used on VB projects, too; thanks to @fsbflavio for the PR! * bugfix: fixed a potential deadlock; thanks to @fsbflavio for the PR! @@ -29,8 +31,7 @@ * bugfix: if a project is not in a solution folder, the tools works now, too 1.0.0: -initial release - +initial release" $(MSBuildAllProjects);$(MSBuildThisFileFullPath) From ec3057934cc9b1e8f48376bdcd049477746129aa Mon Sep 17 00:00:00 2001 From: Mark Hajek Date: Thu, 13 Oct 2022 10:21:53 +0100 Subject: [PATCH 3/7] debug output for linux --- ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs index 82198c3..197fdc4 100644 --- a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs +++ b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using System; +using System.Runtime.InteropServices; using FluentAssertions; using NUnit.Framework; @@ -47,6 +48,11 @@ public void MoveRelativePath() } else { + var absolutePath = @"..\..\.paket.restore".ToAbsolutePath("/users/mine/projects"); + var relative = absolutePath.ToRelativePath("/users/mine/github/projects/first"); + Console.WriteLine(absolutePath); + Console.WriteLine(relative); + @"..\..\.paket.restore" .MoveRelativePath("/users/mine/projects", "/users/mine/github/projects/first") .Should() From 3af9d6979269b19aa7eebd7286443f755049a708 Mon Sep 17 00:00:00 2001 From: Mark Hajek Date: Thu, 13 Oct 2022 10:27:33 +0100 Subject: [PATCH 4/7] fix for linux --- ModernRonin.ProjectRenamer/CommonExtensions.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ModernRonin.ProjectRenamer/CommonExtensions.cs b/ModernRonin.ProjectRenamer/CommonExtensions.cs index b297b07..c6f8e8a 100644 --- a/ModernRonin.ProjectRenamer/CommonExtensions.cs +++ b/ModernRonin.ProjectRenamer/CommonExtensions.cs @@ -28,15 +28,23 @@ public static string Repeat(this string self, int count) return result.ToString(); } + public static string ReplaceBackslashesWithSlashesOnLinux(this string self) + { + if (Path.DirectorySeparatorChar != Path.AltDirectorySeparatorChar) + return self; // we are on Windows + return self.Replace('\\', '/'); + } + public static string ReplaceSlashesWithBackslashes(this string self) => self.Replace('/', '\\'); public static string ToAbsolutePath(this string self, string baseDirectory) { if (self.First().IsDirectorySeparator()) self = self[1..]; + self = self.ReplaceBackslashesWithSlashesOnLinux(); return Path.GetFullPath(self, baseDirectory); } public static string ToRelativePath(this string self, string baseDirectory) => - Path.GetRelativePath(baseDirectory, self); + Path.GetRelativePath(baseDirectory, self.ReplaceBackslashesWithSlashesOnLinux()); } } \ No newline at end of file From 1b71845743ee8d1b884f6042aca94bf1007708f9 Mon Sep 17 00:00:00 2001 From: Mark Hajek Date: Thu, 13 Oct 2022 10:29:46 +0100 Subject: [PATCH 5/7] fixed test --- .../CommonExtensionsTests.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs index 197fdc4..b259ac6 100644 --- a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs +++ b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using FluentAssertions; using NUnit.Framework; @@ -48,15 +47,10 @@ public void MoveRelativePath() } else { - var absolutePath = @"..\..\.paket.restore".ToAbsolutePath("/users/mine/projects"); - var relative = absolutePath.ToRelativePath("/users/mine/github/projects/first"); - Console.WriteLine(absolutePath); - Console.WriteLine(relative); - @"..\..\.paket.restore" .MoveRelativePath("/users/mine/projects", "/users/mine/github/projects/first") .Should() - .Be(@"..\..\..\.paket.restore"); + .Be(@"../../../.paket.restore"); } } From db62fdbf806337b35470c3c86c404f044b5d889c Mon Sep 17 00:00:00 2001 From: Mark Hajek Date: Thu, 13 Oct 2022 10:32:32 +0100 Subject: [PATCH 6/7] corrected test --- ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs index b259ac6..1f03499 100644 --- a/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs +++ b/ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs @@ -50,7 +50,7 @@ public void MoveRelativePath() @"..\..\.paket.restore" .MoveRelativePath("/users/mine/projects", "/users/mine/github/projects/first") .Should() - .Be(@"../../../.paket.restore"); + .Be(@"../../../../.paket.restore"); } } From fcc790f1376278a87bdc2621bd94fcf77169a282 Mon Sep 17 00:00:00 2001 From: Mark Hajek Date: Thu, 13 Oct 2022 10:35:03 +0100 Subject: [PATCH 7/7] updated readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1839d66..1659c0f 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ dotnet tool update --global ModernRonin.ProjectRenamer When I publish a new version, I always post at [my blog](https://modernronin.github.io/) under the [renameproject tag](https://modernronin.github.io/tags/renameproject/), aside from updating this readme here. ### Release History +2.2.1: +* bugfix: all Import Project directives with relative paths will be correctly adjusted now + 2.2.0: * feature: tool can be used on VB projects, too; thanks to [@fsbflavio](https://github.com/fsbflavio) for the PR! * bugfix: fixed a potential deadlock; thanks to [@fsbflavio](https://github.com/fsbflavio) for the PR!