Beispiel #1
0
        private static void BuildVisitorModel()
        {
            Console.Write("Gathering Type and Visitor Metadata Details...");
            var          identityManager = IntermediateCliGateway.CreateIdentityManager(IntermediateCliGateway.GetRuntimeEnvironmentInfo(CliFrameworkPlatform.AnyPlatform));
            List <IType> ElementsToExcludeFromTranslationMethods =
                new[] { typeof(IBinaryOperationExpression <,>).GetTypeReference(identityManager) }.ToList();
            var assemblyToReflect           = identityManager.ObtainAssemblyReference(typeof(IExpression).Assembly);
            var visitorTarget               = typeof(VisitorTargetAttribute).GetTypeReference <IType>(identityManager);
            var visitorImplementationTarget = typeof(VisitorImplementationTargetAttribute).GetTypeReference <IType>(identityManager);
            var visitorContextualDetail     = new Dictionary <VisitorTargetDetail, List <IType> >();
            var distinctImplementations     = new HashSet <VisitorImplementationTargetDetail>();
            var assemblyMetadata            = assemblyToReflect.Metadata.Where(k => k.Type == visitorImplementationTarget);

            foreach (var metadatum in assemblyMetadata.Where(k => k.Type == visitorImplementationTarget))
            {
                var currentDetail = VisitorImplementationTargetDetail.DeriveFromMetadata(metadatum);
                distinctImplementations.Add(currentDetail);
            }
            foreach (var type in assemblyToReflect.GetTypes())
            {
                foreach (var metadatum in type.Metadata.Where(k => k.Type == visitorTarget))
                {
                    var          currentDetail = VisitorTargetDetail.DeriveFromMetadata(metadatum);
                    List <IType> seriesRelevant;
                    if (!visitorContextualDetail.TryGetValue(currentDetail, out seriesRelevant))
                    {
                        visitorContextualDetail.Add(currentDetail, seriesRelevant = new List <IType>());
                    }
                    seriesRelevant.Add(type);
                }
            }
            visitorContextualDetail = (from kvp in visitorContextualDetail
                                       orderby kvp.Key.TargetContext,
                                       kvp.Key.DerivedThroughInheriting == null
                                               ? string.Empty
                                               : kvp.Key.DerivedThroughInheriting,
                                       kvp.Key.ContextualVisitor,
                                       kvp.Key.YieldingVisitor
                                       select kvp).ToDictionary(k => k.Key, v => v.Value);
            Console.WriteLine("Done.");
            Console.Write("Building Scaffolds...");
            Dictionary <VisitorTargetDetail, VisitorBuilder> builders = new Dictionary <VisitorTargetDetail, VisitorBuilder>();
            /* Create the builders. */
            var derivedDetail = visitorContextualDetail.Keys.Where(k => !string.IsNullOrEmpty(k.DerivedThroughInheriting)).ToArray();
            var visitorDetailMinusHierarchy = visitorContextualDetail.Keys.Except(derivedDetail).ToArray();

            foreach (var contextKey in visitorDetailMinusHierarchy)
            {
                builders.Add(contextKey, new VisitorBuilder(contextKey, visitorContextualDetail[contextKey]));
            }

            var assembly     = CreateAssembly("_abs.slf.ast.visitors", identityManager);
            var namespaces   = visitorContextualDetail.Values.SelectMany(k => k).Select(k => k.NamespaceName).Distinct().ToArray();
            var relativeRoot = namespaces.GetRelativeRoot(".");

            assembly.DefaultNamespace = assembly.Namespaces.Add(relativeRoot);
            foreach (var contextKey in visitorDetailMinusHierarchy)
            {
                var builder = builders[contextKey];
                builder.ScaffoldInterface(assembly);
            }
            Console.WriteLine("Done.");
            Console.Write("Building Typeless Visitor Scaffolds...");
            var typelessBuilders = BuildInheritedVisitors(visitorContextualDetail, builders, assembly);

            Console.WriteLine("Done.");
            Console.Write("Building Visitor Interfaces...");
            foreach (var contextKey in visitorDetailMinusHierarchy)
            {
                var builder = builders[contextKey];
                builder.BuildVisitor();
            }
            Console.WriteLine("Done.");
            Console.Write("Building Typeless Visitor Interfaces...");
            foreach (var contextKey in typelessBuilders.Keys)
            {
                var builder = typelessBuilders[contextKey];
                builder.BuildVisitor();
            }
            Console.WriteLine("Done.");

            /*
             * Console.Write("Building Visitor Implementation Scaffolding...");
             * var visitorImplementations = BuildVisitorImplementations(visitorContextualDetail, distinctImplementations.ToList(), builders, typelessBuilders, assembly);
             * Console.WriteLine("Done.");
             * Console.Write("Building Visitor Implementations...");
             * foreach (var visitorImplementationKey in visitorImplementations.Keys)
             * {
             *  var builder = visitorImplementations[visitorImplementationKey];
             *  builder.BuildImplementation(assemblyToReflect);
             * }
             * Console.WriteLine("Done.");
             */
            Console.WriteLine("Press any key to write the output...");
            Console.ReadKey(true);
            Console.Write("Writing Project...");
            WriteProject(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "VisitorModel"), assembly);
            Console.WriteLine("Done.");
        }