Exemplo n.º 1
0
        public static void GenerateScript(ScriptGenerator generator, NamespaceSymbol namespaceSymbol, Dictionary <string, bool> generatedNamespaces)
        {
            Debug.Assert(generator != null);
            Debug.Assert(namespaceSymbol != null);
            Debug.Assert(namespaceSymbol.HasApplicationTypes);

            ScriptTextWriter writer = generator.Writer;

            // First generate enums and interfaces which do not have cross-dependencies
            // (Classes for example, might have dependencies on enums)

            foreach (TypeSymbol type in namespaceSymbol.Types)
            {
                if (type.IsApplicationType)
                {
                    if (type.IsTestType && (generator.Options.IncludeTests == false))
                    {
                        continue;
                    }

                    string namespaceName = type.GeneratedNamespace;
                    if ((namespaceName.Length != 0) &&
                        (generatedNamespaces.ContainsKey(namespaceName) == false))
                    {
                        writer.Write("Type.registerNamespace('");
                        writer.Write(namespaceName);
                        writer.Write("');");
                        writer.WriteNewLine();
                        writer.WriteNewLine();
                        generatedNamespaces[namespaceName] = true;
                    }
                }

                if (type.IsApplicationType &&
                    ((type.Type == SymbolType.Enumeration) ||
                     (type.Type == SymbolType.Interface) ||
                     (type.Type == SymbolType.Record) ||
                     (type.Type == SymbolType.Resources)))
                {
                    TypeGenerator.GenerateScript(generator, type);
                }
            }

            foreach (TypeSymbol type in namespaceSymbol.Types)
            {
                if (type.IsApplicationType && (type.Type == SymbolType.Class))
                {
                    if (type.IsTestType && (generator.Options.IncludeTests == false))
                    {
                        continue;
                    }

                    TypeGenerator.GenerateScript(generator, type);
                }
            }
        }
Exemplo n.º 2
0
        public void GenerateScript(SymbolSet symbolSet)
        {
            Debug.Assert(symbolSet != null);

            Dictionary <string, bool> generatedNamespaces = new Dictionary <string, bool>();

            foreach (NamespaceSymbol namespaceSymbol in symbolSet.Namespaces)
            {
                if (namespaceSymbol.HasApplicationTypes)
                {
                    NamespaceGenerator.GenerateScript(this, namespaceSymbol, generatedNamespaces);
                }
            }

            List <ClassSymbol> generatedClasses = new List <ClassSymbol>(_classes.Count);

            foreach (ClassSymbol generatedClass in _classes)
            {
                if (generatedClass.HasGlobalMethods == false)
                {
                    GenerateClassRegistration(generatedClass, generatedClasses);
                }
            }

            // TODO: Couple of line-breaks would be nice here
            //       but only if there are any classes with static
            //       ctors or members

            foreach (ClassSymbol generatedClass in _classes)
            {
                TypeGenerator.GenerateClassConstructorScript(this, generatedClass);
            }

            if (Options.IncludeTests)
            {
                foreach (ClassSymbol generatedClass in _classes)
                {
                    if (generatedClass.IsTestClass)
                    {
                        TestGenerator.GenerateScript(this, generatedClass);
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void GenerateClassRegistration(ClassSymbol classSymbol, List <ClassSymbol> generatedClasses)
        {
            Debug.Assert(classSymbol != null);

            if (generatedClasses.Contains(classSymbol))
            {
                return;
            }

            ClassSymbol baseClass = classSymbol.BaseClass;

            if ((baseClass != null) && baseClass.IsApplicationType)
            {
                GenerateClassRegistration(baseClass, generatedClasses);
            }

            TypeGenerator.GenerateClassRegistrationScript(this, classSymbol);
            generatedClasses.Add(classSymbol);
        }
Exemplo n.º 4
0
        public void GenerateScript(SymbolSet symbolSet)
        {
            Debug.Assert(symbolSet != null);

            List <TypeSymbol> types         = new List <TypeSymbol>();
            List <TypeSymbol> publicTypes   = new List <TypeSymbol>();
            List <TypeSymbol> internalTypes = new List <TypeSymbol>();

            bool hasNonModuleInternalTypes = false;

            foreach (NamespaceSymbol namespaceSymbol in symbolSet.Namespaces)
            {
                if (namespaceSymbol.HasApplicationTypes)
                {
                    foreach (TypeSymbol type in namespaceSymbol.Types)
                    {
                        if (type.IsApplicationType == false)
                        {
                            continue;
                        }

                        if (type.Type == SymbolType.Delegate)
                        {
                            // Nothing needs to be generated for delegate types.
                            continue;
                        }

                        if (type.IsTestType && (_options.IncludeTests == false))
                        {
                            continue;
                        }

                        if ((type.Type == SymbolType.Enumeration) &&
                            ((type.IsPublic == false) || ((EnumerationSymbol)type).Constants))
                        {
                            // Internal enums can be skipped since their values have been inlined.
                            // Public enums marked as constants can also be skipped since their
                            // values will always be inlined.
                            continue;
                        }

                        types.Add(type);
                        if (type.IsPublic)
                        {
                            publicTypes.Add(type);
                        }
                        else
                        {
                            if ((type.Type != SymbolType.Class) ||
                                (((ClassSymbol)type).IsModuleClass == false))
                            {
                                hasNonModuleInternalTypes = true;
                            }
                            internalTypes.Add(type);
                        }
                    }
                }
            }

            // Sort the types, so similar types of types are grouped, and parent classes
            // come before derived classes.
            IComparer <TypeSymbol> typeComparer = new TypeComparer();

            types         = types.OrderBy(t => t, typeComparer).ToList();
            publicTypes   = publicTypes.OrderBy(t => t, typeComparer).ToList();
            internalTypes = internalTypes.OrderBy(t => t, typeComparer).ToList();

            bool initialIndent = false;

            if (String.IsNullOrEmpty(_options.ScriptInfo.Template) == false)
            {
                int scriptIndex = _options.ScriptInfo.Template.IndexOf("{script}");
                if ((scriptIndex > 0) && (_options.ScriptInfo.Template[scriptIndex - 1] == ' '))
                {
                    // Heuristic to turn on initial indent:
                    // The script template has a space prior to {script}, i.e. {script} is not the
                    // first thing on a line within the template.

                    initialIndent = true;
                }
            }
            if (initialIndent)
            {
                _writer.Indent++;
            }

            foreach (TypeSymbol type in types)
            {
                TypeGenerator.GenerateScript(this, type);
            }

            bool generateModule = (publicTypes.Count != 0) ||
                                  ((internalTypes.Count != 0) && hasNonModuleInternalTypes);

            if (generateModule)
            {
                _writer.Write("var $exports = ss.module('");
                _writer.Write(symbolSet.ScriptName);
                _writer.Write("',");
                if ((internalTypes.Count != 0) && hasNonModuleInternalTypes)
                {
                    _writer.WriteLine();
                    _writer.Indent++;
                    _writer.WriteLine("{");
                    _writer.Indent++;
                    bool firstType = true;
                    foreach (TypeSymbol type in internalTypes)
                    {
                        if ((type.Type == SymbolType.Class) &&
                            (((ClassSymbol)type).IsExtenderClass || ((ClassSymbol)type).IsModuleClass))
                        {
                            continue;
                        }
                        if ((type.Type == SymbolType.Record) &&
                            ((RecordSymbol)type).Constructor == null)
                        {
                            continue;
                        }

                        if (firstType == false)
                        {
                            _writer.WriteLine(",");
                        }
                        TypeGenerator.GenerateRegistrationScript(this, type);
                        firstType = false;
                    }
                    _writer.Indent--;
                    _writer.WriteLine();
                    _writer.Write("},");
                    _writer.Indent--;
                }
                else
                {
                    _writer.Write(" null,");
                }
                if (publicTypes.Count != 0)
                {
                    _writer.WriteLine();
                    _writer.Indent++;
                    _writer.WriteLine("{");
                    _writer.Indent++;
                    bool firstType = true;
                    foreach (TypeSymbol type in publicTypes)
                    {
                        if ((type.Type == SymbolType.Class) &&
                            ((ClassSymbol)type).IsExtenderClass)
                        {
                            continue;
                        }

                        if (firstType == false)
                        {
                            _writer.WriteLine(",");
                        }
                        TypeGenerator.GenerateRegistrationScript(this, type);
                        firstType = false;
                    }
                    _writer.Indent--;
                    _writer.WriteLine();
                    _writer.Write("}");
                    _writer.Indent--;
                }
                else
                {
                    _writer.Write(" null");
                }
                _writer.WriteLine(");");
                _writer.WriteLine();
            }

            foreach (TypeSymbol type in types)
            {
                if (type.Type == SymbolType.Class)
                {
                    TypeGenerator.GenerateClassConstructorScript(this, (ClassSymbol)type);
                }
            }

            if (_options.IncludeTests)
            {
                foreach (TypeSymbol type in types)
                {
                    ClassSymbol classSymbol = type as ClassSymbol;
                    if ((classSymbol != null) && classSymbol.IsTestClass)
                    {
                        TestGenerator.GenerateScript(this, classSymbol);
                    }
                }
            }

            if (initialIndent)
            {
                _writer.Indent--;
            }
        }