Beispiel #1
0
        protected override void WriteEndReplacement(Match match, string result)
        {
            if (_lazyWriter != null)
            {
                if (ConsoleHelpers.Question("Replace?", Options.Indent))
                {
                    _lazyWriter.Value.Write(Input.AsSpan(_writerIndex, match.Index - _writerIndex));
                    _lazyWriter.Value.Write(result);

                    _writerIndex = match.Index + match.Length;

                    ReplacementCount++;
                }
            }
            else if (!ContinueWithoutAsking &&
                     ConsoleHelpers.Question("Continue without asking?", Options.Indent))
            {
                ContinueWithoutAsking = true;
            }
        }
Beispiel #2
0
        private void ReplaceMatches(
            string filePath,
            Encoding encoding,
            string input,
            Match match,
            string indent,
            ContentWriterOptions writerOptions,
            SearchContext context)
        {
            SearchTelemetry telemetry = context.Telemetry;

            ContentWriter  contentWriter = null;
            TextWriter     textWriter    = null;
            List <Capture> groups        = null;

            try
            {
                groups = ListCache <Capture> .GetInstance();

                GetCaptures(match, writerOptions.GroupNumber, context, isPathWritten: !Options.OmitPath, predicate: Options.ContentFilter.Predicate, captures: groups);

                int fileMatchCount       = 0;
                int fileReplacementCount = 0;

                if (!Options.DryRun)
                {
                    if (Options.AskMode == AskMode.File)
                    {
                        textWriter = new StringWriter();
                    }
                    else if (Options.AskMode == AskMode.None)
                    {
                        textWriter = new StreamWriter(filePath, false, encoding);
                    }
                }

                if (Options.AskMode == AskMode.Value ||
                    ShouldLog(Verbosity.Normal))
                {
                    MatchOutputInfo outputInfo = Options.CreateOutputInfo(input, match);

                    if (Options.AskMode == AskMode.Value)
                    {
                        Lazy <TextWriter> lazyWriter = (Options.DryRun)
                            ? null
                            : new Lazy <TextWriter>(() => new StreamWriter(filePath, false, encoding));

                        contentWriter = AskReplacementWriter.Create(Options.ContentDisplayStyle, input, Options.ReplaceOptions, lazyWriter, writerOptions, outputInfo);
                    }
                    else
                    {
                        contentWriter = ContentWriter.CreateReplace(Options.ContentDisplayStyle, input, Options.ReplaceOptions, writerOptions, textWriter, outputInfo);
                    }
                }
                else if (Options.DryRun)
                {
                    contentWriter = new EmptyContentWriter(null, FileWriterOptions);
                }
                else
                {
                    contentWriter = new TextWriterContentWriter(input, Options.ReplaceOptions, textWriter, writerOptions);
                }

                WriteMatches(contentWriter, groups, context);

                fileMatchCount        = contentWriter.MatchCount;
                fileReplacementCount  = (contentWriter is AskReplacementWriter askReplacementWriter) ? askReplacementWriter.ReplacementCount : fileMatchCount;
                telemetry.MatchCount += fileMatchCount;

                if (contentWriter.MatchingLineCount >= 0)
                {
                    if (telemetry.MatchingLineCount == -1)
                    {
                        telemetry.MatchingLineCount = 0;
                    }

                    telemetry.MatchingLineCount += contentWriter.MatchingLineCount;
                }

                if (Options.AskMode == AskMode.File)
                {
                    if (context.TerminationReason == TerminationReason.Canceled)
                    {
                        fileReplacementCount = 0;
                    }
                    else
                    {
                        try
                        {
                            if (Options.DryRun)
                            {
                                if (ConsoleHelpers.Question("Continue without asking?", indent))
                                {
                                    Options.AskMode = AskMode.None;
                                }
                            }
                            else if (ConsoleHelpers.Question("Replace content?", indent))
                            {
                                File.WriteAllText(filePath, textWriter !.ToString(), encoding);
                            }
                            else
                            {
                                fileReplacementCount = 0;
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            context.TerminationReason = TerminationReason.Canceled;
                            fileReplacementCount      = 0;
                        }
                    }
                }
                else if (Options.AskMode == AskMode.Value)
                {
                    if (((AskReplacementWriter)contentWriter).ContinueWithoutAsking)
                    {
                        Options.AskMode = AskMode.None;
                    }
                }

                telemetry.ProcessedMatchCount += fileReplacementCount;

                if (fileReplacementCount > 0)
                {
                    telemetry.ProcessedFileCount++;
                }
            }
            catch (Exception ex) when(ex is IOException ||
                                      ex is UnauthorizedAccessException)
            {
                WriteFileError(ex, indent: indent);
            }
            finally
            {
                contentWriter?.Dispose();

                if (groups != null)
                {
                    ListCache <Capture> .Free(groups);
                }
            }
        }
Beispiel #3
0
        protected override void ExecuteResult(
            FileSystemFinderResult result,
            SearchContext context,
            string baseDirectoryPath  = null,
            ColumnWidths columnWidths = null)
        {
            string indent = GetPathIndent(baseDirectoryPath);

            if (!Options.OmitPath)
            {
                WritePath(context, result, baseDirectoryPath, indent, columnWidths);
            }

            bool deleted = false;

            if (!Options.DryRun &&
                (!Options.Ask || AskToDelete()))
            {
                try
                {
                    FileSystemHelpers.Delete(
                        result,
                        contentOnly: Options.ContentOnly,
                        includingBom: Options.IncludingBom,
                        filesOnly: Options.FilesOnly,
                        directoriesOnly: Options.DirectoriesOnly);

                    deleted = true;
                }
                catch (Exception ex) when(ex is IOException ||
                                          ex is UnauthorizedAccessException)
                {
                    WriteFileError(ex, indent: indent);
                }
            }

            if (Options.DryRun || deleted)
            {
                if (result.IsDirectory)
                {
                    context.Telemetry.ProcessedDirectoryCount++;
                }
                else
                {
                    context.Telemetry.ProcessedFileCount++;
                }
            }

            if (result.IsDirectory &&
                deleted)
            {
                OnDirectoryChanged(new DirectoryChangedEventArgs(result.Path, null));
            }

            bool AskToDelete()
            {
                try
                {
                    return(ConsoleHelpers.Question(
                               (Options.ContentOnly) ? "Delete content?" : "Delete?",
                               indent));
                }
                catch (OperationCanceledException)
                {
                    context.TerminationReason = TerminationReason.Canceled;
                    return(false);
                }
            }
        }