示例#1
0
    static void generate()
    {
        // All code generators that should be used
        var codeGenerators = new ICodeGenerator[] {
            new ComponentIndicesGenerator(),
            new ComponentExtensionsGenerator(),
            new PoolAttributesGenerator(),
            new PoolsGenerator(),
            new BlueprintsGenerator()
        };

        // Specify all pools
        var poolNames = new [] { "Core" };

        // Specify all blueprints
        var blueprintNames = new string[0];

        var assembly = Assembly.GetAssembly(typeof(Entity));
        var provider = new TypeReflectionProvider(assembly.GetTypes(), poolNames, blueprintNames);

        const string path  = "../../Generated/";
        var          files = CodeGenerator.Generate(provider, path, codeGenerators);

        foreach (var file in files)
        {
            Console.WriteLine(file.generatorName + ": " + file.fileName);
        }

        Console.WriteLine("Done. Press any key...");
        Console.Read();
    }
示例#2
0
    static void generates(Type[] types, string[] expectedLookupNames, string[] expectedLookupCodes)
    {
        var infos = TypeReflectionProvider.GetComponentInfos(types);

        var files = new ComponentIndicesGenerator().Generate(infos);

        files.Length.should_be(expectedLookupNames.Length);

        for (int i = 0; i < expectedLookupNames.Length; i++)
        {
            var expectedLookupName = expectedLookupNames[i];
            var expectedLookupCode = expectedLookupCodes[i].ToUnixLineEndings();

            files.Any(f => f.fileName == expectedLookupName).should_be_true();
            var file = files.Single(f => f.fileName == expectedLookupName);
#pragma warning disable
            if (logResults)
            {
                Console.WriteLine("should:\n" + expectedLookupCode);
                Console.WriteLine("was:\n" + file.fileContent);
            }

            file.fileContent.should_be(expectedLookupCode);
        }
    }
    static void generates <T>(string expectedFileName = null)
    {
        var entitasDir  = new DirectoryInfo(Directory.GetCurrentDirectory());
        var fixturesDir = entitasDir + "/Tests/Tests/Entitas/CodeGenerator/Fixtures/Generated/";

        expectedFileName = (expectedFileName ?? typeof(T).FullName) + "GeneratedExtension";
        var path = fixturesDir + expectedFileName + ".cs";

        var expectedFileContent = File.ReadAllText(path);
        var info = TypeReflectionProvider.GetComponentInfos(typeof(T)).Single();

        var codeGenFiles = new ComponentExtensionsGenerator().Generate(new [] { info });

        codeGenFiles.Length.should_be(1);
        var codeGenFile = codeGenFiles.Single();

        #pragma warning disable
        if (logResults)
        {
            Console.WriteLine("should:\n" + expectedFileContent);
            Console.WriteLine("was:\n" + codeGenFile.fileContent);
        }

        codeGenFile.fileName.should_be(expectedFileName);
        codeGenFile.fileContent.should_be(expectedFileContent);
    }
    void when_generating()
    {
        it["component without fields"]        = () => generates <MovableComponent>();
        it["component with fields"]           = () => generates <PersonComponent>();
        it["single component without fields"] = () => generates <AnimatingComponent>();
        it["single component with fields"]    = () => generates <UserComponent>();
        it["component for custom pool"]       = () => generates <OtherPoolComponent>();
        it["supports properties"]             = () => generates <ComponentWithFieldsAndProperties>();
        it["ignores [DontGenerate]"]          = () => {
            var info  = TypeReflectionProvider.GetComponentInfos(typeof(DontGenerateComponent))[0];
            var files = new ComponentExtensionsGenerator().Generate(new [] { info });
            files.Length.should_be(0);
        };

        it["works with namespaces"]                            = () => generates <NamespaceComponent>();
        it["generates matchers for each pool"]                 = () => generates <CComponent>();
        it["generates custom prefix"]                          = () => generates <CustomPrefixComponent>();
        it["generates component with default pool"]            = () => generates <DefaultPoolComponent>();
        it["generates component with default pool and others"] = () => generates <MultiplePoolAndDefaultPoolComponent>();

        it["generates component for class"]  = () => generates <SomeClass>("SomeClassComponent");
        it["generates component for struct"] = () => generates <SomeStruct>("SomeStructComponent");

        it["generates component for class with HideInBlueprintInspectorAttribute"] = () =>
                                                                                     generates <SomeClassHideInBlueprintInspector>("SomeClassHideInBlueprintInspectorComponent");
    }
    static void generates <T>(string expectedFileName = null)
    {
        var projectRoot = TestExtensions.GetProjectRoot();
        var fixturesDir = projectRoot + "/Tests/Tests/Entitas/CodeGenerator/Fixtures/Generated/";

        expectedFileName = (expectedFileName ?? typeof(T).FullName) + "GeneratedExtension";
        var path = fixturesDir + expectedFileName + ".cs";

        var expectedFileContent = File.ReadAllText(path);
        var info = TypeReflectionProvider.GetComponentInfos(typeof(T)).Single();

        var codeGenFiles = new ComponentExtensionsGenerator().Generate(new [] { info });

        codeGenFiles.Length.should_be(1);
        var codeGenFile = codeGenFiles.Single();

        #pragma warning disable
        if (logResults)
        {
            Console.WriteLine("should:\n" + expectedFileContent);
            Console.WriteLine("was:\n" + codeGenFile.fileContent);
        }

        codeGenFile.fileName.should_be(expectedFileName);
        var header = string.Format(CodeGenerator.AUTO_GENERATED_HEADER_FORMAT, typeof(ComponentExtensionsGenerator));
        (header + codeGenFile.fileContent).should_be(expectedFileContent);
    }
        public static void Generate()
        {
            var generatedFolder = getEntitasProjectDir() + "/Tests/Tests/Entitas/CodeGenerator/Fixtures/Generated/";

            var codeGenerators = new ICodeGenerator[] {
                new ComponentExtensionsGenerator(),
                new ComponentIndicesGenerator(),
                new PoolAttributesGenerator(),
                new PoolsGenerator(),
                new BlueprintsGenerator()
            };

            var types = new [] {
                typeof(AnimatingComponent),
                typeof(CComponent),
                typeof(ComponentWithFieldsAndProperties),
                typeof(CustomPrefixComponent),
                typeof(DefaultPoolComponent),
                typeof(MovableComponent),
                typeof(MultiplePoolAndDefaultPoolComponent),
                typeof(NamespaceComponent),
                typeof(OtherPoolComponent),
                typeof(PersonComponent),
                typeof(SomeClass),
                typeof(SomeClassHideInBlueprintInspector),
                typeof(SomeStruct),
                typeof(UserComponent)
            };

            var pools = new [] {
                "PoolA",
                "PoolB",
                "PoolC",
                "OtherPool",
                "SomePool",
                "SomeOtherPool"
            };

            var blueprints = new [] {
                "Gem",
                "Blocker"
            };

            var provider = new TypeReflectionProvider(types, pools, blueprints);
            var files    = CodeGenerator.Generate(provider, generatedFolder, codeGenerators);

            foreach (var file in files)
            {
                Console.WriteLine("Generated: " + file.fileName);
            }

            Console.WriteLine("Done. Press any key...");
            Console.Read();
        }
示例#7
0
        static Dictionary <string, int> getPools(Type[] components)
        {
            return(components.Aggregate(new Dictionary <string, int>(), (lookups, type) => {
                var lookupTags = TypeReflectionProvider.GetPools(type, false);
                if (lookupTags.Length == 0)
                {
                    lookupTags = new [] { "Pool" };
                }
                foreach (var lookupTag in lookupTags)
                {
                    if (!lookups.ContainsKey(lookupTag))
                    {
                        lookups.Add(lookupTag, 0);
                    }

                    lookups[lookupTag] += 1;
                }
                return lookups;
            }));
        }
        public static void Generate()
        {
            Console.WriteLine("Generating code...");
            var generatedDirectory = GetProjectDirectory() + "/Generated/";

            var codeGenerators = new ICodeGenerator[] {
                new ComponentExtensionsGenerator(),
                new ComponentIndicesGenerator(),
                new PoolAttributesGenerator(),
                new PoolsGenerator()
            };

            var assembly = Assembly.GetAssembly(typeof(CodeTemplates));
            var provider = new TypeReflectionProvider(assembly.GetTypes(), new string[0]);
            var files    = CodeGenerator.Generate(provider, generatedDirectory, codeGenerators);

            foreach (var file in files)
            {
                Console.WriteLine("Generated: " + generatedDirectory + file.fileName);
            }
            Console.WriteLine("...done generating code!");
        }
示例#9
0
        static void generate()
        {
            var generatedFolder = getEntitasProjectDir() + "/Readme/Readme/Generated/";

            var codeGenerators = new ICodeGenerator[] {
                new ComponentsGenerator(),
                new ComponentIndicesGenerator(),
                new PoolAttributeGenerator(),
                new PoolsGenerator()
            };

            var assembly = Assembly.GetAssembly(typeof(ReadmeSnippets));
            var provider = new TypeReflectionProvider(assembly.GetTypes(), new string[0]);
            var files    = CodeGenerator.Generate(provider, generatedFolder, codeGenerators);

            foreach (var file in files)
            {
                Console.WriteLine("Generated: " + file.fileName);
            }

            Console.WriteLine("Done. Press any key...");
            Console.Read();
        }
    void when_providing()
    {
        context["pool names"] = () => {
            it["has no pool names if empty"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(object) }, new string[0]);
                provider.poolNames.should_be_empty();
            };

            it["has pool names if set"] = () => {
                var provider = new TypeReflectionProvider(
                    new [] { typeof(TestPoolAttribute) },
                    new [] { "Pool1", "Pool2" }
                    );
                provider.poolNames.Length.should_be(2);
                provider.poolNames[0].should_be("Pool1");
                provider.poolNames[1].should_be("Pool2");
            };
        };

        context["component infos"] = () => {
            it["finds no components if there are no types which implement IComponent"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(object) }, new string[0]);
                provider.componentInfos.should_be_empty();
            };

            it["finds no components and ignores IComponent itself"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(IComponent) }, new string[0]);
                provider.componentInfos.should_be_empty();
            };

            it["finds no components and ignores interfaces"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(AnotherComponentInterface) }, new string[0]);
                provider.componentInfos.should_be_empty();
            };

            it["creates component info from found component"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(ComponentA) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("ComponentA");
                info.typeName.should_be("ComponentA");
                info.fieldInfos.should_be_empty();
                info.pools.should_be_empty();
                info.isSingleEntity.should_be_false();
                info.singleComponentPrefix.should_be("is");
                info.generateMethods.should_be(true);
                info.generateIndex.should_be(true);
                info.isSingletonComponent.should_be(true);
            };

            it["creates component info from found component with namespace"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(NamespaceComponent) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("My.Namespace.NamespaceComponent");
                info.typeName.should_be("NamespaceComponent");
                info.fieldInfos.should_be_empty();
                info.pools.should_be_empty();
                info.isSingleEntity.should_be_false();
                info.singleComponentPrefix.should_be("is");
                info.generateMethods.should_be(true);
                info.generateIndex.should_be(true);
                info.isSingletonComponent.should_be(true);
            };

            it["detects PoolAttribure"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(CComponent) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("CComponent");
                info.typeName.should_be("CComponent");
                info.fieldInfos.should_be_empty();
                info.pools.Length.should_be(3);

                info.pools.should_contain("PoolA");
                info.pools.should_contain("PoolB");
                info.pools.should_contain("PoolC");

                info.isSingleEntity.should_be_false();
                info.singleComponentPrefix.should_be("is");
                info.generateMethods.should_be(true);
                info.generateIndex.should_be(true);
                info.isSingletonComponent.should_be(true);
            };

            it["detects SingleEntityAttribute "] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(AnimatingComponent) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("AnimatingComponent");
                info.typeName.should_be("AnimatingComponent");
                info.fieldInfos.should_be_empty();
                info.pools.Length.should_be(0);
                info.isSingleEntity.should_be_true();
                info.singleComponentPrefix.should_be("is");
                info.generateMethods.should_be(true);
                info.generateIndex.should_be(true);
                info.isSingletonComponent.should_be(true);
            };

            it["detects DontGenerateAttribute "] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(DontGenerateComponent) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("DontGenerateComponent");
                info.typeName.should_be("DontGenerateComponent");
                info.fieldInfos.should_be_empty();
                info.pools.Length.should_be(0);
                info.isSingleEntity.should_be_false();
                info.singleComponentPrefix.should_be("is");
                info.generateMethods.should_be(false);
                info.generateIndex.should_be(true);
                info.isSingletonComponent.should_be(true);
            };

            it["detects DontGenerateAttribute and don't generate index"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(DontGenerateIndexComponent) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("DontGenerateIndexComponent");
                info.typeName.should_be("DontGenerateIndexComponent");
                info.fieldInfos.should_be_empty();
                info.pools.Length.should_be(0);
                info.isSingleEntity.should_be_false();
                info.singleComponentPrefix.should_be("is");
                info.generateMethods.should_be(false);
                info.generateIndex.should_be(false);
                info.isSingletonComponent.should_be(true);
            };

            it["detects CustomPrefixAttribute"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(CustomPrefixComponent) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("CustomPrefixComponent");
                info.typeName.should_be("CustomPrefixComponent");
                info.fieldInfos.should_be_empty();
                info.pools.Length.should_be(0);
                info.isSingleEntity.should_be_true();
                info.singleComponentPrefix.should_be("My");
                info.generateMethods.should_be(true);
                info.generateIndex.should_be(true);
                info.isSingletonComponent.should_be(true);
            };

            it["sets fields"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(PersonComponent) }, new string[0]);
                provider.componentInfos.Length.should_be(1);
                var info = provider.componentInfos[0];

                info.fullTypeName.should_be("PersonComponent");
                info.typeName.should_be("PersonComponent");
                info.fieldInfos.Length.should_be(2);

                info.fieldInfos[0].type.should_be("int");
                info.fieldInfos[0].name.should_be("age");

                info.fieldInfos[1].type.should_be("string");
                info.fieldInfos[1].name.should_be("name");

                info.pools.Length.should_be(0);
                info.isSingleEntity.should_be_false();
                info.singleComponentPrefix.should_be("is");
                info.generateMethods.should_be(true);
                info.generateIndex.should_be(true);
                info.isSingletonComponent.should_be(false);
            };

            it["gets multiple component infos"] = () => {
                var provider = new TypeReflectionProvider(new [] { typeof(ComponentA), typeof(ComponentB) }, new string[0]);
                provider.componentInfos.Length.should_be(2);
                provider.componentInfos[0].fullTypeName.should_be("ComponentA");
                provider.componentInfos[1].fullTypeName.should_be("ComponentB");
            };
        };
    }
        public static void Generate(Assembly assembly, string[] poolNames, string directory, ICodeGenerator[] codeGenerators)
        {
            var provider = new TypeReflectionProvider(assembly.GetTypes(), poolNames);

            CodeGenerator.Generate(provider, directory, codeGenerators);
        }
 public static void Generate(Assembly assembly, string[] poolNames, string directory, ICodeGenerator[] codeGenerators) {
     var provider = new TypeReflectionProvider(assembly.GetTypes(), poolNames);
     CodeGenerator.Generate(provider, directory, codeGenerators);
 }