/// <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 MenuItemCallback(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); var dte = CakePackage.Dte; if (dte == null) { return; } if (dte.Solution == null || dte.Solution.Count == 0) { ServiceProvider.ShowMessageBox("No solution opened"); } else { if (MenuHelpers.DownloadFileToProject(Constants.DotNetFrameworkPowerShellUri, "build.ps1")) { VsShellUtilities.LogMessage(Constants.PackageName, ".NET Framework PowerShell bootstrapper installed into solution", __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION); ServiceProvider.ShowMessageBox(".NET Framework PowerShell bootstrapper script successfully downloaded."); } } // Show a message box to prove we were here }
internal static bool DownloadFileToProject(string downloadPath, string targetFileName) { var dte = CakePackage.Dte; return(DownloadFileToProject(downloadPath, targetFileName, targetPath => { var solItems = ProjectHelpers.GetSolutionItemsProject(dte); solItems?.AddFileToProject(targetPath); if (solItems != null) { VsShellUtilities.LogMessage(Constants.PackageName, $"New file added to Solution Items for solution {dte.Solution.FullName}", __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION); } })); }
/// <summary> /// Gets the details of the currently active connection /// </summary> /// <returns></returns> protected SqlConnectionStringBuilder GetConnectionInfo(bool log) { var scriptFactory = new ScriptFactoryWrapper(ServiceCache.ScriptFactory); var sqlScriptEditorControl = scriptFactory.GetCurrentlyActiveFrameDocView(ServiceCache.VSMonitorSelection, false, out _); if (sqlScriptEditorControl?.ConnectionString == null) { if (log) { VsShellUtilities.LogMessage("SQL 4 CDS", "No currently active window or connection", Microsoft.VisualStudio.Shell.Interop.__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION); } return(null); } return(new SqlConnectionStringBuilder(sqlScriptEditorControl.ConnectionString)); }
internal static bool DownloadFileToProject(string downloadPath, string targetFileName, Action <string> installCallback) { var dte = CakePackage.Dte; try { var slnFilePath = new FileInfo(dte.Solution.FullName).FullName; var cakeScriptPath = dte.Solution.FindProjectItem(Constants.ScriptFileName)?.FileNames[1]; var targetPath = Path.Combine(new FileInfo(string.IsNullOrWhiteSpace(cakeScriptPath) ? slnFilePath : cakeScriptPath).Directory.FullName, targetFileName); bool confirm = true; if (File.Exists(targetPath)) { confirm = VsShellUtilities.PromptYesNo("File already exists. Overwrite?", $"Downloading {targetFileName}", OLEMSGICON.OLEMSGICON_QUERY, CakePackage.Shell); } if (!confirm) { return(true); } try { ProjectHelpers.CheckFileOutOfSourceControl(targetPath); } catch (Exception) { // ignored } using (var wc = new WebClient()) { wc.DownloadFile(downloadPath, targetPath); VsShellUtilities.LogMessage(Constants.PackageName, $"File downloaded from '{downloadPath}' to '{targetPath}'", __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION); installCallback.Invoke(targetPath); } return(true); } catch { VsShellUtilities.LogError(Constants.PackageName, $"There was an error downloading the requested file: '{downloadPath}'"); return(false); } }
/// <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 MenuItemCallback(object sender, EventArgs e) { var dte = CakePackage.Dte; if (dte == null) { return; } if (dte.Solution == null || dte.Solution.Count == 0) { ServiceProvider.ShowMessageBox("No solution opened"); } else { if (MenuHelpers.DownloadFileToProject(Constants.ConfigTemplatePath, Constants.ConfigFileName, MenuHelpers.ProjectInstallCommand)) { VsShellUtilities.LogMessage(Constants.PackageName, "Cake configuration file installed into solution", __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION); ServiceProvider.ShowMessageBox("Cake configuration file successfully downloaded."); } } }
internal static Project GetSolutionItemsProject(DTE2 dte) { var solItems = dte.Solution.Projects.Cast <Project>().FirstOrDefault(p => p.Name == "Solution Items" || p.Kind == EnvDTE.Constants.vsProjectItemKindSolutionItems); var projs = dte.Solution.Projects.Cast <Project>().ToList(); if (solItems == null) { try { var sol2 = (Solution2)dte.Solution; solItems = sol2.AddSolutionFolder("Solution Items"); VsShellUtilities.LogMessage(Constants.PackageName, $"Created Solution Items project for solution {dte.Solution.FullName}", __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION); } catch { // ignored } } return(solItems); }