Skip to content

Commit

Permalink
Add CompileFileAsync menu command handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
allisterb committed Nov 20, 2024
1 parent 0d3e447 commit 69c1da5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
32 changes: 17 additions & 15 deletions src/Stratis.VS.StratisEVM/SolidityCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;

using Stratis.DevEx;

namespace Stratis.VS.StratisEVM
Expand All @@ -18,43 +20,43 @@ public static async Task CompileFileAsync(string file, string workspaceDir)
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VSUtil.ShowLogOutputWindowPane(ServiceProvider.GlobalProvider, "Solidity Compiler");
VSUtil.LogInfo("Solidity Compiler", string.Format("Compiling {0} in {1}...", file, workspaceDir));
await TaskScheduler.Default;
var binfiles = Directory.GetFiles(AssemblyLocation, "*.bin", SearchOption.TopDirectoryOnly);
foreach ( var binfile in binfiles )
{
File.Delete(binfile);
}


var cmd = "cmd.exe";
var args = "/c node " + Path.Combine("node_modules", "solc", "solc.js") + " --base-path=\"" + workspaceDir + "\"" + " \"" + file + "\" --bin";
if (Directory.Exists(Path.Combine(workspaceDir, "node_modules")))
{
args += " --include-path=" + Path.Combine(workspaceDir, "node_modules");
}
var output = await ThreadHelper.JoinableTaskFactory.RunAsync(async () => await RunCmdAsync(cmd, args, AssemblyLocation));
var output = await RunCmdAsync(cmd, args, AssemblyLocation);
if (CheckRunCmdError(output))
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VSUtil.LogError("Solidity Compiler", "Could not run Solidity Compiler process: " + cmd + " " + args + ": " + GetRunCmdError(output));
return;
}
if (output.ContainsKey("stdout"))
else if (output.ContainsKey("stdout"))
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VSUtil.LogInfo("Solidity Compiler", (string)output["stdout"]);
return;
}

if (output.ContainsKey("stderr"))
else if (output.ContainsKey("stderr"))
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VSUtil.LogInfo("Solidity Compiler", (string)output["stderr"]);
}
if (output.ContainsKey("stdout") || output.ContainsKey("stderr"))
{
return;
}
else
{
binfiles = Directory.GetFiles(AssemblyLocation, "*.bin", SearchOption.TopDirectoryOnly);
if (binfiles is null || binfiles.Length == 0)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VSUtil.LogError("Solidity Compiler", "Could not read Solidity compiler output. No compiler output files found.");
return;
}
Expand All @@ -65,21 +67,21 @@ public static async Task CompileFileAsync(string file, string workspaceDir)
{
if (binfile.Contains(Path.GetFileNameWithoutExtension(file)))
{
b = File.ReadAllText(binfile);
VSUtil.LogInfo("Solidity Compiler", "======= " + file + "======= " + "\nBinary: \n" + b);
b = File.ReadAllText(binfile);
}
File.Delete(binfile);
}
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (b == null)
{
VSUtil.LogError("Solidity Compiler", "Error reading Solidity compiler output: could not find compiler output file for " + file + ".");

}

else
{
VSUtil.LogInfo("Solidity Compiler", "======= " + file + "======= " + "\nBinary: \n" + b);
}
return;
}


}
}
}
Expand Down
37 changes: 15 additions & 22 deletions src/Stratis.VS.StratisEVM/SolidityProjectMenuCommands.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Workspace;
using System;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;
Expand Down Expand Up @@ -41,13 +43,14 @@ private SolidityProjectMenuCommands(AsyncPackage package, OleMenuCommandService
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));

var menuCommandID = new CommandID(CommandSet, CompileCommandId);
var menuItem = new MenuCommand(this.Execute, menuCommandID)
var menuItem = new OleMenuCommand(CompileFile, menuCommandID)
{
Supported = false
};
menuItem.ParametersDescription = "$";
commandService.AddCommand(menuItem);
menuCommandID = new CommandID(CommandSet, InstallPackagesCommandId);
menuItem = new MenuCommand(this.Execute, menuCommandID)
menuItem = new OleMenuCommand(CompileFile, menuCommandID)
{
Supported = false
};
Expand Down Expand Up @@ -83,27 +86,17 @@ public static async Task InitializeAsync(AsyncPackage package)
Instance = new SolidityProjectMenuCommands(package, commandService);
}

/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void Execute(object sender, EventArgs e)
private async Task CompileFileAsync()
{
ThreadHelper.ThrowIfNotOnUIThread();
string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
string title = "SolidityProjectMenuCommands";

// Show a message box to prove we were here
VsShellUtilities.ShowMessageBox(
this.package,
message,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
EnvDTE.DTE dte = (EnvDTE.DTE) await ServiceProvider.GetServiceAsync(typeof(EnvDTE.DTE));
Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
EnvDTE.Project dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
await SolidityCompiler.CompileFileAsync(dte.ActiveDocument.FullName, Path.GetDirectoryName(dteProject.FullName));
}

#pragma warning disable VSTHRD110, CS4014
private void CompileFile(object sender, EventArgs e) => CompileFileAsync();
#pragma warning restore VSTHRD110, CS4014
}
}
1 change: 1 addition & 0 deletions src/Stratis.VS.StratisEVM/StratisEVMPackage.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<Icon guid="guidSolidityIcon" id="pngSolidity" />
<CommandFlag>DynamicVisibility</CommandFlag>
<CommandFlag>DefaultInvisible</CommandFlag>
<CommandFlag>AllowParams</CommandFlag>
<Strings>
<ButtonText>Compile Solidity File</ButtonText>
</Strings>
Expand Down

0 comments on commit 69c1da5

Please sign in to comment.