Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Piedone committed Apr 24, 2020
2 parents 1d10f99 + dacba5e commit b9fd2ac
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 138 deletions.
55 changes: 29 additions & 26 deletions Lombiq.Vsix.Orchard/Commands/InjectDependencyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
using Lombiq.Vsix.Orchard.Constants;
using Lombiq.Vsix.Orchard.Forms;
using Lombiq.Vsix.Orchard.Helpers;
using Lombiq.Vsix.Orchard.Services;
using Lombiq.Vsix.Orchard.Services.DependencyInjector;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms;
using Task = System.Threading.Tasks.Task;

namespace Lombiq.Vsix.Orchard.Commands
{
Expand All @@ -19,45 +17,50 @@ internal sealed class InjectDependencyCommand
public const int CommandId = CommandIds.InjectDependencyCommandId;
public static readonly Guid CommandSet = PackageGuids.LombiqOrchardVisualStudioExtensionCommandSetGuid;


private readonly IServiceProvider _serviceProvider;
private readonly IMenuCommandService _menuCommandService;
private readonly AsyncPackage _package;
private readonly DTE _dte;
private readonly IDependencyInjector _dependencyInjector;
private readonly IEnumerable<IFieldNameFromDependencyGenerator> _fieldNameGenerators;
private readonly IEnumerable<IDependencyNameProvider> _dependencyNameProviders;
private readonly DTE _dte;


private InjectDependencyCommand(Package package)
{
_serviceProvider = package;
public static InjectDependencyCommand Instance { get; private set; }

_dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
_dependencyInjector = _serviceProvider.GetService<IDependencyInjector>();
_fieldNameGenerators = _serviceProvider.GetServices<IFieldNameFromDependencyGenerator>();
_dependencyNameProviders = _serviceProvider.GetServices<IDependencyNameProvider>();
_menuCommandService = _serviceProvider.GetService<IMenuCommandService>();

Initialize();
private InjectDependencyCommand(
AsyncPackage package,
DTE dte,
IDependencyInjector dependencyInjector,
IEnumerable<IFieldNameFromDependencyGenerator> fieldNameGenerators,
IEnumerable<IDependencyNameProvider> dependencyNameProviders)
{
_package = package;
_dte = dte;
_dependencyInjector = dependencyInjector;
_fieldNameGenerators = fieldNameGenerators;
_dependencyNameProviders = dependencyNameProviders;
}


public static InjectDependencyCommand Instance { get; private set; }

public static void Initialize(Package package)
public static async Task Create(AsyncPackage package)
{
Instance = Instance ?? new InjectDependencyCommand(package);
Instance = Instance ?? new InjectDependencyCommand(
package,
package.GetDte(),
await package.GetServiceAsync<IDependencyInjector>(),
await package.GetServicesAsync<IFieldNameFromDependencyGenerator>(),
await package.GetServicesAsync<IDependencyNameProvider>());
}


private void Initialize()
public async Task InitializeUI()
{
_menuCommandService.AddCommand(
(await _package.GetServiceAsync<IMenuCommandService>()).AddCommand(
new MenuCommand(
MenuItemCallback,
new CommandID(CommandSet, CommandId)));
}


private void MenuItemCallback(object sender, EventArgs e)
{
var injectDependencyCaption = "Inject Dependency";
Expand All @@ -70,7 +73,7 @@ private void MenuItemCallback(object sender, EventArgs e)
}

using (var injectDependencyDialog = new InjectDependencyDialog(
_fieldNameGenerators,
_fieldNameGenerators,
_dependencyNameProviders,
_dependencyInjector.GetExpectedClassName(_dte.ActiveDocument)))
{
Expand All @@ -89,7 +92,7 @@ private void MenuItemCallback(object sender, EventArgs e)
}

var result = _dependencyInjector.Inject(
_dte.ActiveDocument,
_dte.ActiveDocument,
injectDependencyDialog.GetDependencyInjectionData());

if (!result.Success)
Expand All @@ -98,7 +101,7 @@ private void MenuItemCallback(object sender, EventArgs e)
{
case DependencyInjectorErrorCodes.ClassNotFound:
DialogHelpers.Warning(
"Could not inject dependency because the class was not found in this file.",
"Could not inject dependency because the class was not found in this file.",
injectDependencyCaption);
break;
default:
Expand Down
73 changes: 41 additions & 32 deletions Lombiq.Vsix.Orchard/Commands/OpenErrorLogCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using Lombiq.Vsix.Orchard.Services.LogWatcher;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using Task = System.Threading.Tasks.Task;

namespace Lombiq.Vsix.Orchard.Commands
{
Expand All @@ -19,39 +19,60 @@ internal sealed class OpenErrorLogCommand : IDisposable
public static readonly Guid CommandSet = PackageGuids.LombiqOrchardVisualStudioExtensionCommandSetGuid;


private readonly IServiceProvider _serviceProvider;
private readonly IMenuCommandService _menuCommandService;
private readonly AsyncPackage _package;
private readonly DTE _dte;
private readonly Lazy<ILogWatcherSettings> _lazyLogWatcherSettings;
private readonly IEnumerable<ILogFileWatcher> _logWatchers;
private readonly IBlinkStickManager _blinkStickManager;

private OleMenuCommand _openErrorLogCommand;
private CommandBar _orchardLogWatcherToolbar;
private bool _hasSeenErrorLogUpdate;
private bool _errorIndicatorStateChanged;
private ILogFileStatus _latestUpdatedLogFileStatus;


private OpenErrorLogCommand(Package package)
private OpenErrorLogCommand(
AsyncPackage package,
DTE dte,
Lazy<ILogWatcherSettings> lazyLogWatcherSettings,
IEnumerable<ILogFileWatcher> logWatchers,
IBlinkStickManager blinkStickManager)
{
_serviceProvider = package;

_dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
_menuCommandService = _serviceProvider.GetService<IMenuCommandService>();
_lazyLogWatcherSettings = new Lazy<ILogWatcherSettings>(
_serviceProvider.GetService<ILogWatcherSettingsAccessor>().GetSettings);
_logWatchers = _serviceProvider.GetServices<ILogFileWatcher>();
_blinkStickManager = _serviceProvider.GetService<IBlinkStickManager>();

Initialize();
_package = package;
_dte = dte;
_lazyLogWatcherSettings = lazyLogWatcherSettings;
_logWatchers = logWatchers;
_blinkStickManager = blinkStickManager;
}


public static OpenErrorLogCommand Instance { get; private set; }

public static void Initialize(Package package)
public static async Task Create(AsyncPackage package, ILogWatcherSettingsAccessor logWatcherSettingsAccessor)
{
Instance = Instance ?? new OpenErrorLogCommand(
package,
package.GetDte(),
new Lazy<ILogWatcherSettings>(logWatcherSettingsAccessor.GetSettings),
await package.GetServicesAsync<ILogFileWatcher>(),
await package.GetServiceAsync<IBlinkStickManager>());

Instance.InitalizeWatchers();
}


public async Task InitializeUI()
{
Instance = Instance ?? new OpenErrorLogCommand(package);
_openErrorLogCommand = new OleMenuCommand(OpenErrorLogCallback, new CommandID(CommandSet, CommandId));
_openErrorLogCommand.BeforeQueryStatus += OpenErrorLogCommandBeforeQueryStatusCallback;

(await _package.GetServiceAsync<IMenuCommandService>()).AddCommand(_openErrorLogCommand);

// Store Log Watcher toolbar in a variable to be able to show or hide depending on the Log Watcher settings.
_orchardLogWatcherToolbar = ((CommandBars)_dte.CommandBars)[CommandBarNames.OrchardLogWatcherToolbarName];

if (_lazyLogWatcherSettings.Value.LogWatcherEnabled) _openErrorLogCommand.Visible = true;
}

public void Dispose()
Expand All @@ -68,7 +89,7 @@ public void Dispose()
}


private void Initialize()
private void InitalizeWatchers()
{
_hasSeenErrorLogUpdate = true;
_errorIndicatorStateChanged = true;
Expand All @@ -80,20 +101,7 @@ private void Initialize()

_lazyLogWatcherSettings.Value.SettingsUpdated += LogWatcherSettingsUpdatedCallback;

_openErrorLogCommand = new OleMenuCommand(OpenErrorLogCallback, new CommandID(CommandSet, CommandId));
_openErrorLogCommand.BeforeQueryStatus += OpenErrorLogCommandBeforeQueryStatusCallback;

_menuCommandService.AddCommand(_openErrorLogCommand);

// Store Log Watcher toolbar in a variable to be able to show or hide depending on the Log Watcher settings.
_orchardLogWatcherToolbar = ((CommandBars)_dte.CommandBars)[CommandBarNames.OrchardLogWatcherToolbarName];

if (_lazyLogWatcherSettings.Value.LogWatcherEnabled)
{
_openErrorLogCommand.Visible = true;

StartLogFileWatching();
}
if (_lazyLogWatcherSettings.Value.LogWatcherEnabled) StartLogFileWatching();
}

private void OpenErrorLogCommandBeforeQueryStatusCallback(object sender, EventArgs e) =>
Expand Down Expand Up @@ -166,8 +174,9 @@ private void UpdateOpenErrorLogCommandAccessibilityAndText(ILogFileStatus logFil
{
_openErrorLogCommand.Enabled = false;
_openErrorLogCommand.Text = "Orchard error log doesn't exist or hasn't been updated";
_blinkStickManager.TurnOff();
if (!_errorIndicatorStateChanged) _blinkStickManager.TurnOff();
_errorIndicatorStateChanged = true;

}
}

Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Vsix.Orchard/Constants/ExtensionVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
internal static class ExtensionVersion
{
public const string Current = "1.5.0";
public const string Current = "1.5.1";
}
}
28 changes: 28 additions & 0 deletions Lombiq.Vsix.Orchard/Extensions/AsyncPackageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using EnvDTE;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;

namespace Microsoft.VisualStudio.Shell
{
public static class AsyncPackageExtensions
{
public static void AddService<T>(this AsyncPackage package, Func<System.Threading.Tasks.Task<object>> resolver) =>
package.AddService(typeof(T), (container, cancellationToken, serviceType) => resolver());

public static void AddService<TService, TImplementation>(this AsyncPackage package) where TImplementation : new() =>
package.AddService<TService>(() => System.Threading.Tasks.Task.FromResult((object)new TImplementation()));

public static void AddService<T>(this AsyncPackage package, T instance) =>
((IServiceContainer)package).AddService(typeof(T), instance);

public static async System.Threading.Tasks.Task<T> GetServiceAsync<T>(this AsyncPackage package) =>
(T)(await package.GetServiceAsync(typeof(T)));

public static async System.Threading.Tasks.Task<IEnumerable<T>> GetServicesAsync<T>(this AsyncPackage package) =>
(IEnumerable<T>)(await package.GetServiceAsync(typeof(T)));

public static DTE GetDte(this AsyncPackage package) =>
Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE)) as DTE;
}
}
15 changes: 0 additions & 15 deletions Lombiq.Vsix.Orchard/Extensions/ServiceContainerExtensions.cs

This file was deleted.

13 changes: 0 additions & 13 deletions Lombiq.Vsix.Orchard/Extensions/ServiceProviderExtensions.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Lombiq.Vsix.Orchard/Lombiq.Vsix.Orchard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@
<Compile Include="Constants\DependencyInjectorErrorCodes.cs" />
<Compile Include="Constants\ExtensionVersion.cs" />
<Compile Include="Exceptions\DependencyToConstructorInjectorException.cs" />
<Compile Include="Extensions\ServiceContainerExtensions.cs" />
<Compile Include="Extensions\ServiceProviderExtensions.cs" />
<Compile Include="Extensions\AsyncPackageExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Forms\InjectDependencyDialog.cs">
<SubType>Form</SubType>
Expand Down
Binary file modified Lombiq.Vsix.Orchard/Lombiq.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b9fd2ac

Please sign in to comment.