コード例 #1
0
        private static async Task <object> NewProjectAsync(
            string templatePath,
            string outputPath,
            string templateName,
            string projectName,
            string solutionFolderName)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            await VSSolutionHelper.EnsureSolutionAsync(outputPath);

            string projectTemplateFilePath = null;

            var     dte                   = ServiceLocator.GetInstance <DTE>();
            var     dte2                  = (DTE2)dte;
            var     solution2             = dte2.Solution as Solution2;
            Project solutionFolderProject = null;

            dynamic newProject = null;

            projectTemplateFilePath = await GetProjectTemplateFilePathAsync(solution2, templateName, templatePath);

            var solutionDir = Path.GetDirectoryName(solution2.FullName);

            string destPath = null;

            if (string.IsNullOrEmpty(solutionFolderName))
            {
                destPath = Path.Combine(solutionDir, projectName);
            }
            else
            {
                destPath = Path.Combine(solutionDir, Path.Combine(solutionFolderName, projectName));
            }

            var window = dte2.ActiveWindow as Window2;

            solutionFolderProject = await CreateProjectFromTemplateAsync(
                solution2,
                solutionFolderName,
                projectTemplateFilePath,
                destPath,
                projectName);

            await CloseOpenDocumentsAsync(dte2);

            await Activatex86ConfigurationsAsync(dte2);

            window.SetFocus();

            await GetProjectAsync(solution2, projectName, solutionFolderProject);

            // HACK: May need to be fixed
            if (newProject == null)
            {
                newProject = await GetProjectAsync(solution2, projectName, solutionFolderProject);
            }

            if (newProject == null)
            {
                throw new InvalidOperationException(
                          "Could not create new project or could not locate newly created project");
            }

            return(newProject);
        }