Skip to content

Commit

Permalink
Merge pull request #42 from ModernRonin/fix/other-imports-than-paket
Browse files Browse the repository at this point in the history
Fix/other imports than paket
  • Loading branch information
ModernRonin authored Oct 13, 2022
2 parents 4d2fce3 + fcc790f commit 7a64929
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
18 changes: 18 additions & 0 deletions ModernRonin.ProjectRenamer.Tests/CommonExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
42 changes: 19 additions & 23 deletions ModernRonin.ProjectRenamer/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -82,7 +83,7 @@ public void Run()
removeFromSolution();
removeOldReferences();
gitMove();
updatePaketReference();
updateImportProjects();
addNewReferences();
addToSolution();
updatePaket();
Expand Down Expand Up @@ -120,7 +121,8 @@ bool hasReferenceToOldProject(string p) =>

IEnumerable<string> 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));
}
Expand All @@ -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();
Expand All @@ -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) ? $"<Import Project=\"{paketPath}\" />" : 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("<Import Project")) return false;
if (!trimmed.Contains(restoreTargets)) return false;
return true;
var projectReference = import.Attribute("Project");
if (projectReference != null)
projectReference.Value = projectReference.Value.MoveRelativePath(oldDir, newDir);
}
}

Expand Down Expand Up @@ -208,7 +202,8 @@ void addToSolution()
return project switch
{
null => (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]))
};
Expand All @@ -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);
}
}

Expand Down
18 changes: 17 additions & 1 deletion ModernRonin.ProjectRenamer/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,38 @@ 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);
for (var i = 0; i < count; ++i) result.Append(self);
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());
}
}
7 changes: 4 additions & 3 deletions ModernRonin.ProjectRenamer/release.history
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<PackageReleaseNotes>
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!
Expand Down Expand Up @@ -29,8 +31,7 @@
* bugfix: if a project is not in a solution folder, the tools works now, too

1.0.0:
initial release
</PackageReleaseNotes>
initial release"</PackageReleaseNotes>
<!-- make MSBuild track this file for incremental builds. -->
<!-- ref https://blogs.msdn.microsoft.com/msbuild/2005/09/26/how-to-ensure-changes-to-a-custom-target-file-prompt-a-rebuild/ -->
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit 7a64929

Please sign in to comment.