public static void PreviewTransform(ProjectItem projectItem) { var parent = projectItem.ParentProjectItemOrDefault(); if (parent == null) { VsServices.Instance.ShowMessageBox("Cannot find source config"); return; } var targetFileName = projectItem.Name; var sourceLabel = parent.Name; var sourcePath = parent.GetFullPath(); var targetLabel = targetFileName; // apply transform on file var xmlString = GetTransformString(sourcePath, projectItem.GetFullPath()); // create temp file var tempFilePath = GetTempFilePath(targetFileName); var targetPath = tempFilePath; // write to temp file WriteToFile(tempFilePath, xmlString); VsService.OpenDiff(sourcePath, targetPath, sourceLabel, targetLabel); }
private static XmlTransformableDocument OpenSourceFile(string sourceFile) { try { XmlTransformableDocument transformableDocument = new XmlTransformableDocument(); transformableDocument.PreserveWhitespace = true; transformableDocument.Load(sourceFile); return(transformableDocument); } //catch (XmlException ex) //{ // VsService.OutputLine($"Failed to open {sourceFile}"); // throw; //} catch (Exception e) { VsService.OutputLine($"Failed to open {sourceFile}"); throw; } }
public void ExecuteAction(RecentItemViewModel viewModel) { try { ThreadHelper.ThrowIfNotOnUIThread(); Assumes.Present(VsService); var path = viewModel.Path; var itemType = viewModel.ItemType; switch (itemType) { case RecentItemType.Folder: if (VsService.OpenFolder(path)) { SetLastAccessed(path); SendRefreshMessage(); } ; break; case RecentItemType.Solution: case RecentItemType.CSharpProject: case RecentItemType.VisualBasicProject: if (VsService.OpenProject(path)) { SetLastAccessed(path); SendRefreshMessage(); } break; default: DialogService.ShowMessage($"Unhandled item type:'{itemType}'"); break; } } catch (Exception ex) { DialogService.ShowError(ex); } }
// disclaimer: visual studio doesn't support adding dependent file under linked file // so no dependent transformed config under linked app.config in designer private static void CreateConfigFiles(Project project, ProjectItem projectItem) { var appConfigName = projectItem.Name; var buildConfigurationNames = project.GetBuildConfigurationNames(); // get app.config directory. new transform config will be created there. string path; if (projectItem.IsLink()) { path = Directory.GetParent(projectItem.ContainingProject.FullName).FullName; } else { path = Directory.GetParent(projectItem.GetFullPath()).FullName; } foreach (var buildConfigurationName in buildConfigurationNames) { var dependentConfig = GetTransformConfigName(appConfigName, buildConfigurationName); var dependentConfigFullPath = Path.Combine(path, dependentConfig); // check if config file exist if (FileWrapper.Exists(dependentConfigFullPath)) { VsService.OutputLine($"File {dependentConfig} already exists"); } else { using (var file = FileWrapper.AppendText(dependentConfigFullPath)) { file.Write(DependencyConfigContent); } VsService.OutputLine($"File {dependentConfig} was added"); } // add it to project file anyway, in case it was deleted just from project file projectItem.ProjectItems.AddFromFile(dependentConfigFullPath); } }
//TODO:[Golan] - break this method to small pieces public static bool EditProjectFile(ProjectItem projectItem) { string relativePrefix = null; // get dte from project item var dte = projectItem.DTE; try { // hide UI changes dte.SuppressUI = true; var isLinkAppConfig = projectItem.IsLink(); var project = projectItem.ContainingProject; var createAsLinkedConfig = false; // check if its linked config if (isLinkAppConfig) { // display yes/no message to user. yes - add as lined configs; no - add as concrete configs var result = VsService.ShowMessageBox("Add as linked configs?", OLEMSGBUTTON.OLEMSGBUTTON_YESNO, OLEMSGICON.OLEMSGICON_QUERY); // store relative path relativePrefix = projectItem.GetRelativeDirectory(); if (result == MessageBoxResult.Yes) { createAsLinkedConfig = true; } } if (createAsLinkedConfig) { // since it's link files we only need to copy them as like files to project CreateLinkedConfigFiles(projectItem); } else { // create missing config files CreateConfigFiles(project, projectItem); } // we need to know if we saved the file when displaying message to user var changed = project.IsDirty; // save before making external changes to file if (changed) { project.Save(); } // project file(e.g. c:\myproject\myproject.csproj) var fileName = project.FullName; // config name (e.g. app.config or logging.config) var configName = projectItem.Name; ProjectXmlTransform.Open(fileName); ProjectXmlTransform.AddTransformTask(); if (IsRootAppConfig(configName)) { ProjectXmlTransform.AddAfterCompileTarget(configName, relativePrefix, createAsLinkedConfig); // project is a windows application or console application? if so add click once transform task ProjectXmlTransform.AddAfterPublishTarget(); } else { ProjectXmlTransform.AddAfterBuildTarget(configName, relativePrefix, createAsLinkedConfig); } // save project file var isSaved = ProjectXmlTransform.Save(); // check if need to reload project, remember that we edit the project file externally if (isSaved) { project.SaveReloadProject(); } return(changed || isSaved); } finally { dte.SuppressUI = false; } }