/// <summary> /// Checks if the current project supports NuGet /// </summary> /// <param name="hierarchy">Hierarchy of the project to be verified</param> /// <returns>True if the project supports NuGet</returns> /// <remarks>This implementation is derived of the internal NuGet method IsSupported /// https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Utility/EnvDTEProjectUtility.cs#L441 /// This should be removed when NuGet adds this to their public API</remarks> public bool ProjectSupportsNuget(IVsHierarchy hierarchy) { if (hierarchy == null) { throw new ArgumentNullException(nameof(hierarchy)); } if (SupportsINugetProjectSystem(hierarchy)) { return(true); } try { if (hierarchy.IsCapabilityMatch(SupportedCapabilities)) { return(true); } if (hierarchy.IsCapabilityMatch(UnsupportedCapabilities)) { return(false); } } catch (Exception ex) when(!ErrorHandler.IsCriticalException(ex)) { // Catch exceptions when hierarchy doesn't support the above methods } Project project = PackageUtilities.GetAutomationFromHierarchy <Project>(hierarchy, (uint)VSConstants.VSITEMID.Root); return(project.Kind != null && SupportedProjectTypes.Contains(project.Kind)); }
/// <inheritdoc/> protected override void OnInvoke(object sender, EventArgs e) { uint itemId = VSConstants.VSITEMID_NIL; // Verify only one item is selected if (!ProjectUtilities.IsSingleProjectItemSelection(out IVsHierarchy hierarchy, out itemId)) { return; } // Make sure that the project supports transformations IVsProject project = (IVsProject)hierarchy; if (!this.ScPackage.ProjectSupportsTransforms(project)) { return; } // Get the full path of the selected file if (ErrorHandler.Failed(project.GetMkDocument(itemId, out string transformPath))) { return; } // Checks the SlowCheetah NuGet package installation this.ScPackage.JoinableTaskFactory.Run(() => this.NuGetManager.CheckSlowCheetahInstallation(hierarchy)); // Get the parent of the file to start searching for the source file ErrorHandler.ThrowOnFailure(hierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_Parent, out object parentIdObj)); uint parentId = (uint)(int)parentIdObj; if (parentId == (uint)VSConstants.VSITEMID.Nil) { return; } if (!this.TryGetFileToTransform(hierarchy, parentId, Path.GetFileName(transformPath), out uint docId, out string documentPath)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_FileToTransformNotFound, transformPath)); } try { // Save the source and transform files before previewing PackageUtilities.GetAutomationFromHierarchy <ProjectItem>(hierarchy, docId).Save(); PackageUtilities.GetAutomationFromHierarchy <ProjectItem>(hierarchy, itemId).Save(); } catch { // If the item is not open, an exception is thrown, // but that is not a problem as it is not dirty } this.PreviewTransform(hierarchy, documentPath, transformPath); }
/// <summary> /// This function is the callback used to execute a command when the a menu item is clicked. /// See the Initialize method to see how the menu item is associated to this function using /// the OleMenuCommandService service and the MenuCommand class. /// </summary> /// <param name="sender">>The object that fired the event</param> /// <param name="e">Event arguments</param> private void OnPreviewTransformCommand(object sender, EventArgs e) { uint itemId = VSConstants.VSITEMID_NIL; // verify only one item is selected if (!ProjectUtilities.IsSingleProjectItemSelection(out IVsHierarchy hierarchy, out itemId)) { return; } // make sure that the SlowCheetah project support has been added IVsProject project = (IVsProject)hierarchy; if (!this.ProjectSupportsTransforms(project)) { // TODO: should add a dialog here telling the user that the preview failed because the targets are not yet installed return; } // get the full path of the configuration xdt if (ErrorHandler.Failed(project.GetMkDocument(itemId, out string transformPath))) { return; } // Checks the SlowCheetah NuGet package installation this.NuGetManager.CheckSlowCheetahInstallation(hierarchy); ErrorHandler.ThrowOnFailure(hierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_Parent, out object parentIdObj)); uint parentId = (uint)(int)parentIdObj; if (parentId == (uint)VSConstants.VSITEMID.Nil) { return; } if (!this.TryGetFileToTransform(hierarchy, parentId, Path.GetFileName(transformPath), out uint docId, out string documentPath)) { // TO DO: Possibly tell the user that the transform file was not found. return; } try { PackageUtilities.GetAutomationFromHierarchy <ProjectItem>(hierarchy, docId).Save(); PackageUtilities.GetAutomationFromHierarchy <ProjectItem>(hierarchy, itemId).Save(); } catch { // If the item is not open, an exception is thrown, // but that is not a problem as it is not dirty } this.PreviewTransform(hierarchy, documentPath, transformPath); }
/// <summary> /// Checks the SlowCheetah NuGet package on current project. /// If no version is installed, prompts for install of latest version; /// if an older version is detected, shows update information. /// </summary> /// <param name="hierarchy">Hierarchy of the project to be verified</param> public void CheckSlowCheetahInstallation(IVsHierarchy hierarchy) { if (hierarchy == null) { throw new ArgumentNullException(nameof(hierarchy)); } Project currentProject = PackageUtilities.GetAutomationFromHierarchy <Project>(hierarchy, (uint)VSConstants.VSITEMID.Root); // Whether an older version of SlowCheetah is installed // through manual imports in the user's project file bool isOldScInstalled = IsOldSlowCheetahInstalled(hierarchy as IVsBuildPropertyStorage); // Wheter the old NuGet package is installed bool isOldScPackageInstalled = this.IsPackageInstalled(currentProject, OldPackageName); // Wether the newest NuGet package is installed bool isNewScPackageInstalled = this.IsPackageInstalled(currentProject, OldPackageName); IPackageHandler plan = new EmptyHandler(this.package); if (!isNewScPackageInstalled) { // If the new package is not present, it will need to be installed plan = new NugetInstaller(plan); } if (isOldScPackageInstalled) { // If the old package is present, it will need to be uninstalled plan = new NuGetUninstaller(plan); } if (isOldScInstalled) { // If the older targets are installed, they need to be removed // This needs to be done through a wait dialog since the project file will be altered plan = new TargetsUninstaller(plan); plan = new DialogInstallationHandler(plan); } else if (!(plan is EmptyHandler)) { // If there are actions to execute and no targets are found, // perform these actions in the background plan = new BackgroundInstallationHandler(plan) { // If the old package is installed, this is an update operation IsUpdate = isOldScPackageInstalled }; } plan.Execute(currentProject); }
/// <summary> /// Gets all project configurations /// </summary> /// <param name="hierarchy">Current project hierarchy</param> /// <returns>List of configuration names for that project</returns> public static IEnumerable <string> GetProjectConfigurations(IVsHierarchy hierarchy) { Project project = PackageUtilities.GetAutomationFromHierarchy <Project>(hierarchy, (uint)VSConstants.VSITEMID.Root); return(GetProjectConfigurations(project)); }
/// <inheritdoc/> protected override void OnInvoke(object sender, EventArgs e) { uint itemid = VSConstants.VSITEMID_NIL; if (!ProjectUtilities.IsSingleProjectItemSelection(out IVsHierarchy hierarchy, out itemid)) { return; } IVsProject vsProject = (IVsProject)hierarchy; if (!this.package.ProjectSupportsTransforms(vsProject)) { return; } if (ErrorHandler.Failed(vsProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out string projectFullPath))) { return; } IVsBuildPropertyStorage buildPropertyStorage = vsProject as IVsBuildPropertyStorage; if (buildPropertyStorage == null) { this.logger.LogMessage("Error obtaining IVsBuildPropertyStorage from hierarchy."); return; } // get the name of the item if (ErrorHandler.Failed(vsProject.GetMkDocument(itemid, out string itemFullPath))) { return; } // Save the project file IVsSolution solution = (IVsSolution)Shell.Package.GetGlobalService(typeof(SVsSolution)); int hr = solution.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, hierarchy, 0); ErrorHandler.ThrowOnFailure(hr); ProjectItem selectedProjectItem = PackageUtilities.GetAutomationFromHierarchy <ProjectItem>(hierarchy, itemid); if (selectedProjectItem != null) { try { selectedProjectItem.Save(); } catch { // If the item is not open, an exception is thrown, // but that is not a problem as it is not dirty } // Checks the SlowCheetah NuGet package installation this.nuGetManager.CheckSlowCheetahInstallation(hierarchy); // need to enure that this item has metadata TransformOnBuild set to true buildPropertyStorage.SetItemAttribute(itemid, SlowCheetahPackage.TransformOnBuild, "true"); string itemFolder = Path.GetDirectoryName(itemFullPath); string itemFilename = Path.GetFileNameWithoutExtension(itemFullPath); string itemExtension = Path.GetExtension(itemFullPath); string itemFilenameExtension = Path.GetFileName(itemFullPath); IEnumerable <string> configs = ProjectUtilities.GetProjectConfigurations(selectedProjectItem.ContainingProject); List <string> transformsToCreate = null; if (configs != null) { transformsToCreate = configs.ToList(); } if (transformsToCreate == null) { transformsToCreate = new List <string>(); } // if it is a web project we should add publish profile specific transforms as well var publishProfileTransforms = this.GetPublishProfileTransforms(hierarchy, projectFullPath); if (publishProfileTransforms != null) { transformsToCreate.AddRange(publishProfileTransforms); } using (OptionsDialogPage optionsPage = new OptionsDialogPage()) { optionsPage.LoadSettingsFromStorage(); foreach (string config in transformsToCreate) { string itemName = string.Format(CultureInfo.CurrentCulture, Resources.Resources.String_FormatTransformFilename, itemFilename, config, itemExtension); this.AddTransformFile(selectedProjectItem, itemName, itemFolder, optionsPage.AddDependentUpon); hierarchy.ParseCanonicalName(Path.Combine(itemFolder, itemName), out uint addedFileId); buildPropertyStorage.SetItemAttribute(addedFileId, SlowCheetahPackage.IsTransformFile, "True"); } } } }