/// <summary> /// Creates a new transformation file and adds it to the project. /// </summary> /// <param name="selectedProjectItem">The selected item to be transformed</param> /// <param name="itemName">Full name of the transformation file</param> /// <param name="projectPath">Full path to the current project</param> /// <param name="addDependentUpon">Wheter to add the new file dependent upon the source file</param> private void AddTransformFile(ProjectItem selectedProjectItem, string itemName, string projectPath, bool addDependentUpon) { try { string transformPath = Path.Combine(projectPath, itemName); string sourceFileName = selectedProjectItem.FileNames[1]; ITransformer transformer = TransformerFactory.GetTransformer(sourceFileName, null); transformer.CreateTransformFile(sourceFileName, transformPath, false); // Add the file to the project // If the DependentUpon metadata is required, add it under the original file // If not, add it to the project ProjectItem addedItem = addDependentUpon ? selectedProjectItem.ProjectItems.AddFromFile(transformPath) : selectedProjectItem.ContainingProject.ProjectItems.AddFromFile(transformPath); // We need to set the Build Action to None to ensure that it doesn't get published for web projects addedItem.Properties.Item("ItemType").Value = "None"; IVsHierarchy hierarchy = null; IVsProject vsProject = (IVsProject)hierarchy; IVsBuildPropertyStorage buildPropertyStorage = vsProject as IVsBuildPropertyStorage; if (buildPropertyStorage == null) { this.logger.LogMessage("Error obtaining IVsBuildPropertyStorage from hierarcy."); } } catch (Exception ex) { this.logger.LogMessage("AddTransformFile: Exception> " + ex.Message); } }
/// <summary> /// Creates a new transformation file and adds it to the project. /// </summary> /// <param name="hierarchy">The project hierarchy</param> /// <param name="selectedProjectItem">The selected item to be transformed</param> /// <param name="itemName">Full name of the transformation file</param> /// <param name="projectPath">Full path to the current project</param> /// <param name="addDependentUpon">Wheter to add the new file dependent upon the source file</param> private void AddTransformFile( IVsHierarchy hierarchy, ProjectItem selectedProjectItem, string itemName, string projectPath, bool addDependentUpon) { try { string transformPath = Path.Combine(projectPath, itemName); string sourceFileName = selectedProjectItem.FileNames[1]; ITransformer transformer = TransformerFactory.GetTransformer(sourceFileName, null); transformer.CreateTransformFile(sourceFileName, transformPath, false); // Add the file to the project // If the DependentUpon metadata is required, add it under the original file // If not, add it to the project ProjectItem addedItem = addDependentUpon ? selectedProjectItem.ProjectItems.AddFromFile(transformPath) : selectedProjectItem.ContainingProject.ProjectItems.AddFromFile(transformPath); // We need to set the Build Action to None to ensure that it doesn't get published for web projects addedItem.Properties.Item("ItemType").Value = "None"; IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage; if (buildPropertyStorage == null) { this.logger.LogMessage("Error obtaining IVsBuildPropertyStorage from hierarcy."); } else if (ErrorHandler.Succeeded(hierarchy.ParseCanonicalName(addedItem.FileNames[0], out uint addedItemId))) { buildPropertyStorage.SetItemAttribute(addedItemId, SlowCheetahPackage.IsTransformFile, "true"); if (addDependentUpon) { // Not all projects (like CPS) set the dependent upon metadata when using the automation object buildPropertyStorage.GetItemAttribute(addedItemId, SlowCheetahPackage.DependentUpon, out string dependentUponValue); if (string.IsNullOrEmpty(dependentUponValue)) { // It didm not set it buildPropertyStorage.SetItemAttribute(addedItemId, SlowCheetahPackage.DependentUpon, selectedProjectItem.Name); } } } } catch (Exception ex) { this.logger.LogMessage("AddTransformFile: Exception> " + ex.Message); } }
/// <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); }