Skip to content
This repository has been archived by the owner on Nov 19, 2019. It is now read-only.

Commit

Permalink
Add NuGet package resolution from project.assets.json
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Oct 1, 2019
1 parent 7481d3d commit 2f978df
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 36 deletions.
2 changes: 1 addition & 1 deletion source/Nuke.Common/Execution/BuildManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Maintainers of NUKE.
// Copyright 2019 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

Expand Down
35 changes: 34 additions & 1 deletion source/Nuke.Common/Tooling/NuGetPackageResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,43 @@ public static InstalledPackage GetLocalInstalledPackage(
}

// TODO: add HasLocalInstalledPackage() ?
// ReSharper disable once CyclomaticComplexity
public static IEnumerable<InstalledPackage> GetLocalInstalledPackages(
string packagesConfigFile,
bool resolveDependencies = true)
{
return packagesConfigFile.EndsWithOrdinalIgnoreCase("json")
? GetLocalInstalledPackagesFromAssetsFile(packagesConfigFile, resolveDependencies)
: GetLocalInstalledPackagesFromConfigFile(packagesConfigFile, resolveDependencies);
}

private static IEnumerable<InstalledPackage> GetLocalInstalledPackagesFromAssetsFile(
string packagesConfigFile,
bool resolveDependencies = true)
{
var assetsObject = SerializationTasks.JsonDeserializeFromFile<JObject>(packagesConfigFile);
var directReferences = assetsObject["project"]["frameworks"]
.Single().Single()["dependencies"]
.Children<JProperty>()
.Select(x => x.Name).ToList();

var allReferences = assetsObject["libraries"]
.Children<JProperty>()
.Where(x => x.Value["type"].ToString() == "package")
.Select(x => x.Name.Split('/'))
.Select(x => (PackageId: x.First(), Version: x.Last())).ToList();

foreach (var (name, version) in allReferences)
{
if (!resolveDependencies && !directReferences.Contains(name))
continue;

yield return GetGlobalInstalledPackage(name, version, packagesConfigFile);
}
}

private static IEnumerable<InstalledPackage> GetLocalInstalledPackagesFromConfigFile(
string packagesConfigFile,
bool resolveDependencies = true)
{
var packageIds = XmlTasks.XmlPeek(
packagesConfigFile,
Expand Down
4 changes: 1 addition & 3 deletions source/Nuke.Common/Tooling/PaketPackageResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public static class PaketPackageResolver
public static string GetLocalInstalledPackageDirectory(string packageId, string packagesConfigFile)
{
var packagesDirectory = GetPackagesDirectory(packagesConfigFile);
var packageDirectory = Path.Combine(packagesDirectory, packageId);
ControlFlow.Assert(Directory.Exists(packagesDirectory), $"Directory.Exists({packagesDirectory})");
return packageDirectory;
return Path.Combine(packagesDirectory, packageId);
}

private static string GetPackagesDirectory(string packagesConfigFile)
Expand Down
74 changes: 43 additions & 31 deletions source/Nuke.Common/Tooling/ToolPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Nuke.Common.IO;
using Nuke.Common.Utilities;
using Nuke.Common.Utilities.Collections;

namespace Nuke.Common.Tooling
{
[PublicAPI]
public static class ToolPathResolver
{
public static string ExecutingAssemblyDirectory;
public static string NuGetPackagesConfigFile;
public static string NuGetAssetsConfigFile;
public static string PaketPackagesConfigFile;

[CanBeNull]
Expand All @@ -34,37 +35,48 @@ public static string GetPackageExecutable(string packageId, string packageExecut
{
ControlFlow.Assert(packageId != null && packageExecutable != null, "packageId != null && packageExecutable != null");

string GetEmbeddedPackagesDirectory(string singlePackageId)
{
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var embeddedDirectory = (PathConstruction.AbsolutePath) assemblyDirectory / singlePackageId;
return Directory.Exists(embeddedDirectory) ? embeddedDirectory : null;
}

string GetNuGetPackagesDirectory(string singlePackageId) =>
NuGetPackagesConfigFile != null
? NuGetPackageResolver.GetLocalInstalledPackage(
singlePackageId,
NuGetPackagesConfigFile,
version)
.NotNull($"Could not find package '{packageId}'{(version != null ? $" ({version})" : string.Empty)} via '{NuGetPackagesConfigFile}'.")
.Directory
: null;

string GetPaketPackagesDirectory(string singlePackageId) =>
PaketPackagesConfigFile != null
? PaketPackageResolver.GetLocalInstalledPackageDirectory(
singlePackageId,
PaketPackagesConfigFile)
: null;

var packageIds = packageId.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var packageDirectory = packageIds
.Select(x =>
GetEmbeddedPackagesDirectory(x) ??
GetNuGetPackagesDirectory(x) ??
GetPaketPackagesDirectory(x))
.FirstOrDefault().NotNull("packageDirectory != null");
.SelectMany(x =>
new[]
{
ExecutingAssemblyDirectory != null
? Path.Combine(ExecutingAssemblyDirectory, x)
: null,
NuGetAssetsConfigFile != null
? NuGetPackageResolver.GetLocalInstalledPackage(x, NuGetAssetsConfigFile, version)?.Directory
: null,
NuGetPackagesConfigFile != null
? NuGetPackageResolver.GetLocalInstalledPackage(x, NuGetPackagesConfigFile, version)?.Directory
: null,
PaketPackagesConfigFile != null
? PaketPackageResolver.GetLocalInstalledPackageDirectory(x, PaketPackagesConfigFile)
: null
})
.FirstOrDefault(Directory.Exists)
.NotNull(
new[]
{
$"Could not find package {packageIds.Select(x => x.SingleQuote()).JoinCommaOr()} " +
$"{(version != null ? $"({version}) " : string.Empty)}" +
"using:"
}
.Concat(
new[]
{
NukeBuild.BuildProjectDirectory == null
? $"Embedded packages directory at '{ExecutingAssemblyDirectory}'"
: null,
NuGetAssetsConfigFile != null
? $"Project assets file '{NuGetAssetsConfigFile}'"
: null,
NuGetPackagesConfigFile != null
? $"NuGet packages config '{NuGetPackagesConfigFile}'"
: null,
PaketPackagesConfigFile != null
? $"Paket packages config '{PaketPackagesConfigFile}'"
: null
}.WhereNotNull().Select(x => $" - {x}")).JoinNewLine());

var packageExecutables = packageExecutable.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var absolutePackageExecutables = packageExecutables
Expand Down
18 changes: 18 additions & 0 deletions source/Nuke.Common/Utilities/String.Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ public static string JoinComma(this IEnumerable<string> values)
return values.Join(", ");
}

[Pure]
public static string JoinCommaOr(this IEnumerable<string> values)
{
var valuesList = values.ToArray();
return valuesList.Length >= 2
? valuesList.Reverse().Skip(1).Reverse().JoinComma() + ", or " + valuesList.Last()
: valuesList.JoinComma();
}

[Pure]
public static string JoinCommaAnd(this IEnumerable<string> values)
{
var valuesList = values.ToArray();
return valuesList.Length >= 2
? valuesList.Reverse().Skip(1).Reverse().JoinComma() + ", and " + valuesList.Last()
: valuesList.JoinComma();
}

[Pure]
public static string JoinNewLine(this IEnumerable<string> values, PlatformFamily? platformFamily = null)
{
Expand Down

0 comments on commit 2f978df

Please sign in to comment.