Exemplo n.º 1
0
        protected override void ExecutionImplementation()
        {
            String oldFileName = Path.GetFileNameWithoutExtension(oldPath);
            String newFileName = Path.GetFileNameWithoutExtension(newPath);
            String msg         = TextHelper.GetString("Info.RenamingFile");
            String title       = String.Format(TextHelper.GetString("Title.RenameDialog"), oldFileName);

            if (MessageBox.Show(msg, title, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                var target = RefactoringHelper.GetRefactorTargetFromFile(oldPath, AssociatedDocumentHelper);
                if (target != null)
                {
                    var command = CommandFactoryProvider.GetFactory(target).CreateRenameCommandAndExecute(target, true, newFileName);
                    command.RegisterDocumentHelper(AssociatedDocumentHelper);
                    return;
                }
            }
            // refactor failed or was refused
            if (Path.GetFileName(oldPath).Equals(newPath, StringComparison.OrdinalIgnoreCase))
            {
                // name casing changed
                string tmpPath = oldPath + "$renaming$";
                File.Move(oldPath, tmpPath);
                oldPath = tmpPath;
            }
            if (!Path.IsPathRooted(newPath))
            {
                newPath = Path.Combine(Path.GetDirectoryName(oldPath), newPath);
            }

            if (FileHelper.ConfirmOverwrite(newPath))
            {
                FileHelper.ForceMove(oldPath, newPath);
                DocumentManager.MoveDocuments(oldPath, newPath);
                RefactoringHelper.RaiseMoveEvent(oldPath, newPath);
            }
        }
Exemplo n.º 2
0
        private void MoveRefactoredFiles()
        {
            MessageBar.Locked = true;
            AssociatedDocumentHelper.CloseTemporarilyOpenedDocuments();
            foreach (var target in targets)
            {
                File.Delete(target.OldFilePath);

                if (target.OwnerPath == null)
                {
                    OldPathToNewPath.Remove(target.OldFilePath);
                }

                // Casing changes, we cannot move directly here, there may be conflicts, better leave it to the next step
                if (target.TmpFilePath != null)
                {
                    RefactoringHelper.Move(target.TmpFilePath, target.NewFilePath, true, target.OldFilePath);
                }
                else
                {
                    RefactoringHelper.RaiseMoveEvent(target.OldFilePath, target.NewFilePath);
                }
            }
            // Move non-source files and whole folders
            foreach (KeyValuePair <string, string> item in OldPathToNewPath)
            {
                string oldPath = item.Key;
                string newPath = item.Value;
                if (File.Exists(oldPath))
                {
                    newPath = Path.Combine(newPath, Path.GetFileName(oldPath));
                    if (!Path.IsPathRooted(newPath))
                    {
                        newPath = Path.Combine(Path.GetDirectoryName(oldPath), newPath);
                    }
                    RefactoringHelper.Move(oldPath, newPath, true);
                }
                else if (Directory.Exists(oldPath))
                {
                    newPath = renaming ? Path.Combine(Path.GetDirectoryName(oldPath), newPath) : Path.Combine(newPath, Path.GetFileName(oldPath));

                    // Look for document class changes
                    // Do not use RefactoringHelper to avoid possible dialogs that we don't want
                    Project project          = (Project)PluginBase.CurrentProject;
                    string  newDocumentClass = null;
                    string  searchPattern    = project.DefaultSearchFilter;
                    foreach (string pattern in searchPattern.Split(';'))
                    {
                        foreach (string file in Directory.GetFiles(oldPath, pattern, SearchOption.AllDirectories))
                        {
                            if (project.IsDocumentClass(file))
                            {
                                newDocumentClass = file.Replace(oldPath, newPath);
                                break;
                            }
                        }
                        if (newDocumentClass != null)
                        {
                            break;
                        }
                    }

                    // Check if this is a name casing change
                    if (oldPath.Equals(newPath, StringComparison.OrdinalIgnoreCase))
                    {
                        string tmpPath = oldPath + "$renaming$";
                        FileHelper.ForceMoveDirectory(oldPath, tmpPath);
                        DocumentManager.MoveDocuments(oldPath, tmpPath);
                        oldPath = tmpPath;
                    }

                    // Move directory contents to final location
                    FileHelper.ForceMoveDirectory(oldPath, newPath);
                    DocumentManager.MoveDocuments(oldPath, newPath);

                    if (!string.IsNullOrEmpty(newDocumentClass))
                    {
                        project.SetDocumentClass(newDocumentClass, true);
                        project.Save();
                    }
                    RefactoringHelper.RaiseMoveEvent(oldPath, newPath);
                }
            }

            MessageBar.Locked = false;
        }