Skip to content
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

Support plugins configuration in omnisharp.json #1615

Merged
merged 16 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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
18 changes: 15 additions & 3 deletions src/OmniSharp.Abstractions/Services/IAssemblyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -54,11 +55,22 @@ public static Assembly LoadByAssemblyNameOrPath(
}
}

public static IEnumerable<Assembly> LoadByAssemblyNameOrPath(this IAssemblyLoader loader, IEnumerable<string> assemblyNames)
public static IEnumerable<Assembly> LoadByAssemblyNameOrPath(this IAssemblyLoader loader, ILogger logger, IEnumerable<string> assemblyNames)
dmgonch marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var assemblyName in assemblyNames)
{
yield return loader.LoadByAssemblyNameOrPath(assemblyName);
Assembly assembly;
try
{
assembly = loader.LoadByAssemblyNameOrPath(assemblyName);
}
catch (Exception ex)
{
logger.LogError(ex, $"Failed to load assembly by name or path: {assemblyName}");
continue;
}

yield return assembly;
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/OmniSharp.Host/CommandLineApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using OmniSharp.Options;
using OmniSharp.Plugins;
using OmniSharp.Services;

Expand All @@ -15,9 +16,11 @@ public static OmniSharpEnvironment CreateEnvironment(this CommandLineApplication
application.OtherArgs.ToArray<string>());
}

public static PluginAssemblies CreatePluginAssemblies(this CommandLineApplication application)
public static PluginAssemblies CreatePluginAssemblies(this CommandLineApplication application,
OmniSharpOptions options,
OmniSharpEnvironment environment)
{
return new PluginAssemblies(application.Plugin);
return new PluginAssemblies(application.Plugin.Concat(options.Plugins.GetNormalizedLocationPaths(environment)));
}
}
}
3 changes: 1 addition & 2 deletions src/OmniSharp.Http.Driver/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ static int Main(string[] args) => HostHelpers.Start(() =>
Configuration.ZeroBasedIndices = application.ZeroBasedIndices;

var writer = new SharedTextWriter(Console.Out);
var plugins = application.CreatePluginAssemblies();

var host = new Host(environment, writer, plugins, application.Port, application.Interface);
var host = new Host(environment, writer, application.Plugin, application.Port, application.Interface);
host.Start();

return 0;
Expand Down
9 changes: 5 additions & 4 deletions src/OmniSharp.Http/Host.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand All @@ -14,20 +15,20 @@ internal class Host
{
private readonly IOmniSharpEnvironment _environment;
private readonly ISharedTextWriter _sharedTextWriter;
private readonly PluginAssemblies _pluginAssemblies;
private readonly IEnumerable<string> _commandLinePlugins;
private readonly int _serverPort;
private readonly string _serverInterface;

public Host(
IOmniSharpEnvironment environment,
ISharedTextWriter sharedTextWriter,
PluginAssemblies pluginAssemblies,
IEnumerable<string> commandLinePlugins,
int serverPort,
string serverInterface)
{
_environment = environment;
_sharedTextWriter = sharedTextWriter;
_pluginAssemblies = pluginAssemblies;
_commandLinePlugins = commandLinePlugins;
_serverPort = serverPort;
_serverInterface = serverInterface;
}
Expand All @@ -44,7 +45,7 @@ public void Start()
serviceCollection.AddSingleton(_environment);
serviceCollection.AddSingleton(_sharedTextWriter);
serviceCollection.AddSingleton(NullEventEmitter.Instance);
serviceCollection.AddSingleton(_pluginAssemblies);
serviceCollection.AddSingleton(_commandLinePlugins);
dmgonch marked this conversation as resolved.
Show resolved Hide resolved
serviceCollection.AddSingleton(new HttpEnvironment { Port = _serverPort });
})
.UseUrls($"http://{_serverInterface}:{_serverPort}")
Expand Down
16 changes: 15 additions & 1 deletion src/OmniSharp.Http/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.Collections.Generic;
using System.Composition.Hosting;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OmniSharp.Eventing;
using OmniSharp.Http.Middleware;
using OmniSharp.Options;
using OmniSharp.Plugins;
using OmniSharp.Roslyn;
using OmniSharp.Services;
using OmniSharp.Utilities;
Expand All @@ -17,11 +22,13 @@ internal class Startup
private readonly IOmniSharpEnvironment _environment;
private readonly IEventEmitter _eventEmitter;
private CompositionHost _compositionHost;
private IEnumerable<string> _commandLinePlugins;

public Startup(IOmniSharpEnvironment environment, IEventEmitter eventEmitter, ISharedTextWriter writer)
public Startup(IOmniSharpEnvironment environment, IEventEmitter eventEmitter, IEnumerable<string> commandLinePlugins)
{
_environment = environment;
_eventEmitter = eventEmitter;
_commandLinePlugins = commandLinePlugins;
}

public IServiceProvider ConfigureServices(IServiceCollection services)
Expand All @@ -45,8 +52,15 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
!category.Equals(projectEventForwarder, StringComparison.OrdinalIgnoreCase)));
});

var options = serviceProvider.GetRequiredService<IOptionsMonitor<OmniSharpOptions>>();
var plugins = new PluginAssemblies(_commandLinePlugins.Concat(options.CurrentValue.Plugins.GetNormalizedLocationPaths(_environment)));
dmgonch marked this conversation as resolved.
Show resolved Hide resolved

var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<Startup>();
var assemblyLoader = serviceProvider.GetRequiredService<IAssemblyLoader>();
_compositionHost = new CompositionHostBuilder(serviceProvider)
.WithOmniSharpAssemblies()
.WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(logger, plugins.AssemblyNames).ToArray())
dmgonch marked this conversation as resolved.
Show resolved Hide resolved
.Build();

return serviceProvider;
Expand Down
7 changes: 5 additions & 2 deletions src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
Expand All @@ -16,6 +17,7 @@
using OmniSharp.LanguageServerProtocol.Handlers;
using OmniSharp.Mef;
using OmniSharp.Models.Diagnostics;
using OmniSharp.Options;
using OmniSharp.Roslyn;
using OmniSharp.Services;
using OmniSharp.Utilities;
Expand Down Expand Up @@ -91,13 +93,14 @@ private void CreateCompositionHost(InitializeParams initializeParams)
_eventEmitter = new LanguageServerEventEmitter();
_serviceProvider = CompositionHostBuilder.CreateDefaultServiceProvider(_environment, configurationRoot, _eventEmitter, _services);

var plugins = _application.CreatePluginAssemblies();
var options = _serviceProvider.GetRequiredService<IOptionsMonitor<OmniSharpOptions>>();
var plugins = _application.CreatePluginAssemblies(options.CurrentValue, _environment);

var assemblyLoader = _serviceProvider.GetRequiredService<IAssemblyLoader>();
var compositionHostBuilder = new CompositionHostBuilder(_serviceProvider)
.WithOmniSharpAssemblies()
.WithAssemblies(typeof(LanguageServerHost).Assembly)
.WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(plugins.AssemblyNames).ToArray());
.WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(_logger, plugins.AssemblyNames).ToArray());

_compositionHost = compositionHostBuilder.Build();

Expand Down
2 changes: 2 additions & 0 deletions src/OmniSharp.Shared/Options/OmniSharpOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class OmniSharpOptions
public FileOptions FileOptions { get; set; } = new FileOptions();

public RenameOptions RenameOptions { get; set; } = new RenameOptions();

public OmniSharpExtensionsOptions Plugins { get; set; } = new OmniSharpExtensionsOptions();
}
}
7 changes: 5 additions & 2 deletions src/OmniSharp.Shared/Options/RoslynExtensionsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

namespace OmniSharp.Options
{
public class RoslynExtensionsOptions
public class RoslynExtensionsOptions : OmniSharpExtensionsOptions
{
public bool EnableAnalyzersSupport { get; set; }
public int DocumentAnalysisTimeoutMs { get; set; } = 10 * 1000;
}

public class OmniSharpExtensionsOptions
{
public string[] LocationPaths { get; set; }

public IEnumerable<string> GetNormalizedLocationPaths(IOmniSharpEnvironment env)
Expand All @@ -32,4 +35,4 @@ public IEnumerable<string> GetNormalizedLocationPaths(IOmniSharpEnvironment env)
return normalizePaths;
}
}
}
}
8 changes: 6 additions & 2 deletions src/OmniSharp.Stdio.Driver/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OmniSharp.LanguageServerProtocol;
using OmniSharp.Options;
using OmniSharp.Services;
using OmniSharp.Stdio.Eventing;
using OmniSharp.Stdio.Logging;
Expand Down Expand Up @@ -57,11 +59,13 @@ static int Main(string[] args) => HostHelpers.Start(() =>
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var assemblyLoader = serviceProvider.GetRequiredService<IAssemblyLoader>();

var plugins = application.CreatePluginAssemblies();
var options = serviceProvider.GetRequiredService<IOptionsMonitor<OmniSharpOptions>>();
var plugins = application.CreatePluginAssemblies(options.CurrentValue, environment);

var logger = loggerFactory.CreateLogger<Program>();
var compositionHostBuilder = new CompositionHostBuilder(serviceProvider)
.WithOmniSharpAssemblies()
.WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(plugins.AssemblyNames).ToArray());
.WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(logger, plugins.AssemblyNames).ToArray());

using (var host = new Host(input, writer, environment, serviceProvider, compositionHostBuilder, loggerFactory, cancellation))
{
Expand Down