Esempio n. 1
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);
                }
            }
        }
Esempio n. 2
0
        private void ExecuteMatchWithContentCore(
            IEnumerator <ICapture> groups,
            int count,
            MaxReason maxReason,
            FileMatch fileMatch,
            SearchContext context,
            ContentWriterOptions writerOptions,
            string?baseDirectoryPath  = null,
            ColumnWidths?columnWidths = null)
        {
            string indent = GetPathIndent(baseDirectoryPath);

            if (!Options.OmitPath)
            {
                WritePath(context, fileMatch, baseDirectoryPath, indent, columnWidths, includeNewline: false);
                WriteFilePathEnd(count, maxReason, Options.IncludeCount);
            }

            SearchTelemetry telemetry = context.Telemetry;

            ContentWriter?contentWriter = null;
            TextWriter?   textWriter    = null;

            try
            {
                int fileMatchCount       = 0;
                int fileReplacementCount = 0;

                if (!Options.DryRun)
                {
                    if (Options.AskMode == AskMode.File)
                    {
                        textWriter = new StringWriter();
                    }
                    else if (Options.AskMode != AskMode.Value &&
                             !Options.Interactive)
                    {
                        textWriter = (CanUseStreamWriter)
                            ? new StreamWriter(fileMatch.Path, false, fileMatch.Encoding)
                            : new StringWriter();
                    }
                }

                if (Options.AskMode == AskMode.Value ||
                    Options.Interactive ||
                    (!Options.OmitContent &&
                     ShouldLog(Verbosity.Normal)))
                {
                    MatchOutputInfo?outputInfo = Options.CreateOutputInfo(
                        fileMatch.ContentText,
                        fileMatch.ContentMatch !,
                        ContentFilter !);

                    if (Options.AskMode == AskMode.Value ||
                        Options.Interactive)
                    {
                        Lazy <TextWriter>?lazyWriter = null;

                        if (!Options.DryRun)
                        {
                            lazyWriter = new Lazy <TextWriter>(() =>
                            {
                                textWriter = new StringWriter();
                                return(textWriter);
                            });
                        }

                        contentWriter = AskReplacementWriter.Create(
                            Options.ContentDisplayStyle,
                            fileMatch.ContentText,
                            Options.Replacer,
                            lazyWriter,
                            writerOptions,
                            outputInfo,
                            isInteractive: Options.Interactive,
                            SpellcheckState);
                    }
                    else
                    {
                        contentWriter = CreateReplacementWriter(
                            Options.ContentDisplayStyle,
                            fileMatch.ContentText,
                            Options.Replacer,
                            writerOptions,
                            textWriter,
                            outputInfo);
                    }
                }
                else if (Options.DryRun &&
                         CanUseEmptyWriter)
                {
                    contentWriter = new EmptyContentWriter(FileWriterOptions);
                }
                else
                {
                    contentWriter = new TextWriterContentWriter(
                        fileMatch.ContentText,
                        Options.Replacer,
                        writerOptions,
                        textWriter,
                        SpellcheckState);
                }

                WriteMatches(contentWriter, groups, context);

                fileMatchCount = contentWriter.MatchCount;

                fileReplacementCount = (contentWriter is IReportReplacement reportReplacement)
                    ? reportReplacement.ReplacementCount
                    : fileMatchCount;

                telemetry.MatchCount += fileMatchCount;

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

                    telemetry.MatchingLineCount += contentWriter.MatchingLineCount;
                }

                if (Options.AskMode == AskMode.Value ||
                    Options.Interactive)
                {
                    if (textWriter != null)
                    {
                        File.WriteAllText(fileMatch.Path, textWriter.ToString(), fileMatch.Encoding);

                        if (Options.AskMode == AskMode.Value &&
                            ((AskReplacementWriter)contentWriter).ContinueWithoutAsking)
                        {
                            Options.AskMode = AskMode.None;
                        }
                    }
                }
                else if (Options.AskMode == AskMode.File)
                {
                    if (context.TerminationReason == TerminationReason.Canceled)
                    {
                        fileReplacementCount = 0;
                    }
                    else
                    {
                        try
                        {
                            if (Options.DryRun)
                            {
                                if (ConsoleHelpers.AskToContinue(indent) == DialogResult.YesToAll)
                                {
                                    Options.AskMode = AskMode.None;
                                }
                            }
                            else if (fileReplacementCount > 0 &&
                                     ConsoleHelpers.AskToExecute("Replace content?", indent))
                            {
                                File.WriteAllText(fileMatch.Path, textWriter !.ToString(), fileMatch.Encoding);
                            }
                            else
                            {
                                fileReplacementCount = 0;
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            context.TerminationReason = TerminationReason.Canceled;
                            fileReplacementCount      = 0;
                        }
                    }
                }
                else if (fileReplacementCount > 0 &&
                         textWriter is StringWriter)
                {
                    File.WriteAllText(fileMatch.Path, textWriter.ToString(), fileMatch.Encoding);
                }

                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();
            }
        }