github.com/ijc/docker-app@v0.6.1-0.20181012090447-c7ca8bc483fb/integrations/visualstudio/dockerappvsix/CommandDeploy.cs (about)

     1  using System;
     2  using System.ComponentModel.Design;
     3  using System.Globalization;
     4  using System.IO;
     5  using System.Text;
     6  using System.Threading;
     7  using System.Threading.Tasks;
     8  using EnvDTE;
     9  using Microsoft.VisualStudio;
    10  using Microsoft.VisualStudio.Shell;
    11  using Microsoft.VisualStudio.Shell.Interop;
    12  using Task = System.Threading.Tasks.Task;
    13  
    14  namespace dockerappvsix
    15  {
    16      internal sealed class CommandDeploy
    17      {
    18          public const int CommandId = 4131;
    19          
    20          public static readonly Guid CommandSet = new Guid("0113e9de-ef33-4d36-9c72-75012c5afd35");
    21          
    22          private readonly AsyncPackage _package;
    23  
    24          private CommandDeploy(AsyncPackage package, OleMenuCommandService commandService)
    25          {
    26              _package = package ?? throw new ArgumentNullException(nameof(package));
    27              commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
    28  
    29              var menuCommandID = new CommandID(CommandSet, CommandId);
    30              var menuItem = new MenuCommand(this.ExecuteAsync, menuCommandID);
    31              commandService.AddCommand(menuItem);
    32          }
    33          
    34          public static CommandDeploy Instance
    35          {
    36              get;
    37              private set;
    38          }
    39          
    40          private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider
    41          {
    42              get
    43              {
    44                  return this._package;
    45              }
    46          }
    47          
    48          public static async Task InitializeAsync(AsyncPackage package)
    49          {
    50              ThreadHelper.ThrowIfNotOnUIThread();
    51  
    52              OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService;
    53              Instance = new CommandDeploy(package, commandService);
    54          }
    55  
    56          private void AddArgIfExists(Globals g, string key, string flag, StringBuilder cmd)
    57          {
    58              var value = g.GetOrNull<string>(key);
    59              if (string.IsNullOrEmpty(value)) {
    60                  return;
    61              }
    62              
    63              if (!string.IsNullOrEmpty(flag)) {
    64                  cmd.Append($" {flag}");
    65              }
    66              cmd.Append($" {value}");
    67          }
    68          private async void ExecuteAsync(object sender, EventArgs e)
    69          {
    70              ThreadHelper.ThrowIfNotOnUIThread();
    71              DTE dte = await this._package.GetServiceAsync(typeof(DTE)) as DTE;
    72              Globals g = dte.Solution.Globals;
    73              var argsBuilder = new StringBuilder("deploy");
    74              AddArgIfExists(g, "dockerapp_applocation", null, argsBuilder);
    75              AddArgIfExists(g, "dockerapp_orchestrator", "--orchestrator", argsBuilder);
    76              AddArgIfExists(g, "dockerapp_stackname", "--name", argsBuilder);
    77              AddArgIfExists(g, "dockerapp_namespace", "--namespace", argsBuilder);
    78              AddArgIfExists(g, "dockerapp_kubeconfig", "--kubeconfig", argsBuilder);
    79              var settings = g.GetOrNull<string>("dockerapp_settings");
    80  
    81              if (settings !=null)
    82              {
    83                  foreach (string s in (settings).Split('\n')) {
    84                      argsBuilder.Append($" -s {s}");
    85                  }
    86              }
    87              System.Diagnostics.Process proc = new System.Diagnostics.Process();
    88              proc.StartInfo.FileName = "docker-app";
    89              proc.StartInfo.UseShellExecute = false;
    90              proc.StartInfo.RedirectStandardError = true;
    91              proc.StartInfo.RedirectStandardOutput = true;
    92              proc.StartInfo.Arguments = argsBuilder.ToString();
    93  
    94              if (dte.Solution.FileName != "")
    95              {
    96                  string wd = Path.GetDirectoryName(dte.Solution.FileName);
    97                  proc.StartInfo.WorkingDirectory = wd;
    98              }
    99              proc.Start();
   100              IVsOutputWindow outWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;
   101  
   102              Guid generalPaneGuid = VSConstants.GUID_OutWindowDebugPane; //  GUID_OutWindowGeneralPane fails on vs2017
   103              IVsOutputWindowPane generalPane;
   104              outWindow.GetPane(ref generalPaneGuid, out generalPane);
   105  
   106              generalPane.OutputString("Deploy command: docker-app " + argsBuilder.ToString() + System.Environment.NewLine);
   107              generalPane.Activate(); // Brings this pane into view
   108              while (!proc.StandardOutput.EndOfStream)
   109                  generalPane.OutputString(proc.StandardOutput.ReadLine() + System.Environment.NewLine);
   110              while (!proc.StandardError.EndOfStream)
   111                  generalPane.OutputString(proc.StandardError.ReadLine() + System.Environment.NewLine);
   112          }
   113      }
   114  }