Skip to content

Commit

Permalink
Add support for limiting nuget to current project only. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennawatson authored Jun 10, 2019
1 parent 121a483 commit f4e57a0
Show file tree
Hide file tree
Showing 15 changed files with 8,550 additions and 116 deletions.
28 changes: 9 additions & 19 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Environment.SetVariableNames();

System.Environment.SetEnvironmentVariable("MSBUILDDISABLENODEREUSE", "1");

// Whitelisted Packages
var packageWhitelist = new[]
{
Expand All @@ -16,24 +18,12 @@ var packageTestWhitelist = new[]
MakeAbsolute(File("./src/Pharmacist.Tests/Pharmacist.Tests.csproj")),
};

// var msbuildTask = Task("BuildMsBuild")
// .IsDependentOn("GitVersion")
// .Does(() =>
// {
// var msBuildSettings = new MSBuildSettings() {
// Restore = true,
// ToolPath = ToolSettings.MsBuildPath,
// }
// .WithProperty("TreatWarningsAsErrors", BuildParameters.TreatWarningsAsErrors.ToString())
// .SetMaxCpuCount(ToolSettings.MaxCpuCount)
// .SetConfiguration(BuildParameters.Configuration)
// .WithTarget("build;pack")
// .SetVerbosity(Verbosity.Minimal);

// MSBuild("./src/Pharmacist.MsBuild/Pharmacist.MsBuild.csproj", msBuildSettings);
// });

// BuildParameters.Tasks.TestxUnitCoverletGenerateTask.IsDependentOn(msbuildTask);
var killMsBuildTask = Task("KillMsBuild").Does(() =>
{
StartProcess("taskkill", "/F /IM MSBuild.exe");
});

BuildParameters.Tasks.TestxUnitCoverletGenerateTask.IsDependentOn(killMsBuildTask);

BuildParameters.SetParameters(context: Context,
buildSystem: BuildSystem,
Expand All @@ -43,6 +33,6 @@ BuildParameters.SetParameters(context: Context,
artifactsDirectory: "./artifacts",
sourceDirectory: "./src");

ToolSettings.SetToolSettings(context: Context);
ToolSettings.SetToolSettings(context: Context, maxCpuCount: 1);

Build.Run();
50 changes: 32 additions & 18 deletions src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ namespace Pharmacist.Core.NuGet
/// </summary>
public static class NuGetPackageHelper
{
private const int ProcessingCount = 32;
/// <summary>
/// Gets the default nuget source.
/// </summary>
public const string DefaultNuGetSource = "https://api.nuget.org/v3/index.json";

private const string DefaultNuGetSource = "https://api.nuget.org/v3/index.json";
private const int ProcessingCount = 32;

private static readonly string[] DefaultFoldersToGrab = { PackagingConstants.Folders.Lib, PackagingConstants.Folders.Build, PackagingConstants.Folders.Ref };

Expand All @@ -40,13 +43,12 @@ public static class NuGetPackageHelper
private static readonly NuGetLogger _logger = new NuGetLogger();
private static readonly SourceCacheContext _sourceCacheContext = NullSourceCacheContext.Instance;
private static readonly PackageDownloadContext _downloadContext = new PackageDownloadContext(_sourceCacheContext);
private static readonly List<Lazy<INuGetResourceProvider>> _providers;
private static readonly IFrameworkNameProvider _frameworkNameProvider = DefaultFrameworkNameProvider.Instance;

static NuGetPackageHelper()
{
_providers = new List<Lazy<INuGetResourceProvider>>();
_providers.AddRange(Repository.Provider.GetCoreV3());
Providers = new List<Lazy<INuGetResourceProvider>>();
Providers.AddRange(Repository.Provider.GetCoreV3());

var machineWideSettings = new XPlatMachineWideSetting();
_globalPackagesPath = SettingsUtility.GetGlobalPackagesFolder(machineWideSettings.Settings.LastOrDefault() ?? (ISettings)NullSettings.Instance);
Expand All @@ -57,6 +59,11 @@ static NuGetPackageHelper()
/// </summary>
public static string PackageDirectory { get; } = Path.Combine(Path.GetTempPath(), "ReactiveUI.Pharmacist");

/// <summary>
/// Gets the providers for the nuget resources.
/// </summary>
public static List<Lazy<INuGetResourceProvider>> Providers { get; }

/// <summary>
/// Downloads the specified packages and returns the files and directories where the package NuGet package lives.
/// </summary>
Expand All @@ -79,7 +86,7 @@ static NuGetPackageHelper()
frameworks = frameworks ?? new[] { FrameworkConstants.CommonFrameworks.NetStandard20 };

// Use the provided nuget package source, or use nuget.org
var sourceRepository = new SourceRepository(nugetSource ?? new PackageSource(DefaultNuGetSource), _providers);
var sourceRepository = new SourceRepository(nugetSource ?? new PackageSource(DefaultNuGetSource), Providers);

var packages = await Task.WhenAll(libraryIdentities.Select(x => GetBestMatch(x, sourceRepository, token))).ConfigureAwait(false);

Expand Down Expand Up @@ -108,11 +115,29 @@ static NuGetPackageHelper()
frameworks = frameworks ?? new[] { FrameworkConstants.CommonFrameworks.NetStandard20 };

// Use the provided nuget package source, or use nuget.org
var sourceRepository = new SourceRepository(nugetSource ?? new PackageSource(DefaultNuGetSource), _providers);
var sourceRepository = new SourceRepository(nugetSource ?? new PackageSource(DefaultNuGetSource), Providers);

return DownloadPackageFilesAndFolder(packageIdentities, frameworks, sourceRepository, getDependencies, packageFolders, token);
}

/// <summary>
/// Gets the best matching PackageIdentity for the specified LibraryRange.
/// </summary>
/// <param name="identity">The library range to find the best patch for.</param>
/// <param name="sourceRepository">The source repository where to match.</param>
/// <param name="token">A optional cancellation token.</param>
/// <returns>The best matching PackageIdentity to the specified version range.</returns>
public static async Task<PackageIdentity> GetBestMatch(LibraryRange identity, SourceRepository sourceRepository, CancellationToken token)
{
var findPackageResource = await sourceRepository.GetResourceAsync<FindPackageByIdResource>(token).ConfigureAwait(false);

var versions = await findPackageResource.GetAllVersionsAsync(identity.Name, _sourceCacheContext, _logger, token).ConfigureAwait(false);

var bestPackageVersion = versions?.FindBestMatch(identity.VersionRange, version => version);

return new PackageIdentity(identity.Name, bestPackageVersion);
}

/// <summary>
/// Downloads the specified packages and returns the files and directories where the package NuGet package lives.
/// </summary>
Expand Down Expand Up @@ -276,17 +301,6 @@ private static bool EqualToOrLessThan(this NuGetFramework firstFramework, NuGetF
return firstFramework.Version <= secondFramework.Version;
}

private static async Task<PackageIdentity> GetBestMatch(LibraryRange identity, SourceRepository sourceRepository, CancellationToken token)
{
var findPackageResource = await sourceRepository.GetResourceAsync<FindPackageByIdResource>(token).ConfigureAwait(false);

var versions = await findPackageResource.GetAllVersionsAsync(identity.Name, _sourceCacheContext, _logger, token).ConfigureAwait(false);

var bestPackageVersion = versions?.FindBestMatch(identity.VersionRange, version => version);

return new PackageIdentity(identity.Name, bestPackageVersion);
}

private static IEnumerable<FrameworkSpecificGroup> GetFileGroups(this PackageReaderBase reader, string folder)
{
var groups = new Dictionary<NuGetFramework, List<string>>(new NuGetFrameworkFullComparer());
Expand Down
19 changes: 19 additions & 0 deletions src/Pharmacist.Core/ObservablesForEventGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,24 @@ public static async Task WriteHeader(Stream outputStream)
await streamWriter.WriteAsync(await TemplateManager.GetTemplateAsync(TemplateManager.HeaderTemplate).ConfigureAwait(false)).ConfigureAwait(false);
await streamWriter.FlushAsync().ConfigureAwait(false);
}

/// <summary>
/// Writes the header for a output.
/// </summary>
/// <param name="outputStream">The stream where to write to.</param>
/// <param name="libraryRanges">The library ranges to include as packages included in the output.</param>
/// <returns>A task to monitor the progress.</returns>
public static async Task WriteHeader(Stream outputStream, IReadOnlyCollection<LibraryRange> libraryRanges)
{
StreamWriter streamWriter = new StreamWriter(outputStream);
await streamWriter.WriteAsync(await TemplateManager.GetTemplateAsync(TemplateManager.HeaderTemplate).ConfigureAwait(false)).ConfigureAwait(false);

foreach (var libraryRange in libraryRanges)
{
await streamWriter.WriteLineAsync($"// Package included {libraryRange}").ConfigureAwait(false);
}

await streamWriter.FlushAsync().ConfigureAwait(false);
}
}
}
31 changes: 2 additions & 29 deletions src/Pharmacist.IntegrationTest/Pharmacist.IntegrationTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,10 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Pharmacist.MsBuild\Pharmacist.MsBuild.csproj" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2"/>
</ItemGroup>

<PropertyGroup>
<CoreCompileDependsOn>
$(CoreCompileDependsOn);
GeneratePharmacist;
</CoreCompileDependsOn>
</PropertyGroup>

<PropertyGroup>
<IntermediateOutputPath Condition="$(IntermediateOutputPath) == '' Or $(IntermediateOutputPath) == '*Undefined*'">$(MSBuildProjectDirectory)\obj\$(Configuration)\</IntermediateOutputPath>
<PharmacistTaskAssemblyFile Condition="'$(MSBuildRuntimeType)' != 'Full'">$(MSBuildProjectDirectory)\..\Pharmacist.MsBuild\bin\$(Configuration)\netstandard2.0\Pharmacist.MsBuild.dll</PharmacistTaskAssemblyFile>
<PharmacistTaskAssemblyFile Condition="'$(MSBuildRuntimeType)' == 'Full'">$(MSBuildProjectDirectory)\..\Pharmacist.MsBuild\bin\$(Configuration)\net461\Pharmacist.MsBuild.dll</PharmacistTaskAssemblyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pharmacist.Common" Version="1.*" />
<ProjectReference Include="..\Pharmacist.MsBuild\Pharmacist.MsBuild.csproj" PrivateAssets="all" />
</ItemGroup>

<UsingTask TaskName="Pharmacist.MSBuild.PharmacistNuGetTask" AssemblyFile="$(PharmacistTaskAssemblyFile)" />

<Target Name="GeneratePharmacist" BeforeTargets="CoreCompile">
<PharmacistNuGetTask PackageReferences="@(PackageReference)"
TargetFramework="$(TargetFramework)"
OutputFile="$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs" />

<Message Text="Processed Pharmacist Packages" />

<ItemGroup Condition="Exists('$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs')">
<Compile Include="$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs" />
</ItemGroup>
</Target>
</Project>
15 changes: 9 additions & 6 deletions src/Pharmacist.MsBuild/PharmacistNuGetTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ public override bool Execute()

using (var stream = new FileStream(Path.Combine(OutputFile), FileMode.Create, FileAccess.Write))
{
ObservablesForEventGenerator.WriteHeader(stream).ConfigureAwait(false).GetAwaiter().GetResult();

var packageReferences = PackageReferences.Where(x => !ExclusionPackageReferenceSet.Contains(x.ItemSpec));

var packages = new List<LibraryRange>();

// Include all package references that aren't ourselves.
foreach (var packageReference in packageReferences)
foreach (var packageReference in PackageReferences)
{
var include = packageReference.ItemSpec;
var include = packageReference.GetMetadata("PackageName");

if (ExclusionPackageReferenceSet.Contains(include))
{
continue;
}

if (!VersionRange.TryParse(packageReference.GetMetadata("Version"), out var nuGetVersion))
{
Expand All @@ -92,6 +93,8 @@ public override bool Execute()
packages.Add(packageIdentity);
}

ObservablesForEventGenerator.WriteHeader(stream).ConfigureAwait(false).GetAwaiter().GetResult();

try
{
ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(stream, packages, TargetFramework.ToFrameworks()).GetAwaiter().GetResult();
Expand Down
36 changes: 31 additions & 5 deletions src/Pharmacist.MsBuild/targets/Pharmacist.MSBuild.targets
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- The global detection vs local detection is inspired from the following https://github.com/dotnet/sdk/issues/1151#issuecomment-459275750 -->
<PropertyGroup>
<PharmacistGlobalPackages Condition="'$(PharmacistGlobalPackages)' == ''">false</PharmacistGlobalPackages>
<ProjectFileContent Condition="'$(PharmacistGlobalPackages)' == 'false'">$([System.IO.File]::ReadAllText($(MSBuildProjectFullPath)))</ProjectFileContent>
</PropertyGroup>

<PropertyGroup>
<CoreCompileDependsOn>
$(CoreCompileDependsOn);
GeneratePharmacist;
</CoreCompileDependsOn>
</PropertyGroup>

<Choose>
<When Condition="'$(PharmacistGlobalPackages)' == 'false'">
<ItemGroup>
<PharmacistPackageReference Include="@(PackageReference)">
<PackageName>%(Identity)</PackageName>
<Version>%(Version)</Version>
<InProject>$(ProjectFileContent.Contains('Include="%(Identity)"'))</InProject>
</PharmacistPackageReference>
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<PharmacistPackageReference Include="@(PackageReference)">
<PackageName>%(Identity)</PackageName>
<Version>%(Version)</Version>
<InProject>True</InProject>
</PharmacistPackageReference>
</ItemGroup>
</Otherwise>
</Choose>

<PropertyGroup>
<IntermediateOutputPath Condition="$(IntermediateOutputPath) == '' Or $(IntermediateOutputPath) == '*Undefined*'">$(MSBuildProjectDirectory)\obj\$(Configuration)\</IntermediateOutputPath>
<PharmacistTaskAssemblyFile Condition="'$(MSBuildRuntimeType)' != 'Full'">$(MSBuildThisFileDirectory)..\..\build\netstandard2.0\Pharmacist.MSBuild.dll</PharmacistTaskAssemblyFile>
Expand All @@ -16,15 +43,14 @@
<UsingTask TaskName="Pharmacist.MsBuild.PharmacistNuGetTask" AssemblyFile="$(PharmacistTaskAssemblyFile)" />

<Target Name="GeneratePharmacist" BeforeTargets="CoreCompile">
<PharmacistNuGetTask PackageReferences="@(PackageReference)"
<PharmacistNuGetTask PackageReferences="@(PharmacistPackageReference -> WithMetadataValue('InProject', True) )"
TargetFramework="$(TargetFramework)"
OutputFile="$(IntermediateOutputPath)\Pharmacist.g.cs" />
OutputFile="$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs" />

<Message Text="Processed Pharmacist Packages" />

<ItemGroup Condition="Exists('$(IntermediateOutputPath)\Pharmacist.g.cs')">
<Compile Include="$(IntermediateOutputPath)\Pharmacist.g.cs" />
<ItemGroup Condition="Exists('$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs')">
<Compile Include="$(IntermediateOutputPath)\Pharmacist.NuGet.g.cs" />
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Pharmacist.Core
public static System.Threading.Tasks.Task ExtractEventsFromNuGetPackages(System.IO.Stream outputStream, System.Collections.Generic.IReadOnlyCollection<NuGet.LibraryModel.LibraryRange> packages, System.Collections.Generic.IReadOnlyCollection<NuGet.Frameworks.NuGetFramework> frameworks) { }
public static System.Threading.Tasks.Task ExtractEventsFromPlatforms(string outputPath, string prefix, string defaultReferenceAssemblyLocation, System.Collections.Generic.IEnumerable<Pharmacist.Core.AutoPlatform> platforms) { }
public static System.Threading.Tasks.Task WriteHeader(System.IO.Stream outputStream) { }
public static System.Threading.Tasks.Task WriteHeader(System.IO.Stream outputStream, System.Collections.Generic.IReadOnlyCollection<NuGet.LibraryModel.LibraryRange> libraryRanges) { }
}
public class static PlatformHelper
{
Expand Down Expand Up @@ -100,7 +101,9 @@ namespace Pharmacist.Core.NuGet
}
public class static NuGetPackageHelper
{
public const string DefaultNuGetSource = "https://api.nuget.org/v3/index.json";
public static string PackageDirectory { get; }
public static System.Collections.Generic.List<System.Lazy<NuGet.Protocol.Core.Types.INuGetResourceProvider>> Providers { get; }
[return: System.Runtime.CompilerServices.TupleElementNamesAttribute(new string[] {
"folder",
"files"})]
Expand All @@ -109,6 +112,7 @@ namespace Pharmacist.Core.NuGet
"folder",
"files"})]
public static System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyCollection<System.ValueTuple<string, System.Collections.Generic.IReadOnlyCollection<string>>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection<NuGet.Packaging.Core.PackageIdentity> packageIdentities, System.Collections.Generic.IReadOnlyCollection<NuGet.Frameworks.NuGetFramework> frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection<string> packageFolders = null, System.Threading.CancellationToken token = null) { }
public static System.Threading.Tasks.Task<NuGet.Packaging.Core.PackageIdentity> GetBestMatch(NuGet.LibraryModel.LibraryRange identity, NuGet.Protocol.Core.Types.SourceRepository sourceRepository, System.Threading.CancellationToken token) { }
}
}
namespace Pharmacist.Core.ReferenceLocators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Pharmacist.Core
public static System.Threading.Tasks.Task ExtractEventsFromNuGetPackages(System.IO.Stream outputStream, System.Collections.Generic.IReadOnlyCollection<NuGet.LibraryModel.LibraryRange> packages, System.Collections.Generic.IReadOnlyCollection<NuGet.Frameworks.NuGetFramework> frameworks) { }
public static System.Threading.Tasks.Task ExtractEventsFromPlatforms(string outputPath, string prefix, string defaultReferenceAssemblyLocation, System.Collections.Generic.IEnumerable<Pharmacist.Core.AutoPlatform> platforms) { }
public static System.Threading.Tasks.Task WriteHeader(System.IO.Stream outputStream) { }
public static System.Threading.Tasks.Task WriteHeader(System.IO.Stream outputStream, System.Collections.Generic.IReadOnlyCollection<NuGet.LibraryModel.LibraryRange> libraryRanges) { }
}
public class static PlatformHelper
{
Expand Down Expand Up @@ -100,7 +101,9 @@ namespace Pharmacist.Core.NuGet
}
public class static NuGetPackageHelper
{
public const string DefaultNuGetSource = "https://api.nuget.org/v3/index.json";
public static string PackageDirectory { get; }
public static System.Collections.Generic.List<System.Lazy<NuGet.Protocol.Core.Types.INuGetResourceProvider>> Providers { get; }
[return: System.Runtime.CompilerServices.TupleElementNamesAttribute(new string[] {
"folder",
"files"})]
Expand All @@ -109,6 +112,7 @@ namespace Pharmacist.Core.NuGet
"folder",
"files"})]
public static System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyCollection<System.ValueTuple<string, System.Collections.Generic.IReadOnlyCollection<string>>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection<NuGet.Packaging.Core.PackageIdentity> packageIdentities, System.Collections.Generic.IReadOnlyCollection<NuGet.Frameworks.NuGetFramework> frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection<string> packageFolders = null, System.Threading.CancellationToken token = null) { }
public static System.Threading.Tasks.Task<NuGet.Packaging.Core.PackageIdentity> GetBestMatch(NuGet.LibraryModel.LibraryRange identity, NuGet.Protocol.Core.Types.SourceRepository sourceRepository, System.Threading.CancellationToken token) { }
}
}
namespace Pharmacist.Core.ReferenceLocators
Expand Down
Loading

0 comments on commit f4e57a0

Please sign in to comment.