public async ValueTask <bool> MoveNextAsync() { if (!(await _reader.ReadAsync()) || _reader.TokenType == JsonToken.EndArray) { return(false); } var jObject = await JObject.LoadAsync(_reader, _jsonLoadSettings); Current = new MailData(JsonNetValueResolver.EvalJObject(jObject)); await _reader.SkipAsync(); return(true); }
private int Invoke(SourceTypes sourceType, string sourceData, string templateData, string targetPath, string buildLog, string sourceDataNetType, string sourceDataNetFunction) { PerformanceMetrics = new Dictionary <string, PerformanceMetric>(); using (StartMetric("Whole Operation")) { try { ContextObject.DefaultFormatter.AddFromType(typeof(DynamicLinq)); if (buildLog != null) { BuildLog = new StreamWriter(new FileStream(buildLog, FileMode.OpenOrCreate)); } WriteHeader(); WriteLine("- Take '" + sourceType + "' from '" + sourceData + "', put it into '" + templateData + "' and store the result at '" + targetPath + "'"); if (!File.Exists(sourceData)) { WriteErrorLine($"- The source file at '{sourceData}' does not exist"); CloseMessage(); return(-1); } if (!File.Exists(templateData)) { WriteErrorLine($"- The template file at '{templateData}' does not exist"); CloseMessage(); return(-1); } object data = null; IValueResolver resolver = null; using (StartMetric("Get Data")) { try { switch (sourceType) { case SourceTypes.Json: WriteLine($"- Load file '{sourceData}' and parse Json from it"); data = JsonConvert.DeserializeObject(File.ReadAllText(sourceData)); resolver = new JsonNetValueResolver(); break; case SourceTypes.Xml: throw new NotSupportedException("The XML deserialisation is currently not supported"); case SourceTypes.NetFunction: Console.WriteLine($"- Load Assembly '{sourceData}', search for type '{sourceDataNetType}'" + $" and run public static object {sourceDataNetFunction}(); to obtain data"); if (sourceDataNetType == null) { WriteErrorLine("- Expected the --source-data-net-type argument to contain an valid type"); CloseMessage(); return(-1); } if (sourceDataNetFunction == null) { WriteErrorLine("- Expected the --source-data-net-function argument to contain an valid type"); CloseMessage(); return(-1); } var assembly = Assembly.LoadFrom(sourceData); var type = assembly.GetType(sourceDataNetType); if (type == null) { WriteErrorLine($"- The type '{sourceDataNetType}' was not found"); CloseMessage(); return(-1); } var method = type.GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(e => e.ReturnType != typeof(void)) .Where(e => e.GetParameters().Length == 0) .FirstOrDefault(e => e.Name.Equals(sourceDataNetFunction)); if (method == null) { WriteErrorLine($"- The method '{sourceDataNetFunction}' was not found in type '{type}'. Expected to find a public static void Method without parameters."); CloseMessage(); return(-1); } data = method.Invoke(null, null); //File.WriteAllText("documentation.json", JsonConvert.SerializeObject(data)); break; default: throw new ArgumentOutOfRangeException(nameof(sourceType), sourceType, null); } } catch (Exception e) { WriteErrorLine("- Error while loading the source file"); WriteErrorLine("----"); WriteErrorLine(e.ToString()); CloseMessage(); return(-1); } } string template; using (StartMetric("Get Template Content")) { WriteLine($"- Read all contents from file '{templateData}'"); template = File.ReadAllText(templateData); WriteLine($"- Read '{template.Length}' chars from file"); } using (var sourceFs = new FileStream(targetPath, FileMode.OpenOrCreate)) { MorestachioDocumentInfo document; WriteLine("- Parse the template"); using (StartMetric("Parse Template")) { document = Parser.ParseWithOptions(new ParserOptions(template, () => sourceFs, Encoding.UTF8, true) { Timeout = TimeSpan.FromMinutes(1), ValueResolver = resolver, }); } if (document.Errors.Any()) { var sb = new StringBuilder(); sb.AppendLine("- Template Errors: "); foreach (var morestachioError in document.Errors) { morestachioError.Format(sb); sb.AppendLine(); sb.AppendLine("----"); } WriteErrorLine(sb.ToString()); CloseMessage(); return(-1); } WriteLine("- Execute the parsed template"); using (StartMetric("Create Document")) { document.Create(data); } WriteLine("- Done"); Hr(); } } finally { BuildLog?.Dispose(); } } WriteLine("Performance Metrics: "); foreach (var performanceMetric in PerformanceMetrics) { WriteLine("\t Name: " + performanceMetric.Key); WriteLine("\t Time: " + performanceMetric.Value.Stopwatch.Elapsed.ToString("g")); Hr(); } CloseMessage(); return(1); }