public string AssembleCode() { var apiModel = new DbContextParser(typeof(AdventureWorksContext)).Construct(); var controllerCode = apiModel.Controllers.Select(controller => $@" public class {controller.ResourceCollectionName}GeneratedController: BaseRoslynController<{controller.ResourceCollectionName}> {{ public {controller.ResourceCollectionName}GeneratedController(AdventureWorksContext dbContext):base(dbContext) {{ }} }} "); var aggregatedCode = $@" using Learning.DAL.Generation.Mvc; using Learning.DAL.Models.AdventureWorksModels; using Learning.DAL.Models; namespace Learning.DAL.Server.Generated {{ { controllerCode.Aggregate((final, next) => final += next) } }}"; return(aggregatedCode); }
public Task <SyntaxList <MemberDeclarationSyntax> > GenerateAsync(int y)//TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken) { throw new NotImplementedException(); var generator = SyntaxGenerator.GetGenerator(new AdhocWorkspace(), LanguageNames.CSharp); var baseNode = generator.IdentifierName("BaseGeneratedController"); var apiModel = new DbContextParser(typeof(AdventureWorksContext)).Construct(); var classes = apiModel.Controllers.Select(controller => { string controllerName = $"{controller.ResourceCollectionName}Controller"; var members = CreateMembers(generator, controllerName); return(generator.ClassDeclaration( controllerName, typeParameters: null, accessibility: Accessibility.Public, modifiers: DeclarationModifiers.Abstract, baseType: baseNode, members: members)); }); // Generate the class ////// Our generator is applied to any class that our attribute is applied to. //var applyToClass = (ClassDeclarationSyntax)context.ProcessingNode; //var apiModel = new DbContextParser(typeof(GoodDogDbContext)).Construct(); //var controllerCopies = apiModel.Controllers.Select(controller => //{ // var genericType = SyntaxFactory.SingletonSeparatedList<TypeSyntax>(SyntaxFactory.ParseTypeName(controller.ResourceCollectionName)); // var newBaseList = applyToClass.BaseList.Types.Insert(0, SyntaxFactory.SimpleBaseType( // SyntaxFactory.GenericName(SyntaxFactory.Identifier("BaseGeneratedController")) // .WithTypeArgumentList(SyntaxFactory.TypeArgumentList(genericType)))); // return applyToClass.WithIdentifier(SyntaxFactory.Identifier($"{controller.ResourceCollectionName}Controller")) // .WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.SingletonSeparatedList( // SyntaxFactory.SimpleBaseType(SyntaxFactory.GenericName(SyntaxFactory.Identifier("BaseGeneratedController")) // .WithTypeArgumentList(SyntaxFactory.TypeArgumentList(genericType)))); //}); ////var constructor = SyntaxFactory.ConstructorDeclaration("TestClientApi") //// .WithInitializer( //// SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer) //// // could be BaseConstructorInitializer or ThisConstructorInitializer //// .AddArgumentListArguments( //// SyntaxFactory.Argument(SyntaxFactory.IdentifierName("entryPoint")) //// )); ////applyToClass.AddMembers(constructor); //// // Return our modified copy. It will be added to the user's project for compilation. //var results = SyntaxFactory.List<MemberDeclarationSyntax>(controllerCopies); //return Task.FromResult(results); }