Пример #1
0
        public Result Handle()
        {
            var analyzer  = new ModelAnalyzer(new ModelAnalysisContext());
            var converter = new ModelConverter();
            var generator = new ModelGenerator();
            var emitter   = new TypeScriptEmitter();

            var analysisResult = analyzer.Analyze(_configuration.InputPath);

            if (!analysisResult.Success)
            {
                return(Result.CreateError($"Source analysis error: {analysisResult.ErrorMessage}"));
            }

            log.Debug("Source analyzed");

            var tsClassModels = converter.ConvertClasses(analysisResult.Value.Classes);
            var tsEnumModels  = converter.ConvertEnums(analysisResult.Value.Enums);

            log.Debug("Models converted");

            foreach (var tsModel in tsClassModels)
            {
                var contents = generator.GenerateClass(tsModel);

                emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Model, contents);

                log.Debug($"Class {tsModel.Name} emitted");

                new TsGenerator().GenerateDataModelAST(tsModel, _configuration.OutputPath);
            }

            foreach (var tsModel in tsEnumModels)
            {
                var contents = generator.GenerateEnum(tsModel);

                emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Enum, contents);

                log.Debug($"Enum {tsModel.Name} emitted");

                new TsGenerator().GenerateEnumAST(tsModel, _configuration.OutputPath);
            }

            return(Result.CreateSuccess());
        }
Пример #2
0
        public void ModelGeneration_Enum_ConstantValues()
        {
            // create syntax tree
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
public enum Gender
{
    None = 0,
    Male = 1,
    Female = 2
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { syntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csEnumModel = analyzer.AnalyzeEnumSymbol(RoslynTestHelpers.GetEnumSymbol(compilation, syntaxTree));

            // convert
            var converter   = new ModelConverter();
            var tsEnumModel = converter.ConvertEnum(csEnumModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateEnum(tsEnumModel, false);

            // assert
            var expected = @"export enum Gender {
	None = 0,
	Male = 1,
	Female = 2
}
";

            Assert.AreEqual(expected, generated);
        }
Пример #3
0
        public Result Handle()
        {
            var syncer = new Synchronizer();

            var modelConverter  = new ModelConverter();
            var webApiConverter = new WebApiConverter();

            var modelGenerator     = new ModelGenerator();
            var webClientGenerator = new WebClientGenerator();
            var tsGenerator        = new TsGenerator();

            var emitter = new TypeScriptEmitter();

            Generator generatorEnum;

            if (!Enum.TryParse(_configuration.Generator, true, out generatorEnum))
            {
                return(Result.CreateError("Unknown generator was specified"));
            }

            var analysisResult = syncer.Synchronize(_configuration.InputPath);

            if (!analysisResult.Success)
            {
                return(Result.CreateError($"Source analysis error: {analysisResult.ErrorMessage}"));
            }

            log.Debug("Source analyzed");

            var tsClassModels = modelConverter.ConvertClasses(analysisResult.Value.DataModels.Classes);
            var tsEnumModels  = modelConverter.ConvertEnums(analysisResult.Value.DataModels.Enums);
            var serviceModels = webApiConverter.ConvertControllers(analysisResult.Value.Controllers);

            log.Debug("Models converted");

            foreach (var tsModel in tsClassModels)
            {
                if (generatorEnum == Generator.Template)
                {
                    var contents = modelGenerator.GenerateClass(tsModel);
                    emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Model, contents);
                }
                else if (generatorEnum == Generator.Compiler)
                {
                    tsGenerator.GenerateDataModelAST(tsModel, _configuration.OutputPath);
                }

                log.Debug($"Class {tsModel.Name} emitted");
            }

            foreach (var tsModel in tsEnumModels)
            {
                if (generatorEnum == Generator.Template)
                {
                    var contents = modelGenerator.GenerateEnum(tsModel);
                    emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Enum, contents);
                }
                else if (generatorEnum == Generator.Compiler)
                {
                    tsGenerator.GenerateEnumAST(tsModel, _configuration.OutputPath);
                }

                log.Debug($"Enum {tsModel.Name} emitted");
            }

            foreach (var tsModel in serviceModels)
            {
                if (generatorEnum == Generator.Template)
                {
                    var contents = webClientGenerator.GenerateService(tsModel);
                    emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Service, contents);
                }
                else if (generatorEnum == Generator.Compiler)
                {
                    tsGenerator.GenerateServiceAST(tsModel, _configuration.OutputPath);
                }

                log.Debug($"Service {tsModel.Name} emitted");
            }

            return(Result.CreateSuccess());
        }