Example #1
0
        /// <summary>
        /// Searches for a file to transform based on a transformation file.
        /// Starts the search with the parent of the file then checks all visible children.
        /// </summary>
        /// <param name="hierarchy">Current project hierarchy</param>
        /// <param name="parentId">Parent ID of the file.</param>
        /// <param name="transformName">Name of the transformation file</param>
        /// <param name="docId">ID of the file to transform</param>
        /// <param name="documentPath">Resulting path of the file to transform</param>
        /// <returns>True if the correct file was found</returns>
        private bool TryGetFileToTransform(IVsHierarchy hierarchy, uint parentId, string transformName, out uint docId, out string documentPath)
        {
            IVsProject project = (IVsProject)hierarchy;

            IEnumerable <string> configs = ProjectUtilities.GetProjectConfigurations(hierarchy);

            if (ErrorHandler.Failed(project.GetMkDocument(parentId, out documentPath)))
            {
                docId = 0;
                return(false);
            }

            if (PackageUtilities.IsFileTransform(Path.GetFileName(documentPath), transformName, configs))
            {
                docId = parentId;
                return(true);
            }
            else
            {
                hierarchy.GetProperty(parentId, (int)__VSHPROPID.VSHPROPID_FirstVisibleChild, out object childIdObj);
                docId = (uint)(int)childIdObj;
                if (ErrorHandler.Failed(project.GetMkDocument(docId, out documentPath)))
                {
                    docId        = 0;
                    documentPath = null;
                    return(false);
                }

                if (PackageUtilities.IsFileTransform(Path.GetFileName(documentPath), transformName, configs))
                {
                    return(true);
                }
                else
                {
                    while (docId != VSConstants.VSITEMID_NIL)
                    {
                        hierarchy.GetProperty(docId, (int)__VSHPROPID.VSHPROPID_NextVisibleSibling, out childIdObj);
                        docId = (uint)(int)childIdObj;
                        if (ErrorHandler.Succeeded(project.GetMkDocument(docId, out documentPath)))
                        {
                            if (PackageUtilities.IsFileTransform(Path.GetFileName(documentPath), transformName, configs))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }

            docId        = 0;
            documentPath = null;
            return(false);
        }
        /// <summary>
        /// Verifies if the item has a trasform configured already
        /// </summary>
        /// <param name="vsProject">The current project</param>
        /// <param name="itemid">The id of the selected item inside the project</param>
        /// <returns>True if the item has a transform</returns>
        public bool IsItemTransformItem(IVsProject vsProject, uint itemid)
        {
            IVsBuildPropertyStorage buildPropertyStorage = vsProject as IVsBuildPropertyStorage;

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

            buildPropertyStorage.GetItemAttribute(itemid, IsTransformFile, out string value);
            if (bool.TryParse(value, out bool valueAsBool) && valueAsBool)
            {
                return(true);
            }

            // we need to special case web.config transform files
            buildPropertyStorage.GetItemAttribute(itemid, "FullPath", out string filePath);
            IEnumerable <string> configs = ProjectUtilities.GetProjectConfigurations(vsProject as IVsHierarchy);

            // If the project is a web app, check for the Web.config files added by default
            return(ProjectUtilities.IsProjectWebApp(vsProject) && PackageUtilities.IsFileTransformForBuildConfiguration("web.config", Path.GetFileName(filePath), configs));
        }
        /// <summary>
        /// Shows a preview of the transformation in a temporary file.
        /// </summary>
        /// <param name="hier">Current IVsHierarchy</param>
        /// <param name="sourceFile">Full path to the file to be transformed</param>
        /// <param name="transformFile">Full path to the transformation file</param>
        private void PreviewTransform(IVsHierarchy hier, string sourceFile, string transformFile)
        {
            if (string.IsNullOrWhiteSpace(sourceFile))
            {
                throw new ArgumentNullException(nameof(sourceFile));
            }

            if (string.IsNullOrWhiteSpace(transformFile))
            {
                throw new ArgumentNullException(nameof(transformFile));
            }

            if (!File.Exists(sourceFile))
            {
                throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_SourceFileNotFound, sourceFile), sourceFile);
            }

            if (!File.Exists(transformFile))
            {
                throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_TransformFileNotFound, transformFile), transformFile);
            }

            // Get our options
            using (OptionsDialogPage optionsPage = new OptionsDialogPage())
                using (AdvancedOptionsDialogPage advancedOptionsPage = new AdvancedOptionsDialogPage())
                {
                    optionsPage.LoadSettingsFromStorage();
                    advancedOptionsPage.LoadSettingsFromStorage();

                    this.Logger.LogMessage("SlowCheetah PreviewTransform");
                    FileInfo sourceFileInfo = new FileInfo(sourceFile);

                    // Destination file
                    // This should be kept as a temp file in case a custom diff tool is being used
                    string destFile = PackageUtilities.GetTempFilename(true, sourceFileInfo.Extension);
                    this.TempFilesCreated.Add(destFile);

                    // Perform the transform and then display the result into the diffmerge tool that comes with VS.
                    this.ErrorListProvider.Tasks.Clear();
                    ITransformationLogger logger      = new TransformationPreviewLogger(this.ErrorListProvider, hier);
                    ITransformer          transformer = TransformerFactory.GetTransformer(sourceFile, logger);
                    if (!transformer.Transform(sourceFile, transformFile, destFile))
                    {
                        throw new TransformFailedException(Resources.Resources.TransformPreview_ErrorMessage);
                    }

                    // Does the customer want a preview? If not, just open an editor window
                    if (optionsPage.EnablePreview == false)
                    {
                        ProjectUtilities.GetDTE().ItemOperations.OpenFile(destFile);
                    }
                    else
                    {
                        // If the diffmerge service is available and no diff tool is specified, or diffmerge.exe is specifed we use the service
                        if (((IServiceProvider)this.ScPackage).GetService(typeof(SVsDifferenceService)) is IVsDifferenceService diffService && (!File.Exists(advancedOptionsPage.PreviewToolExecutablePath) || advancedOptionsPage.PreviewToolExecutablePath.EndsWith("diffmerge.exe", StringComparison.OrdinalIgnoreCase)))
                        {
                            if (!string.IsNullOrEmpty(advancedOptionsPage.PreviewToolExecutablePath) && !File.Exists(advancedOptionsPage.PreviewToolExecutablePath))
                            {
                                // If the user specified a preview tool, but it doesn't exist, log a warning
                                logger.LogWarning(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_CantFindPreviewTool, advancedOptionsPage.PreviewToolExecutablePath));
                            }

                            // Write all the labels for the diff tool
                            string sourceName = Path.GetFileName(sourceFile);
                            string leftLabel  = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_LeftLabel, sourceName);
                            string rightLabel = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_RightLabel, sourceName, Path.GetFileName(transformFile));
                            string caption    = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_Caption, sourceName);
                            string tooltip    = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_ToolTip, sourceName);

                            diffService.OpenComparisonWindow2(sourceFile, destFile, caption, tooltip, leftLabel, rightLabel, null, null, (uint)__VSDIFFSERVICEOPTIONS.VSDIFFOPT_RightFileIsTemporary);
                        }
Example #4
0
        /// <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");
                    }
                }
            }
        }