This commit is contained in:
Mads Kristensen 2017-04-03 20:38:18 -07:00
Родитель a7e290e272
Коммит c2fb383661
12 изменённых файлов: 52 добавлений и 123 удалений

Просмотреть файл

@ -1,24 +0,0 @@
// 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.Threading;
using System.Threading.Tasks;
namespace LibraryInstaller.Contracts
{
/// <summary>
/// Represents what is needed for an individual libary to be displayed.
/// </summary>
public interface ILibraryDisplayInfo
{
/// <summary>
/// Gets the unique identifier of the library.
/// </summary>
string LibraryId { get; }
/// <summary>
/// The version of the the specific <see cref="ILibrary"/>.
/// </summary>
string Version { get; }
}
}

Просмотреть файл

@ -23,10 +23,10 @@ namespace LibraryInstaller.Contracts
string Description { get; }
/// <summary>
/// Gets the <see cref="ILibraryDisplayInfo"/> objects for the different versions of the library.
/// Gets a list of IDs for the different versions of the library.
/// </summary>
/// <param name="cancellationToken">A token that allows cancellation of the operation.</param>
/// <returns>A list of <see cref="ILibraryDisplayInfo"/> used to display library information to the user.</returns>
Task<IReadOnlyList<ILibraryDisplayInfo>> GetDisplayInfosAsync(CancellationToken cancellationToken);
/// <returns>A list of library IDs used to display library information to the user.</returns>
Task<IEnumerable<string>> GetLibraryIdsAsync(CancellationToken cancellationToken);
}
}

Просмотреть файл

@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
@ -20,7 +21,7 @@ namespace LibraryInstaller.Vsix.Models
private string _description;
private string _homepage;
private ImageSource _icon;
private Lazy<Task<ILibraryDisplayInfo>> _infoTask;
private Lazy<Task<string>> _infoTask;
private static ConcurrentDictionary<string, PackageSearchItem> _cache = new ConcurrentDictionary<string, PackageSearchItem>();
private bool _special;
private static PackageSearchItem _missing;
@ -47,12 +48,12 @@ namespace LibraryInstaller.Vsix.Models
_dispatcher = Dispatcher.CurrentDispatcher;
CollapsedItemText = name;
Icon = WpfUtil.GetIconForImageMoniker(KnownMonikers.Package, 24, 24);
_infoTask = new Lazy<Task<ILibraryDisplayInfo>>(async () =>
_infoTask = new Lazy<Task<string>>(async () =>
{
ILibraryCatalog catalog = provider.GetCatalog();
IReadOnlyList<ILibraryGroup> packageGroups = await catalog.SearchAsync(name, 1, CancellationToken.None).ConfigureAwait(false);
IReadOnlyList<ILibraryDisplayInfo> displayInfos = await packageGroups[0].GetDisplayInfosAsync(CancellationToken.None).ConfigureAwait(false);
return displayInfos[0];
IEnumerable<string> displayInfos = await packageGroups[0].GetLibraryIdsAsync(CancellationToken.None).ConfigureAwait(false);
return displayInfos.FirstOrDefault();
});
}

Просмотреть файл

@ -73,14 +73,14 @@ namespace LibraryInstaller.Providers.Cdnjs
if (group != null)
{
IReadOnlyList<ILibraryDisplayInfo> infos = await GetDisplayInfosAsync(group.DisplayName, CancellationToken.None);
IEnumerable<Asset> assets = await GetAssetsAsync(name, CancellationToken.None);
foreach (ILibraryDisplayInfo info in infos)
foreach (string version in assets.Select(a => a.Version))
{
var completion = new CompletionItem
{
DisplayText = info.Version,
InsertionText = $"{name}@{info.Version}"
DisplayText = version,
InsertionText = $"{name}@{version}"
};
completions.Add(completion);
@ -126,13 +126,13 @@ namespace LibraryInstaller.Providers.Cdnjs
string name = args[0];
string version = args[1];
IReadOnlyList<ILibraryDisplayInfo> displayInfos = await GetDisplayInfosAsync(name, cancellationToken).ConfigureAwait(false);
var info = displayInfos.First(d => d.Version == version) as CdnjsLibraryDisplayInfo;
IEnumerable<Asset> assets = await GetAssetsAsync(name, cancellationToken);
Asset asset = assets.FirstOrDefault(a => a.Version == version);
return new CdnjsLibrary
{
Version = info.Version,
Files = info.Asset.Files.ToDictionary(k => k, b => b == info.Asset.DefaultFile),
Version = asset.Version,
Files = asset.Files.ToDictionary(k => k, b => b == asset.DefaultFile),
Name = name,
ProviderId = _providerId
};
@ -188,10 +188,17 @@ namespace LibraryInstaller.Providers.Cdnjs
}
}
private async Task<IReadOnlyList<ILibraryDisplayInfo>> GetDisplayInfosAsync(string groupName, CancellationToken cancellationToken)
private async Task<IEnumerable<string>> GetDisplayInfosAsync(string groupName, CancellationToken cancellationToken)
{
IEnumerable<Asset> assets = await GetAssetsAsync(groupName, cancellationToken);
return assets?.Select(a => $"{groupName}@{a.Version}");
}
private async Task<IEnumerable<Asset>> GetAssetsAsync(string groupName, CancellationToken cancellationToken)
{
string metaFile = Path.Combine(_providerStorePath, groupName, "metadata.json");
var list = new List<ILibraryDisplayInfo>();
var list = new List<Asset>();
try
{
@ -206,9 +213,7 @@ namespace LibraryInstaller.Providers.Cdnjs
foreach (Asset asset in assets)
{
asset.DefaultFile = root["filename"]?.Value<string>();
var info = new CdnjsLibraryDisplayInfo(asset, groupName);
list.Add(info);
list.Add(asset);
}
}
}

Просмотреть файл

@ -1,23 +0,0 @@
// 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 LibraryInstaller.Contracts;
namespace LibraryInstaller.Providers.Cdnjs
{
internal class CdnjsLibraryDisplayInfo : ILibraryDisplayInfo
{
public CdnjsLibraryDisplayInfo(Asset asset, string libraryGroupName)
{
Asset = asset;
Version = asset.Version;
LibraryId = $"{libraryGroupName}@{asset.Version}";
}
public string LibraryId { get; }
public string Version { get; }
public Asset Asset { get; }
}
}

Просмотреть файл

@ -21,12 +21,12 @@ namespace LibraryInstaller.Providers.Cdnjs
[JsonProperty("version")]
public string Version { get; set; }
public Task<IReadOnlyList<ILibraryDisplayInfo>> GetDisplayInfosAsync(CancellationToken cancellationToken)
public Task<IEnumerable<string>> GetLibraryIdsAsync(CancellationToken cancellationToken)
{
return DisplayInfosTask?.Invoke(cancellationToken) ?? Task.FromResult<IReadOnlyList<ILibraryDisplayInfo>>(new ILibraryDisplayInfo[0]);
return DisplayInfosTask?.Invoke(cancellationToken) ?? Task.FromResult<IEnumerable<string>>(new string[0]);
}
public Func<CancellationToken, Task<IReadOnlyList<ILibraryDisplayInfo>>> DisplayInfosTask { get; set; }
public Func<CancellationToken, Task<IEnumerable<string>>> DisplayInfosTask { get; set; }
public override string ToString()
{

Просмотреть файл

@ -24,22 +24,16 @@ namespace LibraryInstaller.Providers.FileSystem
return Task.FromResult(default(CompletionSet));
}
public async Task<ILibrary> GetLibraryAsync(string libraryId, CancellationToken cancellationToken)
public Task<ILibrary> GetLibraryAsync(string libraryId, CancellationToken cancellationToken)
{
var group = new FileSystemLibraryGroup(libraryId);
IReadOnlyList<ILibraryDisplayInfo> info = await group.GetDisplayInfosAsync(cancellationToken).ConfigureAwait(false);
if (info.Count > 0 && info[0] != null)
var library = new FileSystemLibrary
{
return new FileSystemLibrary
{
Name = libraryId,
ProviderId = _providerId,
Files = GetFiles(libraryId)
};
}
Name = libraryId,
ProviderId = _providerId,
Files = GetFiles(libraryId)
};
return null;
return Task.FromResult<ILibrary>(library);
}
private IReadOnlyDictionary<string, bool> GetFiles(string libraryId)

Просмотреть файл

@ -1,19 +0,0 @@
// 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 LibraryInstaller.Contracts;
namespace LibraryInstaller.Providers.FileSystem
{
internal class FileSystemDisplayInfo : ILibraryDisplayInfo
{
public FileSystemDisplayInfo(string libraryId)
{
LibraryId = libraryId;
}
public string LibraryId { get; }
public string Version => string.Empty;
}
}

Просмотреть файл

@ -19,14 +19,11 @@ namespace LibraryInstaller.Providers.FileSystem
public string Description => string.Empty;
public Task<IReadOnlyList<ILibraryDisplayInfo>> GetDisplayInfosAsync(CancellationToken cancellationToken)
public Task<IEnumerable<string>> GetLibraryIdsAsync(CancellationToken cancellationToken)
{
var infos = new List<ILibraryDisplayInfo>
{
new FileSystemDisplayInfo(DisplayName)
};
string[] ids = { DisplayName };
return Task.FromResult<IReadOnlyList<ILibraryDisplayInfo>>(infos);
return Task.FromResult<IEnumerable<string>>(ids);
}
public override string ToString()

Просмотреть файл

@ -55,10 +55,10 @@ namespace LibraryInstaller.Test.Providers.Cdnjs
IReadOnlyList<ILibraryGroup> absolute = await catalog.SearchAsync(searchTerm, 1, token);
Assert.AreEqual(1, absolute.Count);
IReadOnlyList<ILibraryDisplayInfo> info = await absolute[0].GetDisplayInfosAsync(token);
Assert.IsTrue(info.Count > 0);
IEnumerable<string> libraryId = await absolute[0].GetLibraryIdsAsync(token);
Assert.IsTrue(libraryId.Count() > 0);
ILibrary library = await catalog.GetLibraryAsync(info[0].LibraryId, token);
ILibrary library = await catalog.GetLibraryAsync(libraryId.First(), token);
Assert.IsTrue(library.Files.Count > 0);
Assert.AreEqual(1, library.Files.Count(f => f.Value));
Assert.IsNotNull(library.Name);

Просмотреть файл

@ -55,15 +55,13 @@ namespace LibraryInstaller.Test.Providers.Cdnjs
Assert.IsNotNull(group.Description);
// Get all libraries in group to display version list
IReadOnlyList<ILibraryDisplayInfo> displayInfos = await group.GetDisplayInfosAsync(CancellationToken.None);
Assert.IsTrue(displayInfos.Count >= 67);
Assert.AreEqual("1.2.3", displayInfos.ElementAt(displayInfos.Count - 1).Version, "Library version mismatch");
IEnumerable<string> libraryIds = await group.GetLibraryIdsAsync(CancellationToken.None);
Assert.IsTrue(libraryIds.Count() >= 67);
Assert.AreEqual("jquery@1.2.3", libraryIds.Last(), "Library version mismatch");
// Get the library to install
ILibraryDisplayInfo displayInfo = displayInfos.FirstOrDefault();
ILibrary library = await catalog.GetLibraryAsync(displayInfo.LibraryId, CancellationToken.None);
ILibrary library = await catalog.GetLibraryAsync(libraryIds.First(), CancellationToken.None);
Assert.AreEqual(group.DisplayName, library.Name);
Assert.AreEqual(displayInfo.Version, library.Version);
var desiredState = new LibraryInstallationState
{

Просмотреть файл

@ -62,10 +62,10 @@ namespace LibraryInstaller.Test.Providers.FileSystem
IReadOnlyList<ILibraryGroup> absolute = await _catalog.SearchAsync(folder, 3, token);
Assert.AreEqual(1, absolute.Count);
IReadOnlyList<ILibraryDisplayInfo> info = await absolute.First().GetDisplayInfosAsync(token);
Assert.AreEqual(1, info.Count);
IEnumerable<string> info = await absolute.First().GetLibraryIdsAsync(token);
Assert.AreEqual(1, info.Count());
ILibrary library = await _catalog.GetLibraryAsync(info.First().LibraryId, token);
ILibrary library = await _catalog.GetLibraryAsync(info.First(), token);
Assert.AreEqual(3, library.Files.Count);
Directory.Delete(folder, true);
@ -77,10 +77,10 @@ namespace LibraryInstaller.Test.Providers.FileSystem
IReadOnlyList<ILibraryGroup> absolute = await catalog.SearchAsync(file, 1, token);
Assert.AreEqual(1, absolute.Count);
IReadOnlyList<ILibraryDisplayInfo> info = await absolute[0].GetDisplayInfosAsync(token);
Assert.AreEqual(1, info.Count);
IEnumerable<string> info = await absolute[0].GetLibraryIdsAsync(token);
Assert.AreEqual(1, info.Count());
ILibrary library = await catalog.GetLibraryAsync(info[0].LibraryId, token);
ILibrary library = await catalog.GetLibraryAsync(info.First(), token);
Assert.AreEqual(1, library.Files.Count);
Assert.AreEqual(1, library.Files.Count(f => f.Value));
Assert.AreEqual(file, library.Name);