コード例 #1
0
ファイル: Domain.cs プロジェクト: mbican/cron-hosts
        public async Task Execute(TextReader input, TextWriter output, DateTime dateTimeUtc)
        {
            string?lineOrNull;
            // we are inside #cronhosts block
            var inside = false;
            // wheter to uncomment or comment out lines inside #cronhosts block
            var uncomment  = false;
            var lineNumber = 0;

            while ((lineOrNull = await input.ReadLineAsync()) != null)
            {
                var line = lineOrNull !;
                lineNumber++;
                if (!inside)
                {
                    var match = BeginRegex.Match(line);
                    if (match.Success)
                    {
                        var beginCron = new CronExpression(match.Groups[1].Value, dateTimeUtc);
                        var endCron   = new CronExpression(match.Groups[2].Value, dateTimeUtc);
                        inside = true;
                        var prevEnd   = endCron.PreviousOccurrence();
                        var prevBegin = beginCron.PreviousOccurrence();
                        if (endCron.GetNextOccurrence(prevEnd) <= dateTimeUtc)
                        {
                            endCron.NextOccurrence();
                        }
                        if (beginCron.GetNextOccurrence(prevBegin) <= dateTimeUtc)
                        {
                            beginCron.NextOccurrence();
                        }
                        uncomment = endCron.CurrentDate < beginCron.CurrentDate;
                    }
                    await output.WriteLineAsync(line);
                }
                else
                {
                    if (EndRegex.Match(line).Success)
                    {
                        inside = false;
                        await output.WriteLineAsync(line);
                    }
                    else if (uncomment)
                    {
                        await output.WriteLineAsync(UncommentLine(line));
                    }
                    else
                    {
                        await output.WriteLineAsync(CommentOutLine(line));
                    }
                }
            }
        }
コード例 #2
0
        protected override List <TplResult> InnerProcess(List <TplResult> input)
        {
            var results  = new List <TplResult>();
            var sb       = new StringBuilder();
            var grouping = false;

            foreach (var i in input)
            {
                if (i.HasField(TargetField))
                {
                    var currentValue = i.StringValueOf(TargetField);

                    //Start our group
                    if (!grouping && StartRegex.IsMatch(currentValue))
                    {
                        grouping = true;
                    }

                    //Add to our group
                    if (grouping)
                    {
                        sb.AppendLine(currentValue);
                    }

                    //End our group
                    if (grouping && EndRegex.IsMatch(currentValue))
                    {
                        i.AddOrUpdateField(TargetField, sb.ToString().TrimEnd());
                        results.Add(i);

                        grouping = false;
                        sb.Clear();
                    }
                }
            }

            return(results);
        }