예제 #1
0
 public ValueContentWriter(
     string input,
     ContentWriterOptions options = null,
     IResultStorage storage       = null,
     MatchOutputInfo outputInfo   = null) : this(input, ContentTextWriter.Default, options, storage, outputInfo)
 {
 }
예제 #2
0
        public static AskReplacementWriter Create(
            ContentDisplayStyle contentDisplayStyle,
            string input,
            ReplaceOptions replaceOptions,
            Lazy <TextWriter> lazyWriter,
            ContentWriterOptions options,
            MatchOutputInfo outputInfo)
        {
            switch (contentDisplayStyle)
            {
            case ContentDisplayStyle.Value:
            case ContentDisplayStyle.ValueDetail:
                return(new AskValueReplacementWriter(input, replaceOptions, lazyWriter, options, outputInfo));

            case ContentDisplayStyle.Line:
                return(new AskLineReplacementWriter(input, replaceOptions, lazyWriter, options));

            case ContentDisplayStyle.UnmatchedLines:
            case ContentDisplayStyle.AllLines:
                throw new InvalidOperationException();

            default:
                throw new InvalidOperationException($"Unknown enum value '{contentDisplayStyle}'.");
            }
        }
예제 #3
0
        public static ContentWriter CreateReplace(
            ContentDisplayStyle contentDisplayStyle,
            string input,
            ReplaceOptions replaceOptions,
            ContentWriterOptions options,
            TextWriter textWriter      = null,
            MatchOutputInfo outputInfo = null)
        {
            switch (contentDisplayStyle)
            {
            case ContentDisplayStyle.Value:
            case ContentDisplayStyle.ValueDetail:
                return(new ValueReplacementWriter(input, replaceOptions, options, textWriter, outputInfo));

            case ContentDisplayStyle.Line:
                return(new LineReplacementWriter(input, replaceOptions, options, textWriter));

            case ContentDisplayStyle.AllLines:
                return(new AllLinesReplacementWriter(input, replaceOptions, options, textWriter));

            case ContentDisplayStyle.UnmatchedLines:
                throw new NotSupportedException($"Value '{contentDisplayStyle}' is not supported.");

            default:
                throw new InvalidOperationException($"Unknown enum value '{contentDisplayStyle}'.");
            }
        }
예제 #4
0
 public AskValueContentWriter(
     string input,
     ContentWriterOptions options = null,
     IResultStorage storage       = null,
     MatchOutputInfo outputInfo   = null) : base(input, options, storage, outputInfo)
 {
     Ask = true;
 }
예제 #5
0
 public AskValueReplacementWriter(
     string input,
     ReplaceOptions replaceOptions,
     Lazy <TextWriter> lazyWriter,
     ContentWriterOptions options,
     MatchOutputInfo outputInfo) : base(input, replaceOptions, lazyWriter, options)
 {
     OutputInfo = outputInfo;
 }
예제 #6
0
 public ValueReplacementWriter(
     string input,
     ReplaceOptions replaceOptions,
     ContentWriterOptions options = null,
     TextWriter textWriter        = null,
     MatchOutputInfo outputInfo   = null) : base(input, options, outputInfo: outputInfo)
 {
     ReplaceOptions = replaceOptions;
     _textWriter    = textWriter;
 }
예제 #7
0
 public ValueContentWriter(
     string input,
     ContentTextWriter writer,
     ContentWriterOptions options = null,
     IResultStorage storage       = null,
     MatchOutputInfo outputInfo   = null) : base(input, writer, options)
 {
     ResultStorage = storage;
     OutputInfo    = outputInfo;
 }
예제 #8
0
        private void ExecuteInput(SearchContext context)
        {
            string input     = Options.Input;
            int    count     = 0;
            var    maxReason = MaxReason.None;
            Match  match     = Options.ContentFilter.Match(input, context.CancellationToken);

            if (match.Success)
            {
                ContentWriter  contentWriter = null;
                List <Capture> groups        = null;

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

                    maxReason = GetCaptures(match, FileWriterOptions.GroupNumber, context, isPathWritten: false, predicate: Options.ContentFilter.Predicate, captures: groups);

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

                        contentWriter = ContentWriter.CreateReplace(Options.ContentDisplayStyle, input, Options.ReplaceOptions, FileWriterOptions, outputInfo: outputInfo);
                    }
                    else
                    {
                        contentWriter = new EmptyContentWriter(null, FileWriterOptions);
                    }

                    WriteMatches(contentWriter, groups, context);
                    count = contentWriter.MatchCount;
                }
                finally
                {
                    contentWriter?.Dispose();

                    if (groups != null)
                    {
                        ListCache <Capture> .Free(groups);
                    }
                }
            }

            if (ShouldLog(Verbosity.Detailed) ||
                Options.IncludeSummary)
            {
                Verbosity verbosity = (Options.IncludeSummary) ? Verbosity.Minimal : Verbosity.Detailed;

                WriteLine(verbosity);
                WriteCount("Replacements", count, Colors.Message_OK, verbosity);
                WriteIf(maxReason == MaxReason.CountExceedsMax, "+", Colors.Message_OK, verbosity);
                WriteLine(verbosity);
            }
        }
예제 #9
0
        public static ContentWriter CreateFind(
            ContentDisplayStyle contentDisplayStyle,
            string input,
            ContentWriterOptions options,
            IResultStorage storage,
            MatchOutputInfo outputInfo,
            ContentTextWriter writer,
            bool ask = false)
        {
            if (ask)
            {
                switch (contentDisplayStyle)
                {
                case ContentDisplayStyle.Value:
                case ContentDisplayStyle.ValueDetail:
                    return(new AskValueContentWriter(input, options, storage, outputInfo));

                case ContentDisplayStyle.Line:
                    return(new AskLineContentWriter(input, options, storage));

                case ContentDisplayStyle.UnmatchedLines:
                case ContentDisplayStyle.AllLines:
                    throw new InvalidOperationException();

                default:
                    throw new InvalidOperationException($"Unknown enum value '{contentDisplayStyle}'.");
                }
            }
            else
            {
                switch (contentDisplayStyle)
                {
                case ContentDisplayStyle.Value:
                case ContentDisplayStyle.ValueDetail:
                    return(new ValueContentWriter(input, writer, options, storage, outputInfo));

                case ContentDisplayStyle.Line:
                    return(new LineContentWriter(input, writer, options, storage));

                case ContentDisplayStyle.UnmatchedLines:
                    return(new UnmatchedLineWriter(input, writer, options, storage));

                case ContentDisplayStyle.AllLines:
                    return(new AllLinesContentWriter(input, writer, options, storage));

                default:
                    throw new InvalidOperationException($"Unknown enum value '{contentDisplayStyle}'.");
                }
            }
        }
예제 #10
0
        internal MatchOutputInfo CreateOutputInfo(string input, Match match)
        {
            if (ContentDisplayStyle != ContentDisplayStyle.ValueDetail)
            {
                return(null);
            }

            int groupNumber = ContentFilter.GroupNumber;

            return(MatchOutputInfo.Create(
                       MatchData.Create(input, ContentFilter.Regex, match),
                       groupNumber,
                       includeGroupNumber: groupNumber >= 0,
                       includeCaptureNumber: false,
                       new OutputCaptions(shortMatch: "")));
        }
예제 #11
0
 public static ContentWriter CreateFind(
     ContentDisplayStyle contentDisplayStyle,
     string input,
     ContentWriterOptions options,
     IResultStorage storage,
     MatchOutputInfo outputInfo,
     bool ask = false)
 {
     return(CreateFind(contentDisplayStyle,
                       input,
                       options,
                       storage,
                       outputInfo,
                       ContentTextWriter.Default,
                       ask));
 }
예제 #12
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);
                }
            }
        }