Beispiel #1
0
        public static MorestachioDocumentInfo ParseWithOptions([NotNull] ParserOptions parsingOptions)
        {
            if (parsingOptions == null)
            {
                throw new ArgumentNullException(nameof(parsingOptions));
            }

            if (parsingOptions.SourceFactory == null)
            {
                throw new ArgumentNullException(nameof(parsingOptions), "The given Stream is null");
            }

            var errors   = new List <IMorestachioError>();
            var profiler = new PerformanceProfiler(parsingOptions.ProfileExecution);
            Queue <TokenPair> tokens;

            using (profiler.Begin("Tokenize"))
            {
                tokens = new Queue <TokenPair>(Tokenizer.Tokenize(parsingOptions, errors, profiler));
            }

            //if there are any errors do not parse the template
            var documentInfo = new MorestachioDocumentInfo(parsingOptions,
                                                           errors.Any() ? null : Parse(tokens), errors);

            documentInfo.Profiler = profiler;
            return(documentInfo);
        }
Beispiel #2
0
 internal MorestachioDocumentResult(IByteCounterStream stream,
                                    PerformanceProfiler profiler,
                                    IDictionary <string, object> variables)
 {
     Stream            = stream;
     Profiler          = profiler;
     CapturedVariables = variables;
 }
 public static IDisposable BeginSafe(this PerformanceProfiler profiler, string name)
 {
     return(profiler?.Begin(name) ?? _instance);
 }
Beispiel #4
0
 internal MorestachioDocumentResult(IByteCounterStream stream,
                                    PerformanceProfiler profiler)
 {
     Stream   = stream;
     Profiler = profiler;
 }
        public async MorestachioDocumentResultPromise CreateAsync([NotNull] object data, CancellationToken token)
        {
            if (Errors.Any())
            {
                throw new AggregateException("You cannot Create this Template as there are one or more Errors. See Inner Exception for more infos.", Errors.Select(e => e.GetException())).Flatten();
            }

            if (Document is MorestachioDocument morestachioDocument && morestachioDocument.MorestachioVersion !=
                MorestachioDocument.GetMorestachioVersion())
            {
                throw new InvalidOperationException($"The supplied version in the Morestachio document " +
                                                    $"'{morestachioDocument.MorestachioVersion}'" +
                                                    $" is not compatible with the current morestachio version of " +
                                                    $"'{MorestachioDocument.GetMorestachioVersion()}'");
            }

            var timeoutCancellation = new CancellationTokenSource();

            if (ParserOptions.Timeout != TimeSpan.Zero)
            {
                timeoutCancellation.CancelAfter(ParserOptions.Timeout);
                var anyCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(token, timeoutCancellation.Token);
                token = anyCancellationToken.Token;
            }

            PerformanceProfiler profiler = null;

            using (var byteCounterStream = ParserOptions.StreamFactory.GetByteCounterStream(ParserOptions))
            {
                if (byteCounterStream?.Stream == null)
                {
                    throw new NullReferenceException("The created stream is null.");
                }
                var context   = ParserOptions.CreateContextObject("", token, data);
                var scopeData = new ScopeData();
                try
                {
                    if (ParserOptions.ProfileExecution)
                    {
                        scopeData.Profiler = profiler = new PerformanceProfiler(true);
                    }
                    await MorestachioDocument.ProcessItemsAndChildren(new[] { Document }, byteCounterStream,
                                                                      context, scopeData);

                    if (timeoutCancellation.IsCancellationRequested)
                    {
                        throw new TimeoutException($"The requested timeout of '{ParserOptions.Timeout:g}' for template generation was reached.");
                    }
                }
                finally
                {
                    if (!CaptureVariables)
                    {
                        scopeData.Dispose();
                        scopeData.Variables.Clear();
                    }
                }

                return(new MorestachioDocumentResult(byteCounterStream.Stream,
                                                     profiler,
                                                     scopeData.Variables.ToDictionary(e => e.Key, e => scopeData.GetFromVariable(e.Value).Value)));
            }
        }