Beispiel #1
0
        private Project RecreateProjectOfCorrectType(string projectName, ProjectType projectType)
        {
            var dte = Services.DTE;
            var solution = dte.Solution as Solution2;
            string solutionDir = SolutionUtilities.GetSolutionDir();
            string projectDir = Path.Combine(solutionDir, projectName);

            var allProjects = solution.Projects.OfType<Project>();
            var oldProject = allProjects.SingleOrDefault(p => p.Name == projectName);
            if (oldProject != null && projectType.Belongs(oldProject))
                return oldProject;

            ProjectType oldProjectType = oldProject == null ? null : GetProjectType(oldProject);
            bool needBackup = projectType.RequiresEmptyDirectoryForCreation ||
                (oldProject != null && (oldProjectType == null || oldProjectType.RequiresEmptyDirectoryForCreation));

            Project newProject = null;

            string tempDir = null;
            try
            {
                // Backup current directory
                if (needBackup)
                {
                    tempDir = Path.Combine(Path.GetTempPath(), "vscaide", projectName);
                    if (Directory.Exists(tempDir))
                    {
                        Directory.Delete(tempDir, recursive: true);
                    }

                    // Don't keep VS files
                    var filesToDelete = new[] { ".vcproj", ".vcxproj", ".csproj", ".user", ".exe", ".pdb" };
                    var foldersToDelete = new[] { "obj", "Release", "Debug" };
                    Func<FileInfo, bool> filter = fi =>
                        foldersToDelete.Any(f => fi.FullName.StartsWith(Path.Combine(projectDir, f), StringComparison.CurrentCultureIgnoreCase)) ||
                        !filesToDelete.Contains(fi.Extension, StringComparer.CurrentCultureIgnoreCase);

                    FileUtility.DirectoryCopy(projectDir, tempDir, copySubDirs: true, fileFilter: filter);
                }

                // Remove the old project
                if (oldProject != null)
                {
                    SolutionUtilities.IgnoreSolutionEvents = true;
                    try
                    {
                        solution.Remove(oldProject);
                    }
                    finally
                    {
                        SolutionUtilities.IgnoreSolutionEvents = false;
                    }
                }

                // Create the project
                if (projectType.RequiresEmptyDirectoryForCreation)
                {
                    FileUtility.RemoveFiles(projectDir);
                }
                solution.AddFromTemplate(projectType.ProjectTemplate,
                    Destination: Path.Combine(SolutionUtilities.GetSolutionDir(), projectName),
                    ProjectName: projectName,
                    Exclusive: false);

                allProjects = solution.Projects.OfType<Project>();
                newProject = allProjects.SingleOrDefault(p => p.Name == projectName);
                if (newProject == null)
                {
                    Logger.LogError("Couldn't create {0} project", projectName);
                }
            }
            finally
            {
                // Restore backup
                if (tempDir != null)
                {
                    FileUtility.DirectoryCopy(tempDir, projectDir, copySubDirs: true, fileFilter: null);
                    Directory.Delete(tempDir, recursive: true);
                }
            }

            return newProject;
        }
Beispiel #2
0
        private Project RecreateProjectOfCorrectType(string projectName, ProjectType projectType)
        {
            var    dte         = Services.DTE;
            var    solution    = dte.Solution as Solution2;
            string solutionDir = SolutionUtilities.GetSolutionDir();
            string projectDir  = Path.Combine(solutionDir, projectName);

            var allProjects = solution.Projects.OfType <Project>();
            var oldProject  = allProjects.SingleOrDefault(p => p.Name == projectName);

            if (oldProject != null && projectType.Belongs(oldProject))
            {
                return(oldProject);
            }

            ProjectType oldProjectType = oldProject == null ? null : GetProjectType(oldProject);
            bool        needBackup     = projectType.RequiresEmptyDirectoryForCreation ||
                                         (oldProject != null && (oldProjectType == null || oldProjectType.RequiresEmptyDirectoryForCreation));

            Project newProject = null;

            string tempDir = null;

            try
            {
                // Backup current directory
                if (needBackup)
                {
                    tempDir = Path.Combine(Path.GetTempPath(), "vscaide", projectName);
                    if (Directory.Exists(tempDir))
                    {
                        Directory.Delete(tempDir, recursive: true);
                    }

                    // Don't keep VS files
                    var filesToDelete            = new[] { ".vcproj", ".vcxproj", ".csproj", ".user", ".exe", ".pdb" };
                    var foldersToDelete          = new[] { "obj", "Release", "Debug" };
                    Func <FileInfo, bool> filter = fi =>
                                                   foldersToDelete.Any(f => fi.FullName.StartsWith(Path.Combine(projectDir, f), StringComparison.CurrentCultureIgnoreCase)) ||
                                                   !filesToDelete.Contains(fi.Extension, StringComparer.CurrentCultureIgnoreCase);

                    FileUtility.DirectoryCopy(projectDir, tempDir, copySubDirs: true, fileFilter: filter);
                }

                // Remove the old project
                if (oldProject != null)
                {
                    SolutionUtilities.IgnoreSolutionEvents = true;
                    try
                    {
                        solution.Remove(oldProject);
                    }
                    finally
                    {
                        SolutionUtilities.IgnoreSolutionEvents = false;
                    }
                }

                // Create the project
                if (projectType.RequiresEmptyDirectoryForCreation)
                {
                    FileUtility.RemoveFiles(projectDir);
                }
                solution.AddFromTemplate(projectType.ProjectTemplate,
                                         Destination: Path.Combine(SolutionUtilities.GetSolutionDir(), projectName),
                                         ProjectName: projectName,
                                         Exclusive: false);

                allProjects = solution.Projects.OfType <Project>();
                newProject  = allProjects.SingleOrDefault(p => p.Name == projectName);
                if (newProject == null)
                {
                    Logger.LogError("Couldn't create {0} project", projectName);
                }
            }
            finally
            {
                // Restore backup
                if (tempDir != null)
                {
                    FileUtility.DirectoryCopy(tempDir, projectDir, copySubDirs: true, fileFilter: null);
                    Directory.Delete(tempDir, recursive: true);
                }
            }

            return(newProject);
        }