Block() public method

public Block ( System.Action insideBlock ) : void
insideBlock System.Action
return void
Exemplo n.º 1
0
        public SortedDictionary<string, string> GenerateCode()
        {
            this.sb = new StringBuilder(4096);
            this.cw = new CodeWriter(sb, 4);
            this.generateQueue = new Queue<Type>();
            this.visited = new HashSet<Type>();
            this.lookupScripts = new List<Type>();

            foreach (var assembly in this.Assemblies)
            foreach (var fromType in assembly.GetTypes())
            {
                if (fromType.IsAbstract)
                    continue;

                if (fromType.IsSubclassOf(typeof(ServiceRequest)) ||
                    fromType.IsSubclassOf(typeof(ServiceResponse)) ||
                    fromType.IsSubclassOf(typeof(Row)) ||
                    fromType.GetCustomAttribute<ScriptIncludeAttribute>() != null)
                {
                    EnqueueType(fromType);
                    continue;
                }

                if (fromType.GetCustomAttribute<FormScriptAttribute>() != null ||
                    fromType.GetCustomAttribute<ColumnsScriptAttribute>() != null)
                {
                    EnqueueTypeMembers(fromType);
                    continue;
                }

                if (fromType.GetCustomAttribute<LookupScriptAttribute>() != null)
                {
                    lookupScripts.Add(fromType);
                    continue;
                }
            }

            Dictionary<Type, string> generatedCode = new Dictionary<Type, string>();
            while (generateQueue.Count > 0)
            {
                var type = generateQueue.Dequeue();

                if (!this.Assemblies.Contains(type.Assembly))
                    continue;

                GenerateCodeFor(type);
                generatedCode[type] = sb.ToString();
                sb.Clear();
            }

            sb.Clear();

            sb.AppendLine();

            var ordered = generatedCode.Keys.OrderBy(x => GetNamespace(x)).ThenBy(x => x.Name);
            var byNameSpace = ordered.ToLookup(x => GetNamespace(x));
            var byOwnerType = ordered.ToLookup(x => (x.IsNested ? x.DeclaringType : null));
            var outputted = new HashSet<Type>();

            var result = new SortedDictionary<string, string>();

            foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key))
            {
                Action<Type> outputType = delegate(Type type)
                {
                    var filename = ns.Key + "." + type.Name + ".cs";

                    foreach (var rn in RootNamespaces)
                    {
                        if (filename.StartsWith(rn + "."))
                            filename = filename.Substring(rn.Length + 1);
                    }

                    result.Add(filename, sb.ToString());
                };

                foreach (var owner in byOwnerType)
                {
                    bool skip = false;

                    sb.Clear();
                    sb.AppendLine();
                    cw.Indented("namespace ");
                    sb.AppendLine(ns.Key);

                    cw.InBrace(delegate
                    {
                        foreach (var usingNamespace in UsingNamespaces.OrderBy(x => x))
                        {
                            cw.Indented("using ");
                            sb.Append(usingNamespace);
                            sb.AppendLine(";");
                        }

                        sb.AppendLine();

                        if (owner.Key == null)
                        {
                            skip = true;
                            return;
                        }

                        if (GetNamespace(owner.Key) != ns.Key)
                        {
                            skip = true;
                            return;
                        }

                        if (outputted.Contains(owner.Key))
                        {
                            skip = true;
                            return;
                        }

                        outputted.Add(owner.Key);

                        if (!generatedCode.ContainsKey(owner.Key))
                            return;

                        string code = generatedCode[owner.Key].TrimEnd();
                        code = code.Substring(0, code.Length - 1).TrimEnd();
                        cw.IndentedMultiLine(code);

                        cw.Block(delegate
                        {
                            sb.AppendLine();

                            foreach (var subType in owner)
                            {
                                cw.IndentedMultiLine(generatedCode[subType]);
                                outputted.Add(subType);
                            }
                        });

                        cw.IndentedLine("}");
                        sb.AppendLine();
                    });

                    if (skip)
                        continue;

                    outputType(owner.Key);
                }

                foreach (var type in ns)
                {
                    if (outputted.Contains(type))
                        continue;

                    sb.Clear();
                    sb.AppendLine();
                    cw.Indented("namespace ");
                    sb.AppendLine(ns.Key);

                    cw.InBrace(() =>
                    {
                        foreach (var usingNamespace in UsingNamespaces.OrderBy(x => x))
                        {
                            cw.Indented("using ");
                            sb.Append(usingNamespace);
                            sb.AppendLine(";");
                        }

                        sb.AppendLine();

                        cw.IndentedMultiLine(generatedCode[type]);
                    });

                    outputType(type);
                    outputted.Add(type);
                }
            }

            return result;
        }
Exemplo n.º 2
0
        public string GenerateCode()
        {
            this.sb            = new StringBuilder(4096);
            this.cw            = new CodeWriter(sb, 4);
            this.generateQueue = new Queue <Type>();
            this.visited       = new HashSet <Type>();

            foreach (var fromType in this.Assembly.GetTypes())
            {
                if (fromType.IsAbstract)
                {
                    continue;
                }

                if (fromType.IsSubclassOf(typeof(ServiceRequest)) ||
                    fromType.IsSubclassOf(typeof(ServiceResponse)) ||
                    fromType.IsSubclassOf(typeof(Row)) ||
                    fromType.GetCustomAttribute <ScriptIncludeAttribute>() != null)
                {
                    EnqueueType(fromType);
                }
            }

            Dictionary <Type, string> generatedCode = new Dictionary <Type, string>();

            while (generateQueue.Count > 0)
            {
                var type = generateQueue.Dequeue();

                if (type.Assembly != this.Assembly)
                {
                    continue;
                }

                GenerateCodeFor(type);
                generatedCode[type] = sb.ToString();
                sb.Clear();
            }

            sb.Clear();

            foreach (var ns in UsingNamespaces.OrderBy(x => x))
            {
                cw.Indented("using ");
                sb.Append(ns);
                sb.AppendLine(";");
            }

            sb.AppendLine();

            var ordered     = generatedCode.Keys.OrderBy(x => GetNamespace(x)).ThenBy(x => x.Name);
            var byNameSpace = ordered.ToLookup(x => GetNamespace(x));
            var byOwnerType = ordered.ToLookup(x => (x.IsNested ? x.DeclaringType : null));
            var outputted   = new HashSet <Type>();

            foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key))
            {
                sb.AppendLine();
                cw.Indented("namespace ");
                sb.AppendLine(ns.Key);
                cw.InBrace(delegate
                {
                    foreach (var owner in byOwnerType)
                    {
                        if (owner.Key == null)
                        {
                            continue;
                        }

                        if (GetNamespace(owner.Key) != ns.Key)
                        {
                            continue;
                        }

                        if (outputted.Contains(owner.Key))
                        {
                            continue;
                        }

                        if (!generatedCode.ContainsKey(owner.Key))
                        {
                            continue;
                        }

                        outputted.Add(owner.Key);

                        string code = generatedCode[owner.Key].TrimEnd();
                        code        = code.Substring(0, code.Length - 1).TrimEnd();
                        cw.IndentedMultiLine(code);

                        cw.Block(delegate
                        {
                            sb.AppendLine();

                            foreach (var subType in owner)
                            {
                                cw.IndentedMultiLine(generatedCode[subType]);
                                outputted.Add(subType);
                            }
                        });

                        cw.IndentedLine("}");
                        sb.AppendLine();
                    }

                    foreach (var type in ns)
                    {
                        if (outputted.Contains(type))
                        {
                            continue;
                        }

                        cw.IndentedMultiLine(generatedCode[type]);
                        outputted.Add(type);
                    }
                });
            }

            return(sb.ToString());
        }