示例#1
0
        public static void Main(params string[] args)
        {
            const string modelsNamespace     = "AspNetCoreExample.Api.Models";
            var          customTypeGenerator = new CustomTypeGenerator()
                                               .WithTypeRedirect <Guid>("Guid", @"dataTypes\Guid")
                                               .WithTypeLocationRule(
                x => TypeInfo.From <ControllerBase>().IsAssignableFrom(x),
                x => $"api/{x.Name}".Replace("Controller", "Api")
                )
                                               .WithTypeLocationRule(
                x => x.FullName.StartsWith(modelsNamespace),
                x => "dto/" + x.FullName.Substring(modelsNamespace.Length + 1).Replace(".", "/")
                )
                                               .WithTypeBuildingContext(ApiControllerTypeBuildingContext.Accept, (unit, type) => new ApiControllerTypeBuildingContext(unit, type));

            var typeScriptCodeGenerator = new TypeScriptGenerator(
                new TypeScriptGenerationOptions
            {
                EnableExplicitNullability = true,
                EnableOptionalProperties  = false,
                EnumGenerationMode        = EnumGenerationMode.TypeScriptEnum,
                LinterDisableMode         = LinterDisableMode.TsLint,
                UseGlobalNullable         = true,
                NullabilityMode           = NullabilityMode.Optimistic,
            },
                customTypeGenerator,
                new TypesProvider()
                );
            var targetPath = Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName, "../../../output");

            typeScriptCodeGenerator.GenerateFiles(targetPath, JavaScriptTypeChecker.TypeScript);
        }
        public void CustomGeneratorBuilderTest()
        {
            var customGenerator = new CustomTypeGenerator()
                .WithTypeLocation<AnotherCustomType>(x => "a/b/c")
                .WithTypeRedirect<byte[]>("ByteArray", @"DataTypes\ByteArray")
                .WithTypeBuildingContext<HashSet<string>>(x => new CollectionTypeBuildingContext(x));

            var generator = new TypeScriptGenerator(TypeScriptGenerationOptions.Default, customGenerator, new RootTypesProvider(typeof(ArrayRootType)));
            var units = generator.Generate();
            var code = units.Select(x => x.GenerateCode(new DefaultCodeGenerationContext(JavaScriptTypeChecker)).Replace("\r\n", "\n")).ToArray();

            units.Select(x => x.Path).Should().Equal("", "a/b/c");
            var expectedCodeRoot = GetExpectedCode("CustomGenerator/custom-generator-builder");
            var expectedCodeChild = GetExpectedCode("CustomGenerator/custom-generator-builder-child");
            code.Length.Should().Be(2);
            code[0].Diff(expectedCodeRoot).ShouldBeEmpty();
            code[1].Diff(expectedCodeChild).ShouldBeEmpty();
        }
示例#3
0
        static void Main(string[] args)
        {
            var apiAssemblies = new[] { typeof(SkillApiInstaller).Assembly, typeof(CommentsApiInstaller).Assembly };

            var services = apiAssemblies.SelectMany(x => x.GetTypes().Where(x => x.BaseType == typeof(BusBasedService)));

            var publicServiceMethods = services.SelectMany(x => x.GetMethods().Where(m => m.DeclaringType == x && m.IsPublic));

            //service methods have to use only one public parameter - input param. And it should have output parameter - out param.
            var inputParameters  = publicServiceMethods.Select(x => x.GetParameters().Single().ParameterType);
            var outputParameters = publicServiceMethods.Select(x => x.ReturnType.GetGenericArguments().Single());

            var customGen = new CustomTypeGenerator()
                            .WithTypeLocationRule(x => true, x => DeterminePath(x))
                            .WithPropertyResolver(new CamelCasePropertyResolver());

            var typesToGenerate   = inputParameters.Concat(outputParameters).Concat(new[] { typeof(Page <>) }).ToArray();
            var rootTypesProvider = new RootTypesProvider(typesToGenerate);

            var generator = new TypeScriptGenerator(TypeScriptGenerationOptions.Default, customGen, rootTypesProvider);

            generator.GenerateFiles("../../src/Kaizen.Ui.Angular/src/app/shared/models");
        }
 public CustomTypeBuilderBuilding(CustomTypeGenerator dynamicTypeObject) => _dynamicTypeObject = dynamicTypeObject;