Skip to content
This repository was archived by the owner on Dec 18, 2017. It is now read-only.

"kpm restore" ignores nonexistent local package feeds and gives warnings #1032

Merged
merged 1 commit into from
Jan 12, 2015
Merged
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 @@ -55,15 +55,14 @@ public async Task<bool> ExecuteCommand()
var packageFeeds = new List<IPackageFeed>();
foreach (var source in effectiveSources)
{
if (new Uri(source.Source).IsFile)
var feed = PackageSourceUtils.CreatePackageFeed(
source,
_restoreCommand.NoCache,
_restoreCommand.IgnoreFailedSources,
Reports);
if (feed != null)
{
packageFeeds.Add(PackageFolderFactory.CreatePackageFolderFromPath(source.Source, Reports.Quiet));
}
else
{
packageFeeds.Add(new NuGetv2Feed(
source.Source, source.UserName, source.Password, _restoreCommand.NoCache, Reports,
_restoreCommand.IgnoreFailedSources));
packageFeeds.Add(feed);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Framework.PackageManager.Restore.NuGet
{
public interface IPackageFeed
{
string Source { get; }
Task<IEnumerable<PackageInfo>> FindPackagesByIdAsync(string id);
Task<Stream> OpenNupkgStreamAsync(PackageInfo package);
Task<Stream> OpenNuspecStreamAsync(PackageInfo package);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using NuGet;
Expand All @@ -12,36 +11,68 @@ namespace Microsoft.Framework.PackageManager.Restore.NuGet
{
public class KpmPackageFolder : IPackageFeed
{
private readonly IReport _report;
private bool _ignored;
private readonly bool _ignoreFailure;
private readonly Reports _reports;
private readonly PackageRepository _repository;
private readonly IFileSystem _fileSystem;
private readonly IPackagePathResolver _pathResolver;

public string Source { get; }

public KpmPackageFolder(
string physicalPath,
IReport report)
bool ignoreFailure,
Reports reports)
{
// We need to help "kpm restore" to ensure case-sensitivity here
// Turn on the flag to get package ids in accurate casing
_repository = new PackageRepository(physicalPath, checkPackageIdCase: true);
_fileSystem = new PhysicalFileSystem(physicalPath);
_pathResolver = new DefaultPackagePathResolver(_fileSystem);
_report = report;
_reports = reports;
Source = physicalPath;
_ignored = false;
_ignoreFailure = ignoreFailure;
}

public Task<IEnumerable<PackageInfo>> FindPackagesByIdAsync(string id)
{
return Task.FromResult(_repository.FindPackagesById(id).Select(p => new PackageInfo
if (_ignored)
{
return Task.FromResult(Enumerable.Empty<PackageInfo>());
}

if (Directory.Exists(Source))
{
Id = p.Id,
Version = p.Version
}));
return Task.FromResult(_repository.FindPackagesById(id).Select(p => new PackageInfo
{
Id = p.Id,
Version = p.Version
}));
}

var exception = new FileNotFoundException(
message: string.Format("The local package source {0} doesn't exist", Source.Bold()),
fileName: Source);

if (_ignoreFailure)
{
_reports.Information.WriteLine(string.Format("Warning: FindPackagesById: {1}\r\n {0}",
exception.Message, id.Yellow().Bold()));
_ignored = true;
return Task.FromResult(Enumerable.Empty<PackageInfo>());
}

_reports.Error.WriteLine(string.Format("Error: FindPackagesById: {1}\r\n {0}",
exception.Message, id.Red().Bold()));
throw exception;
}

public Task<Stream> OpenNuspecStreamAsync(PackageInfo package)
{
var nuspecPath = _pathResolver.GetManifestFilePath(package.Id, package.Version);
_report.WriteLine(string.Format(" OPEN {0}", _fileSystem.GetFullPath(nuspecPath)));
_reports.Quiet.WriteLine(string.Format(" OPEN {0}", _fileSystem.GetFullPath(nuspecPath)));
return Task.FromResult<Stream>(File.OpenRead(nuspecPath));
}

Expand All @@ -51,7 +82,7 @@ public Task<Stream> OpenNupkgStreamAsync(PackageInfo package)
var unzippedPackage = new UnzippedPackage(_fileSystem, nuspecPath);

var nupkgPath = _pathResolver.GetPackageFilePath(package.Id, package.Version);
_report.WriteLine(string.Format(" OPEN {0}", _fileSystem.GetFullPath(nupkgPath)));
_reports.Quiet.WriteLine(string.Format(" OPEN {0}", _fileSystem.GetFullPath(nupkgPath)));

return Task.FromResult(unzippedPackage.GetStream());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ namespace Microsoft.Framework.PackageManager.Restore.NuGet
{
public class NuGetPackageFolder : IPackageFeed
{
private readonly IReport _report;
private readonly Reports _reports;
private readonly LocalPackageRepository _repository;

public string Source { get; }

public NuGetPackageFolder(
string physicalPath,
IReport report)
Reports reports)
{
_repository = new LocalPackageRepository(physicalPath, report);
_report = report;
_repository = new LocalPackageRepository(physicalPath, reports.Quiet);
_reports = reports;
Source = physicalPath;
}

public Task<IEnumerable<PackageInfo>> FindPackagesByIdAsync(string id)
Expand All @@ -33,7 +36,7 @@ public Task<IEnumerable<PackageInfo>> FindPackagesByIdAsync(string id)

public async Task<Stream> OpenNuspecStreamAsync(PackageInfo package)
{
return await PackageUtilities.OpenNuspecStreamFromNupkgAsync(package, OpenNupkgStreamAsync, _report);
return await PackageUtilities.OpenNuspecStreamFromNupkgAsync(package, OpenNupkgStreamAsync, _reports.Quiet);
}

public Task<Stream> OpenNupkgStreamAsync(PackageInfo package)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class NuGetv2Feed : IPackageFeed
private readonly Dictionary<string, Task<IEnumerable<PackageInfo>>> _packageVersionsCache = new Dictionary<string, Task<IEnumerable<PackageInfo>>>();
private readonly Dictionary<string, Task<NupkgEntry>> _nupkgCache = new Dictionary<string, Task<NupkgEntry>>();

public string Source { get; }

public NuGetv2Feed(
string baseUri,
string userName,
Expand All @@ -55,6 +57,7 @@ public NuGetv2Feed(
_cacheAgeLimitList = TimeSpan.FromMinutes(30);
_cacheAgeLimitNupkg = TimeSpan.FromHours(24);
}
Source = baseUri;
}

public Task<IEnumerable<PackageInfo>> FindPackagesByIdAsync(string id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ namespace Microsoft.Framework.PackageManager
{
internal static class PackageFolderFactory
{
public static IPackageFeed CreatePackageFolderFromPath(string path, IReport report)
public static IPackageFeed CreatePackageFolderFromPath(string path, bool ignoreFailedSources, Reports reports)
{
Func<string, bool> containsNupkg = dir => Directory.EnumerateFiles(dir, "*" + Constants.PackageExtension)
Func<string, bool> containsNupkg = dir => Directory.Exists(dir) &&
Directory.EnumerateFiles(dir, "*" + Constants.PackageExtension)
.Where(x => !Path.GetFileNameWithoutExtension(x).EndsWith(".symbols"))
.Any();

if (containsNupkg(path) || Directory.EnumerateDirectories(path).Any(x => containsNupkg(x)))
if (Directory.Exists(path) &&
(containsNupkg(path) || Directory.EnumerateDirectories(path).Any(x => containsNupkg(x))))
{
return new NuGetPackageFolder(path, report);
return new NuGetPackageFolder(path, reports);
}
else
{
return new KpmPackageFolder(path, report);
return new KpmPackageFolder(path, ignoreFailedSources, reports);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -16,10 +17,10 @@ public class RemoteWalkProvider : IWalkProvider
{
private readonly IPackageFeed _source;

public RemoteWalkProvider(IPackageFeed source, bool isHttp)
public RemoteWalkProvider(IPackageFeed source)
{
_source = source;
IsHttp = isHttp;
IsHttp = IsHttpSource(source);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving initialization of IsHttp here can simplify extraction of shared code (PackageSourceUtils.CreatePackageFeed()). Moreover, the source argument already contains IsHttp information, so passing isHttp introduces duplicated info.

}

public bool IsHttp { get; private set; }
Expand Down Expand Up @@ -87,6 +88,12 @@ public async Task CopyToAsync(WalkProviderMatch match, Stream stream)
await nupkgStream.CopyToAsync(stream);
}
}

private static bool IsHttpSource(IPackageFeed source)
{
return source.Source.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
source.Source.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
}
}
}

23 changes: 3 additions & 20 deletions src/Microsoft.Framework.PackageManager/Restore/RestoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,27 +460,10 @@ private void AddRemoteProvidersFromSources(List<IWalkProvider> remoteProviders,
{
foreach (var source in effectiveSources)
{
if (new Uri(source.Source).IsFile)
var feed = PackageSourceUtils.CreatePackageFeed(source, NoCache, IgnoreFailedSources, Reports);
if (feed != null)
{
remoteProviders.Add(
new RemoteWalkProvider(
PackageFolderFactory.CreatePackageFolderFromPath(
source.Source,
Reports.Quiet),
isHttp: false));
}
else
{
remoteProviders.Add(
new RemoteWalkProvider(
new NuGetv2Feed(
source.Source,
source.UserName,
source.Password,
NoCache,
Reports,
ignoreFailure: IgnoreFailedSources),
isHttp: true));
remoteProviders.Add(new RemoteWalkProvider(feed));
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.Framework.PackageManager/Utils/PackageSourceUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using NuGet;
using Microsoft.Framework.PackageManager.Restore.NuGet;

namespace Microsoft.Framework.PackageManager
{
Expand All @@ -25,6 +26,25 @@ public static List<PackageSource> GetEffectivePackageSources(IPackageSourceProvi
return enabledSources.Concat(addedSources).Distinct().ToList();
}

public static IPackageFeed CreatePackageFeed(PackageSource source, bool noCache, bool ignoreFailedSources,
Reports reports)
{
if (new Uri(source.Source).IsFile)
{
return PackageFolderFactory.CreatePackageFolderFromPath(source.Source, ignoreFailedSources, reports);
}
else
{
return new NuGetv2Feed(
source.Source,
source.UserName,
source.Password,
noCache,
reports,
ignoreFailedSources);
}
}

private static bool CorrectName(string value, PackageSource source)
{
return source.Name.Equals(value, StringComparison.CurrentCultureIgnoreCase) ||
Expand Down