/// <summary> /// Transforms the template and adds the content with the given name to the specified parent. /// </summary> public IItemContainer Unfold(string name, IItemContainer parent) { Guard.NotNull(() => parent, parent); Guard.NotNullOrEmpty(() => name, name); //var sessionHost = this.templating as ITextTemplatingSessionHost; using (tracer.StartActivity(Resources.TextTemplate_TraceStartUnfold, this.templateFile, parent.GetLogicalPath())) { this.templating.BeginErrorSession(); try { // TODO: add support for parameters. //foreach (var parameter in Parameters) //{ // sessionHost.Session[parameter.Name] = parameter.GetTypedValue(); //} var callback = new TemplateCallback(tracer, templateFile); var content = this.templating.ProcessTemplate(templateFile, templateContent, callback); var targetName = name; if (!Path.HasExtension(targetName)) { targetName = targetName + callback.FileExtension; } // BUGFIX: FBRT does not checkout the file, if SCC. var targetItem = parent.Find <IItem>(targetName).FirstOrDefault(); if (targetItem != null) { targetItem.Checkout(); } using (new TempFileCleaner()) { // HACK: \o/ feature runtime VsFileContentTemplate creates a temp file and // doesn't delete it, so we go FSW to detect it. return(parent.AddContent(content, targetName, true, false, callback.OutputEncoding)); } } finally { this.templating.EndErrorSession(); } } }
/// <summary> /// When overriden by a derived class, changes the default behavior for adding the state file /// to the given parent folder. /// </summary> /// <returns>The added item.</returns> protected virtual IItemContainer AddStateToSolution(IItemContainer targetParent, string targetName, string sourceFile) { Guard.NotNull(() => targetParent, targetParent); Guard.NotNullOrEmpty(() => targetName, targetName); Guard.NotNullOrEmpty(() => sourceFile, sourceFile); tracer.Verbose(Resources.PatternManager_TraceAddingFileToSolution, targetParent.PhysicalPath, targetName); // BUGFIX: FBRT does not checkout the file if it exists in the 'Solution Items' directory, // as the path of the 'Solution Items' folder is the path of the solution. var targetItem = targetParent.Find <IItem>(targetName).FirstOrDefault(); if (targetItem != null) { targetItem.Checkout(); } // This should be doing the right thing, checking out the file, replacing content, etc. return(targetParent.Add(sourceFile, targetName, true, false)); }
public static IItemContainer FindOrCreate(this IItemContainer parent, string pathExpression) { Guard.NotNull(() => parent, parent); // If no path, we mean the parent. if ((string.IsNullOrEmpty(pathExpression)) || (pathExpression.Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase))) { return(parent); } var targetContainer = parent.Find(pathExpression).FirstOrDefault(); if (targetContainer != null) { return(targetContainer); } var paths = pathExpression.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); var pathSeparator = Path.DirectorySeparatorChar.ToString(); for (int targetPathIndex = paths.Length - 1; targetPathIndex >= 0; targetPathIndex--) { var currentPath = string.Join(pathSeparator, paths.Take(targetPathIndex)); // When we have an empty path, we have reached the topmost "path" which is the received parent itself. targetContainer = currentPath.Length > 0 ? parent.Find(currentPath).FirstOrDefault() : parent; if (targetContainer != null) { if (targetContainer.Kind == ItemKind.Project) { return(CreateFolderPath( ((IProject)targetContainer).CreateFolder(paths[targetPathIndex]), string.Join(pathSeparator, paths.Skip(targetPathIndex + 1)))); } else if (targetContainer.Kind == ItemKind.Folder) { return(CreateFolderPath( (IFolder)targetContainer, string.Join(pathSeparator, paths.Skip(targetPathIndex)))); } else if (targetContainer.Kind == ItemKind.Solution) { return(CreateFolderPath( ((ISolution)targetContainer).CreateSolutionFolder(paths[targetPathIndex]), string.Join(pathSeparator, paths.Skip(targetPathIndex + 1)))); } else if (targetContainer.Kind == ItemKind.SolutionFolder) { return(CreateFolderPath( (ISolutionFolder)targetContainer, string.Join(pathSeparator, paths.Skip(targetPathIndex)))); } else { throw new NotSupportedException(string.Format( CultureInfo.CurrentCulture, Resources.SolutionExtensions_InvalidFolderPath, targetContainer.Kind, currentPath, pathExpression)); } } } if (targetContainer == null) { throw new NotSupportedException(string.Format( CultureInfo.CurrentCulture, Resources.SolutionExtensions_CouldNotFindOrCreate, pathExpression)); } return(targetContainer); }
private void SyncGeneratedProjectFiles(IDictionary <string, string> generatedFiles, string sourceFolderPath, IItemContainer targetContainer) { var targetContainerPath = targetContainer.GetLogicalPath(); tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingExcludedFiles, targetContainer.GetLogicalPath()); // Ensure that all (*.MHT) files on the disk are included the targetContainer var targetContainerFiles = Directory.GetFiles(targetContainer.PhysicalPath, GeneratedFileExtensionFilter); foreach (var targetContainerFile in targetContainerFiles) { var projectItem = targetContainer.Find <IItem>(targetContainerFile).FirstOrDefault(); if (projectItem == null) { // Add existing file to project (SCC handles the 'Add' automatically) tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingExcludedFile, targetContainerFile, targetContainerPath); targetContainer.Add(targetContainerFile); } } tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingNewFiles, targetContainer.GetLogicalPath()); // Copy all generated files to project (overwrite existing) foreach (var generatedFile in generatedFiles) { var generatedFileName = generatedFile.Key; var targetContainerItem = targetContainer.Find <IItem>(generatedFileName).FirstOrDefault(); if (targetContainerItem == null) { // Add new file to project (SCC Handles the 'Add' automatically) tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingNewFile, generatedFileName, targetContainerPath); targetContainer.Add(Path.Combine(sourceFolderPath, generatedFileName)); } else { // SCC 'Checkout' existing file (or make writable), and copy file to disk location tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceOverwriteExistingFile, generatedFileName, targetContainerPath); targetContainerItem.Checkout(); File.Copy(Path.Combine(sourceFolderPath, generatedFileName), Path.Combine(targetContainer.PhysicalPath, generatedFileName), true); } } // Remove any non-current items var itemsToDelete = new List <string>(); var targetContainerItems = targetContainer.Items.OfType <IItem>(); foreach (var targetContainerItem in targetContainerItems) { if (!generatedFiles.ContainsKey(targetContainerItem.Name)) { itemsToDelete.Add(targetContainerItem.Name); } else { // Ensure build properties tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceSettingBuildAction, targetContainerItem.Name, targetContainerPath); targetContainerItem.Data.ItemType = BuildAction.Content.ToString(); targetContainerItem.Data.IncludeInVSIX = Boolean.TrueString.ToLowerInvariant(); } } tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceDeleteSuperfluousFiles, targetContainer.GetLogicalPath()); if (itemsToDelete.Any()) { foreach (var item in itemsToDelete) { tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceDeleteSuperfluousFile, item, targetContainerPath); var itemToDelete = targetContainer.Find <IItem>(item).FirstOrDefault(); if (itemToDelete != null) { itemToDelete.Delete(); } } } }
private void SyncGeneratedProjectFiles(IDictionary<string, string> generatedFiles, string sourceFolderPath, IItemContainer targetContainer) { var targetContainerPath = targetContainer.GetLogicalPath(); tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingExcludedFiles, targetContainer.GetLogicalPath()); // Ensure that all (*.MHT) files on the disk are included the targetContainer var targetContainerFiles = Directory.GetFiles(targetContainer.PhysicalPath, GeneratedFileExtensionFilter); foreach (var targetContainerFile in targetContainerFiles) { var projectItem = targetContainer.Find<IItem>(targetContainerFile).FirstOrDefault(); if (projectItem == null) { // Add existing file to project (SCC handles the 'Add' automatically) tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingExcludedFile, targetContainerFile, targetContainerPath); targetContainer.Add(targetContainerFile); } } tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingNewFiles, targetContainer.GetLogicalPath()); // Copy all generated files to project (overwrite existing) foreach (var generatedFile in generatedFiles) { var generatedFileName = generatedFile.Key; var targetContainerItem = targetContainer.Find<IItem>(generatedFileName).FirstOrDefault(); if (targetContainerItem == null) { // Add new file to project (SCC Handles the 'Add' automatically) tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceAddingNewFile, generatedFileName, targetContainerPath); targetContainer.Add(Path.Combine(sourceFolderPath, generatedFileName)); } else { // SCC 'Checkout' existing file (or make writable), and copy file to disk location tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceOverwriteExistingFile, generatedFileName, targetContainerPath); targetContainerItem.Checkout(); File.Copy(Path.Combine(sourceFolderPath, generatedFileName), Path.Combine(targetContainer.PhysicalPath, generatedFileName), true); } } // Remove any non-current items var itemsToDelete = new List<string>(); var targetContainerItems = targetContainer.Items.OfType<IItem>(); foreach (var targetContainerItem in targetContainerItems) { if (!generatedFiles.ContainsKey(targetContainerItem.Name)) { itemsToDelete.Add(targetContainerItem.Name); } else { // Ensure build properties tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceSettingBuildAction, targetContainerItem.Name, targetContainerPath); targetContainerItem.Data.ItemType = BuildAction.Content.ToString(); targetContainerItem.Data.IncludeInVSIX = Boolean.TrueString.ToLowerInvariant(); } } tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceDeleteSuperfluousFiles, targetContainer.GetLogicalPath()); if (itemsToDelete.Any()) { foreach (var item in itemsToDelete) { tracer.Info( Resources.CreateGuidanceDocumentsCommand_TraceDeleteSuperfluousFile, item, targetContainerPath); var itemToDelete = targetContainer.Find<IItem>(item).FirstOrDefault(); if (itemToDelete != null) { itemToDelete.Delete(); } } } }
/// <summary> /// Transforms the template and adds the content with the given name to the specified parent. /// </summary> public IItemContainer Unfold(string name, IItemContainer parent) { Guard.NotNull(() => parent, parent); Guard.NotNullOrEmpty(() => name, name); //var sessionHost = this.templating as ITextTemplatingSessionHost; using (tracer.StartActivity(Resources.TextTemplate_TraceStartUnfold, this.templateFile, parent.GetLogicalPath())) { this.templating.BeginErrorSession(); try { // TODO: add support for parameters. //foreach (var parameter in Parameters) //{ // sessionHost.Session[parameter.Name] = parameter.GetTypedValue(); //} var callback = new TemplateCallback(tracer, templateFile); var content = this.templating.ProcessTemplate(templateFile, templateContent, callback); var targetName = name; if (!Path.HasExtension(targetName)) { targetName = targetName + callback.FileExtension; } // BUGFIX: FBRT does not checkout the file, if SCC. var targetItem = parent.Find<IItem>(targetName).FirstOrDefault(); if (targetItem != null) { targetItem.Checkout(); } using (new TempFileCleaner()) { // HACK: \o/ feature runtime VsFileContentTemplate creates a temp file and // doesn't delete it, so we go FSW to detect it. return parent.AddContent(content, targetName, true, false, callback.OutputEncoding); } } finally { this.templating.EndErrorSession(); } } }