/// <summary>
        /// Shows the rename project dialog, deleting the project if the dialog result was true.
        /// </summary>
        /// <param name="projectName">The project name to potentially rename.</param>
        public Boolean ShowDialog(Window owner, String projectName)
        {
            this.NewProjectName = String.Empty;
            this.ProjectName    = projectName;

            RenameProjectDialog renameProjectDialog = new RenameProjectDialog()
            {
                Owner = owner
            };

            if (renameProjectDialog.ShowDialog() == true && this.IsProjectNameValid)
            {
                try
                {
                    String projectPath    = Path.Combine(SettingsViewModel.GetInstance().ProjectRoot, projectName);
                    String newProjectPath = Path.Combine(SettingsViewModel.GetInstance().ProjectRoot, this.NewProjectName);
                    // ProjectQueryer.RenameProject(projectPath, newProjectPath);

                    return(true);
                }
                catch (Exception ex)
                {
                    Logger.Log(LogLevel.Error, "Error renaming project folder", ex);
                }
            }

            return(false);
        }
Example #2
0
        private void OnRenameProject(object sender, EventArgs e)
        {
            try
            {
                Logger.Trace("############################### New renaming procedure started... ###############################");

                // Get the currently selected project within the solution explorer.
                var currentProject = GetSelectedProject();

                // Check if there's a project selected and if it's a C# project. All other types aren't yet supported.
                // (And we don't allow renaming on solution folders.)
                if (!IsProjectTypeValid(currentProject))
                {
                    return;
                }

                // Get the new project name from the user.
                var renameDialog = new RenameProjectDialog(RenameData, currentProject)
                {
                    Owner = Application.Current.MainWindow
                };

                var result = renameDialog.ShowDialog();
                if (!result.HasValue || !result.Value)
                {
                    return;
                }

                UpdateStatusBar(string.Format("The project '{0}' is selected and will be renamed...", currentProject.Name));

                // This is the new project name the user typed in.
                RenameData.NewProjectName = renameDialog.GetProjectName();
                UpdateStatusBar(string.Format("The new project name is '{0}'...", RenameData.NewProjectName));

                // Check if this is necessary when the references check was refactored!
                RenameData.ProjectsWithReferences.Clear();

                // Save all changes that were made before the renaming process. Just for safety!
                SaveSolution();

                // Check if there's an solution folder or return null.
                var solutionFolder = GetSolutionFolder(currentProject);

                // Get the file name and the parent directory of the current project before it gets renamed!
                RenameData.OldProjectFileName = currentProject.Name;
                var projectParentDirectory = GetProjectParentDirectoryName(currentProject);

                // Check if the current project is the startup project before it gets renamed and temporarily deleted.
                var isStartupProject = IsStartupProject(currentProject);

                // Before the project gets renamed we need to safe the old full name of it.
                // This is needed later for the search of old references in other projects within the solution.
                RenameData.RenamedProject = currentProject;

                // Rename the project. This changes the project filename too!
                UpdateStatusBar(string.Format("Renaming the project '{0}' to '{1}'...", currentProject.Name,
                                              RenameData.NewProjectName));
                currentProject.Name = RenameData.NewProjectName;

                // The hierarchy is needed for some of the following actions.
                IVsHierarchy currentProjectHierarchy;
                RenameData.Solution.GetProjectOfUniqueName(currentProject.UniqueName, out currentProjectHierarchy);

                if (RenameData.OldProjectFileName == projectParentDirectory)
                {
                    UpdateStatusBar(string.Format("The directory name '{0}' is the same as the old project name '{1}'",
                                                  projectParentDirectory, RenameData.OldProjectFileName));

                    // Check if other projects have references to the currently selected project. These references must be changed too!
                    if (OptionsStore.ChangeProjectReferencesAfterRenaming)
                    {
                        CheckProjectsForReferences();
                    }

                    // We need some data for future actions. Collect them here because the project is ready to get removed from the solution!
                    var newProjectFileName  = Path.GetFileName(currentProject.FileName);
                    var fullProjectName     = currentProject.FullName;
                    var newProjectDirectory = currentProject.Name;

                    // Remove the project from the solution file!
                    RemoveProjectFromSolution(currentProjectHierarchy);

                    // Move the project folder on the file system within the solution folder!
                    MoveProjectFolder(fullProjectName, newProjectDirectory);

                    // Add the renamed project to the solution. Either directly or within a solution folder.
                    // The return project is the new current project we're using for all other steps.
                    currentProject = AddProjectToSolution(solutionFolder, newProjectFileName, fullProjectName,
                                                          newProjectDirectory);

                    // Save the solution file after we moved the project.
                    SaveSolution();
                }

                // Change the reference of the renamed project within all other projects that had such a reference.
                if (OptionsStore.ChangeProjectReferencesAfterRenaming)
                {
                    ChangeRenamedProjectReferences(currentProject);
                }

                // Change some project data like the default namespace and the assembly name.
                if (OptionsStore.ChangeProjectPropertiesAfterRenaming)
                {
                    ChangeProjectProperties(currentProject);
                }

                // Save the project after we made so many changes to it.
                SaveProject(currentProject, currentProjectHierarchy);

                // Change some data in the AssemblyInfo.cs file if those data matches the old project name (AssemblyTitle and AssemblyProduct)
                if (OptionsStore.ChangeAssemblyInfoAfterRenaming)
                {
                    ChangeAssemblyInfo(currentProject);
                }

                // Save the project after we made so many changes to it.
                SaveProject(currentProject, currentProjectHierarchy);

                // If the renamed project was the startup project, we need to refresh this setting after it was deleted.
                if (isStartupProject)
                {
                    UpdateStatusBar(string.Format("Set the startup project to '{0}'...", currentProject.Name));

                    RenameData.Dte.Solution.Properties.Item("StartupProject").Value = currentProject.Name;
                }

                // Rebuild the complete solution.
                if (OptionsStore.RebuildSolutionAfterRenaming)
                {
                    RebuildSolution();
                }
            }
            catch (COMException comException)
            {
                Logger.Fatal(comException);

                VsMessageBox.ShowErrorMessageBox("COMException", comException.ToString());
            }
            catch (IOException ioException)
            {
                Logger.Fatal(ioException);

                VsMessageBox.ShowErrorMessageBox("IOException", ioException.ToString());
            }
            catch (Exception exception)
            {
                Logger.Fatal(exception);

                VsMessageBox.ShowErrorMessageBox("Unknown Exception", exception.ToString());
            }
        }