1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.WebMatrix.Core;
using Microsoft.WebMatrix.Extensibility;
using Microsoft.WebMatrix.Utility;
using NuGet;
namespace NuGet.WebMatrix
{
/// <summary>
/// Interface that represents a custom NuGet-style gallery.
/// </summary>
public interface INuGetExtensionGallery
{
/// <summary>
/// Shows a custom NuGet-style gallery.
/// </summary>
/// <param name="gallery">A gallery descriptor for the gallery to be shown</param>
/// <param name="installRoot">Root path for installing packages.</param>
/// <returns>Task that shows the gallery.</returns>
Task<bool?> ShowGallery(
INuGetGalleryDescriptor gallery,
string installRoot);
}
/// <summary>
/// MEF-usable class that creates a NuGet-style gallery.
/// </summary>
[Export(typeof(INuGetExtensionGallery))]
internal class NuGetGallery : INuGetExtensionGallery
{
internal static TaskScheduler GetCurrentTaskScheduler()
{
TaskScheduler scheduler = null;
try
{
// the scheduler should be the current Sync Context
scheduler = TaskScheduler.FromCurrentSynchronizationContext();
}
catch (InvalidOperationException)
{
scheduler = TaskScheduler.Default;
}
return scheduler;
}
[Import]
private IWebMatrixHost Host
{
get;
set;
}
/// <summary>
/// Shows a custom NuGet-style gallery.
/// </summary>
/// <param name="galleryId">A gallery descriptor for the gallery</param>
/// <param name="installRoot">Root path for installing packages.</param>
/// <returns>Task that shows the gallery.</returns>
public Task<bool?> ShowGallery(
INuGetGalleryDescriptor descriptor,
string installRoot)
{
var feedSource = new FeedSource(descriptor.FeedUri, descriptor.FeedName)
{
IsBuiltIn = true,
FilterTag = descriptor.FilterTag,
};
var preferences = this.Host.GetExtensionSpecificPreferences(descriptor.PreferencesStore);
IFeedSourceStore feedSourceStore;
// we special case the NuGet gallery to use the system-wide NuGet store
feedSourceStore = new NuGetFeedSourceStore(preferences);
// Customize the custom gallery
var packageSourcesViewModel = new PackageSourcesViewModel(new PackageSourcesModel(feedSource, feedSourceStore));
var viewModel = new NuGetViewModel(
descriptor,
this.Host,
packageSourcesViewModel,
(sourceUrl, siteRoot) => new NuGetPackageManager(
sourceUrl,
siteRoot,
this.Host),
installRoot,
GetCurrentTaskScheduler());
viewModel.ShouldShowPrereleaseFilter = true;
return Task.Factory.StartNew<bool?>(() =>
{
var view = new NuGetView(descriptor.DialogTitle);
// show the gallery view
view.DataContext = viewModel;
return this.Host.ShowDialog(null, view);
},
CancellationToken.None,
TaskCreationOptions.None,
GetCurrentTaskScheduler());
}
}
}
|