From 8bc728b29831742b02d7688b72bf25e5fdddf782 Mon Sep 17 00:00:00 2001 From: Nikita Bogdanov Date: Tue, 6 Aug 2024 15:19:52 +0500 Subject: [PATCH 1/3] add ModuleReference support --- dotnetcementrefs/FileUtilities.cs | 25 +++++++++++ dotnetcementrefs/dotnetcementrefs.cs | 57 +++++++++++++++++++++++- dotnetcementrefs/dotnetcementrefs.csproj | 6 +++ module.yaml | 2 + 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 dotnetcementrefs/FileUtilities.cs diff --git a/dotnetcementrefs/FileUtilities.cs b/dotnetcementrefs/FileUtilities.cs new file mode 100644 index 0000000..488952e --- /dev/null +++ b/dotnetcementrefs/FileUtilities.cs @@ -0,0 +1,25 @@ +using System.IO; + +namespace dotnetcementrefs; + +internal static class FileUtilities +{ + public static string? GetDirectoryNameOfDirectoryAbove(string startingDirectory, string directoryName) + { + var lookInDirectory = Path.GetFullPath(startingDirectory); + + do + { + var possibleDirectory = Path.Combine(lookInDirectory, directoryName); + + if (Directory.Exists(possibleDirectory)) + { + return lookInDirectory; + } + + lookInDirectory = Path.GetDirectoryName(lookInDirectory); + } while (lookInDirectory != null); + + return null; + } +} \ No newline at end of file diff --git a/dotnetcementrefs/dotnetcementrefs.cs b/dotnetcementrefs/dotnetcementrefs.cs index b5f807b..7eb1c1c 100644 --- a/dotnetcementrefs/dotnetcementrefs.cs +++ b/dotnetcementrefs/dotnetcementrefs.cs @@ -4,15 +4,16 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using Cement.Vostok.Devtools; using Microsoft.Build.Construction; -using Microsoft.Build.Definition; using Microsoft.Build.Evaluation; using NuGet.Common; using NuGet.Configuration; -using NuGet.Protocol; using NuGet.Protocol.Core.Types; using NuGet.Versioning; +namespace dotnetcementrefs; + public static class Program { private static readonly Dictionary<(string package, bool includePrerelease, string[] sourceUrls), NuGetVersion> NugetCache = new(); @@ -105,6 +106,8 @@ private static async Task HandleProjectAsync( return; } + ReplaceModuleReferences(project); + var references = FindCementReferences(project, allProjectsInSolution, parameters.CementReferencePrefixes); if (parameters.AllowLocalProjects) { @@ -151,6 +154,56 @@ private static async Task HandleProjectAsync( await Console.Out.WriteLineAsync(); } + private static void ReplaceModuleReferences(Project project) + { + var moduleReferences = project.ItemsIgnoringCondition + .Where(item => item.ItemType == "ModuleReference") + .ToList(); + + if (moduleReferences.Count == 0) + { + return; + } + + var workspacePath = FileUtilities.GetDirectoryNameOfDirectoryAbove(project.FullPath, ".cement"); + if (workspacePath == null) + { + throw new($"Failed to find cement workspace for '{project.FullPath}'. " + + $"Make sure project is inside cement workspace."); + } + + var moduleReferenceTransformer = new ModuleReferenceTransformer(); + + foreach (var moduleReference in moduleReferences) + { + try + { + var references = moduleReferenceTransformer + .ToReferences(workspacePath, moduleReference.EvaluatedInclude); + + foreach (var reference in references) + { + if (moduleReference.Xml.Parent is ProjectItemGroupElement groupElement) + { + groupElement.AddItem("Reference", reference.Include, reference.Metadata); + } + else + { + project.AddItem("Reference", reference.Include, reference.Metadata); + } + + project.RemoveItem(moduleReference); + } + } + catch (Exception e) + { + throw new($"Failed to replace ModuleReference at '{moduleReference.Xml.Location}'", e); + } + } + + project.ReevaluateIfNecessary(); + } + private static string[] GetUsePrereleaseForPrefixes(Project project) { var property = project.Properties.FirstOrDefault(x => x.Name == "DotnetCementRefsUsePrereleaseForPrefixes"); diff --git a/dotnetcementrefs/dotnetcementrefs.csproj b/dotnetcementrefs/dotnetcementrefs.csproj index aa72763..958bada 100644 --- a/dotnetcementrefs/dotnetcementrefs.csproj +++ b/dotnetcementrefs/dotnetcementrefs.csproj @@ -30,4 +30,10 @@ + + + ..\..\idevops.cement.cli\src\Cement.Vostok.Devtools\bin\Release\netstandard2.0\Cement.Vostok.Devtools.dll + + + diff --git a/module.yaml b/module.yaml index 4a144c8..8e94ac4 100644 --- a/module.yaml +++ b/module.yaml @@ -11,6 +11,8 @@ full-build: configuration: None dotnetcementrefs: + deps: + - idevops.cement.cli/vostok-devtools@ID-12903 build: - name: Build tool: dotnet From 3080fcad809b2891d7d37c42ae64684c6335c8ae Mon Sep 17 00:00:00 2001 From: Nikita Bogdanov Date: Thu, 8 Aug 2024 14:13:52 +0500 Subject: [PATCH 2/3] pin idevops.cement.cli/vostok-devtools to v7.9.1 --- module.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module.yaml b/module.yaml index 8e94ac4..022147c 100644 --- a/module.yaml +++ b/module.yaml @@ -12,7 +12,7 @@ full-build: dotnetcementrefs: deps: - - idevops.cement.cli/vostok-devtools@ID-12903 + - idevops.cement.cli/vostok-devtools@v7.9.1 build: - name: Build tool: dotnet From d223e901b6f543fd047b035ffd24de23da61bbe3 Mon Sep 17 00:00:00 2001 From: Nikita Bogdanov Date: Thu, 8 Aug 2024 15:07:57 +0500 Subject: [PATCH 3/3] reference idevops.cement.cli as a project --- dotnetcementrefs/dotnetcementrefs.csproj | 4 +--- module.yaml | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dotnetcementrefs/dotnetcementrefs.csproj b/dotnetcementrefs/dotnetcementrefs.csproj index 958bada..6bcdea5 100644 --- a/dotnetcementrefs/dotnetcementrefs.csproj +++ b/dotnetcementrefs/dotnetcementrefs.csproj @@ -31,9 +31,7 @@ - - ..\..\idevops.cement.cli\src\Cement.Vostok.Devtools\bin\Release\netstandard2.0\Cement.Vostok.Devtools.dll - + diff --git a/module.yaml b/module.yaml index 022147c..2304b32 100644 --- a/module.yaml +++ b/module.yaml @@ -6,6 +6,7 @@ src: full-build: deps: - vostok.devtools.ilrepack.bin + - idevops.cement.cli/vostok-devtools@v7.9.1 build: target: None configuration: None