github.com/simonferquel/app@v0.6.1-0.20181012141724-68b7cccf26ac/integrations/visualstudio/dockerappvsix/CommandNew.cs (about) 1 using System; 2 using System.ComponentModel.Design; 3 using System.Globalization; 4 using System.IO; 5 using System.Linq; 6 using System.Text; 7 using System.Threading; 8 using System.Threading.Tasks; 9 using System.Windows.Controls; 10 using EnvDTE; 11 using EnvDTE80; 12 using Microsoft.VisualStudio.PlatformUI; 13 using Microsoft.VisualStudio.Shell; 14 using Microsoft.VisualStudio.Shell.Interop; 15 using Task = System.Threading.Tasks.Task; 16 17 namespace dockerappvsix 18 { 19 internal sealed class CommandNew 20 { 21 public const int CommandId = 4132; 22 23 public static readonly Guid CommandSet = new Guid("0113e9de-ef33-4d36-9c72-75012c5afd35"); 24 25 private readonly AsyncPackage _package; 26 27 private CommandNew(AsyncPackage package, OleMenuCommandService commandService) 28 { 29 _package = package ?? throw new ArgumentNullException(nameof(package)); 30 commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); 31 32 var menuCommandID = new CommandID(CommandSet, CommandId); 33 var menuItem = new MenuCommand(this.ExecuteAsync, menuCommandID); 34 commandService.AddCommand(menuItem); 35 } 36 37 public static CommandNew Instance 38 { 39 get; 40 private set; 41 } 42 43 private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider 44 { 45 get 46 { 47 return this._package; 48 } 49 } 50 51 public static async Task InitializeAsync(AsyncPackage package) 52 { 53 ThreadHelper.ThrowIfNotOnUIThread(); 54 55 OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService; 56 Instance = new CommandNew(package, commandService); 57 } 58 59 private async void ExecuteAsync(object sender, EventArgs e) 60 { 61 ThreadHelper.ThrowIfNotOnUIThread(); 62 NewAppDialog ns = new NewAppDialog(); 63 if (!(ns.ShowDialog() ?? false)) { 64 return; 65 } 66 var s = ns.Settings; 67 var argsBuilder = new StringBuilder("init " + s.Name.QuoteAndStripCarriageReturns()); 68 if (s.Description != "") 69 argsBuilder.Append($" --description {s.Description.QuoteAndStripCarriageReturns()}"); 70 if (s.Maintainers != "") 71 { 72 foreach (string m in s.Maintainers?.Split('\n')??Enumerable.Empty<string>()) { 73 argsBuilder.Append($" --maintainer {m.QuoteAndStripCarriageReturns()}"); 74 } 75 } 76 if (s.SingleFile) 77 argsBuilder.Append(" -s"); 78 System.Diagnostics.Process proc = new System.Diagnostics.Process(); 79 proc.StartInfo.FileName = "docker-app"; 80 proc.StartInfo.UseShellExecute = false; 81 proc.StartInfo.RedirectStandardError = true; 82 proc.StartInfo.RedirectStandardOutput = true; 83 proc.StartInfo.Arguments = argsBuilder.ToString(); 84 DTE dte = await this._package.GetServiceAsync(typeof(DTE)) as DTE; 85 var solutionDir = ""; 86 if (dte.Solution.FileName != "") 87 { 88 solutionDir = Path.GetDirectoryName(dte.Solution.FileName); 89 proc.StartInfo.WorkingDirectory = solutionDir; 90 } 91 string message; 92 proc.Start(); 93 proc.WaitForExit(); 94 string serr = proc.StandardError.ReadToEnd(); 95 string sout = proc.StandardOutput.ReadToEnd(); 96 message = serr + sout; 97 if (proc.ExitCode != 0) 98 message = "Error creating application:" + System.Environment.NewLine + message; 99 else 100 message = "Application created!" + System.Environment.NewLine + message; 101 string title = "Create Application"; 102 // Show a message box to prove we were here 103 VsShellUtilities.ShowMessageBox( 104 this._package, 105 message, 106 title, 107 OLEMSGICON.OLEMSGICON_INFO, 108 OLEMSGBUTTON.OLEMSGBUTTON_OK, 109 OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); 110 var sol2 = (EnvDTE80.Solution2)dte.Solution; 111 112 var solutionItems = FindDockerFolderProject(sol2); 113 var path = Path.Combine(solutionDir, s.Name + ".dockerapp"); 114 if (Directory.Exists(path)) { 115 var sf = (SolutionFolder)solutionItems.Object; 116 var f = sf.AddSolutionFolder(s.Name+".dockerapp"); 117 foreach(var file in Directory.GetFiles(path)) { 118 f.ProjectItems.AddFromFile(file); 119 } 120 } else if (File.Exists(path)) { 121 solutionItems.ProjectItems.AddFromFile(path); 122 } 123 } 124 Project FindDockerFolderProject(EnvDTE80.Solution2 s) 125 { 126 foreach(Project p in s.Projects) { 127 if (p.Globals.VariableExists["docker-app-solution-folder"]) { 128 return p; 129 } 130 } 131 var proj = s.AddSolutionFolder("Docker"); 132 proj.Globals["docker-app-solution-folder"] = true; 133 proj.Globals.VariablePersists["docker-app-solution-folder"] = true; 134 return proj; 135 } 136 } 137 }