private IEnumerable <Member> GetStaticMembers(string methodName, Type[] parameterTypes)
        {
            var pos = methodName.LastIndexOf('.');

            if (pos < 0)
            {
                yield break;
            }

            string typeName    = methodName.Substring(0, pos);
            string methodName2 = methodName.Substring(pos + 1);

            foreach (var assembly in Assemblies)
            {
                foreach (var usingDirective in UsingDirectives.Union(new[] { "" }))
                {
                    var name = string.IsNullOrEmpty(usingDirective) ? typeName : usingDirective + "." + typeName;

                    var t = assembly.GetType(name);
                    if (t != null)
                    {
                        var methods = TypeExtensions.GetStaticMembers(methodName2, t, parameterTypes);
                        foreach (var method in methods)
                        {
                            yield return(method);
                        }
                    }
                }
            }
        }
 public void LoadUsingDirectives(List <string> usingDirectives)
 {
     foreach (var usingDirective in usingDirectives)
     {
         UsingDirectives.Add(usingDirective);
     }
 }
Beispiel #3
0
 protected override string BeginFile()
 {
     return($"// Date: {GenerationTime:O}\r\n"
            + $"// Base URL: {Api.BaseUrl}\r\n"
            + $"// Source: {Api.Title} ({Api.Version})\r\n"
            + "\r\n"
            + string.Join("", UsingDirectives.Select(name => $"using {name};\r\n"))
            + "// ReSharper disable InconsistentNaming\r\n"
            + "\r\n");
 }
Beispiel #4
0
 protected override string BeginFile()
 {
     return($"// Date: {GenerationTime:O}\r\n"
            + $"// Base URL: {Api.BaseUrl}\r\n"
            + $"// Source: {Api.Title} ({Api.Version})\r\n"
            + "\r\n"
            + $"package {Namespace};\r\n"
            + "\r\n"
            + string.Join("", UsingDirectives.Select(name => $"import {name};\r\n"))
            + "\r\n");
 }
Beispiel #5
0
        public string Generate(string name)
        {
            var classDefinition = Generator.ClassDeclaration(
                name, typeParameters: null,
                accessibility: Accessibility.Public,
                modifiers: DeclarationModifiers.Partial,
                baseType: BaseType,
                members: Members);

            ContentDeclarations.Add(classDefinition);

            var compilationDeclarations = UsingDirectives.Union(ContentDeclarations).ToList();

            return(Generator.CompilationUnit(compilationDeclarations).NormalizeWhitespace().ToFullString());
        }
Beispiel #6
0
        public void AddUsing(string namespaceDirective)
        {
            foreach (var existingDirective in UsingDirectives)
            {
                if (existingDirective is UsingDirectiveSyntax existingUsingDirective)
                {
                    var name = existingUsingDirective.Name.GetText().ToString();
                    if (name == namespaceDirective)
                    {
                        return;
                    }
                }
            }

            UsingDirectives.Add(Generator.NamespaceImportDeclaration(namespaceDirective));
        }
 public void AddUsingDirectives(params string[] namespaces)
 {
     UsingDirectives.AddRange(namespaces.Where(n => !string.IsNullOrEmpty(n)));
 }
        protected virtual Microsoft.CodeAnalysis.SyntaxTree ToSyntaxTree(VSGraphModel graphModel, CompilationOptions options)
        {
            //TODO fix graph name, do not use the asset name
            var className        = graphModel.TypeName;
            var baseClass        = graphModel.Stencil.GetBaseClass().Name;
            var classDeclaration = ClassDeclaration(className)
                                   .WithModifiers(
                TokenList(
                    Token(SyntaxKind.PublicKeyword)));

            if (!String.IsNullOrEmpty(baseClass))
            {
                classDeclaration = classDeclaration.WithBaseList(
                    BaseList(
                        SingletonSeparatedList <BaseTypeSyntax>(
                            SimpleBaseType(
                                IdentifierName(baseClass)))));
            }

            if (graphModel.Stencil.addCreateAssetMenuAttribute)
            {
                classDeclaration = classDeclaration.WithAttributeLists(
                    SingletonList(
                        AttributeList(
                            SingletonSeparatedList(
                                Attribute(
                                    IdentifierName("CreateAssetMenu"))
                                .WithArgumentList(
                                    AttributeArgumentList(
                                        SeparatedList <AttributeArgumentSyntax>(
                                            new SyntaxNodeOrToken[]
                {
                    AttributeArgument(
                        LiteralExpression(
                            SyntaxKind.StringLiteralExpression,
                            Literal(graphModel.Stencil.fileName)))
                    .WithNameEquals(
                        NameEquals(
                            IdentifierName("fileName"))),
                    Token(SyntaxKind.CommaToken),
                    AttributeArgument(
                        LiteralExpression(
                            SyntaxKind.StringLiteralExpression,
                            Literal(graphModel.Stencil.menuName)))
                    .WithNameEquals(
                        NameEquals(
                            IdentifierName("menuName")))
                })))))));
            }

            var allMembers = new List <MemberDeclarationSyntax>();

            m_AllFields = new List <MemberDeclarationSyntax>();
            var allRemainingMembers = new List <MemberDeclarationSyntax>();

            foreach (var fieldDecl in graphModel.GraphVariableModels)
            {
                var fieldSyntaxNode = fieldDecl.DeclareField(this);
                m_AllFields.Add(fieldSyntaxNode);
            }

            var entryPoints = graphModel.GetEntryPoints();

            Dictionary <string, MethodDeclarationSyntax> declaredMethods = new Dictionary <string, MethodDeclarationSyntax>();

            foreach (var stack in entryPoints)
            {
                var entrySyntaxNode = BuildNode(stack);
                foreach (var memberDeclaration in entrySyntaxNode.Cast <MemberDeclarationSyntax>())
                {
                    if (memberDeclaration is MethodDeclarationSyntax methodDeclarationSyntax)
                    {
                        string key = methodDeclarationSyntax.Identifier.ToString();
                        declaredMethods.Add(key, methodDeclarationSyntax);
                    }
                    else
                    {
                        allRemainingMembers.Add(memberDeclaration);
                    }
                }
            }

            allMembers.AddRange(m_AllFields);
            m_AllFields = null;
            allMembers.AddRange(allRemainingMembers);

            if (m_EventRegistrations.Any())
            {
                if (!declaredMethods.TryGetValue("Update", out var method))
                {
                    method = RoslynBuilder.DeclareMethod("Update", AccessibilityFlags.Public, typeof(void))
                             .WithParameterList(
                        ParameterList(
                            SeparatedList(
                                Enumerable.Empty <ParameterSyntax>())))
                             .WithBody(Block());
                }

                BlockSyntax blockSyntax = Block(m_EventRegistrations.Concat(method.Body.Statements));

                method = method.WithBody(blockSyntax);
                declaredMethods["Update"] = method;
            }

            allMembers.AddRange(declaredMethods.Values);
            classDeclaration = classDeclaration.AddMembers(allMembers.ToArray());

            var referencedNamespaces = UsingDirectives.OrderBy(n => n).Select(ns => UsingDirective(ParseName(ns)));
            var namespaceAliases     = UsingAliases.OrderBy(n => n.Key).Select(pair =>
                                                                               UsingDirective(ParseName(pair.Key))
                                                                               .WithAlias(NameEquals(
                                                                                              IdentifierName(pair.Value))));
            var usings          = referencedNamespaces.Concat(namespaceAliases).ToArray();
            var compilationUnit = CompilationUnit()
                                  .WithUsings(
                List(usings))
                                  .WithMembers(
                SingletonList <MemberDeclarationSyntax>(classDeclaration)).NormalizeWhitespace();

            return(compilationUnit.SyntaxTree);
        }
Beispiel #9
0
 protected UsingDirectiveList GetUsingDirectiveList()
 => Any(UsingDirectives) || Any(UsingStaticDirectives)
         ? UsingDirectiveLists.Create(
     (UsingDirectives?.Select(each => DevOps.Primitives.CSharp.Helpers.Common.UsingDirectives.Using(each)) ?? new UsingDirective[] { })
     .Concat((UsingStaticDirectives?.Select(each => DevOps.Primitives.CSharp.Helpers.Common.UsingDirectives.UsingStatic(each)) ?? new UsingDirective[] { })).ToArray())
         : null;
        public override void ToSource(StringBuilder sb)
        {
            sb.Append(IdentNewLine(DocComment));

            if (name != null)
            {
                if (attributes != null)
                {
                    attributes.ToSource(sb);
                }

                sb.Append("namespace ");
                name.ToSource(sb);
                NewLine(sb);
                sb.Append("{");
                indent++;
                NewLine(sb);
            }

            if (ExternAliases.Count > 0)
            {
                ExternAliases.ToSource(sb);
                NewLine(sb);
            }

            if (UsingDirectives.Count > 0)
            {
                UsingDirectives.ToSource(sb);
                NewLine(sb);
            }

            if (name == null)
            {
                if (attributes != null)
                {
                    attributes.ToSource(sb);
                }
            }

            if (namespaces != null)
            {
                namespaces.ToSource(sb);
            }

            if (interfaces != null)
            {
                interfaces.ToSource(sb);
            }

            if (classes != null)
            {
                classes.ToSource(sb);
            }

            if (structs != null)
            {
                structs.ToSource(sb);
            }

            if (delegates != null)
            {
                delegates.ToSource(sb);
            }

            if (enums != null)
            {
                enums.ToSource(sb);
            }

            if (name != null)
            {
                indent--;
                NewLine(sb);
                sb.Append("}");
                NewLine(sb);
            }
        }