private static void AddTargetAssets(IUnitTestGeneratorOptions options, KeyValuePair <Project, Tuple <HashSet <TargetAsset>, HashSet <IReferencedAssembly> > > pair) { ThreadHelper.ThrowIfNotOnUIThread(); foreach (var targetAsset in pair.Value.Item1) { var asset = AssetFactory.Create(targetAsset); if (asset != null) { var targetProjectFileName = pair.Key.FileName; var targetProjectPath = Path.GetDirectoryName(targetProjectFileName); if (string.IsNullOrWhiteSpace(targetProjectPath)) { continue; } #pragma warning disable VSTHRD010 if (!pair.Key.ProjectItems.OfType <ProjectItem>().Any(x => string.Equals(x.Name, asset.AssetFileName, StringComparison.OrdinalIgnoreCase))) #pragma warning restore VSTHRD010 { var nameSpace = VsProjectHelper.GetProjectRootNamespace(pair.Key); var fileName = Path.Combine(targetProjectPath, asset.AssetFileName); File.WriteAllText(fileName, asset.Content(nameSpace, options.GenerationOptions.FrameworkType)); pair.Key.ProjectItems.AddFromFile(fileName); } } } }
public static FindTargetStatus FindExistingTargetItem(ProjectItemModel source, IGenerationOptions options, out ProjectItem targetItem) { ThreadHelper.ThrowIfNotOnUIThread(); #pragma warning disable VSTHRD010 var nameParts = VsProjectHelper.GetNameParts(source.Item); targetItem = null; var targetProject = source.TargetProject; if (targetProject == null) { return(FindTargetStatus.ProjectNotFound); } var targetProjectItems = FindTargetFolder(targetProject, nameParts, false, out _); if (targetProjectItems == null) { return(FindTargetStatus.FolderNotFound); } var extension = Path.GetExtension(source.FilePath); var baseName = Path.GetFileNameWithoutExtension(source.FilePath); var testFileName = options.GetTargetFileName(baseName); targetItem = targetProjectItems.OfType <ProjectItem>().FirstOrDefault(x => string.Equals(x.Name, testFileName + extension, StringComparison.OrdinalIgnoreCase)); return(targetItem == null ? FindTargetStatus.FileNotFound : FindTargetStatus.Found); #pragma warning restore VSTHRD010 }
private static void AddTargetItem(IReadOnlyCollection <GenerationItem> generationItems, IUnitTestGeneratorOptions options, GenerationItem generationItem) { ThreadHelper.ThrowIfNotOnUIThread(); if (TargetFinder.FindExistingTargetItem(generationItem.Source, options.GenerationOptions, out var targetItem) != FindTargetStatus.Found) { File.WriteAllText(generationItem.TargetFileName, generationItem.TargetContent); targetItem = generationItem.TargetProjectItems.AddFromFile(generationItem.TargetFileName); } else { if (targetItem.Document is null) { File.WriteAllText(generationItem.TargetFileName, generationItem.TargetContent); } else { var textSelection = (TextSelection)targetItem.Document.Selection; textSelection.SelectAll(); textSelection.Insert(generationItem.TargetContent); targetItem.Document.Save(); } } if (generationItems.Count == 1) { VsProjectHelper.ActivateItem(targetItem); } }