예제 #1
0
        protected virtual void ExecuteOperation(SearchContext context, string sourcePath, string destinationPath, bool isDirectory, string indent)
        {
            bool fileExists      = File.Exists(destinationPath);
            bool directoryExists = !fileExists && Directory.Exists(destinationPath);
            bool ask             = false;

            if (isDirectory)
            {
                if (fileExists)
                {
                    ask = true;
                }
                else if (directoryExists)
                {
                    if (File.GetAttributes(sourcePath) == File.GetAttributes(destinationPath))
                    {
                        return;
                    }

                    ask = true;
                }
            }
            else if (fileExists)
            {
                if (Options.CompareOptions != FileCompareOptions.None &&
                    FileSystemHelpers.FileEquals(sourcePath, destinationPath, Options.CompareOptions))
                {
                    return;
                }

                ask = true;
            }
            else if (directoryExists)
            {
                ask = true;
            }

            if (ask &&
                ConflictResolution == ConflictResolution.Skip)
            {
                return;
            }

            if (!isDirectory &&
                fileExists &&
                ConflictResolution == ConflictResolution.Rename)
            {
                destinationPath = CreateNewFile(destinationPath);
            }

            if (!Options.OmitPath)
            {
                LogHelpers.WritePath(destinationPath, basePath: Target, relativePath: Options.DisplayRelativePath, colors: Colors.Matched_Path, indent: indent, verbosity: Verbosity.Minimal);
                WriteLine(Verbosity.Minimal);
            }

            if (ask &&
                ConflictResolution == ConflictResolution.Ask)
            {
                string question;
                if (directoryExists)
                {
                    question = (isDirectory) ? "Update directory attributes?" : "Overwrite directory?";
                }
                else
                {
                    question = "Overwrite file?";
                }

                DialogResult dialogResult = ConsoleHelpers.QuestionWithResult(question, indent);
                switch (dialogResult)
                {
                case DialogResult.Yes:
                {
                    break;
                }

                case DialogResult.YesToAll:
                {
                    ConflictResolution = ConflictResolution.Overwrite;
                    break;
                }

                case DialogResult.No:
                case DialogResult.None:
                {
                    return;
                }

                case DialogResult.NoToAll:
                {
                    ConflictResolution = ConflictResolution.Skip;
                    return;
                }

                case DialogResult.Cancel:
                {
                    context.TerminationReason = TerminationReason.Canceled;
                    return;
                }

                default:
                {
                    throw new InvalidOperationException($"Unknown enum value '{dialogResult}'.");
                }
                }
            }

            if (isDirectory)
            {
                if (directoryExists)
                {
                    if (!Options.DryRun)
                    {
                        FileSystemHelpers.UpdateAttributes(sourcePath, destinationPath);
                    }
                }
                else
                {
                    if (fileExists &&
                        !Options.DryRun)
                    {
                        File.Delete(destinationPath);
                    }

                    if (!Options.DryRun)
                    {
                        Directory.CreateDirectory(destinationPath);
                    }
                }

                context.Telemetry.ProcessedDirectoryCount++;
            }
            else
            {
                if (fileExists)
                {
                    if (!Options.DryRun)
                    {
                        File.Delete(destinationPath);
                    }
                }
                else if (directoryExists)
                {
                    if (!Options.DryRun)
                    {
                        Directory.Delete(destinationPath, recursive: true);
                    }
                }
                else if (!Options.DryRun)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
                }

                if (!Options.DryRun)
                {
                    ExecuteOperation(sourcePath, destinationPath);
                }

                context.Telemetry.ProcessedFileCount++;
            }