public void TestTransform01() { string sourceFile = this.WriteTextToTempFile(Consts.Source01); string transformFile = this.WriteTextToTempFile(Consts.Transform01); string expectedResultFile = this.WriteTextToTempFile(Consts.Result01); string destFile = this.GetTempFilename(true); ITransformer transformer = new Transformer(); transformer.Transform(sourceFile, transformFile, destFile); Assert.IsTrue(File.Exists(sourceFile)); Assert.IsTrue(File.Exists(transformFile)); Assert.IsTrue(File.Exists(destFile)); string actualResult = File.ReadAllText(destFile); string expectedResult = File.ReadAllText(expectedResultFile); Assert.AreEqual(expectedResult.Trim(), actualResult.Trim()); }
private void PreviewTransform(IVsHierarchy hier, string sourceFile, string transformFile) { if (string.IsNullOrWhiteSpace(sourceFile)) { throw new ArgumentNullException("sourceFile"); } if (string.IsNullOrWhiteSpace(transformFile)) { throw new ArgumentNullException("transformFile"); } if (!File.Exists(sourceFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Error_SourceFileNotFound, sourceFile), sourceFile); } if (!File.Exists(transformFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Error_TransformFileNotFound, transformFile), transformFile); } // Get our options using (OptionsDialogPage optionsPage = new OptionsDialogPage()) { optionsPage.LoadSettingsFromStorage(); this.LogMessageWriteLineFormat("SlowCheetah PreviewTransform"); FileInfo sourceFileInfo = new FileInfo(sourceFile); // dest file string destFile = this.GetTempFilename(true, sourceFileInfo.Extension); // perform the transform and then display the result into the diffmerge tool that comes with VS. If for // some reason we can't find it, we just open it in an editor window ITransformer transformer = new Transformer(); transformer.Transform(sourceFile, transformFile, destFile); // Does the customer want a preview? if (optionsPage.EnablePreview == false) { GetDTE().ItemOperations.OpenFile(destFile); } else { Guid SID_SVsDifferenceService = new Guid("{77115E75-EF9E-4F30-92F2-3FE78BCAF6CF}"); Guid IID_IVsDifferenceService = new Guid("{E20E53BE-8B7A-408F-AEA7-C0AAD6D1B946}"); uint VSDIFFOPT_RightFileIsTemporary = 0x00000020; //The right file is a temporary file explicitly created for diff. // If the diffmerge service is available (dev11) and no diff tool is specified, or diffmerge.exe is specifed we use the service IOleServiceProvider sp; hier.GetSite(out sp); IntPtr diffSvcIntPtr = IntPtr.Zero; int hr = sp.QueryService(ref SID_SVsDifferenceService, ref IID_IVsDifferenceService, out diffSvcIntPtr); if(diffSvcIntPtr != IntPtr.Zero && (string.IsNullOrEmpty(optionsPage.PreviewToolExecutablePath) || optionsPage.PreviewToolExecutablePath.EndsWith(@"\diffmerge.exe", StringComparison.OrdinalIgnoreCase))) { try { object diffSvc = Marshal.GetObjectForIUnknown(diffSvcIntPtr); Type t = diffSvc.GetType(); Type[] paramTypes = new Type[] {typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(uint)}; MethodInfo openComparisonWindow2 = t.GetMethod("OpenComparisonWindow2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, paramTypes, null); Debug.Assert(openComparisonWindow2 != null); if(openComparisonWindow2 != null) { string sourceName = Path.GetFileName(sourceFile); string leftLabel = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_LeftLabel, sourceName); string rightLabel = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_RightLabel, sourceName, Path.GetFileName(transformFile)); string caption = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_Caption, sourceName); string tooltip = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_ToolTip, sourceName); object[] paras = new object[] {sourceFile, destFile, caption, tooltip, leftLabel, rightLabel, null, null, VSDIFFOPT_RightFileIsTemporary}; openComparisonWindow2.Invoke(diffSvc, paras); } } finally { Marshal.Release(diffSvcIntPtr); } } else if (string.IsNullOrEmpty(optionsPage.PreviewToolExecutablePath)) { throw new FileNotFoundException(Resources.Error_NoPreviewToolSpecified); } else if (!File.Exists(optionsPage.PreviewToolExecutablePath)) { throw new FileNotFoundException(string.Format(Resources.Error_CantFindPreviewTool, optionsPage.PreviewToolExecutablePath), optionsPage.PreviewToolExecutablePath); } else { // Quote the filenames... ProcessStartInfo psi = new ProcessStartInfo(optionsPage.PreviewToolExecutablePath, string.Format(optionsPage.PreviewToolCommandLine, "\"" + sourceFile + "\"", "\"" + destFile + "\"")); psi.CreateNoWindow = true; psi.UseShellExecute = false; Process.Start(psi); } } } // TODO: Instead of creating a file and then deleting it later we could instead do this // http://matthewmanela.com/blog/the-problem-with-the-envdte-itemoperations-newfile-method/ // http://social.msdn.microsoft.com/Forums/en/vsx/thread/eb032063-eb4d-42e0-84e8-dec64bf42abf }
private void PreviewTransform(IVsHierarchy hier, string sourceFile, string transformFile) { if (string.IsNullOrWhiteSpace(sourceFile)) { throw new ArgumentNullException("sourceFile"); } if (string.IsNullOrWhiteSpace(transformFile)) { throw new ArgumentNullException("transformFile"); } if (!File.Exists(sourceFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Error_SourceFileNotFound, sourceFile), sourceFile); } if (!File.Exists(transformFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Error_TransformFileNotFound, transformFile), transformFile); } // Get our options using (OptionsDialogPage optionsPage = new OptionsDialogPage()) { optionsPage.LoadSettingsFromStorage(); this.LogMessageWriteLineFormat("SlowCheetah PreviewTransform"); FileInfo sourceFileInfo = new FileInfo(sourceFile); // dest file IVsProject vsProject = (IVsProject)hier; string projectFullPath; if (ErrorHandler.Failed(vsProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out projectFullPath))) { projectFullPath = null; } // Determine if the transformFile is a publish profile transform and if it is then see if a configuration transform exists string configTransformFile = null; if (!string.IsNullOrWhiteSpace(projectFullPath)) { List<string> publishProfiles = this.GetPublishProfileTransforms(hier, projectFullPath); FileInfo transformFileInfo = new FileInfo(transformFile); string transformName = transformFileInfo.Name; string transformExt = transformFileInfo.Extension; foreach (string publishProfile in publishProfiles) { int dotPos = publishProfile.IndexOf(".", StringComparison.Ordinal); string prefixedProfile = (dotPos < 0) ? "." + publishProfile : publishProfile.Substring(dotPos); int profilePos = transformName.IndexOf(prefixedProfile + transformExt, StringComparison.OrdinalIgnoreCase); if (profilePos >= 0) { string configTransformName = transformName.Remove(profilePos, prefixedProfile.Length); string configTransformPath = transformFileInfo.DirectoryName; int found; uint itemId; VSDOCUMENTPRIORITY[] priority = new VSDOCUMENTPRIORITY[1]; int success = vsProject.IsDocumentInProject(configTransformName, out found, priority, out itemId); if ((success == VSConstants.S_OK) && (found != 0)) { var configTransformTemp = !string.IsNullOrWhiteSpace(configTransformPath) ? Path.Combine(configTransformPath, configTransformName) : configTransformName; if (File.Exists(configTransformTemp)) { configTransformFile = configTransformTemp; break; } } } } } string destFile = this.GetTempFilename(true, sourceFileInfo.Extension); // Perform the appropriate transforms if (!string.IsNullOrWhiteSpace(configTransformFile)) { ITransformer transformer = new Transformer(); transformer.Transform(sourceFile, configTransformFile, destFile); transformer.Transform(destFile, transformFile, destFile); } else { ITransformer transformer = new Transformer(); transformer.Transform(sourceFile, transformFile, destFile); } // Does the customer want a preview? if (optionsPage.EnablePreview == false) { GetDTE().ItemOperations.OpenFile(destFile); } else { // Display the transform result into the diffmerge tool that comes with VS. If for // some reason we can't find it, we just open it in an editor window Guid SID_SVsDifferenceService = new Guid("{77115E75-EF9E-4F30-92F2-3FE78BCAF6CF}"); Guid IID_IVsDifferenceService = new Guid("{E20E53BE-8B7A-408F-AEA7-C0AAD6D1B946}"); uint VSDIFFOPT_RightFileIsTemporary = 0x00000020; //The right file is a temporary file explicitly created for diff. // If the diffmerge service is available (dev11) and no diff tool is specified, or diffmerge.exe is specifed we use the service IOleServiceProvider sp; hier.GetSite(out sp); IntPtr diffSvcIntPtr = IntPtr.Zero; int hr = sp.QueryService(ref SID_SVsDifferenceService, ref IID_IVsDifferenceService, out diffSvcIntPtr); if(diffSvcIntPtr != IntPtr.Zero && (string.IsNullOrEmpty(optionsPage.PreviewToolExecutablePath) || optionsPage.PreviewToolExecutablePath.EndsWith(@"\diffmerge.exe", StringComparison.OrdinalIgnoreCase))) { try { object diffSvc = Marshal.GetObjectForIUnknown(diffSvcIntPtr); Type t = diffSvc.GetType(); Type[] paramTypes = new Type[] {typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(uint)}; MethodInfo openComparisonWindow2 = t.GetMethod("OpenComparisonWindow2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, paramTypes, null); Debug.Assert(openComparisonWindow2 != null); if(openComparisonWindow2 != null) { var transformLabel = !string.IsNullOrWhiteSpace(configTransformFile) ? string.Format("{0}, {1}", Path.GetFileName(configTransformFile), Path.GetFileName(transformFile)) : Path.GetFileName(transformFile); string sourceName = Path.GetFileName(sourceFile); string leftLabel = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_LeftLabel, sourceName); string rightLabel = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_RightLabel, sourceName, transformLabel); string caption = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_Caption, sourceName); string tooltip = string.Format(CultureInfo.CurrentCulture, Resources.TransformPreview_ToolTip, sourceName); object[] paras = new object[] {sourceFile, destFile, caption, tooltip, leftLabel, rightLabel, null, null, VSDIFFOPT_RightFileIsTemporary}; openComparisonWindow2.Invoke(diffSvc, paras); } } finally { Marshal.Release(diffSvcIntPtr); } } else if (string.IsNullOrEmpty(optionsPage.PreviewToolExecutablePath)) { throw new FileNotFoundException(Resources.Error_NoPreviewToolSpecified); } else if (!File.Exists(optionsPage.PreviewToolExecutablePath)) { throw new FileNotFoundException(string.Format(Resources.Error_CantFindPreviewTool, optionsPage.PreviewToolExecutablePath), optionsPage.PreviewToolExecutablePath); } else { // Quote the filenames... ProcessStartInfo psi = new ProcessStartInfo(optionsPage.PreviewToolExecutablePath, string.Format(optionsPage.PreviewToolCommandLine, "\"" + sourceFile + "\"", "\"" + destFile + "\"")); psi.CreateNoWindow = true; psi.UseShellExecute = false; Process.Start(psi); } } } // TODO: Instead of creating a file and then deleting it later we could instead do this // http://matthewmanela.com/blog/the-problem-with-the-envdte-itemoperations-newfile-method/ // http://social.msdn.microsoft.com/Forums/en/vsx/thread/eb032063-eb4d-42e0-84e8-dec64bf42abf }