Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making sure explicit source parameter with IVS apis is added for restore #2692

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ await _logger.DoAsync((l, _) =>
var dgSpec = await DependencyGraphRestoreUtility.GetSolutionRestoreSpec(_solutionManager, cacheContext);

// Avoid restoring solutions with zero potential PackageReference projects.
if (DependencyGraphRestoreUtility.IsRestoreRequired(dgSpec))
if (dgSpec.Restore.Count > 0)
{
// NOTE: During restore for build integrated projects,
// We might show the dialog even if there are no packages to restore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static async Task<IReadOnlyList<RestoreSummary>> RestoreAsync(
// Update cache context
cacheContextModifier(sourceCacheContext);

var restoreContext = GetRestoreContext(
var restoreContext = GetRestoreArgs(
context,
providerCache,
sourceCacheContext,
Expand All @@ -76,28 +76,6 @@ public static async Task<IReadOnlyList<RestoreSummary>> RestoreAsync(
return new List<RestoreSummary>();
}

private static async Task PersistDGSpec(DependencyGraphSpec dgSpec)
{
try
{
var filePath = Path.Combine(
NuGetEnvironment.GetFolderPath(NuGetFolderPath.Temp),
"nuget-dg",
"nugetSpec.dg");

// create nuget temp folder if not exists
DirectoryUtility.CreateSharedDirectory(Path.GetDirectoryName(filePath));

// delete existing dg spec file (if exists) then replace it with new file.
await FileUtility.ReplaceWithLock(
(tempFile) => dgSpec.Save(tempFile), filePath);
}
catch (Exception)
{
//ignore any failure if it fails to write or replace dg spec file.
}
}

/// <summary>
/// Restore without writing the lock file
/// </summary>
Expand Down Expand Up @@ -130,7 +108,7 @@ internal static async Task<RestoreResultPair> PreviewRestoreAsync(
cacheContextModifier(sourceCacheContext);

// Settings passed here will be used to populate the restore requests.
var restoreContext = GetRestoreContext(
var restoreContext = GetRestoreArgs(
context,
providerCache,
sourceCacheContext,
Expand All @@ -148,7 +126,7 @@ internal static async Task<RestoreResultPair> PreviewRestoreAsync(
}

/// <summary>
/// Restore a build integrated project(PackageReference and Project.Json only) and update the lock file
/// Restore a build integrated project(PackageReference and Project.Json only) and update the assets file
/// </summary>
public static async Task<RestoreResult> RestoreProjectAsync(
ISolutionManager solutionManager,
Expand Down Expand Up @@ -189,18 +167,6 @@ public static async Task<RestoreResult> RestoreProjectAsync(
return result.Result;
}

public static bool IsRestoreRequired(
DependencyGraphSpec solutionDgSpec)
{
if (solutionDgSpec.Restore.Count < 1)
{
// Nothing to restore
return false;
}
// NO Op will be checked in the restore command
return true;
}

public static async Task<PackageSpec> GetProjectSpec(IDependencyGraphProject project, DependencyGraphCacheContext context)
{
var specs = await project.GetPackageSpecsAsync(context);
Expand Down Expand Up @@ -273,7 +239,7 @@ public static async Task<DependencyGraphSpec> GetSolutionRestoreSpec(
/// <summary>
/// Create a restore context.
/// </summary>
private static RestoreArgs GetRestoreContext(
private static RestoreArgs GetRestoreArgs(
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
SourceCacheContext sourceCacheContext,
Expand All @@ -285,10 +251,6 @@ private static RestoreArgs GetRestoreContext(
bool restoreForceEvaluate)
{
var caching = new CachingSourceProvider(new PackageSourceProvider(context.Settings));
foreach( var source in sources)
{
caching.AddSourceRepository(source);
}

var dgProvider = new DependencyGraphSpecRequestProvider(providerCache, dgFile);

Expand All @@ -301,10 +263,33 @@ private static RestoreArgs GetRestoreContext(
CachingSourceProvider = caching,
ParentId = parentId,
IsRestoreOriginalAction = isRestoreOriginalAction,
RestoreForceEvaluate = restoreForceEvaluate
RestoreForceEvaluate = restoreForceEvaluate,
Sources = sources.Select(source => source.PackageSource.Source).ToList()
};

return restoreContext;
}

private static async Task PersistDGSpec(DependencyGraphSpec dgSpec)
{
try
{
var filePath = Path.Combine(
NuGetEnvironment.GetFolderPath(NuGetFolderPath.Temp),
"nuget-dg",
"nugetSpec.dg");

// create nuget temp folder if not exists
DirectoryUtility.CreateSharedDirectory(Path.GetDirectoryName(filePath));

// delete existing dg spec file (if exists) then replace it with new file.
await FileUtility.ReplaceWithLock(
(tempFile) => dgSpec.Save(tempFile), filePath);
}
catch (Exception)
{
//ignore any failure if it fails to write or replace dg spec file.
}
}
}
}
7 changes: 1 addition & 6 deletions src/NuGet.Core/NuGet.Protocol/CachingSourceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -65,11 +65,6 @@ public SourceRepository CreateRepository(PackageSource source, FeedType type)
return _cachedSources.GetOrAdd(source.Source, new SourceRepository(source, _resourceProviders, type));
}

public void AddSourceRepository(SourceRepository source)
{
_cachedSources.TryAdd(source.PackageSource.Source, source);
}

public IPackageSourceProvider PackageSourceProvider
{
get { return _packageSourceProvider; }
Expand Down
18 changes: 18 additions & 0 deletions test/EndToEnd/tests/ServicesTest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1002,3 +1002,21 @@ function Test-InstallPackageAsyncWithPackageReferenceFormat {
Assert-AreEqual $packageRefs[0].GetMetadataValue("Identity") 'owin'
Assert-AreEqual $packageRefs[0].GetMetadataValue("Version") '1.0.0'
}

function Test-BuildIntegratedProjectInstallPackage {

# Arrange
$project = New-Project BuildIntegratedClassLibrary Project1
$id = 'CustomBuildIntegratedProjectInstallPackage'
$version = '1.0.0'

$solutionFile = Get-SolutionFullName
$solutionDir = Split-Path $solutionFile -Parent
$source = Join-Path $solutionDir "CustomSource"

# Act
[API.Test.InternalAPITestHook]::InstallPackageApiFromSource($source, $id, $version)

# Assert
Assert-ProjectJsonLockFilePackage $project $id $version
}
68 changes: 68 additions & 0 deletions test/TestExtensions/API.Test/InternalAPITestHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell;
using NuGet.Common;
using NuGet.PackageManagement.VisualStudio;
using NuGet.Packaging;
using NuGet.Versioning;
using NuGet.VisualStudio;
using Task = System.Threading.Tasks.Task;

Expand Down Expand Up @@ -56,6 +60,49 @@ public static void InstallPackageApi(string source, string id, string version, b
});
}

public static void InstallPackageApiFromSource(string source, string id, string version)
{
CreatePackage(source, id, version);
InstallPackageApi(source, id, version, prerelease: false);
}

private static void CreatePackage(string path, string id, string version)
{
var builder = new NuGet.Packaging.PackageBuilder()
{
Id = id,
Version = NuGetVersion.Parse(version),
Description = "Description.",
DevelopmentDependency = false
};

builder.Authors.Add("testAuthor");
builder.Files.Add(CreatePackageFile("lib/net45/a.dll"));

Directory.CreateDirectory(path);

using (var stream = File.OpenWrite(Path.Combine(path, $"{id}.{version}.nupkg")))
{
builder.Save(stream);
}
}

private static IPackageFile CreatePackageFile(string name)
{
var file = new InMemoryFile
{
Path = name,
Stream = new MemoryStream()
};

string effectivePath;
var fx = FrameworkNameUtility.ParseFrameworkNameFromFilePath(name, out effectivePath);
file.EffectivePath = effectivePath;
file.TargetFramework = fx;

return file;
}

public static void UninstallPackageApi(string id, bool dependency)
{
ThreadHelper.JoinableTaskFactory.Run(
Expand Down Expand Up @@ -185,5 +232,26 @@ public static bool IsFileExistsInProject(string projectUniqueName, string filePa
return false;
});
}

private class InMemoryFile : IPackageFile
{
private DateTimeOffset _lastWriteTime;

public string EffectivePath { get; set; }

public string Path { get; set; }

public FrameworkName TargetFramework { get; set; }

public MemoryStream Stream { get; set; }

public Stream GetStream()
{
_lastWriteTime = DateTimeOffset.UtcNow;
return Stream;
}

public DateTimeOffset LastWriteTime => _lastWriteTime;
}
}
}