Beispiel #1
0
        public static void Run(NpgsqlConnection connection)
        {
            ConnectionName = (Settings.Value.Connection ?? $"{connection.Host}_{connection.Port}_{connection.Database}").SanitazePath();

            if (Settings.Value.Execute != null)
            {
                DumpTitle("** EXECUTION **");
                ExecuteFromSetting(connection);
                return;
            }

            if (Settings.Value.Psql)
            {
                DumpTitle("** PSQL TERMINAL **");
                new PsqlRunner(Settings.Value, connection).TryRunFromTerminal();
                return;
            }

            if (Settings.Value.CommitMd)
            {
                DumpTitle("** COMMIT MARKDOWN (MD) EDITS **");
                BuildMdDiff(connection);
                return;
            }

            if (Settings.Value.SchemaDumpFile != null)
            {
                SchemaFile = string.Format(Path.GetFullPath(Path.Combine(Program.CurrentDir, Settings.Value.SchemaDumpFile)), ConnectionName);
            }

            if (Settings.Value.DataDumpFile != null)
            {
                DataFile = string.Format(Path.GetFullPath(Path.Combine(Program.CurrentDir, Settings.Value.DataDumpFile)), ConnectionName);
            }

            if (Settings.Value.DbObjects || Settings.Value.SchemaDump || Settings.Value.DataDump)
            {
                var builder = new PgDumpBuilder(Settings.Value, connection);
                if (Settings.CheckPgDumpVersion(builder))
                {
                    if (Settings.Value.SchemaDump)
                    {
                        DumpTitle("** SCHEMA DUMP SCRIPT GENERATION **");
                        BuildDump(
                            dumpFile: Settings.Value.SchemaDumpFile,
                            file: SchemaFile,
                            overwrite: Settings.Value.SchemaDumpOverwrite,
                            askOverwrite: Settings.Value.SchemaDumpAskOverwrite,
                            contentFunc: () => builder.GetSchemaContent());
                    }
                    if (Settings.Value.DataDump)
                    {
                        DumpTitle("** DATA DUMP SCRIPT GENERATION **");
                        BuildDump(
                            dumpFile: Settings.Value.DataDumpFile,
                            file: DataFile,
                            overwrite: Settings.Value.DataDumpOverwrite,
                            askOverwrite: Settings.Value.DataDumpAskOverwrite,
                            contentFunc: () => builder.GetDataContent());
                    }
                    if (Settings.Value.DbObjects)
                    {
                        DumpTitle("** DATA OBJECTS SCRIPTS TREE GENERATION **");
                        BuildObjectDumps(builder);
                    }
                }
            }

            if (Settings.Value.Diff)
            {
                DumpTitle("** DIFF  SCRIPT GENERATION **");
                BuildDiffScript(connection);
            }

            if (Settings.Value.Routines)
            {
                DumpTitle("** ROUTINE SOURCE CODE GENERATION **");
                new CodeRoutinesBuilder(connection, Settings.Value, CodeSettings.ToRoutineSettings(Settings.Value)).Build();
            }

            if (Settings.Value.Crud)
            {
                DumpTitle("** CRUD SOURCE CODE GENERATION **");
                new CodeCrudBuilder(connection, Settings.Value, CodeSettings.ToCrudSettings(Settings.Value)).Build();
            }

            if (Settings.Value.UnitTests)
            {
                DumpTitle("** UNIT TEST PROJECT TEMPLATE CODE GENERATION **");
                BuildUnitTests(connection);
            }

            if (Settings.Value.Markdown)
            {
                DumpTitle("** MARKDOWN (MD) GENERATION **");
                BuilMd(connection);
            }

            DumpTitle("", "", "**** FINISHED ****");
        }
 public CodeCrudBuilder(NpgsqlConnection connection, Settings settings, CodeSettings codeSettings) :
     base(connection, settings, codeSettings)
 {
 }
Beispiel #3
0
        private static void BuildUnitTests(NpgsqlConnection connection)
        {
            if (!Settings.Value.UnitTests || Settings.Value.UnitTestsDir == null)
            {
                return;
            }
            string sufix;

            if (Settings.Value.Namespace != null)
            {
                sufix = Settings.Value.Namespace;
            }
            else
            {
                var projFile = Directory.EnumerateFiles(Program.CurrentDir, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault();
                if (projFile != null)
                {
                    sufix = Path.GetFileNameWithoutExtension(projFile);
                }
                else
                {
                    sufix = Path.GetFileName(Path.GetFullPath(Program.CurrentDir));
                }
            }
            var shortDir    = string.Format(Settings.Value.UnitTestsDir, sufix);
            var dir         = Path.GetFullPath(Path.Join(Program.CurrentDir, shortDir));
            var name        = Path.GetFileName(dir);
            var relativeDir = dir.GetRelativePath();

            var exists = Directory.Exists(dir);

            if (exists && Settings.Value.UnitTestsAskRecreate)
            {
                var answer = Program.Ask($"Directory {relativeDir} already exists. Do you want to recreate this dir? This will delete all existing files. [Y/N]", ConsoleKey.Y, ConsoleKey.N);
                if (answer == ConsoleKey.Y)
                {
                    if (Directory.GetFiles(dir).Length > 0)
                    {
                        DumpFormat("Deleting all existing files in dir: {0} ...", relativeDir);
                        foreach (FileInfo fi in new DirectoryInfo(dir).GetFiles())
                        {
                            fi.Delete();
                        }
                    }
                    DumpFormat("Removing dir: {0} ...", relativeDir);
                    Directory.Delete(dir, true);
                    exists = false;
                }
            }
            if (!exists)
            {
                DumpFormat("Creating dir: {0} ...", relativeDir);
                Directory.CreateDirectory(dir);
            }

            var settingsFile = Path.GetFullPath(Path.Join(dir, "testsettings.json"));

            if (!File.Exists(settingsFile))
            {
                DumpRelativePath("Creating file: {0} ...", settingsFile);
                if (!WriteFile(settingsFile, GetTestSettingsContent(connection, dir)))
                {
                    return;
                }
            }
            else
            {
                DumpRelativePath("Skipping {0}, already exists ...", settingsFile);
            }

            var projectFile = Path.GetFullPath(Path.Join(dir, $"{name}.csproj"));
            List <ExtensionMethods> extensions = new();

            extensions.AddRange(new CodeRoutinesBuilder(connection, Settings.Value, CodeSettings.ToRoutineSettings(Settings.Value)).GetMethods());
            extensions.AddRange(new CodeCrudBuilder(connection, Settings.Value, CodeSettings.ToCrudSettings(Settings.Value)).GetMethods());

            if (!File.Exists(projectFile))
            {
                DumpRelativePath("Creating file: {0} ...", projectFile);
                if (!WriteFile(projectFile, GetTestCsprojContent(dir)))
                {
                    return;
                }
                Program.RunProcess("dotnet", "add package Microsoft.NET.Test.Sdk", dir);
                Program.RunProcess("dotnet", "add package Microsoft.Extensions.Configuration", dir);
                Program.RunProcess("dotnet", "add package Microsoft.Extensions.Configuration.Json", dir);
                Program.RunProcess("dotnet", "add package Microsoft.Extensions.Configuration.Binder", dir);
                Program.RunProcess("dotnet", "add package Norm.net", dir);
                if (extensions.Any(e => e.Methods.Any(m => m.Sync == false)))
                {
                    Program.RunProcess("dotnet", "add package System.Linq.Async", dir);
                }
                Program.RunProcess("dotnet", "add package Npgsql", dir);
                Program.RunProcess("dotnet", "add package xunit", dir);
                Program.RunProcess("dotnet", "add package xunit.runner.visualstudio", dir);
                Program.RunProcess("dotnet", "add package coverlet.collector", dir);
                Program.RunProcess("dotnet", "add package FluentAssertions", dir);
            }
            else
            {
                DumpRelativePath("Skipping {0}, already exists ...", projectFile);
            }

            var fixtureFile = Path.GetFullPath(Path.Join(dir, "TestFixtures.cs"));

            if (!File.Exists(fixtureFile))
            {
                DumpRelativePath("Creating file: {0} ...", fixtureFile);
                if (!WriteFile(fixtureFile, GetTestFixturesFile(name)))
                {
                    return;
                }
            }
            else
            {
                DumpRelativePath("Skipping {0}, already exists ...", fixtureFile);
            }

            if (extensions.Any())
            {
                foreach (var ext in extensions)
                {
                    var className  = $"{ext.Name}UnitTests";
                    var moduleFile = Path.GetFullPath(Path.Join(dir, $"{className}.cs"));
                    if (File.Exists(moduleFile))
                    {
                        DumpRelativePath("Skipping {0}, already exists ...", moduleFile);
                        continue;
                    }
                    var module = new Module(new Settings {
                        Namespace = name
                    });
                    if (ext.Methods.Any(m => m.Sync == false))
                    {
                        module.AddUsing("System.Threading.Tasks");
                    }
                    module.AddUsing("Xunit");
                    module.AddUsing("Norm");
                    module.AddUsing("FluentAssertions");
                    module.AddUsing(ext.Namespace);
                    if (!string.IsNullOrEmpty(ext.ModelNamespace))
                    {
                        module.AddUsing(ext.ModelNamespace);
                    }
                    var code = new UnitTestCode(Settings.Value, className, ext);
                    module.AddItems(code.Class);
                    DumpRelativePath("Creating file: {0} ...", moduleFile);
                    WriteFile(moduleFile, module.ToString());
                }
            }
            else
            {
                var className  = $"UnitTest1";
                var moduleFile = Path.GetFullPath(Path.Join(dir, $"{className}.cs"));
                if (File.Exists(moduleFile))
                {
                    DumpRelativePath("Skipping {0}, already exists ...", moduleFile);
                    return;
                }
                var module = new Module(new Settings {
                    Namespace = name
                });
                module.AddUsing("Xunit");
                module.AddUsing("Norm");

                var code = new UnitTestCode(Settings.Value, className, null);
                module.AddItems(code.Class);
                DumpRelativePath("Creating file: {0} ...", moduleFile);
                WriteFile(moduleFile, module.ToString());
            }
        }