github.com/simonferquel/app@v0.6.1-0.20181012141724-68b7cccf26ac/integrations/visualstudio/dockerappvsix/CommandRender.cs (about)

     1  using System;
     2  using System.ComponentModel.Design;
     3  using System.Diagnostics;
     4  using System.Globalization;
     5  using System.IO;
     6  using System.Reflection;
     7  using System.Threading;
     8  using System.Threading.Tasks;
     9  using EnvDTE;
    10  using Microsoft.VisualStudio.Settings;
    11  using Microsoft.VisualStudio.Shell;
    12  using Microsoft.VisualStudio.Shell.Interop;
    13  using Task = System.Threading.Tasks.Task;
    14  
    15  namespace dockerappvsix
    16  {
    17      internal sealed class CommandRender
    18      {
    19          public const int CommandId = 0x0100;
    20          
    21          public static readonly Guid CommandSet = new Guid("0113e9de-ef33-4d36-9c72-75012c5afd35");
    22          
    23          private readonly AsyncPackage _package;
    24          
    25          private CommandRender(AsyncPackage package, OleMenuCommandService commandService)
    26          {
    27              _package = package ?? throw new ArgumentNullException(nameof(package));
    28              commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
    29  
    30              var menuCommandID = new CommandID(CommandSet, CommandId);
    31              var menuItem = new MenuCommand(this.ExecuteAsync, menuCommandID);
    32              commandService.AddCommand(menuItem);
    33          }
    34          
    35          public static CommandRender Instance
    36          {
    37              get;
    38              private set;
    39          }
    40          
    41          private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider
    42          {
    43              get
    44              {
    45                  return this._package;
    46              }
    47          }
    48          
    49          public static async Task InitializeAsync(AsyncPackage package)
    50          {
    51              // Verify the current thread is the UI thread - the call to AddCommand in Command1's constructor requires
    52              // the UI thread.
    53              ThreadHelper.ThrowIfNotOnUIThread();
    54  
    55              OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService;
    56              Instance = new CommandRender(package, commandService);
    57          }
    58          
    59          private async void ExecuteAsync(object sender, EventArgs e)
    60          {
    61              ThreadHelper.ThrowIfNotOnUIThread();
    62              System.Diagnostics.Process proc = new System.Diagnostics.Process();
    63              proc.StartInfo.FileName = "docker-app";
    64              proc.StartInfo.UseShellExecute = false;
    65              proc.StartInfo.RedirectStandardError = true;
    66              proc.StartInfo.RedirectStandardOutput = true;
    67              proc.StartInfo.Arguments = "render";
    68              DTE dte = await this._package.GetServiceAsync(typeof(DTE)) as DTE;
    69              Globals g = dte.Solution.Globals;
    70              var appLocation = g.GetOrNull<string>("dockerapp_applocation");
    71              if (!string.IsNullOrEmpty(appLocation))
    72              {
    73                  proc.StartInfo.Arguments += " " + appLocation;
    74              }
    75              if (dte.Solution.FileName != "")
    76              {
    77                  string wd = Path.GetDirectoryName(dte.Solution.FileName);
    78                  proc.StartInfo.WorkingDirectory = wd;
    79              }
    80              string message;
    81              try
    82              {
    83                  proc.Start();
    84                  proc.WaitForExit();
    85                  string serr = proc.StandardError.ReadToEnd();
    86                  string sout = proc.StandardOutput.ReadToEnd();
    87                  message = serr + sout;
    88              } catch (Exception ex)
    89              {
    90                  message = "Cannot run docker-app: " + ex.ToString();
    91              }
    92              
    93              string title = "Docker Application Render";
    94  
    95              // Show a message box to prove we were here
    96              VsShellUtilities.ShowMessageBox(
    97                  this._package,
    98                  message,
    99                  title,
   100                  OLEMSGICON.OLEMSGICON_INFO,
   101                  OLEMSGBUTTON.OLEMSGBUTTON_OK,
   102                  OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
   103          }
   104      }
   105  }