Example #1
0
        public async Task Generate(Options options)
        {
            if (!options.NoBanner)
            {
                this.PrintBanner();
            }

            this.logger.LogInformation("Extracting Variables");
            IDictionary <string, Extraction> variables = await this.ExtractVariables(options.Variable);

            this.logger.LogInformation("Variables Extracted");

            this.logger.LogInformation("Start token extraction");

            Extraction head = await this.GetFirstExtraction(options.Head, options.MultiLine, tokenOpening : options.TokenOpening, tokenClosing : options.TokenClosing);

            List <Extraction> extractions = await this.extractor.Extract(options.Template, options.MultiLine, tokenOpening : options.TokenOpening, tokenClosing : options.TokenClosing);

            Extraction tail = await this.GetFirstExtraction(options.Tail, options.MultiLine, tokenOpening : options.TokenOpening, tokenClosing : options.TokenClosing);

            this.logger.LogInformation("Finished token extraction");

            if (options.Infinite)
            {
                this.logger.LogInformation("infinite flag == true, will run until the process is stopped");
            }

            this.logger.LogInformation("Start generation");
            do
            {
                List <Task> tasks = new List <Task>();
                for (int n = 0; n < options.Runs; n++)
                {
                    IEnumerable <IGrouping <string, Extraction> > grouping = extractions.GroupBy(x => x.SourceFileName);
                    foreach (IGrouping <string, Extraction> group in grouping)
                    {
                        var task = Task.Run(() =>
                        {
                            string fileName = !string.IsNullOrEmpty(group.Key) ? group.Key : "lignator";
                            string path     = options.Output == "/dev/stdout" ? options.Output : $"{options.Output}/{fileName}.{options.Extension}";

                            int seed = 0;
                            lock (this.randomLock)
                            {
                                seed = this.random.Next();
                            }
                            Random taskRandom = new Random(seed);

                            using (ISink sink = this.sink.Start(path, options.MultiLine, options.Clean))
                            {
                                if (head != null)
                                {
                                    sink.Sink(this.transformer.Transform(head.Template, head.Tokens, taskRandom, variables));
                                }

                                this.logger.LogInformation($"Starting run {n + 1} of {options.Runs}");

                                List <Extraction> subGroup = group.ToList();
                                for (int l = 0; l < options.Logs; l++)
                                {
                                    Extraction extraction = subGroup[taskRandom.Next(0, subGroup.Count())];
                                    string transformed    = this.transformer.Transform(extraction.Template, extraction.Tokens, taskRandom, variables);
                                    sink.Sink(transformed);
                                }

                                if (tail != null)
                                {
                                    sink.Sink(this.transformer.Transform(tail.Template, tail.Tokens, taskRandom, variables));
                                }
                            }
                        });

                        if (options.Output == "/dev/stdout")
                        {
                            await task;
                        }
                        else
                        {
                            tasks.Add(task);
                        }
                    }

                    await Task.WhenAll(tasks);
                }
            }while (options.Infinite);

            this.logger.LogInformation("Finished generation");
        }