public static async Task InitializeAsync(AsyncPackage package) { // Switch to the main thread - the call to AddCommand in Command1's constructor requires // the UI thread. await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); Dte = await package.GetServiceAsync(typeof(DTE)) as DTE2; Assumes.Present(Dte); if (!(await package.GetServiceAsync(typeof(IMenuCommandService)) is IMenuCommandService commandService)) { return; } if (!(await package.GetServiceAsync(typeof(IStatusBarService)) is IStatusBarService statusBarService)) { return; } if (!(await package.GetServiceAsync(typeof(IPowerShellService)) is IPowerShellService powerShellService)) { return; } if (!(await package.GetServiceAsync(typeof(ILogger)) is ILogger logger)) { return; } Instance = new PowerCleanSolutionCommand(package, commandService, powerShellService, logger); }
public void RunStarted(object automationObject, Dictionary <string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) { _targetPath = replacementsDictionary["$destinationdirectory$"]; if (runKind == WizardRunKind.AsMultiProject) { _dte = (DTE2)automationObject; } }
public T GetOption <T>(string category, string page, string option, T defaultValue) { DTE2?dte = _dte.Value; Assumes.Present(dte); EnvDTE.Properties?properties = dte.Properties[category, page]; if (properties != null) { return((T)properties.Item(option).Value); } return(defaultValue); }
///<summary>Gets the full paths to the currently selected item(s) in the Solution Explorer.</summary> public async Task <IEnumerable <string>?> GetSelectedItemFilePathsAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); DTE2?dte = await VS.GetServiceAsync <SDTE, DTE2>(); var items = (Array)dte.ToolWindows.SolutionExplorer.SelectedItems; List <string> list = new(); foreach (UIHierarchyItem selItem in items) { if (selItem.Object is ProjectItem item && item.Properties != null) { list.Add(item.Properties.Item("FullPath").Value.ToString()); } } return(list); }
/// <summary>Adds one or more files to the project.</summary> public static async Task AddFilesToProjectAsync(this Project project, params string[] files) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); if (project == null || project.IsKind(ProjectTypes.ASPNET_CORE) || project.IsKind(ProjectTypes.DOTNET_CORE) || project.IsKind(ProjectTypes.SSDT)) { return; } DTE2?dte = await VS.GetDTEAsync(); if (project.IsKind(ProjectTypes.WEBSITE)) { Command command = dte.Commands.Item("SolutionExplorer.Refresh"); if (command.IsAvailable) { dte.ExecuteCommand(command.Name); } return; } IVsSolution?solutionService = await VS.GetRequiredServiceAsync <SVsSolution, IVsSolution>(); solutionService.GetProjectOfUniqueName(project.UniqueName, out IVsHierarchy? hierarchy); if (hierarchy == null) { return; } var ip = (IVsProject)hierarchy; var result = new VSADDRESULT[files.Count()]; ip.AddItem(VSConstants.VSITEMID_ROOT, VSADDITEMOPERATION.VSADDITEMOP_LINKTOFILE, string.Empty, (uint)files.Count(), files.ToArray(), IntPtr.Zero, result); }
/// <summary>Gets the active project.</summary> public async Task <Project?> GetActiveProjectAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); DTE2?dte = await VS.GetServiceAsync <SDTE, DTE2>(); try { if (dte.ActiveSolutionProjects is Array projects && projects.Length > 0) { return(projects.GetValue(0) as Project); } } catch (Exception ex) { await ex.LogAsync(); } return(null); }
/// <summary> /// Builds the specified project asynchronously /// </summary> /// <returns>Returns <c>true</c> if the project builds successfully.</returns> public static async Task <bool> BuildAsync(this Project project) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var buildTaskCompletionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); DTE2?dte = await VS.GetDTEAsync(); dte.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone; var configuration = dte.Solution.SolutionBuild.ActiveConfiguration.Name; dte.Solution.SolutionBuild.BuildProject(configuration, project.UniqueName, false); return(await buildTaskCompletionSource.Task); void BuildEvents_OnBuildDone(vsBuildScope scope, vsBuildAction action) { dte.Events.BuildEvents.OnBuildDone -= BuildEvents_OnBuildDone; // Returns 'true' if the number of failed projects == 0 buildTaskCompletionSource.TrySetResult(dte.Solution.SolutionBuild.LastBuildInfo == 0); } }
/// <summary> /// Create a file with the given template file and add it to the parent node. /// </summary> /// <param name="templateFile">The name of the template zip file.</param> /// <param name="path">The path to the file to be created.</param> /// <returns>true if file is added successfully.</returns> public async Task <bool> CreateFileAsync(string templateFile, string path) { Requires.NotNull(templateFile, nameof(templateFile)); Requires.NotNullOrEmpty(path, nameof(path)); string directoryName = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); string?templateLanguage = await GetTemplateLanguageAsync(); if (string.IsNullOrEmpty(templateLanguage)) { return(false); } await _projectVsServices.ThreadingService.SwitchToUIThread(); DTE2?dte = _dte.Value; Assumes.Present(dte); string templateFilePath = ((Solution2)dte.Solution).GetProjectItemTemplate(templateFile, templateLanguage); if (templateFilePath != null) { HierarchyId parentId = _projectVsServices.VsProject.GetHierarchyId(directoryName); var result = new VSADDRESULT[1]; string[] files = new string[] { templateFilePath }; _projectVsServices.VsProject.AddItemWithSpecific(parentId, VSADDITEMOPERATION.VSADDITEMOP_RUNWIZARD, fileName, (uint)files.Length, files, IntPtr.Zero, 0, Guid.Empty, null, Guid.Empty, result); if (result[0] == VSADDRESULT.ADDRESULT_Success) { return(true); } } return(false); }
public void RunStarted(object automationObject, Dictionary <string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) { _targetPath = replacementsDictionary["$destinationdirectory$"]; _projectName = replacementsDictionary["$projectname$"]; ValidateProjectName(); if (runKind == WizardRunKind.AsMultiProject) { _dte = (DTE2)automationObject; } using (DpiAwareness.EnterDpiScope(DpiAwarenessContext.SystemAware)) { using DialogParentWindow owner = new DialogParentWindow(IntPtr.Zero, enableModeless: true, VisualStudioServiceProvider); using UnoOptions targetPlatformWizardPicker = new UnoOptions(VisualStudioServiceProvider); switch (targetPlatformWizardPicker.ShowDialog(owner)) { case DialogResult.OK: _useWebAssembly = targetPlatformWizardPicker.UseWebAssembly; _useiOS = targetPlatformWizardPicker.UseiOS; _useAndroid = targetPlatformWizardPicker.UseAndroid; _useCatalyst = targetPlatformWizardPicker.UseCatalyst; _useAppKit = targetPlatformWizardPicker.UseAppKit; _useGtk = targetPlatformWizardPicker.UseGtk; _useFramebuffer = targetPlatformWizardPicker.UseFramebuffer; _useWpf = targetPlatformWizardPicker.UseWpf; _useWinUI = targetPlatformWizardPicker.UseWinUI; replacementsDictionary["$UseWebAssembly$"] = _useWebAssembly.ToString(); replacementsDictionary["$UseIOS$"] = _useiOS.ToString(); replacementsDictionary["$UseAndroid$"] = _useAndroid.ToString(); replacementsDictionary["$UseCatalyst$"] = _useCatalyst.ToString(); replacementsDictionary["$UseAppKit$"] = _useAppKit.ToString(); replacementsDictionary["$UseGtk$"] = _useGtk.ToString(); replacementsDictionary["$UseFrameBuffer$"] = _useFramebuffer.ToString(); replacementsDictionary["$UseWPF$"] = _useWpf.ToString(); replacementsDictionary["$UseWinUI$"] = _useWinUI.ToString(); replacementsDictionary["$ext_safeprojectname$"] = replacementsDictionary["$safeprojectname$"]; _replacementDictionary = replacementsDictionary.ToDictionary(p => p.Key, p => p.Value); var version = GetVisualStudioReleaseVersion(); if (version < new Version(17, 3) && (_useiOS || _useAndroid || _useCatalyst || _useAppKit)) { MessageBox.Show("iOS, Android, Mac Catalyst, and mac AppKit are only supported starting from Visual Studio 17.3 Preview 1 or later.", "Unable to create the solution"); throw new WizardCancelledException(); } break; case DialogResult.Abort: MessageBox.Show("Aborted" /*targetPlatformWizardPicker.Error*/); throw new WizardCancelledException(); default: throw new WizardBackoutException(); } } }