Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhili1208 committed Aug 20, 2015
1 parent 1da344e commit 9480217
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 20 deletions.
27 changes: 20 additions & 7 deletions src/NuGet.CommandLine/Commands/RestoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public RestoreCommand()

public override async Task ExecuteCommandAsync()
{
bool restoreResult = true;

_msbuildDirectory = MsBuildUtility.GetMsbuildDirectory(MSBuildVersion);

if (!string.IsNullOrEmpty(PackagesDirectory))
Expand All @@ -62,7 +64,8 @@ public override async Task ExecuteCommandAsync()
var restoreInputs = DetermineRestoreInputs();
if (restoreInputs.PackageReferenceFiles.Count > 0)
{
await PerformNuGetV2RestoreAsync(restoreInputs);
var v2RestoreResult = await PerformNuGetV2RestoreAsync(restoreInputs);
restoreResult &= v2RestoreResult;
}

if (restoreInputs.V3RestoreFiles.Count > 0)
Expand All @@ -78,7 +81,7 @@ public override async Task ExecuteCommandAsync()
restoreInputs,
globalPackagesFolder);

var v3RestoreTasks = new List<Task>();
var v3RestoreTasks = new List<Task<bool>>();
foreach (var file in restoreInputs.V3RestoreFiles)
{
if (DisableParallelProcessing)
Expand All @@ -93,9 +96,15 @@ public override async Task ExecuteCommandAsync()

if (v3RestoreTasks.Count > 0)
{
await Task.WhenAll(v3RestoreTasks);
var results = await Task.WhenAll(v3RestoreTasks);
restoreResult &= results.All(r => r);
}
}

if (!restoreResult)
{
throw new CommandLineException();
}
}

private static string GetEffectiveGlobalPackagesFolder(
Expand Down Expand Up @@ -136,7 +145,7 @@ private static string GetEffectiveGlobalPackagesFolder(
throw new CommandLineException(message);
}

private async Task PerformNuGetV3RestoreAsync(string packagesDir, string projectPath)
private async Task<bool> PerformNuGetV3RestoreAsync(string packagesDir, string projectPath)
{
var projectFileName = Path.GetFileName(projectPath);
PackageSpec project;
Expand Down Expand Up @@ -242,6 +251,8 @@ private async Task PerformNuGetV3RestoreAsync(string packagesDir, string project
var command = new Commands.RestoreCommand(Console, request);
var result = await command.ExecuteAsync();
result.Commit(Console);

return result.Success;
}

private void ReadSettings(PackageRestoreInputs packageRestoreInputs)
Expand Down Expand Up @@ -273,7 +284,7 @@ private void ReadSettings(PackageRestoreInputs packageRestoreInputs)
}
}

private async Task PerformNuGetV2RestoreAsync(PackageRestoreInputs packageRestoreInputs)
private async Task<bool> PerformNuGetV2RestoreAsync(PackageRestoreInputs packageRestoreInputs)
{
ReadSettings(packageRestoreInputs);
var packagesFolderPath = GetPackagesFolder(packageRestoreInputs);
Expand Down Expand Up @@ -326,7 +337,7 @@ private async Task PerformNuGetV2RestoreAsync(PackageRestoreInputs packageRestor
"packages.config");

Console.LogInformation(message);
return;
return true;
}

var packageRestoreData = missingPackageReferences.Select(reference =>
Expand Down Expand Up @@ -356,14 +367,16 @@ private async Task PerformNuGetV2RestoreAsync(PackageRestoreInputs packageRestor
: PackageManagementConstants.DefaultMaxDegreeOfParallelism);

CheckRequireConsent();
await PackageRestoreManager.RestoreMissingPackagesAsync(
var result = await PackageRestoreManager.RestoreMissingPackagesAsync(
packageRestoreContext,
new ConsoleProjectContext(Console));

foreach (var item in bag)
{
Console.WriteError(item.Exception.Message);
}

return result.Restored;
}

private void CheckRequireConsent()
Expand Down
30 changes: 17 additions & 13 deletions src/PackageManagement/IDE/PackageRestoreManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ public static async Task<PackageRestoreResult> RestoreMissingPackagesAsync(Packa
var missingPackages = packageRestoreContext.Packages.Where(p => p.IsMissing).ToList();
if (!missingPackages.Any())
{
return new PackageRestoreResult(false);
return new PackageRestoreResult(true);
}

bool packageRestoreResult = true;
// It is possible that the dictionary passed in may not have used the PackageReferenceComparer.
// So, just to be sure, create a hashset with the keys from the dictionary using the PackageReferenceComparer
// Now, we are guaranteed to not restore the same package more than once
Expand All @@ -309,12 +310,19 @@ public static async Task<PackageRestoreResult> RestoreMissingPackagesAsync(Packa

packageRestoreContext.Token.ThrowIfCancellationRequested();

await ThrottledPackageRestoreAsync(hashSetOfMissingPackageReferences, packageRestoreContext, nuGetProjectContext);
var restoreResults = await ThrottledPackageRestoreAsync(hashSetOfMissingPackageReferences, packageRestoreContext, nuGetProjectContext);

packageRestoreContext.Token.ThrowIfCancellationRequested();

await ThrottledCopySatelliteFilesAsync(hashSetOfMissingPackageReferences, packageRestoreContext, nuGetProjectContext);

packageRestoreResult &= restoreResults.All(r => r);

if (packageRestoreResult)
{
packageRestoreContext.SetRestored();
}

return new PackageRestoreResult(packageRestoreContext.WasRestored);
}

Expand All @@ -328,12 +336,12 @@ public static async Task<PackageRestoreResult> RestoreMissingPackagesAsync(Packa
/// that dequeue from the ConcurrentQueue and perform package restore. So, this method should pre-populate the
/// queue and must not enqueued to by other methods
/// </summary>
private static Task ThrottledPackageRestoreAsync(HashSet<Packaging.PackageReference> packageReferences,
private static Task<bool[]> ThrottledPackageRestoreAsync(HashSet<Packaging.PackageReference> packageReferences,
PackageRestoreContext packageRestoreContext,
INuGetProjectContext nuGetProjectContext)
{
var packageReferencesQueue = new ConcurrentQueue<Packaging.PackageReference>(packageReferences);
var tasks = new List<Task>();
var tasks = new List<Task<bool>>();
for (var i = 0; i < Math.Min(packageRestoreContext.MaxNumberOfParallelTasks, packageReferences.Count); i++)
{
tasks.Add(Task.Run(() => PackageRestoreRunnerAsync(packageReferencesQueue, packageRestoreContext, nuGetProjectContext)));
Expand All @@ -347,19 +355,19 @@ private static Task ThrottledPackageRestoreAsync(HashSet<Packaging.PackageRefere
/// performs package restore
/// Note that this method should only Dequeue from the concurrent queue and not Enqueue
/// </summary>
private static async Task PackageRestoreRunnerAsync(ConcurrentQueue<Packaging.PackageReference> packageReferencesQueue,
private static async Task<bool> PackageRestoreRunnerAsync(ConcurrentQueue<Packaging.PackageReference> packageReferencesQueue,
PackageRestoreContext packageRestoreContext,
INuGetProjectContext nuGetProjectContext)
{
Packaging.PackageReference currentPackageReference = null;
var restoreResult = true;
while (packageReferencesQueue.TryDequeue(out currentPackageReference))
{
var result = await RestorePackageAsync(currentPackageReference, packageRestoreContext, nuGetProjectContext);
if (result)
{
packageRestoreContext.SetRestored();
}
restoreResult &= result;
}

return restoreResult;
}

/// <summary>
Expand Down Expand Up @@ -445,10 +453,6 @@ private static async Task CopySatelliteFilesRunnerAsync(ConcurrentQueue<Packagin
while (packageReferencesQueue.TryDequeue(out currentPackageReference))
{
var result = await packageRestoreContext.PackageManager.CopySatelliteFilesAsync(currentPackageReference.PackageIdentity, nuGetProjectContext, packageRestoreContext.Token);
if (result)
{
packageRestoreContext.SetRestored();
}
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/NuGet.CommandLine.Test/NuGetRestoreCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,5 +1382,48 @@ public void RestoreCommand_InvalidSolutionFile()
TestFilesystemUtility.DeleteRandomTestFolders(randomTestFolder);
}
}

// return code should be 1 when restore failed
[Fact]
public void RestoreCommand_FromPackagesConfigFileFailed()
{
// Arrange
var tempPath = Path.GetTempPath();
var workingPath = Path.Combine(tempPath, Guid.NewGuid().ToString());
var repositoryPath = Path.Combine(workingPath, Guid.NewGuid().ToString());
var currentDirectory = Directory.GetCurrentDirectory();
var nugetexe = Util.GetNuGetExePath();

try
{
Util.CreateDirectory(workingPath);
Util.CreateDirectory(repositoryPath);
Util.CreateFile(workingPath, "packages.config",
@"<packages>
<package id=""packageA"" version=""1.1.0"" targetFramework=""net45"" />
<package id=""packageB"" version=""2.2.0"" targetFramework=""net45"" />
</packages>");

string[] args = new string[] { "restore", "-PackagesDirectory", "outputDir", "-Source", repositoryPath };

// Act
var path = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", null);
var r = CommandRunner.Run(
nugetexe,
workingPath,
string.Join(" ", args),
waitForExit: true);
Environment.SetEnvironmentVariable("PATH", path);

// Assert
Assert.Equal(1, r.Item1);
}
finally
{
Directory.SetCurrentDirectory(currentDirectory);
Util.DeleteDirectory(workingPath);
}
}
}
}

0 comments on commit 9480217

Please sign in to comment.