Skip to content

Commit

Permalink
Activate Solidity Compiler output pane on compile. Use ReadToEndAsync…
Browse files Browse the repository at this point in the history
… to get compiler process output. Log error if can't init VS util services.
  • Loading branch information
allisterb committed Dec 28, 2023
1 parent 7348461 commit d3bce8a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 49 deletions.
49 changes: 23 additions & 26 deletions src/Stratis.VS.StratisEVM/SolidityCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,42 @@ public class SolidityCompiler : Runtime
public static async Task CompileFileAsync(string file)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VSUtil.ShowLogOutputWindowPane(ServiceProvider.GlobalProvider, "Solidity Compiler");
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.Arguments = "/c solc " + file + " --bin";
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
var process = new Process();
process.StartInfo = info;
process.EnableRaisingEvents = true;
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
using (var process = new Process())
{
if (e.Data != null && e.Data.Length > 0)
process.StartInfo = info;
process.EnableRaisingEvents = true;
try
{
VSUtil.LogInfo("Solidity Compiler", e.Data.Trim());
if (!process.Start())
{
VSUtil.LogError("Could not start Solidity Compiler process {process}.", info.FileName + " " + info.Arguments);
return;
}
var stdout = await process.StandardOutput.ReadToEndAsync();
var stderr = await process.StandardError.ReadToEndAsync();
if (stdout != null && stdout.Length > 0)
{
VSUtil.LogInfo("Solidity Compiler", stdout);
}
if (stderr != null && stderr.Length > 0)
{
VSUtil.LogError("Solidity Compiler", stderr);
}
await process.WaitForExitAsync();
}
};
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
{
if (e.Data != null && e.Data.Length > 0)
{
VSUtil.LogError("Solidity Compiler", e.Data.Trim());
}
};
try
{
if (!process.Start())
catch (Exception ex)
{
VSUtil.LogError("Could not start Solidity Compiler process {process}.", info.FileName + " " + info.Arguments);
VSUtil.LogError("Solidity Compiler", ex);
return;
}
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync();
}
catch (Exception ex)
{
VSUtil.LogError("Solidity Compiler", ex);
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,14 @@ public Task<IReadOnlyList<IFileContextAction>> GetActionsAsync(string filePath,
return Task.FromResult<IReadOnlyList<IFileContextAction>>(new IFileContextAction[]
{
// Word count command:
new MyContextAction(
new SolidityFileContextAction(
fileContext,
new Tuple<Guid, uint>(ProviderCommandGroup, StratisEVMPackageIds.Cmd1Id),
"Compile Solidity File" + fileContext.DisplayName,
async (fCtxt, progress, ct) =>
{
await SolidityCompiler.CompileFileAsync(filePath);
}),

// Toggle word count type command:
/*
new MyContextAction(
fileContext,
new Tuple<Guid, uint>(ProviderCommandGroup, StratisEVMPackageIds.Cmd2Id),
"My Action" + fileContext.DisplayName,
async (fCtxt, progress, ct) =>
{
await OutputWindowPaneAsync("command 2");
}),
*/
});
}

Expand Down Expand Up @@ -100,11 +88,11 @@ internal static async Task OutputWindowPaneAsync(string message)
outputPane?.OutputStringThreadSafe(message);
}

internal class MyContextAction : IFileContextAction, IVsCommandItem
internal class SolidityFileContextAction : IFileContextAction, IVsCommandItem
{
private Func<FileContext, IProgress<IFileContextActionProgressUpdate>, CancellationToken, Task> executeAction;

internal MyContextAction(
internal SolidityFileContextAction(
FileContext fileContext,
Tuple<Guid, uint> command,
string displayName,
Expand Down
3 changes: 1 addition & 2 deletions src/Stratis.VS.StratisEVM/SolidityLanguageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ protected bool StartLanguageServerProcess()
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = false;

info.CreateNoWindow = true;
var process = new System.Diagnostics.Process();
process.StartInfo = info;
process.EnableRaisingEvents = true;
Expand Down
14 changes: 8 additions & 6 deletions src/Stratis.VS.StratisEVM/StratisEVMPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,17 @@ public void OnAfterLoadAllDeferredProjects()
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await base.InitializeAsync(cancellationToken, progress);
Runtime.Info("StratisEVM package initialized.");
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

VSUtil.InitializeVSServices(this);
if (VSUtil.InitializeVSServices(ServiceProvider.GlobalProvider))
{
Runtime.Info("StratisEVM package initialized.");
}
else
{
Runtime.Error("Could not initialize StratisEVM package VS services.");
}
// When initialized asynchronously, the current thread may be a background thread at this point.
// Do any initialization that requires the UI thread after switching to the UI thread.
VSUtil.LogInfo("Stratis EVM", "log init");


}
#endregion

Expand Down
24 changes: 24 additions & 0 deletions src/Stratis.VS.StratisEVM/VSUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ public static bool InitializeVSServices(IServiceProvider provider)
return VSServicesInitialized;
}

public static void ShowLogOutputWindowPane(IServiceProvider provider, string pane)
{
ThreadHelper.ThrowIfNotOnUIThread();
IVsWindowFrame windowFrame;
if (uiShell != null)
{
uint flags = (uint)__VSFINDTOOLWIN.FTW_fForceCreate;
uiShell.FindToolWindow(flags, VSConstants.StandardToolWindows.Output, out windowFrame);
windowFrame.Show();
var p = GetLogOutputPane(pane);
if (p != null)
{
p.Activate();
}
else
{
Error("Could not get a reference to the VsUIShell.");
}
}
else
{
Error("Could not get a reference to the VsUIShell.");
}
}
public static bool VSServicesInitialized = false;
}
}
4 changes: 4 additions & 0 deletions tests/solidity/test2/BasicContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ contract BasicContract {
function foo(uint s) private pure {
s = 4;
}

function foo2(string s) {

}
}

0 comments on commit d3bce8a

Please sign in to comment.