Skip to content

Changed the composition engine to Microsoft.VisualStudio.Composition #10

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Avalonia" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
<PackageReference Include="Microsoft.VisualStudio.Composition" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="ReactiveUI" />
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
Expand Down
28 changes: 14 additions & 14 deletions src/AvalonStudio.Shell.Extensibility/IoC.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
using System;
using System.Collections.Generic;
using System.Composition.Hosting;
using System.Linq;
using Microsoft.VisualStudio.Composition;

namespace AvalonStudio.Extensibility
{
public static class IoC
{
private static CompositionHost s_compositionHost;
private static ExportProvider s_exportProvider;

public static object Get(Type t, string contract = null)
{
if (s_compositionHost != null)
if (s_exportProvider != null)
{
return s_compositionHost.GetExport(t, contract);
return s_exportProvider.AsExportProvider().GetExports(t, null, contract).SingleOrDefault();
}

return default;
}

public static T Get<T>(string contract)
{
if (s_compositionHost != null)
if (s_exportProvider != null)
{
return s_compositionHost.GetExport<T>(contract);
return s_exportProvider.GetExportedValue<T>(contract);
}

return default;
}

public static T Get<T>()
{
if (s_compositionHost != null)
if (s_exportProvider != null)
{
return s_compositionHost.GetExport<T>();
return s_exportProvider.GetExportedValue<T>();
}

return default;
}

public static IEnumerable<T> GetInstances<T>()
{
if (s_compositionHost != null)
if (s_exportProvider != null)
{
return s_compositionHost.GetExports<T>();
return s_exportProvider.GetExportedValues<T>();
}

return Enumerable.Empty<T>();
}

public static IEnumerable<T> GetInstances<T>(string contract)
{
if (s_compositionHost != null)
if (s_exportProvider != null)
{
return s_compositionHost.GetExports<T>(contract);
return s_exportProvider.GetExportedValues<T>(contract);
}

return Enumerable.Empty<T>();
}

public static void Initialise(CompositionHost host)
public static void Initialise(ExportProvider exportProvider)
{
s_compositionHost = host;
s_exportProvider = exportProvider;
}
}
}
1 change: 1 addition & 0 deletions src/AvalonStudio.Shell/AvalonStudio.Shell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Avalonia.ReactiveUI" />
<PackageReference Include="Avalonia.Xaml.Behaviors" />
<PackageReference Include="Avalonia.Xaml.Interactions.Custom" />
<PackageReference Include="Microsoft.VisualStudio.Composition" />
<PackageReference Include="ReactiveUI" />
</ItemGroup>

Expand Down
91 changes: 75 additions & 16 deletions src/AvalonStudio.Shell/CompositionRoot.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,88 @@
using AvalonStudio.Extensibility;
using AvalonStudio.Extensibility.Utils;
using Microsoft.VisualStudio.Composition;
using System.Collections.Generic;
using System.Composition.Convention;
using System.Composition.Hosting;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace AvalonStudio
{
public static class CompositionRoot
{
public static CompositionHost CreateContainer(ExtensionManager extensionManager)
public static async Task<ExportProvider> CreateExportProviderAsync(
ExtensionManager extensionManager,
CancellationToken cancellationToken = default)
{
var conventions = new ConventionBuilder();
conventions.ForTypesDerivedFrom<IExtension>().Export<IExtension>();

// TODO AppDomain here is a custom appdomain from namespace AvalonStudio.Extensibility.Utils. It is able
// to load any assembly in the bin directory (so not really appdomain) we need to get rid of this
// once all our default extensions are published with a manifest and copied to extensions dir.
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var extensionAssemblies = LoadMefComponents(extensionManager);

var configuration = new ContainerConfiguration()
.WithAssemblies(assemblies, conventions)
.WithAssemblies(extensionAssemblies);
return configuration.CreateContainer();
var resolver = Resolver.DefaultInstance;

ComposableCatalog catalog;

var cachedCatalog = new CachedCatalog();

var cacheIsValid = false;
var cachePath = "";

if (cacheIsValid)
{
using (var cacheStream = File.OpenRead(cachePath))
{
catalog = await cachedCatalog.LoadAsync(cacheStream, resolver, cancellationToken);
}
}
else
{
var discovery = new AttributedPartDiscovery(resolver, true);

// TODO AppDomain here is a custom appdomain from namespace AvalonStudio.Extensibility.Utils. It is able
// to load any assembly in the bin directory (so not really appdomain) we need to get rid of this
// once all our default extensions are published with a manifest and copied to extensions dir.
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var extensionAssemblies = LoadMefComponents(extensionManager);

catalog = ComposableCatalog.Create(resolver)
.AddParts(await discovery.CreatePartsAsync(assemblies).ConfigureAwait(false))
.AddParts(await discovery.CreatePartsAsync(extensionAssemblies).ConfigureAwait(false));

// TODO: cache catalog
//using (var cacheStream = File.OpenWrite(cachePath))
//{
// await cachedCatalog.SaveAsync(catalog, cacheStream, cancellationToken);
//}
}

var configuration = CompositionConfiguration.Create(catalog);

// TODO: log errors to file

var compositionErrors = configuration.CompositionErrors;

while (!compositionErrors.IsEmpty)
{
var error = compositionErrors.Peek();

System.Console.WriteLine();
System.Console.WriteLine("Composition Error:");
System.Console.WriteLine();

foreach (var entry in error)
{
System.Console.WriteLine(entry.Message);

foreach (var part in entry.Parts)
{
System.Console.WriteLine($" Part: {part.Definition.Id}");
}
}

compositionErrors = compositionErrors.Pop();
}

var exportProviderFactory = configuration.CreateExportProviderFactory();
var exportProvider = exportProviderFactory.CreateExportProvider();

return exportProvider;
}

private static IEnumerable<Assembly> LoadMefComponents(ExtensionManager extensionManager)
Expand Down
3 changes: 2 additions & 1 deletion src/AvalonStudio.Shell/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static class Shell
Platform.Initialise();

var extensionManager = new ExtensionManager();
var container = CompositionRoot.CreateContainer(extensionManager);
// we shouldn't do this, instead a progress dialog should be shown
var container = CompositionRoot.CreateExportProviderAsync(extensionManager).Result;

IoC.Initialise(container);

Expand Down
1 change: 1 addition & 0 deletions src/Packages.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageReference Update="Avalonia.Xaml.Behaviors" Version="$(AvaloniaBehaviorsVersion)" />
<PackageReference Update="Avalonia.Xaml.Interactions" Version="$(AvaloniaBehaviorsVersion)" />
<PackageReference Update="Avalonia.Xaml.Interactions.Custom" Version="$(AvaloniaBehaviorsVersion)" />
<PackageReference Update="Microsoft.VisualStudio.Composition" Version="$(MicrosoftVisualStudioCompositionVersion)" />
<PackageReference Update="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
<PackageReference Update="ReactiveUI" Version="$(ReactiveUIVersion)" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<AvaloniaBehaviorsVersion>0.6.2-build536</AvaloniaBehaviorsVersion>
<AvaloniaVersion>0.6.2-build5984-beta</AvaloniaVersion>
<MicrosoftVisualStudioCompositionVersion>15.8.98</MicrosoftVisualStudioCompositionVersion>
<NewtonsoftJsonVersion>10.0.3</NewtonsoftJsonVersion>
<ReactiveUIVersion>8.0.1</ReactiveUIVersion>
</PropertyGroup>
Expand Down