/// <summary> /// Adds solution items from template to target solution - copy the files and add them to solution as Solution Items (when not excluded) /// </summary> /// <param name="templateDirInfo">Template directory's <see cref="DirectoryInfo"/></param> /// <param name="solutionInfo">Information about current solution</param> /// <param name="templateInfo">Information about the template used</param> private void AddSolutionItems(DirectoryInfo templateDirInfo, SolutionInfo solutionInfo, TemplateInfo templateInfo) { ThreadHelper.ThrowIfNotOnUIThread(); foreach (var sourceFileInfo in templateDirInfo.GetFiles().Where(fi => !fi.Name.ToLower().EndsWith(".sln"))) { var destFile = solutionInfo.SolutionDir.AddPath(sourceFileInfo.Name); Package.Output($" Copying solution item from {sourceFileInfo.FullName} to {destFile}..."); FileUtils.FileCopy(sourceFileInfo.FullName, destFile, true, templateInfo.DoNotOverwriteFileNames.ToList()); if (solutionInfo.HasSolutionItem(sourceFileInfo.Name) || templateInfo.DoNotAddToSolution(sourceFileInfo.Name)) { Package.Output( $" Solution item {sourceFileInfo.Name} not added to solution - already exists or blacklisted"); continue; } Package.Output($" Adding solution item {sourceFileInfo.Name} to solution..."); Dte.ToolWindows.SolutionExplorer.UIHierarchyItems.Item(1).Select(vsUISelectionType.vsUISelectionTypeSelect); solutionInfo.Solution.DTE.ItemOperations.AddExistingItem(destFile); Package.Output($" Added solution item {sourceFileInfo.Name} to solution"); } }
/// <summary> /// Adds projects from template to target solution - copy the project and add to the solution (if needed) /// </summary> /// <param name="templateDir">Template directory</param> /// <param name="solutionInfo">Information about current solution</param> /// <param name="templateInfo">Information about the template used</param> /// <returns>Returns template directory <see cref="DirectoryInfo"/></returns> private DirectoryInfo AddProjects(string templateDir, SolutionInfo solutionInfo, TemplateInfo templateInfo) { ThreadHelper.ThrowIfNotOnUIThread(); var templateDirInfo = new DirectoryInfo(templateDir); foreach (var sourceProjectDirectoryInfo in templateDirInfo.GetDirectories() .Where(di => !di.Name.StartsWith(".") && di.GetFiles("*.csproj").Length > 0)) { //copy project dir to solution dir var destinationDir = solutionInfo.SolutionDir.AddPath(sourceProjectDirectoryInfo.Name); Package.Output($" Copying project from {sourceProjectDirectoryInfo.FullName} to {destinationDir}..."); FileUtils.DirectoryCopy(sourceProjectDirectoryInfo.FullName, destinationDir, true, true, templateInfo.DoNotOverwriteFileNames.ToList()); //add project to solution var sourceProjectFiles = sourceProjectDirectoryInfo.GetFiles("*.csproj"); if (sourceProjectFiles.Length != 1) { throw new Exception("Can't find unique project file within " + sourceProjectDirectoryInfo.FullName); } var projectFileName = sourceProjectFiles[0].Name; var destinationProjectFileFullName = destinationDir.AddPath(projectFileName); if (!solutionInfo.ProjectExists(projectFileName)) { Package.Output($" Adding project {destinationProjectFileFullName}..."); // ReSharper disable once RedundantArgumentDefaultValue solutionInfo.Solution.AddFromFile(destinationProjectFileFullName, false); Package.Output($" Added project {destinationProjectFileFullName}"); } else { Package.Output($" Project {destinationProjectFileFullName} already exists - skip add"); } } return(templateDirInfo); }