コード例 #1
0
 private void Export(Field pe)
 {
     ExportXmlDoc2(pe);
     pe.Attributes.ForEach(Export);
     Write("public {0}{1}{2}{3}{4} {5}", pe.IsStatic.If("static "), pe.IsVirtual.If("virtual "), pe.IsOverride.If("override "), pe.IsNew.If("new "), Class(pe.Type), Identifier(pe.Name));
     if (pe.Initializer.IsNotNullOrEmpty())
         Write("={0}", pe.Initializer);
     WriteLine(";");
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: fjgandrade/sharpkit
        private static Element Clone( Element el, object newPropertys = null )
        {
            Element newEl = null;
            if ( el is Method )
            {
                newEl = new Method();
            }
            else if ( el is Property )
            {
                newEl = new Property();
            }
            else if ( el is Field )
            {
                newEl = new Field();
            }
            el.GetType().GetProperties().ForEach( p =>
            {
                var val = p.GetValue( el, new object[] { } );
                newEl.GetType().GetProperty( p.Name ).SetValue( newEl, val, new object[] { } );
            } );

            return newEl;
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: fjgandrade/sharpkit
        private static List<Class> Fix( List<Class> list )
        {
            list.ForEach( t => t.Members.Where( m => m.DeclaringClass == null ).ForEach( m => m.DeclaringClass = t ) );
            list.ForEach( t => t.Members.OfType<Method>().Where( m => m.Name.Equals( "callParent", StringComparison.Ordinal ) && m.Parameters.Count == 1 ).ForEach( m => m.Parameters.ForEach( p =>
            {
                p.IsOptional = true;
                p.Type = GetClass( "object" );
            } ) ) );
            var ceExt = GetClass( "Ext.ExtContext", false );

            if ( InheritFromJsContext )
                ceExt.BaseClass = GetClass( "JsContext", false );

            ceExt.Attributes[ 0 ].NamedParamters.Add( "Name", "\"Ext\"" );

            //Fixing Ext aliases.
            ceExt.Members.Where( m => m.Name.Equals( "getDoc" ) || m.Name.Equals( "getDom" ) || m.Name.Equals( "getElementById" ) || m.Name.Equals( "getBody" ) ).ForEach( m => m.Type = GetClass( "Element" ) );
            ceExt.Members.OfType<Method>().Where( m => m.Name.Equals( "getClass" ) ).ForEach( m =>
            {
                m.Type = GetClass( "Ext.Class" );
                m.Parameters.Add( new Parameter { DeclaringClass = ceExt, Name = "cls", Type = GetClass( "object" ) } );
            } );
            ceExt.Members.OfType<Method>().Where( m => m.Name.Equals( "getClassName" ) ).ForEach( m =>
            {
                m.Type = GetClass( "JsString" );
                m.Parameters.Add( new Parameter { DeclaringClass = ceExt, Name = "cls", Type = GetClass( "object" ) } );
            } );
            ceExt.Members.OfType<Method>().Where( m => m.Name.Equals( "createByAlias" ) ).ForEach( m =>
            {
                m.Type = GetClass( "object" );
                m.Parameters.Add( new Parameter { DeclaringClass = ceExt, Name = "name", Type = GetClass( "JsString" ) } );
                m.Parameters.Add( new Parameter { DeclaringClass = ceExt, Name = "args", Type = GetClass( "object" ), IsParams = true } );
            } );
            ceExt.Members.OfType<Method>().Where( m => m.Name.Equals( "create" ) ).ForEach( m =>
            {
                m.Parameters[ 0 ].IsOptional = false;
                m.Parameters[ 1 ].IsOptional = false;
                m.Parameters[ 1 ].IsParams = true;
            } );
            ceExt.Members.Where( m => m.Name.Equals( "getCmp" ) ).ForEach( m => m.Type = GetClass( "Ext.Component" ) );
            ceExt.Members.OfType<Method>().Where( m => m.Name.Equals( "require" ) ).ForEach( m => m.Parameters.Add( new Parameter { Type = GetClass( "object" ), Name = "args", IsParams = true, DeclaringClass = ceExt } ) );

            //End fixing Ext aliases

            //Adding Ext non existant aliases.
            ceExt.Members.Add( new Property { Name = "Msg", Type = GetClass( "Ext.window.MessageBox" ), DeclaringClass = ceExt, IsStatic = true } );
            ceExt.Members.Add( new Property { Name = "Direct", Type = GetClass( "Ext.direct.Manager" ), DeclaringClass = ceExt, IsStatic = true } );
            //END adding non existant aliases.

            //Add a new utility class to deal with this
            var cellModel = GetClass( "Ext.selection.CellModel" );
            var jsonPositionClass = GetClass( "Ext.selection.CellModel.Position", true );
            jsonPositionClass.Attributes.Add( CreateJsAttribute( "Json" ) );
            jsonPositionClass.Members.AddRange( new Element[] { new Property { Name = "row", Type = GetClass( "int" ), DeclaringClass = jsonPositionClass }, new Property { Name = "column", Type = GetClass( "int" ), DeclaringClass = jsonPositionClass } } );
            cellModel.Members.Where( m => m.Name.Equals( "getCurrentPosition", StringComparison.Ordinal ) ).ForEach( m => m.Type = jsonPositionClass );

            var bExt = GetClass( "Ext.Base", false );

            if ( InheritFromJsContext )
                bExt.BaseClass = GetClass( "JsContext", false );

            //Explicitly setting these members as public to be able to use it in anonymous objects via @this.As<XXX>().callXXX(); (and borrow should be public but docs are wrong according to Ext forum)
            bExt.Members.OfType<Method>().Where( m => m.Name.Equals( "create", StringComparison.Ordinal ) || m.Name.Equals( "implement", StringComparison.Ordinal ) ).ForEach( m =>
            {
                m.IsStatic = false;
                m.Parameters.Add( new Parameter { Type = GetClass( "object" ), Name = "args", IsParams = true, DeclaringClass = bExt } );
            } );
            bExt.Members.Where( m => new[] { "@className", "configMap", "initConfigList", "initConfigMap", "isInstance", "self", "callParent", "callOverridden", "destroy" }.Any( s => s.Equals( m.Name, StringComparison.Ordinal ) ) ).ForEach( m => m.IsProtected = m.IsPrivate = false );
            list.Where( t => t.FullName == "Ext.util.Sortable" || t.Interfaces.FirstOrDefault( i => i.FullName == "Ext.util.Sortable" ) != null ).ForEach( t => t.Members.Where( m => m.Name.IsNullOrEmpty() ).ForEach( m => m.Name = "sortRoot" ) );

            list.ForEach( t => t.Members.Where( m => m.Type != null && m.Type.Name == "this" ).ForEach( m => m.Type = t ) );
            list = list.Where( t => t.Name != "this" ).ToList();

            list.ForEach( t => t.Members.OfType<Method>().Where( m => m.Name == "toFixed" && m.Type == VoidClass ).ForEach( m => m.Type = GetClass( "JsString" ) ) );
            list.ForEach( t => t.Members.OfType<Method>().Where( m => ( m.Name == "child" || m.Name == "down" || m.Name == "up" ) && m.Type == VoidClass ).ForEach( m => m.Type = GetClass( "Ext.container.AbstractContainer" ) ) );
            list.Where( t => t.FullName == "Ext.Action" ).ForEach( t => t.Members.OfType<Method>().Where( m => ( m.Name == "getText" || m.Name == "getIconCls" ) && m.Type == VoidClass ).ForEach( m => m.Type = GetClass( "JsString" ) ) );

            list.Where( t => t.FullName == "Ext.AbstractComponent" ).ForEach( t => t.Members.OfType<Method>().Where( m => m.Name == "addChildEls" ).ForEach( m => m.Parameters.Add( new Parameter { Type = GetClass( "object" ), DeclaringClass = t, Name = "args", IsParams = true, Summary = "<param name=\"args\">An object or array describing the child elements of the Component. <see cref=\"Ext.AbstractComponent.childEls\" /></param>" } ) ) );

            list.Where( t => t.FullName == "Ext.container.AbstractContainer" ).ForEach( t => t.Members.OfType<Method>().Where( m => m.Name.Equals( "add", StringComparison.Ordinal ) ).ForEach( m =>
            {
                m.Parameters[ 0 ].IsParams = true;
                m.Parameters[ 0 ].IsOptional = false;
            } ) );
            list.Where( t => t.FullName == "Ext.data.Store" ).ForEach( t =>
            {
                t.Members.OfType<Method>().Where( m => m.Name.Equals( "guaranteeRange", StringComparison.Ordinal ) ).ForEach( m =>
                {
                    m.Parameters[ 2 ].IsOptional = true;
                    m.Parameters[ 3 ].IsOptional = true;
                } );
                t.Members.OfType<Method>().Where( m => m.Name.Equals( "loadPage" ) ).ForEach( m => m.Parameters[ 1 ].IsOptional = true );
            } );
            list.Where( t => t.FullName == "Ext.EventObject" ).ForEach( t => t.Members.OfType<Method>().ForEach( m => m.IsStatic = false ) );

            list.Where( t => t.FullName == "Ext.CompositeElement" ).ForEach( t => t.Members.Where( m => m.Name.IsNullOrEmpty() ).ForEach( m => m.Name = "UNKNOWN" ) );

            list.ForEach( t => t.Members.OfType<Method>().Where( m => m.Name == "getStore" ).ForEach( m => m.Type = GetClass( "Ext.data.Store" ) ) );

            list.Where( t => t.FullName == "Ext.Error" ).ForEach( t => t.BaseClass = null );
            list.Where( t => t.IsInterface && t.BaseClass != null ).ForEach( t => t.BaseClass = null );
            list.ForEach( t => t.Members.Where( m => m.Name == "isValid" && ( m.Type == null || m.Type == VoidClass ) ).ForEach( m => m.Type = GetClass( "Boolean" ) ) );
            list.ForEach( t => t.Members.Where( m => m.Name == "getState" && ( m.Type == null || m.Type == VoidClass ) ).ForEach( m => m.Type = ObjectClass ) );
            list.ForEach( t => t.Members.Where( m => m.Name == "getSubTplMarkup" && m.Type == ObjectClass ).ForEach( m => m.Type = GetClass( "JsString" ) ) );
            list.ForEach( t => t.Members.Where( m => m.Name == "getInputId" && m.Type == ObjectClass ).ForEach( m => m.Type = GetClass( "JsString" ) ) );
            list.ForEach( t => t.Members.Where( m => m.Name == "getFieldLabel" && m.Type == ObjectClass ).ForEach( m => m.Type = GetClass( "JsString" ) ) );
            list.Where( t => t.FullName.StartsWith( "Ext.selection" ) ).ForEach( t => t.Members.Where( m => m.Name == "getLastSelected" && m.Type == VoidClass ).ForEach( m => m.Type = GetClass( "Ext.data.Model" ) ) );
            list.Where( t => t.Namespace.Equals( "Ext.ux.event", StringComparison.Ordinal ) ).ForEach( t => t.Namespace = "Ext.ux.@event" );

            var ceField = GetClass( "Ext.form.field.Field" );
            list.ForEach( t => t.Members.Where( m => m.Name == "setValue" && m.Type.Interfaces.Contains( ceField ) ).ForEach( m => m.Type = ceField ) );
            list.Where( t => t.BaseClass != null && t.BaseClass.IsInterface ).ForEach( t =>
            {
                t.Interfaces.Insert( 0, t.BaseClass );
                t.BaseClass = null;
            } );

            //Implement missing interface methods
            list.Where( t => !t.IsInterface && t.Interfaces.Count > 0 ).ForEach( t => t.Interfaces.ForEach( i => i.Members.ForEach( im =>
            {
                if ( !InterfaceMemberIsImplementedBySelfOrBaseClass( im, t ) )
                {
                    var ne = new Element();
                    if ( im is Method )
                    {
                        ne = new Method();
                        ( ne as Method ).Parameters = ( im as Method ).Parameters;
                        ( ne as Method ).ReturnsArray = ( im as Method ).ReturnsArray;
                        ( ne as Method ).IsVirtual = true;//Interface implemented methods are virtual by default.
                    }
                    else if ( im is Property )
                    {
                        ne = new Property();
                    }
                    else if ( im is Field )
                    {
                        ne = new Field();
                        ( ne as Field ).Initializer = ( im as Field ).Initializer;
                    }
                    ne.Attributes = im.Attributes;
                    ne.DeclaringClass = t;
                    ne.IsNew = im.IsNew;
                    ne.IsOverride = false;
                    ne.IsPrivate = false;
                    ne.IsProtected = false;
                    ne.IsStatic = false;
                    ne.IsVirtual = true;
                    ne.Name = im.Name;
                    ne.Remarks = im.Remarks;
                    ne.Summary = im.Summary;
                    ne.Type = im.Type ?? ObjectClass;

                    t.Members.Add( ne );
                }
            } ) ) );

            //Make sure interface implemented methods are public, non static and have proper return type
            list.Where( t => !t.IsInterface && t.Interfaces.Count > 0 ).ForEach( t => t.Members.ForEach( m => t.Interfaces.ForEach( i => i.Members.Where( im => im.Name.Equals( m.Name, StringComparison.Ordinal ) ).ForEach( im =>
            {
                var implementedInBase = AnyBaseClassImplements( m );
                m.IsPrivate = m.IsProtected = m.IsStatic = false;
                if ( im is Method && m is Method && ( im as Method ).Parameters.Count == ( m as Method ).Parameters.Count )
                {
                    m.Type = im.Type;
                    ( m as Method ).ReturnsArray = ( im as Method ).ReturnsArray;
                    var index = 0;
                    //If this is the only implementation, lets fix parameter types and make it virtual
                    if ( !implementedInBase )
                    {
                        ( m as Method ).IsVirtual = true;
                        ( im as Method ).Parameters.ForEach( ip =>
                        {
                            if ( ( m as Method ).Parameters[ index ].Type != ip.Type )
                            {
                                ( m as Method ).Parameters[ index ].Type = ip.Type;
                            }
                            index++;
                        } );
                    }
                }

                else if ( !( ( im is Method ) || !( m is Method ) ) )
                {
                    m.Type = im.Type;
                }
            } ) ) ) );

            //list.Where( t => !t.IsInterface && t.Interfaces.Count > 0 ).ForEach( t => t.Members.ForEach( m => t.Interfaces.Where( i => i.Members.Any( im => im.Name.Equals( m.Name, StringComparison.Ordinal ) ) ).ForEach( im =>
            //{
            //    m.IsPrivate = m.IsProtected = m.IsStatic = false;
            //    m.Type = im.Type;
            //} ) ) );

            var invalidMethods = new List<Method>();
            foreach ( var ce in list )
            {
                if ( !ce.IsInterface )
                {
                    var ce2 = ce.BaseClass;
                    while ( ce2 != null )
                    {
                        if ( !ce2.IsInterface )
                        {
                            ce.Members.ForEach( pe =>
                            {
                                var parentM = ce2.Members.FirstOrDefault( t => HasSameNameAndParameterTypes( t, pe ) );
                                if ( parentM != null )
                                {
                                    if ( parentM.IsVirtual && !pe.IsStatic )
                                    {
                                        pe.IsPrivate = parentM.IsPrivate;
                                        pe.IsProtected = parentM.IsProtected;
                                        pe.IsOverride = true;
                                        pe.IsNew = false;
                                    }
                                    else
                                    {
                                        pe.IsNew = true;
                                    }
                                }
                            } );
                        }
                        ce2 = ce2.BaseClass;
                    }
                }
                foreach ( var me in ce.Members.OfType<Method>().ToList() )
                {
                    if ( ce.IsInterface )
                    {
                        me.IsVirtual = false;
                    }
                    var i1 = me.Parameters.FindIndex( p => p.IsOptional );
                    var i2 = me.Parameters.FindLastIndex( p => !p.IsOptional );
                    if ( i2 >= 0 && i1 >= 0 && i2 > i1 )
                    {
                        //This rarelly happens, so far, only one method and we can get away setting the param to optional.
                        me.Parameters[ i2 ].IsOptional = true;
                    }
                    else if ( me.Parameters.FirstOrDefault( t => t.Name == "*" ) != null )
                    {
                        invalidMethods.Add( me );
                        ce.Members.Remove( me );//invalid method prm name
                    }
                }
            }
            File.WriteAllLines( Path.Combine( OutputDir, "InvalidMethods.txt" ), invalidMethods.Select( t => t.DeclaringClass.FullName + ":" + t.Name ).ToArray() );
            list.RemoveAll( t => t.Name == "*" );
            list.Where( ce => !ce.IsInterface ).ForEach( ce =>
            {
                var ceCfg = GetClass( ce.FullName + "Config", false );
                if ( ceCfg != null )
                {
                    var ctors = ce.Members.OfType<Method>().Where( t => t.IsConstructor ).ToList();
                    if ( ctors.FirstOrDefault( t => t.Parameters.Count == 1 && t.Parameters[ 0 ].Type == ceCfg ) == null )
                    {
                        ce.Members.Add( new Method { IsConstructor = true, Parameters = new List<Parameter> { new Parameter { Name = "config", Type = ceCfg } } } );
                    }
                }
                if ( !HasEmptyOrDefaultConstructor( ce ) )
                    ce.Members.Add( new Method { IsConstructor = true } );

                if ( GenerateParamsConstructor )
                {
                    ce.Members.Add( new Method { IsConstructor = true, Parameters = new List<Parameter> { new Parameter { Name = "args", IsParams = true, Type = GetClass( "object" ) } } } );
                }
            } );

            list.ForEach( t => t.Members.Where( m => m.DeclaringClass == null ).ForEach( m => m.DeclaringClass = t ) );
            list.Where( t => t.IsInterface && t.Members.OfType<Method>().Any( m => m.IsConstructor ) ).ForEach( t =>
                                                                                                       {
                                                                                                           var toRemove = new List<Element>();
                                                                                                           t.Members.ForEach( toRemove.Add );
                                                                                                           toRemove.ForEach( r => t.Members.Remove( r ) );
                                                                                                       } );
            //Remove members already implemented in any base class
            list.Where( t => !t.IsInterface && t.BaseClass != null ).ForEach( t =>
            {
                var toRemove = new List<Element>();
                t.Members.ForEach( m =>
                {
                    if ( AnyBaseClassImplements( m ) )
                    {
                        if ( m is Method )
                        {
                            var mt = m as Method;
                            if ( !mt.IsOverride )
                            {
                                toRemove.Add( m );
                            }
                            else
                            {
                                list.ForEach( ty =>
                                                     {
                                                         var parentM = ty.Members.OfType<Method>().FirstOrDefault( me => me.Name != null && me.Name.Equals( mt.Name ) );
                                                         if ( parentM != null && HasSameNameAndParameterTypes( mt, parentM ) )
                                                         {
                                                             toRemove.Add( m );
                                                         }
                                                     } );
                            }
                        }
                        else
                        {
                            toRemove.Add( m );
                        }
                    }
                } );
                toRemove.ForEach( m => t.Members.Remove( m ) );
            } );

            if ( UseProperCaseForPublicMethods )
            {
                list.ForEach( t => t.Members.OfType<Method>().Where( m => !m.IsConstructor && ( !m.IsPrivate || t.IsInterface ) ).ForEach( m =>
                {
                    var needsExtraRename = false;
                    m.Name = m.Name.Trim( '@' );
                    var newName = m.Name.Substring( 0, 1 ).ToUpperInvariant() + m.Name.Substring( 1 );
                    if ( m.DeclaringClass != null && newName.Equals( m.DeclaringClass.Name, StringComparison.Ordinal ) )
                    {
                        needsExtraRename = true;
                    }
                    if ( !needsExtraRename && t.SubClasses.Any( s => s.Name.Equals( newName ) ) )
                    {
                        needsExtraRename = true;
                    }
                    if ( needsExtraRename )
                    {
                        newName = "Mt" + newName;
                        m.Remarks = "This method was renamed from: " + m.Name + " to: Mt" + m.Name + ", this is only for C# code, in Javascript it will appear with the original 'Ext' name (" + m.Name + ")";
                    }

                    if ( m.Attributes.Count == 0 )
                    {
                        m.Attributes.Add( CreateNameAttribute( "JsMethod", m.Name ) );
                    }
                    FixNewName( list, m, newName );
                    m.Name = newName;
                } ) );
            }

            return FixSameNameAsNamespace( list );
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: fjgandrade/sharpkit
 private static Field ProcessCfg( ExtMember t )
 {
     //ProcessMemberDocs( t );
     var pe = new Field { Name = t.name, Type = GetClass( t.type ), Summary = t.shortDoc };
     SetModifiers( t, pe );
     //pe.Initializer = "\"" + t.name + "\"";
     if ( pe.Name.Contains( "$" ) )
     {
         pe.Attributes.Add( new Attribute() { Name = "JsField", NamedParamters = new Dictionary<string, string>() { { "Name", "\"" + pe.Name + "\"" } } } );
         pe.Name = pe.Name.Replace( "$", "@" );
     }
     return pe;
 }