Esempio n. 1
0
        /// <summary>
        /// Adds the projects.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="path">The path.</param>
        /// <param name="projectsInfos">The projects infos.</param>
        /// <param name="referenceFirstProject">if set to <c>true</c> [reference first project].</param>
        /// <param name="includeLibFolderInProjects">if set to <c>true</c> [include lib folder in projects].</param>
        /// <returns>The messages.</returns>
        public static IEnumerable <string> AddProjects(
            this Solution2 instance,
            string path,
            IEnumerable <ProjectTemplateInfo> projectsInfos,
            bool referenceFirstProject,
            bool includeLibFolderInProjects)
        {
            string message = string.Format(
                "SolutionExtensions::AddProjects project count={0} path={1}", projectsInfos.Count(), path);

            TraceService.WriteLine(message);

            Project firstProject = null;

            Solution solution = instance as Solution;

            List <string> messages = new List <string>();

            foreach (ProjectTemplateInfo projectInfo in projectsInfos)
            {
                try
                {
                    //// Project may actually already exist - if so just skip it!

                    string projectPath = string.Format(@"{0}\{1}\", path, projectInfo.Name);

                    if (Directory.Exists(projectPath) == false)
                    {
                        try
                        {
                            string template = instance.GetProjectTemplate(projectInfo.TemplateName);
                            solution.AddProjectToSolution(projectPath, template, projectInfo.Name);

                            //// remove the lib folder if that's what the developer wants to happen.
                            //// if the develop has selected use nuget then also remove the project
                            if (includeLibFolderInProjects == false ||
                                projectInfo.UseNuget)
                            {
                                Project project = instance.GetProject(projectInfo.Name);

                                if (project != null)
                                {
                                    ProjectItem projectItem = project.RemoveFolder("Lib");

                                    //// remove the local files if we are going to use nuget.
                                    if (projectInfo.UseNuget)
                                    {
                                        projectItem.DeleteFolder();
                                    }
                                }
                            }

                            if (projectInfo.UseNuget)
                            {
                                //// now execute Nuget command.

                                DTE2 dte2 = instance.DTE as DTE2;

                                dte2.ExecuteNugetCommand(projectInfo.NugetCommand);
                            }

                            messages.Add(projectInfo.Name + " project successfully added.");
                        }
                        catch (Exception exception)
                        {
                            string exceptionMessage = string.Format(
                                "Unsupported project {0} not added to the solution.", projectInfo.Name);

                            TraceService.WriteError(exceptionMessage + " exception=" + exception.Message);

                            messages.Add(exceptionMessage);
                        }
                    }

                    if (referenceFirstProject)
                    {
                        Project project = instance.GetProject(projectInfo.Name);

                        if (project != null)
                        {
                            if (firstProject == null)
                            {
                                firstProject = project;
                            }
                            else
                            {
                                try
                                {
                                    project.AddProjectReference(firstProject);
                                }
                                catch (Exception exception)
                                {
                                    string exceptionMessage = "SolutionExtensions::AddProjects Error=" + exception.Message;

                                    TraceService.WriteError(exceptionMessage);
                                    messages.Add(exceptionMessage);
                                }
                            }
                        }
                    }
                }
                catch (FileNotFoundException exception)
                {
                    //// if template not found just miss it out for now.
                    message = string.Format(
                        "Template not found for {0} Error {1}", projectInfo.TemplateName, exception.Message);

                    TraceService.WriteError(message);
                    messages.Add(message);
                }
            }

            return(messages);
        }