Skip to content

Commit

Permalink
Refactor SolutionExplorerMenuCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mushketyk committed Mar 18, 2017
1 parent d26e00c commit 8d21a76
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 66 deletions.
27 changes: 27 additions & 0 deletions VisualStudioPlugin/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,36 @@ namespace QuantConnect.VisualStudioPlugin
static class PathUtils
{

private static Dictionary<string, Language> _extensionsDictionary = new Dictionary<string, Language>();

static PathUtils()
{
_extensionsDictionary[".cs"] = Language.CSharp;
_extensionsDictionary[".java"] = Language.Java;
_extensionsDictionary[".vb"] = Language.VisualBasic;
_extensionsDictionary[".fs"] = Language.FSharp;
}

public static string GetSolutionFolder(DTE2 dte2)
{
return Path.GetDirectoryName(dte2.Solution.FullName);
}

public static Language? DetermineProjectLanguage(List<string> filePaths)
{

var extensionsSet = new HashSet<string>();
foreach (var filePath in filePaths)
{
extensionsSet.Add(Path.GetExtension(filePath));
}

if (extensionsSet.Count == 1 && _extensionsDictionary.ContainsKey(extensionsSet.First()))
{
return _extensionsDictionary[extensionsSet.First()];
}

return null;
}
}
}
78 changes: 12 additions & 66 deletions VisualStudioPlugin/SolutionExplorerMenuCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace QuantConnect.VisualStudioPlugin
{
Expand Down Expand Up @@ -52,8 +54,6 @@ internal sealed class SolutionExplorerMenuCommand

private LogInCommand _logInCommand;

private Dictionary<string, Language> _estensionToLanguage;

/// <summary>
/// Initializes a new instance of the <see cref="SolutionExplorerMenuCommand"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
Expand All @@ -69,7 +69,6 @@ private SolutionExplorerMenuCommand(Package package)
_package = package as QuantConnectPackage;
_dte2 = ServiceProvider.GetService(typeof(SDTE)) as DTE2;
_logInCommand = CreateLogInCommand();
_estensionToLanguage = CreateExtensionsDictionary();

var commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
Expand All @@ -79,16 +78,6 @@ private SolutionExplorerMenuCommand(Package package)
}
}

public Dictionary<string, Language> CreateExtensionsDictionary()
{
var dict = new Dictionary<string, Language>();
dict[".cs"] = Language.CSharp;
dict[".java"] = Language.Java;
dict[".vb"] = Language.VisualBasic;
dict[".fs"] = Language.FSharp;

return dict;
}

private ProjectFinder CreateProjectFinder()
{
Expand Down Expand Up @@ -160,13 +149,6 @@ private void SendForBacktestingCallback(object sender, EventArgs e)
ExecuteOnProject(sender, (selectedProjectId, selectedProjectName, files) =>
{
var fileNames = files.Select(f => f.Item1).ToList();
var message = string.Format(
CultureInfo.CurrentCulture,
"Send for backtesting to project {0}, files: {1}",
selectedProjectId + " : " + selectedProjectName,
string.Join(" ", fileNames)
);
ShowMessageBox("Sending To Backtest", message);

VsUtils.DisplayInStatusBar(this.ServiceProvider, "Uploading files to server ...");
UploadFilesToServer(selectedProjectId, files);
Expand All @@ -176,7 +158,7 @@ private void SendForBacktestingCallback(object sender, EventArgs e)
if (compileStatus.State == Api.CompileState.BuildError)
{
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Compile error.");
ShowMessageBox("Compile Error", "Error when compiling project.");
VsUtils.ShowMessageBox(this.ServiceProvider, "Compile Error", "Error when compiling project.");
return;
}

Expand All @@ -196,7 +178,7 @@ private void SendForBacktestingCallback(object sender, EventArgs e)
"https://www.quantconnect.com/terminal/#open/{0}",
selectedProjectId
);
System.Diagnostics.Process.Start(projectUrl);
Process.Start(projectUrl);
});
}

Expand All @@ -205,51 +187,32 @@ private void SaveToQuantConnectCallback(object sender, EventArgs e)
ExecuteOnProject(sender, (selectedProjectId, selectedProjectName, files) =>
{
var fileNames = files.Select(f => f.Item1).ToList();
var message = string.Format(
CultureInfo.CurrentCulture,
"Save to project {0}, files {1}",
selectedProjectId + " : " + selectedProjectName,
string.Join(" ", fileNames)
);
ShowMessageBox("Saving To Project", message);

VsUtils.DisplayInStatusBar(this.ServiceProvider, "Uploading files to server ...");
UploadFilesToServer(selectedProjectId, files);
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Files upload complete.");
});
}

private void ShowMessageBox(string title, string message)
{
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
message,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST
);
}

private void UploadFilesToServer(int selectedProjectId, List<Tuple<string, string>> files)
{
var api = AuthorizationManager.GetInstance().GetApi();
foreach (Tuple<string, string> file in files)
{
api.DeleteProjectFile(selectedProjectId, file.Item1);
string fileContent = System.IO.File.ReadAllText(file.Item2);
var fileContent = File.ReadAllText(file.Item2);
api.AddProjectFile(selectedProjectId, file.Item1, fileContent);
}
}

private Api.Compile CompileProjectOnServer(int projectId)
{
var api = AuthorizationManager.GetInstance().GetApi();
Api.Compile compileStatus = api.CreateCompile(projectId);
var compileStatus = api.CreateCompile(projectId);
var compileId = compileStatus.CompileId;
while (compileStatus.State == Api.CompileState.InQueue)
{
System.Threading.Thread.Sleep(5000);
Thread.Sleep(5000);
compileStatus = api.ReadCompile(projectId, compileId);
}
return compileStatus;
Expand All @@ -258,11 +221,11 @@ private Api.Compile CompileProjectOnServer(int projectId)
private Api.Backtest BacktestProjectOnServer(int projectId, string compileId)
{
var api = AuthorizationManager.GetInstance().GetApi();
Api.Backtest backtestStatus = api.CreateBacktest(projectId, compileId, "My New Backtest");
var backtestStatus = api.CreateBacktest(projectId, compileId, "My New Backtest");
var backtestId = backtestStatus.BacktestId;
while (!backtestStatus.Completed)
{
System.Threading.Thread.Sleep(5000);
Thread.Sleep(5000);
backtestStatus = api.ReadBacktest(projectId, backtestId);
}
return backtestStatus;
Expand Down Expand Up @@ -290,18 +253,18 @@ private void ExecuteOnProject(object sender, Action<int, string, List<Tuple<stri

if (!selectedProjectId.HasValue)
{
var newProjectLanguage = DetermineProjectLanguage(files);
var newProjectLanguage = PathUtils.DetermineProjectLanguage(files.Select(f => f.Item2).ToList());
if (!newProjectLanguage.HasValue)
{
ShowMessageBox("Failed to determine project language",
VsUtils.ShowMessageBox(this.ServiceProvider, "Failed to determine project language",
$"Failed to determine programming laguage for a project");
return;
}

selectedProjectId = CreateQuantConnectProject(selectedProjectName, newProjectLanguage.Value);
if (!selectedProjectId.HasValue)
{
ShowMessageBox("Failed to create a project", $"Failed to create a project {selectedProjectName}");
VsUtils.ShowMessageBox(this.ServiceProvider, "Failed to create a project", $"Failed to create a project {selectedProjectName}");
}
onProject.Invoke(selectedProjectId.Value, selectedProjectName, files);
}
Expand All @@ -324,23 +287,6 @@ private void ExecuteOnProject(object sender, Action<int, string, List<Tuple<stri
return projectResponse.Projects[0].ProjectId;
}

private Language? DetermineProjectLanguage(List<Tuple<string, string>> files)
{
var extensionsSet = new HashSet<string>();
foreach (var fileTuple in files)
{
var fullPath = fileTuple.Item2;
extensionsSet.Add(Path.GetExtension(fullPath));
}

if (extensionsSet.Count == 1 && _estensionToLanguage.ContainsKey(extensionsSet.First()))
{
return _estensionToLanguage[extensionsSet.First()];
}

return null;
}

private List<Tuple<string, string>> GetSelectedFiles(object sender)
{
var myCommand = sender as OleMenuCommand;
Expand Down
13 changes: 13 additions & 0 deletions VisualStudioPlugin/VsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;

Expand Down Expand Up @@ -51,5 +52,17 @@ public static void DisplayDialogWindow(DialogWindow dialogWindow)
dialogWindow.HasMaximizeButton = false;
dialogWindow.ShowModal();
}

public static void ShowMessageBox(IServiceProvider serviceProvider, string title, string message)
{
VsShellUtilities.ShowMessageBox(
serviceProvider,
message,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST
);
}
}
}

0 comments on commit 8d21a76

Please sign in to comment.