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

added support for disabling any project system via configuration #902

Merged
merged 3 commits into from
Jul 4, 2017
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
11 changes: 0 additions & 11 deletions src/OmniSharp.DotNet/DotNetProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ Task<object> IProjectSystem.GetProjectModelAsync(string filePath)

public void Initalize(IConfiguration configuration)
{
if (!bool.TryParse(configuration["enabled"], out var enabled))
{
enabled = true;
}

if (!enabled)
{
_logger.LogInformation("DotNetProjectSystem is disabled");
return;
}

_logger.LogInformation($"Initializing in {_environment.TargetDirectory}");

if (!bool.TryParse(configuration["enablePackageRestore"], out _enableRestorePackages))
Expand Down
81 changes: 81 additions & 0 deletions src/OmniSharp.Host/Services/WorkspaceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Composition.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OmniSharp.Options;
using OmniSharp.Roslyn;
using OmniSharp.Roslyn.Options;

namespace OmniSharp.Services
{
public class WorkspaceHelper
{
private readonly CompositionHost _compositionHost;
private readonly IConfiguration _configuration;
private readonly OmniSharpOptions _options;
private readonly ILogger _logger;

public WorkspaceHelper(CompositionHost compositionHost, IConfiguration configuration, OmniSharpOptions options, ILoggerFactory loggerFactory)
{
_compositionHost = compositionHost;
_configuration = configuration;
_options = options;
_logger = loggerFactory.CreateLogger<WorkspaceHelper>();
}

public void Initialize(OmniSharpWorkspace workspace)
{
var projectEventForwarder = _compositionHost.GetExport<ProjectEventForwarder>();
projectEventForwarder.Initialize();

// Initialize all the project systems discovered with MEF
foreach (var projectSystem in _compositionHost.GetExports<IProjectSystem>())
{
try
{
var projectConfiguration = _configuration.GetSection(projectSystem.Key);
var enabledProjectFlag = projectConfiguration.GetValue<bool>("enabled", defaultValue: true);
if (enabledProjectFlag)
{
projectSystem.Initalize(projectConfiguration);
}
else
{
_logger.LogInformation($"Project system '{projectSystem.GetType().FullName}' is disabled in the configuration.");
}
}
catch (Exception e)
{
var message = $"The project system '{projectSystem.GetType().FullName}' threw exception during initialization.";
// if a project system throws an unhandled exception it should not crash the entire server
_logger.LogError(e, message);
}
}

ProvideOptions(workspace, _options);

// Mark the workspace as initialized
workspace.Initialized = true;
}

public void ProvideOptions(OmniSharpWorkspace workspace, OmniSharpOptions options)
{
// run all workspace options providers discovered with MEF
foreach (var workspaceOptionsProvider in _compositionHost.GetExports<IWorkspaceOptionsProvider>())
{
var providerName = workspaceOptionsProvider.GetType().FullName;

try
{
_logger.LogInformation($"Invoking Workspace Options Provider: {providerName}");
workspace.Options = workspaceOptionsProvider.Process(workspace.Options, options.FormattingOptions);
}
catch (Exception e)
{
var message = $"The workspace options provider '{providerName}' threw exception during initialization.";
_logger.LogError(e, message);
}
}
}
}
}
51 changes: 3 additions & 48 deletions src/OmniSharp.Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,52 +122,6 @@ public static CompositionHost CreateCompositionHost(IServiceProvider serviceProv
return config.CreateContainer();
}

public static void InitializeWorkspace(OmniSharpWorkspace workspace, CompositionHost compositionHost, IConfiguration configuration, ILogger logger, OmniSharpOptions options)
{
var projectEventForwarder = compositionHost.GetExport<ProjectEventForwarder>();
projectEventForwarder.Initialize();

// Initialize all the project systems
foreach (var projectSystem in compositionHost.GetExports<IProjectSystem>())
{
try
{
projectSystem.Initalize(configuration.GetSection(projectSystem.Key));
}
catch (Exception e)
{
var message = $"The project system '{projectSystem.GetType().FullName}' threw exception during initialization.";
// if a project system throws an unhandled exception it should not crash the entire server
logger.LogError(e, message);
}
}

ProvideWorkspaceOptions(workspace, compositionHost, logger, options);

// Mark the workspace as initialized
workspace.Initialized = true;
}

private static void ProvideWorkspaceOptions(OmniSharpWorkspace workspace, CompositionHost compositionHost, ILogger logger, OmniSharpOptions options)
{
// run all workspace options providers
foreach (var workspaceOptionsProvider in compositionHost.GetExports<IWorkspaceOptionsProvider>())
{
var providerName = workspaceOptionsProvider.GetType().FullName;

try
{
logger.LogInformation($"Invoking Workspace Options Provider: {providerName}");
workspace.Options = workspaceOptionsProvider.Process(workspace.Options, options.FormattingOptions);
}
catch (Exception e)
{
var message = $"The workspace options provider '{providerName}' threw exception during initialization.";
logger.LogError(e, message);
}
}
}

public void Configure(
IApplicationBuilder app,
IServiceProvider serviceProvider,
Expand Down Expand Up @@ -206,13 +160,14 @@ public void Configure(
logger.LogInformation($"Omnisharp server running on port '{_env.Port}' at location '{_env.TargetDirectory}' on host {_env.HostProcessId}.");
}

InitializeWorkspace(Workspace, PluginHost, Configuration, logger, options.CurrentValue);
var workspaceHelper = new WorkspaceHelper(PluginHost, Configuration, options.CurrentValue, loggerFactory);
workspaceHelper.Initialize(Workspace);

// when configuration options change
// run workspace options providers automatically
options.OnChange(o =>
{
ProvideWorkspaceOptions(Workspace, PluginHost, logger, o);
workspaceHelper.ProvideOptions(Workspace, o);
});

logger.LogInformation("Configuration finished.");
Expand Down
3 changes: 2 additions & 1 deletion tests/TestUtility/OmniSharpTestHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public static OmniSharpTestHost Create(string path = null, ITestOutputHelper tes
var dotNetCli = compositionHost.GetExport<DotNetCliService>();
dotNetCli.SetDotNetPath(dotNetPath);

Startup.InitializeWorkspace(workspace, compositionHost, configuration, logger, omnisharpOptions);
var workspaceHelper = new WorkspaceHelper(compositionHost, configuration, omnisharpOptions, loggerFactory);
workspaceHelper.Initialize(workspace);

return new OmniSharpTestHost(serviceProvider, loggerFactory, workspace, compositionHost);
}
Expand Down