Exemplo n.º 1
0
        /// <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 or not an even older version of SlowCheetah (before NuGet) is installed
            bool isOldScInstalled = IsOldSlowCheetahInstalled(hierarchy as IVsBuildPropertyStorage);

            if (isOldScInstalled)
            {
                this.UpdateSlowCheetah(currentProject);
            }
            else if (!this.IsSlowCheetahInstalled(currentProject))
            {
                // If SlowCheetah is not installed at all
                this.BackgroundInstallSlowCheetah(currentProject);
            }
            else if (!this.IsSlowCheetahUpdated(currentProject))
            {
                // In this case, an older NuGet package is installed,
                // but traces of old SlowCheetah installation were not found
                // This means the user may have manually edited their project file.
                // In this case, show the update information so that they know the proper way to uninstall
                INugetPackageHandler nugetHandler = NugetHandlerFactory.GetHandler(this.package);
                nugetHandler.ShowUpdateInfo();
            }
        }
        /// <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));
        }
        /// <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);
        }
Exemplo n.º 4
0
        /// <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 OnAddTransformCommand(object sender, EventArgs e)
        {
            uint itemid = VSConstants.VSITEMID_NIL;

            IVsHierarchy hierarchy;

            if (!ProjectUtilities.IsSingleProjectItemSelection(out hierarchy, out itemid))
            {
                return;
            }

            IVsProject vsProject = (IVsProject)hierarchy;

            if (!this.ProjectSupportsTransforms(vsProject))
            {
                return;
            }

            string projectFullPath;

            if (ErrorHandler.Failed(vsProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out projectFullPath)))
            {
                return;
            }

            IVsBuildPropertyStorage buildPropertyStorage = vsProject as IVsBuildPropertyStorage;

            if (buildPropertyStorage == null)
            {
                this.LogMessageWriteLineFormat("Error obtaining IVsBuildPropertyStorage from hierarcy.");
                return;
            }

            // get the name of the item
            string itemFullPath;

            if (ErrorHandler.Failed(vsProject.GetMkDocument(itemid, out itemFullPath)))
            {
                return;
            }

            // Save the project file
            IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
            int         hr       = solution.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, hierarchy, 0);

            if (ErrorHandler.Failed(hr))
            {
                throw new COMException(string.Format(Resources.Resources.Error_SavingProjectFile, itemFullPath, this.GetErrorInfo()), 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
                if (buildPropertyStorage != null)
                {
                    buildPropertyStorage.SetItemAttribute(itemid, TransformOnBuild, "true");
                }

                string itemFolder            = Path.GetDirectoryName(itemFullPath);
                string itemFilename          = Path.GetFileNameWithoutExtension(itemFullPath);
                string itemExtension         = Path.GetExtension(itemFullPath);
                string itemFilenameExtension = Path.GetFileName(itemFullPath);

                string content = this.BuildXdtContent(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);
                }

                foreach (string config in transformsToCreate)
                {
                    uint   addedFileId;
                    string itemName = string.Format(Resources.Resources.String_FormatTransformFilename, itemFilename, config, itemExtension);
                    this.AddXdtTransformFile(selectedProjectItem, content, itemName, itemFolder);
                    hierarchy.ParseCanonicalName(Path.Combine(itemFolder, itemName), out addedFileId);
                    buildPropertyStorage.SetItemAttribute(addedFileId, IsTransformFile, "True");
                    buildPropertyStorage.SetItemAttribute(addedFileId, DependentUpon, itemFilenameExtension);
                }
            }
        }
Exemplo n.º 5
0
        /// <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));
        }