public async Task GenerateFileAsync( string input, string output, bool unuseUnityAttr, string @namespace, string conditionalSymbol) { // Prepare args var namespaceDot = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : @namespace + "."; var conditionalSymbols = conditionalSymbol?.Split(',') ?? Array.Empty <string>(); // Generator Start... var sw = Stopwatch.StartNew(); logger("Project Compilation Start:" + input); var collector = new MethodCollector(input, conditionalSymbols, logger); logger("Project Compilation Complete:" + sw.Elapsed.ToString()); sw.Restart(); logger("Method Collect Start"); var definitions = collector.CollectServiceInterface(); var hubDefinitions = collector.CollectHubInterface(); GenericSerializationInfo[] genericInfos; EnumSerializationInfo[] enumInfos; ExtractResolverInfo(definitions, out genericInfos, out enumInfos); ExtractResolverInfo(hubDefinitions.Select(x => x.hubDefinition).ToArray(), out var genericInfos2, out var enumInfos2); ExtractResolverInfo(hubDefinitions.Select(x => x.receiverDefintion).ToArray(), out var genericInfos3, out var enumInfos3); enumInfos = enumInfos.Concat(enumInfos2).Concat(enumInfos3).Distinct().OrderBy(x => x.FullName).ToArray(); genericInfos = genericInfos.Concat(genericInfos2).Concat(genericInfos3).Distinct().OrderBy(x => x.FullName).ToArray(); logger("Method Collect Complete:" + sw.Elapsed.ToString()); logger("Output Generation Start"); sw.Restart(); var resolverTemplate = new ResolverTemplate() { Namespace = namespaceDot + "Resolvers", FormatterNamespace = namespaceDot + "Formatters", ResolverName = "MagicOnionResolver", registerInfos = genericInfos.OrderBy(x => x.FullName).Cast <IResolverRegisterInfo>().Concat(enumInfos.OrderBy(x => x.FullName)).ToArray() }; var registerTemplate = new RegisterTemplate { Namespace = @namespace, Interfaces = definitions.Where(x => x.IsServiceDefinition).ToArray(), HubInterfaces = hubDefinitions, UnuseUnityAttribute = unuseUnityAttr }; if (Path.GetExtension(output) == ".cs") { var enumTemplates = enumInfos.GroupBy(x => x.Namespace) .OrderBy(x => x.Key) .Select(x => new EnumTemplate() { Namespace = namespaceDot + "Formatters", enumSerializationInfos = x.ToArray() }) .ToArray(); var texts = definitions .GroupBy(x => x.Namespace) .OrderBy(x => x.Key) .Select(x => new CodeTemplate() { Namespace = x.Key, Interfaces = x.ToArray() }) .ToArray(); var hubTexts = hubDefinitions .GroupBy(x => x.hubDefinition.Namespace) .OrderBy(x => x.Key) .Select(x => new HubTemplate() { Namespace = x.Key, Interfaces = x.ToArray() }) .ToArray(); var sb = new StringBuilder(); sb.AppendLine("// <auto-generated />"); sb.AppendLine(registerTemplate.TransformText()); sb.AppendLine(resolverTemplate.TransformText()); foreach (var item in enumTemplates) { sb.AppendLine(item.TransformText()); } foreach (var item in texts) { sb.AppendLine(item.TransformText()); } foreach (var item in hubTexts) { sb.AppendLine(item.TransformText()); } Output(output, sb.ToString()); } else { Output(NormalizePath(output, registerTemplate.Namespace, "MagicOnionInitializer"), WithAutoGenerated(registerTemplate.TransformText())); Output(NormalizePath(output, resolverTemplate.Namespace, resolverTemplate.ResolverName), WithAutoGenerated(resolverTemplate.TransformText())); foreach (var enumTemplate in enumInfos) { var x = new EnumTemplate() { Namespace = namespaceDot + "Formatters", enumSerializationInfos = new[] { enumTemplate } }; Output(NormalizePath(output, x.Namespace, enumTemplate.Name + "Formatter"), WithAutoGenerated(x.TransformText())); } foreach (var serviceClient in definitions) { var x = new CodeTemplate() { Namespace = serviceClient.Namespace, Interfaces = new[] { serviceClient } }; Output(NormalizePath(output, serviceClient.Namespace, serviceClient.ClientName), WithAutoGenerated(x.TransformText())); } foreach (var hub in hubDefinitions) { var x = new HubTemplate() { Namespace = hub.hubDefinition.Namespace, Interfaces = new[] { hub } }; Output(NormalizePath(output, hub.hubDefinition.Namespace, hub.hubDefinition.ClientName), WithAutoGenerated(x.TransformText())); } } if (definitions.Length == 0 && hubDefinitions.Length == 0) { logger("Generated result is empty, unexpected result?"); } logger("Output Generation Complete:" + sw.Elapsed.ToString()); }
public async Task GenerateFileAsync( string input, string output, string conditionalSymbol, string resolverName, string @namespace, bool useMapMode, string multipleIfDirectiveOutputSymbols) { var namespaceDot = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : @namespace + "."; var conditionalSymbols = conditionalSymbol?.Split(',') ?? Array.Empty <string>(); var multipleOutputSymbols = multipleIfDirectiveOutputSymbols?.Split(',') ?? Array.Empty <string>(); var sw = Stopwatch.StartNew(); foreach (var multioutSymbol in multipleOutputSymbols.Length == 0 ? new[] { string.Empty } : multipleOutputSymbols) { logger("Project Compilation Start:" + input); var compilation = (Path.GetExtension(input) == ".csproj") ? await MessagePackCompilation.CreateFromProjectAsync(input.Split(','), conditionalSymbols.Concat(new[] { multioutSymbol }).ToArray(), cancellationToken) .ConfigureAwait(false) : await MessagePackCompilation.CreateFromDirectoryAsync(input, conditionalSymbols.Concat(new[] { multioutSymbol }).ToArray(), cancellationToken).ConfigureAwait(false); var collector = new TypeCollector(compilation, true, useMapMode, x => Console.WriteLine(x)); logger("Project Compilation Complete:" + sw.Elapsed.ToString()); sw.Restart(); logger("Method Collect Start"); var(objectInfo, enumInfo, genericInfo, unionInfo, closedTypeGenericInfo) = collector.Collect(); logger("Method Collect Complete:" + sw.Elapsed.ToString()); logger("Output Generation Start"); sw.Restart(); if (Path.GetExtension(output) == ".cs") { // SingleFile Output var objectFormatterTemplates = objectInfo .Concat(closedTypeGenericInfo) .GroupBy(x => x.Namespace) .Select(x => new FormatterTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), ObjectSerializationInfos = x.ToArray(), }) .ToArray(); var enumFormatterTemplates = enumInfo .GroupBy(x => x.Namespace) .Select(x => new EnumTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), EnumSerializationInfos = x.ToArray(), }) .ToArray(); var unionFormatterTemplates = unionInfo .GroupBy(x => x.Namespace) .Select(x => new UnionTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), UnionSerializationInfos = x.ToArray(), }) .ToArray(); var resolverTemplate = new ResolverTemplate() { Namespace = namespaceDot + "Resolvers", FormatterNamespace = namespaceDot + "Formatters", ResolverName = resolverName, RegisterInfos = genericInfo.Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo).ToArray(), }; var sb = new StringBuilder(); sb.AppendLine(resolverTemplate.TransformText()); sb.AppendLine(); foreach (var item in enumFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } sb.AppendLine(); foreach (var item in unionFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } sb.AppendLine(); foreach (var item in objectFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } if (multioutSymbol == string.Empty) { await OutputAsync(output, sb.ToString(), cancellationToken).ConfigureAwait(false); } else { var fname = Path.GetFileNameWithoutExtension(output) + "." + MultiSymbolToSafeFilePath(multioutSymbol) + ".cs"; var text = $"#if {multioutSymbol}" + Environment.NewLine + sb.ToString() + Environment.NewLine + "#endif"; await OutputAsync(Path.Combine(Path.GetDirectoryName(output), fname), text, cancellationToken).ConfigureAwait(false); } } else { // Multiple File output foreach (var x in objectInfo.Concat(closedTypeGenericInfo)) { var template = new FormatterTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), ObjectSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false); } foreach (var x in enumInfo) { var template = new EnumTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), EnumSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false); } foreach (var x in unionInfo) { var template = new UnionTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), UnionSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false); } var resolverTemplate = new ResolverTemplate() { Namespace = namespaceDot + "Resolvers", FormatterNamespace = namespaceDot + "Formatters", ResolverName = resolverName, RegisterInfos = genericInfo.Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo).ToArray(), }; await OutputToDirAsync(output, resolverTemplate.Namespace, resolverTemplate.ResolverName, multioutSymbol, resolverTemplate.TransformText(), cancellationToken).ConfigureAwait(false); } if (objectInfo.Length == 0 && enumInfo.Length == 0 && genericInfo.Length == 0 && unionInfo.Length == 0 && closedTypeGenericInfo.Length == 0) { logger("Generated result is empty, unexpected result?"); } } logger("Output Generation Complete:" + sw.Elapsed.ToString()); }
void GenerateCode() { Directory.CreateDirectory(OutputDir); // Generate Libs file using (TextWriter w = File.CreateText(Path.Combine(OutputDir, "Libs.cs"))) { LibsTemplate.Generator = this; LibsTemplate.Libs = new[] { Lib }; w.Write(LibsTemplate.TransformText()); } // Generate user types foreach (Namespace ns in Lib.Namespaces) { if (ns.ParentNamespace is Class) { continue; } var klass = ns as Class; if (klass != null) { if (klass.Disable) { continue; } using (TextWriter w = File.CreateText(Path.Combine(OutputDir, GetSafeFileName(klass.Name) + ".cs"))) { ClassTemplate.Generator = this; ClassTemplate.Class = klass; ClassTemplate.Nested = false; w.Write(ClassTemplate.TransformText()); } continue; } var @enum = ns as Enumeration; if (@enum != null) { using (TextWriter w = File.CreateText(Path.Combine(OutputDir, @enum.Name + ".cs"))) { EnumTemplate.Generator = this; EnumTemplate.Enum = @enum; EnumTemplate.Nested = false; w.Write(EnumTemplate.TransformText()); } continue; } var func = ns as Function; if (func != null) { using (TextWriter w = File.CreateText(Path.Combine(OutputDir, func.Name + ".cs"))) { FunctionTemplate.Generator = this; FunctionTemplate.Function = func; FunctionTemplate.Nested = false; w.Write(FunctionTemplate.TransformText()); } continue; } } }
private void TransformEnums(SmartAppInfo manifest) { var enums = manifest.DataModel.Entities.Where(e => e.IsEnum).AsEnumerable(); foreach (var model in enums) { var template = new EnumTemplate(model, manifest.Id, Constants.DataNamespace); _writingService.WriteFile(Path.Combine(_context.BasePath, template.OutputPath, $"{model.Id}.g.cs"), template.TransformText()); } }
private void GenerateEnum(CodeGenerateDto dto) { foreach (var enumModel in dto.Enums) { var enumTemplate = new EnumTemplate(); enumTemplate.Model = enumModel; Directory.CreateDirectory($@"{dto.BasePath}\ApplicationCore\Enums"); System.IO.File.WriteAllText($@"{dto.BasePath}\ApplicationCore\Enums\{enumModel.Name}.cs", enumTemplate.TransformText()); } }