Skip to content

Commit

Permalink
Merge branch 'ID-12903' into 'master'
Browse files Browse the repository at this point in the history
Добавил поддержку ModuleReference

See merge request vostok-libraries/devtools!2
  • Loading branch information
Тебайкин Максим Дмитриевич committed Aug 9, 2024
2 parents 01dbe40 + d223e90 commit e414e36
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
25 changes: 25 additions & 0 deletions dotnetcementrefs/FileUtilities.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
57 changes: 55 additions & 2 deletions dotnetcementrefs/dotnetcementrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -105,6 +106,8 @@ private static async Task HandleProjectAsync(
return;
}

ReplaceModuleReferences(project);

var references = FindCementReferences(project, allProjectsInSolution, parameters.CementReferencePrefixes);
if (parameters.AllowLocalProjects)
{
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions dotnetcementrefs/dotnetcementrefs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../idevops.cement.cli/src/Cement.Vostok.Devtools/Cement.Vostok.Devtools.csproj"/>
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions module.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ src:
full-build:
deps:
- vostok.devtools.ilrepack.bin
- idevops.cement.cli/[email protected]
build:
target: None
configuration: None

dotnetcementrefs:
deps:
- idevops.cement.cli/[email protected]
build:
- name: Build
tool: dotnet
Expand Down

0 comments on commit e414e36

Please sign in to comment.