Esempio n. 1
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);
        }
Esempio n. 2
0
        public AnalysisResult <CSharpSyncModels> Synchronize(string path)
        {
            _context.Init(path);

            var typesForGeneration = new List <ITypeSymbol>();

            // get all controller classes
            var controllers = ControllerHelper.GetControllers(_context.Project, _context.Compilation);

            // process controllers
            foreach (var controller in controllers)
            {
                // get all controller methods that are exposed via HTTP
                var httpMethods = ControllerHelper.GetPublicMethods(controller);

                foreach (var httpMethod in httpMethods)
                {
                    // collect unique return types
                    if (!typesForGeneration.Exists(t => t.Equals(httpMethod.ReturnType)))
                    {
                        typesForGeneration.Add(httpMethod.ReturnType);
                    }

                    // collect unique parameter types
                    foreach (var parameter in httpMethod.Parameters)
                    {
                        if (!typesForGeneration.Exists(t => t.Equals(parameter.Type)))
                        {
                            typesForGeneration.Add(parameter.Type);
                        }
                    }
                }
            }

            var namedTypes = new List <INamedTypeSymbol>();

            // process collected types
            foreach (var type in typesForGeneration)
            {
                var namedType = type as INamedTypeSymbol;

                if (!namedTypes.Exists(t => t.Equals(namedType)) && TypeHelper.IsSupportedType(type))
                {
                    namedTypes.Add(namedType);
                }

                // handle type arguments
                if (!namedType.TypeArguments.IsDefaultOrEmpty)
                {
                    // TODO: get type members recursively
                    foreach (var typeArgument in namedType.TypeArguments)
                    {
                        var namedArgument = typeArgument as INamedTypeSymbol;

                        if (!namedTypes.Exists(t => t.Equals(namedArgument)) && TypeHelper.IsSupportedType(namedArgument))
                        {
                            namedTypes.Add(namedArgument);
                        }
                    }
                }
            }

            var depGraphs = new List <DirectedSparseGraph <DependencyNode> >();

            foreach (var namedType in namedTypes)
            {
                var depGraphFactory = new DependencyGraphService();

                var graph = depGraphFactory.BuildForNamedTypeSymbol(namedType);

                var readable = graph.ToReadable();

                depGraphs.Add(graph);
            }

            var nodes       = depGraphs.SelectMany(g => g.Vertices).Distinct().ToList();
            var uniqueNodes = nodes
                              .GroupBy(n => n.NamedTypeSymbol.ContainingNamespace + "." + n.NamedTypeSymbol.Name)
                              .Select(n => n.First())
                              .ToList();

            var modelAnalyzer = new ModelAnalyzer(_context);

            var classModels = new List <CSharpClassModel>();
            var enumModels  = new List <CSharpEnumModel>();

            foreach (var node in uniqueNodes)
            {
                var symbol = node.NamedTypeSymbol;

                if (symbol.TypeKind == TypeKind.Class || symbol.TypeKind == TypeKind.Struct)
                {
                    classModels.Add(modelAnalyzer.AnalyzeClassSymbol(symbol));
                }
                else if (symbol.TypeKind == TypeKind.Enum)
                {
                    enumModels.Add(modelAnalyzer.AnalyzeEnumSymbol(symbol));
                }
            }

            var webApiAnalyzer = new WebApiAnalyzer(_context);

            var controllerModels = new List <CSharpControllerModel>();

            foreach (var controller in controllers)
            {
                controllerModels.Add(webApiAnalyzer.AnalyzeController(controller, false));
            }

            return(new AnalysisResult <CSharpSyncModels>()
            {
                Success = true,
                Value = new CSharpSyncModels()
                {
                    DataModels = new CSharpDataModels()
                    {
                        Classes = classModels,
                        Enums = enumModels
                    },
                    Controllers = controllerModels
                }
            });
        }