|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using CodeFactory.WinVs; |
| 7 | +using CodeFactory.WinVs.Models.CSharp; |
| 8 | +using CodeFactory.WinVs.Models.CSharp.Builder; |
| 9 | + |
| 10 | +namespace CodeFactory.Automation.Standard.NDF.Logic |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Logic for adding missing members. |
| 14 | + /// </summary> |
| 15 | + public static class AddMissingMembers |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Name of the Microsoft logging library. |
| 19 | + /// </summary> |
| 20 | + public const string MicrosoftLoggingNamespace = "Microsoft.Extensions.Logging"; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Name of the NDF namespace. |
| 24 | + /// </summary> |
| 25 | + public const string NDFNamespace = "CodeFactory.NDF"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Add missing interface members |
| 29 | + /// </summary> |
| 30 | + /// <param name="source">The CodeFactory automation for Visual Studio Windows</param> |
| 31 | + /// <param name="sourceCode">Source code model to be updated with add members in the target class.</param> |
| 32 | + /// <param name="updateClass">Class model to add missing members to.</param> |
| 33 | + /// <param name="supportsLogging">Flag that determines if logging is enabled.</param> |
| 34 | + /// <param name="loggerFieldName">Optional, the name of the field to use for logging.</param> |
| 35 | + /// <returns></returns> |
| 36 | + public static async Task<CsClass> AddMissingMembersStandardNDFAsync(this IVsActions source, CsSource sourceCode, |
| 37 | + CsClass updateClass, bool supportsLogging, string loggerFieldName = "_logger") |
| 38 | + { |
| 39 | + //Bounds checks to make sure all data needed is provided. |
| 40 | + if (sourceCode == null) |
| 41 | + throw new CodeFactoryException( |
| 42 | + "Visual Studio automation for CodeFactory was not provided cannot add missing members."); |
| 43 | + |
| 44 | + if (sourceCode == null) |
| 45 | + throw new CodeFactoryException("No source code was provided, cannot add the missing members."); |
| 46 | + |
| 47 | + if (updateClass == null) |
| 48 | + throw new CodeFactoryException( |
| 49 | + "No target class to add missing members was provided, cannot add the missing members."); |
| 50 | + |
| 51 | + //Get the missing members to be added |
| 52 | + var missingMembers = updateClass.GetMissingInterfaceMembers(); |
| 53 | + |
| 54 | + //If no missing members are found just return the current class. |
| 55 | + if (!missingMembers.Any()) return updateClass; |
| 56 | + |
| 57 | + //Creating the source code manager for the class. |
| 58 | + var manager = new SourceClassManager(sourceCode, updateClass, source); |
| 59 | + manager.LoadNamespaceManager(); |
| 60 | + |
| 61 | + |
| 62 | + //Creating the blocks to be used for code generation |
| 63 | + ILoggerBlock loggerBlock = supportsLogging ? new LoggerBlockNDF(loggerFieldName) : null; |
| 64 | + |
| 65 | + var boundsChecks = new IBoundsCheckBlock[] |
| 66 | + { |
| 67 | + new BoundsCheckBlockStringNDF(true, loggerBlock), |
| 68 | + new BoundsCheckBlockNullNDF(true, loggerBlock) |
| 69 | + }; |
| 70 | + |
| 71 | + var catchBlocks = new ICatchBlock[] |
| 72 | + { |
| 73 | + new CatchBlockManagedExceptionNDF(loggerBlock), |
| 74 | + new CatchBlockExceptionNDF(loggerBlock) |
| 75 | + }; |
| 76 | + |
| 77 | + ITryBlock tryBlock = new TryBlockStandard(loggerBlock, catchBlocks); |
| 78 | + |
| 79 | + if(supportsLogging) await manager.UsingStatementAddAsync(MicrosoftLoggingNamespace); |
| 80 | + await manager.UsingStatementAddAsync(NDFNamespace); |
| 81 | + |
| 82 | + //Creating the builders to generate code by member type. |
| 83 | + IMethodBuilder methodBuilder = new MethodBuilderStandard(loggerBlock, boundsChecks, tryBlock); |
| 84 | + IPropertyBuilder propertyBuilder = new PropertyBuilderStandard(); |
| 85 | + IEventBuilder eventBuilder = new EventBuilderStandard(); |
| 86 | + |
| 87 | + //Process all missing properties. |
| 88 | + var missingProperties = missingMembers.Where(m => m.MemberType == CsMemberType.Property).Cast<CsProperty>() |
| 89 | + .ToList(); |
| 90 | + |
| 91 | + foreach (var missingProperty in missingProperties) |
| 92 | + { |
| 93 | + var propertySyntax = await propertyBuilder.BuildPropertyAsync(missingProperty, manager, 2); |
| 94 | + |
| 95 | + if(propertySyntax == null) continue; |
| 96 | + |
| 97 | + await manager.PropertiesAddAfterAsync(propertySyntax); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + //Process all missing methods. |
| 102 | + var missingMethods = missingMembers.Where(m => m.MemberType == CsMemberType.Method).Cast<CsMethod>() |
| 103 | + .ToList(); |
| 104 | + |
| 105 | + foreach (var missingMethod in missingMethods) |
| 106 | + { |
| 107 | + var methodSyntax = await methodBuilder.BuildMethodAsync(missingMethod, manager, 2); |
| 108 | + |
| 109 | + if(methodSyntax == null) continue; |
| 110 | + |
| 111 | + await manager.MethodsAddAfterAsync(methodSyntax); |
| 112 | + } |
| 113 | + |
| 114 | + //Process all missing events. |
| 115 | + var missingEvents = missingMembers.Where(m => m.MemberType == CsMemberType.Event).Cast<CsEvent>() |
| 116 | + .ToList(); |
| 117 | + |
| 118 | + foreach (var missingEvent in missingEvents) |
| 119 | + { |
| 120 | + var eventSyntax = await eventBuilder.BuildEventAsync(missingEvent, manager, 2); |
| 121 | + |
| 122 | + if(eventSyntax == null) continue; |
| 123 | + |
| 124 | + await manager.EventsAddAfterAsync(eventSyntax); |
| 125 | + } |
| 126 | + |
| 127 | + return manager.Container; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments