示例#1
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!");
        }
示例#2
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!");
        }