Пример #1
0
        private static void GenerateModels(string dir)
        {
            // var recordDescriptors = Tests.GetRecordDescriptors(dir);
            var recordDescriptors = CtrlGenParser.ParseAll(dir);

            foreach (var desc in recordDescriptors)
            {
                ModelClassGenerator.GenerateClass(desc);
            }
        }
Пример #2
0
        public void BasicTest()
        {
            TestFileSystem fileSystem = new TestFileSystem();

            fileSystem.AddFile("A\\table1.cs", TestTable1Text);

            var generated = ExistingCodeExplorer
                            .EnumerateTableDescriptorsModelAttributes("A", fileSystem)
                            .ParseAttribute(true)
                            .CreateAnalysis()
                            .Select(meta => ModelClassGenerator.Generate(meta, "Org", "", true, ModelType.ImmutableClass, fileSystem, out _).SyntaxTree)
                            .ToList();

            var trees = new List <SyntaxTree>();

            foreach (var syntaxTree in generated)
            {
                trees.Add(CSharpSyntaxTree.ParseText(syntaxTree.ToString()));
            }

            trees.Add(CSharpSyntaxTree.ParseText(TestTable1Text));

            var compilation = CSharpCompilation.Create("SqModels",
                                                       trees,
                                                       options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: NullableContextOptions.Enable));

            compilation = compilation.AddReferences(
                MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location),
                MetadataReference.CreateFromFile(typeof(object).Assembly.GetAssemblyLocation()),
                MetadataReference.CreateFromFile(Assembly
                                                 .Load("System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
                                                 .Location),
                MetadataReference.CreateFromFile(typeof(SqQueryBuilder).Assembly.GetAssemblyLocation()));

            MemoryStream ms = new MemoryStream();

            var emitResult = compilation.Emit(ms);

            if (!emitResult.Success)
            {
                Diagnostic first = emitResult.Diagnostics.First();

                var sourceCode = first.Location.SourceTree?.ToString();
                var s          = sourceCode?.Substring(first.Location.SourceSpan.Start, first.Location.SourceSpan.Length);
                Console.WriteLine(sourceCode);
                Assert.Fail(first.GetMessage() + (string.IsNullOrEmpty(s)?null:$" \"{s}\""));
            }

            var assembly = Assembly.Load(ms.ToArray());

            var allTypes = assembly.GetTypes();

            Assert.AreEqual(21, allTypes.Length);
        }
Пример #3
0
        public static Type GetModelType(this RecordDescriptor desc)
        {
            var typeName = "Apache.Ignite.Sybase.Ingest.Cache." + ModelClassGenerator.GetClassName(desc.TableName);
            var type     = Type.GetType(typeName);

            if (type == null)
            {
                throw new Exception("Model class not found: " + typeName);
            }

            return(type);
        }
Пример #4
0
        private static async Task RunGenModelsOptions(GenModelsOptions options)
        {
            ILogger logger = new DefaultLogger(Console.Out, options.Verbosity);

            logger.LogMinimal("Model classes generation is running...");

            string inDirectory  = EnsureDirectory(options.InputDir, logger, "Input", false);
            string outDirectory = EnsureDirectory(options.OutputDir, logger, "Output", true);

            var analysis = ExistingCodeExplorer
                           .EnumerateTableDescriptorsModelAttributes(inDirectory)
                           .ParseAttribute(options.NullRefTypes)
                           .CreateAnalysis();

            if (analysis.Count < 1)
            {
                logger.LogNormal("No model attributes detected in the input directory.");
            }
            else
            {
                logger.LogNormal($"Found {analysis.Count} models in the input directory.");
            }

            if (logger.IsDetailed)
            {
                foreach (var model in analysis)
                {
                    logger.LogDetailed(model.Name);
                    foreach (var property in model.Properties)
                    {
                        logger.LogDetailed(
                            $" -{property.Type} {property.Name}");
                        foreach (var col in property.Column)
                        {
                            logger.LogDetailed(
                                $"   ={(property.CastType != null ? $"({property.CastType})" : null)}{col.TableRef.TableTypeName}.{col.ColumnName}");
                        }
                    }
                }
            }

            logger.LogNormal("Code generation...");

            foreach (var meta in analysis)
            {
                string path = Path.Combine(outDirectory, $"{meta.Name}.cs");
                if (logger.IsDetailed)
                {
                    logger.LogDetailed(path);
                }
                await File.WriteAllTextAsync(path, ModelClassGenerator.Generate(meta, options.Namespace, path, out var existing).ToFullString());

                if (logger.IsDetailed)
                {
                    logger.LogDetailed(existing ? "Existing file updated." : "New file created.");
                }
            }


            logger.LogMinimal("Model classes generation successfully completed!");
        }
Пример #5
0
        private static async Task RunGenModelsOptions(GenModelsOptions options)
        {
            ILogger logger = new DefaultLogger(Console.Out, options.Verbosity);

            logger.LogMinimal("Model classes generation is running...");

            string inDirectory  = EnsureDirectory(options.InputDir, logger, "Input", false);
            string outDirectory = EnsureDirectory(options.OutputDir, logger, "Output", true);

            var analysis = ExistingCodeExplorer
                           .EnumerateTableDescriptorsModelAttributes(inDirectory, DefaultFileSystem.Instance)
                           .ParseAttribute(options.NullRefTypes)
                           .CreateAnalysis();

            if (analysis.Count < 1)
            {
                logger.LogNormal("No model attributes detected in the input directory.");
            }
            else
            {
                logger.LogNormal($"Found {analysis.Count} models in the input directory.");
            }

            if (logger.IsDetailed)
            {
                foreach (var model in analysis)
                {
                    logger.LogDetailed(model.Name);
                    foreach (var property in model.Properties)
                    {
                        logger.LogDetailed(
                            $" -{property.Type} {property.Name}");
                        foreach (var col in property.Column)
                        {
                            logger.LogDetailed(
                                $"   ={(property.CastType != null ? $"({property.CastType})" : null)}{col.TableRef.TableTypeName}.{col.ColumnName}");
                        }
                    }
                }
            }

            logger.LogNormal("Code generation...");

            foreach (var meta in analysis)
            {
                string path = Path.Combine(outDirectory, $"{meta.Name}.cs");
                if (logger.IsDetailed)
                {
                    logger.LogDetailed(path);
                }
                await File.WriteAllTextAsync(path, ModelClassGenerator.Generate(meta, options.Namespace, path, options.RwClasses, options.ModelType, DefaultFileSystem.Instance, out var existing).ToFullString());

                if (logger.IsDetailed)
                {
                    logger.LogDetailed(existing ? "Existing file updated." : "New file created.");
                }
            }

            if (options.CleanOutput)
            {
                var modelFiles = analysis.Select(meta => $"{meta.Name}.cs").ToHashSet(StringComparer.InvariantCultureIgnoreCase);

                var toRemove = Directory.EnumerateFiles(outDirectory).Where(p => !modelFiles.Contains(Path.GetFileName(p))).ToList();

                foreach (var delPath in toRemove)
                {
                    File.Delete(delPath);
                    if (logger.IsNormalOrHigher)
                    {
                        logger.LogNormal($"File {Path.GetFileName(delPath)} has been removed since it does not contain any model class");
                    }
                }
            }


            logger.LogMinimal("Model classes generation successfully completed!");
        }