public void Execute(GeneratorExecutionContext context) { var generatedClass = new CodeGenClass("XmlPrinter", Scope.Public, ClassType.Static); var xmlFiles = context.AdditionalFiles.Where(f => f.Path.EndsWith(".xml")); foreach (var xmlFile in xmlFiles) { var itemElements = XDocument.Load(xmlFile.Path)?.Root?.Elements("item"); if (itemElements == null) { continue; } foreach (var itemElement in itemElements) { string name = itemElement.Attribute("name")?.Value; string value = itemElement.Value; if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(value)) { var generatedMethod = new CodeGenMethod( $"Print{name}", null, Scope.Public, MethodType.Static, null, null, $@" Console.WriteLine($""Hello, {value}""); "); generatedClass.Methods.Add(generatedMethod); } } } var generatedNamespace = new CodeGenNamespace("GeneratedNamespace"); generatedNamespace.Usings.Add("System"); generatedNamespace.Content.Add(generatedClass); var generatedCodeString = generatedNamespace.GenerateCode(); var sourceText = SourceText.From(generatedCodeString, Encoding.UTF8); context.AddSource("XmlPrinter.cs", sourceText); }
public CodeGenClass Create(DecoratorClassInformation factoryInformation) { var generatedExtensionClass = new CodeGenClass( $"{factoryInformation.ParentInformation.DecoratorName}Extensions", Scope.Public, ClassType.Static); IEnumerable <CodeGenGeneric> genericTypes = factoryInformation.GenericTypes?.Select( parameter => FactoryHelpers.GenerateMethodParameter(parameter, factoryInformation.TypeConstraints)); var extensionParams = new List <string>(new[] { $"this {factoryInformation.DecoratedType} decorated" }); var decoratedConstructorParams = new List <string>(new[] { "decorated" }); if (factoryInformation.Template.ConstructorParams != null) { foreach (ConstructorParam constructorParam in factoryInformation.Template.ConstructorParams) { extensionParams.Add($"{constructorParam.Type} {constructorParam.Name}"); decoratedConstructorParams.Add(constructorParam.Name); } } string genericTypesString = factoryInformation.GenericTypes.ToTypeParamList(); string paramNames = string.Join(", ", decoratedConstructorParams); string body = $"return new {factoryInformation.ParentInformation.DecoratorName}{genericTypesString}({paramNames});"; string returnType = factoryInformation.DerviedFrom != null ? factoryInformation.DerviedFrom : $"{factoryInformation.ParentInformation.DecoratorName}{genericTypesString}"; var generatedExtensionMethod = new CodeGenMethod( $"DecorateWith{factoryInformation.Template.Label}", returnType, Scope.Public, MethodType.Static, genericTypes, extensionParams, body); generatedExtensionClass.Methods.Add(generatedExtensionMethod); return(generatedExtensionClass); }
protected override void GenerateClassMethods(GeneratorExecutionContext context, CodeGenNamespace @namespace, CodeGenClass @class) { StaticMethodCallSyntaxReceiver syntaxReceiver = (StaticMethodCallSyntaxReceiver)context.SyntaxReceiver; // singleton store @class.Variables.Add(new CodeGenVariable("_SingletonLock", "object", Scope.Private, VariableType.Static, true, "new object()")); @class.Variables.Add(new CodeGenVariable("_Singletons", "IDictionary<Type, object>", Scope.Private, VariableType.Static, true, "new Dictionary<Type, object>()")); // static methods AddRegistrationMethods(@class); AddGetTMethod(@class); // get registrations IDictionary <string, DIRegistration> registrations = ProcessCalls(context, syntaxReceiver.Calls); // Function dictionary used by get method AddGetFunctionsDictionary(@class, registrations); foreach (DIRegistration registration in registrations.Values) { // Build stack contining the initialization of this registered resource, including any dependencies. Stack <DIDependencyInitialization> initStack = BuildInitStack(registration, registrations); // Use init stack to generate body string body = BuildTypedGetMethodBody(initStack); var method = new CodeGenMethod( registration.DirectGetMethodName, registration.IdentifierType, Scope.Private, MethodType.Static, null, null, body); @class.Methods.Add(method); } ModifyNamespace(@namespace); }
private CodeGenMethod GenerateMethod(ApiMethod apiMethod) { if (apiMethod is null) { throw new ArgumentNullException(nameof(apiMethod)); } var methodParams = new List <string>(); var methodParamComments = new Dictionary <string, string>(); foreach (ApiType qp in apiMethod.QueryParams) { methodParams.Add($"[FromQuery] {ResolveModelType(qp.Type)} {qp.Name}"); if (!string.IsNullOrWhiteSpace(qp.Description)) { methodParamComments.Add(qp.Name, qp.Description); } } if (apiMethod.RequestBodyType != null) { methodParams.Add($"[FromBody] {ResolveModelType(apiMethod.RequestBodyType.Type)} requestBody"); methodParamComments.Add("requestBody", "The body of the request."); } string returnComment = apiMethod.ResponseBodyType != null ? @$ "Response containing the content of type <see cref=" "{ResolveModelType(apiMethod.ResponseBodyType.Type)}" "/>" : null; var method = new CodeGenMethod( apiMethod.Name, apiMethod.Async ? "Task<IActionResult>" : "IActionResult", Scope.Public, MethodType.Normal, null, methodParams, GenerateMethodBody(apiMethod)); method.Comment = new CodeGenComment( apiMethod.Description, paramComments: methodParamComments, returnComment: returnComment); var methodAttributeType = apiMethod.Method switch { "get" => "HttpGet", "post" => "HttpPost", "put" => "HttpPut", "patch" => "HttpPatch", "delete" => "HttpDelete", _ => throw new Exception("Oh no!") }; var methodAttributeRoute = !string.IsNullOrWhiteSpace(apiMethod.MethodRoute) ? new[] { $"\"{apiMethod.MethodRoute}\"" } : null; method.Attributes.Add(new CodeGenAttribute(methodAttributeType, methodAttributeRoute)); return(method); }
public CodeGenClass Create(DecoratorClassInformation factoryInformation) { IEnumerable <CodeGenGeneric> genericTypes = factoryInformation.GenericTypes?.Select( parameter => FactoryHelpers.GenerateMethodParameter(parameter, factoryInformation.TypeConstraints)); string[] derivedFrom = !string.IsNullOrEmpty(factoryInformation.DerviedFrom) ? new[] { factoryInformation.DerviedFrom } : null; var generatedDecoratorClass = new CodeGenClass( factoryInformation.ParentInformation.DecoratorName, Scope.Public, ClassType.Normal, genericTypes, derivedFrom); generatedDecoratorClass.Comment = new CodeGenComment($@"Generated {factoryInformation.Template.Label} decorator for the {factoryInformation.DecoratedType } type. Auto-generated on {DateTimeOffset.Now}"); var constructorParams = new List <string>(new[] { $"{factoryInformation.DecoratedType} decorated" }); var constructorBodyBuilder = new StringBuilder(); constructorBodyBuilder.AppendLine("_Decorated = decorated ?? throw new ArgumentNullException(nameof(decorated));"); if (factoryInformation.Template.ConstructorParams != null) { foreach (ConstructorParam constructorParam in factoryInformation.Template.ConstructorParams) { constructorParams.Add($"{constructorParam.Type} {constructorParam.Name}"); constructorBodyBuilder.AppendLine($"_{constructorParam.Name} = {constructorParam.Name};"); generatedDecoratorClass.Variables.Add(new CodeGenVariable( $"_{constructorParam.Name}", constructorParam.Type, Scope.Private, readOnly: true)); } } // Constructor generatedDecoratorClass.Constructors.Add(new CodeGenConstructor( factoryInformation.ParentInformation.DecoratorName, Scope.Public, constructorParams, constructorBodyBuilder.ToString())); // Decorated variable generatedDecoratorClass.Variables.Add(new CodeGenVariable( "_Decorated", factoryInformation.DecoratedType, Scope.Private, readOnly: true)); foreach (DecoratorMethodInformation methodInformation in factoryInformation.MethodInformation) { CodeGenMethod method = _MethodFactory.Create(methodInformation); generatedDecoratorClass.Methods.Add(method); } foreach (DecoratorPropertyInformation propertyInformation in factoryInformation.PropertyInformation) { CodeGenProperty property = _PropertyFactory.Create(propertyInformation); generatedDecoratorClass.Properties.Add(property); } return(generatedDecoratorClass); }