Exemplo n.º 1
0
        private static void BuildDump(string dumpFile, string file, bool overwrite, bool askOverwrite, Func <string> contentFunc)
        {
            if (string.IsNullOrEmpty(dumpFile))
            {
                return;
            }
            var shortFilename = Path.GetFileName(file);
            var dir           = Path.GetFullPath(Path.GetDirectoryName(Path.GetFullPath(file)));
            var exists        = File.Exists(file);
            var relative      = file.GetRelativePath();

            if (!Settings.Value.Dump && !Directory.Exists(dir))
            {
                DumpRelativePath("Creating dir: {0} ...", dir);
                Directory.CreateDirectory(dir);
            }

            if (!Settings.Value.Dump && exists && overwrite == false)
            {
                DumpFormat("File {0} exists, overwrite is set to false, skipping ...", relative);
                return;
            }
            if (!Settings.Value.Dump && exists && Settings.Value.SkipIfExists != null &&
                (
                    Settings.Value.SkipIfExists.Contains(shortFilename) ||
                    Settings.Value.SkipIfExists.Contains(relative))
                )
            {
                DumpFormat("Skipping {0}, already exists ...", relative);
                return;
            }
            if (!Settings.Value.Dump && exists && askOverwrite &&
                Program.Ask($"File {relative} already exists, overwrite? [Y/N]", ConsoleKey.Y, ConsoleKey.N) == ConsoleKey.N)
            {
                DumpFormat("Skipping {0} ...", relative);
                return;
            }

            DumpFormat("Creating dump file {0} ...", relative);
            try
            {
                WriteFile(file, contentFunc());
            }
            catch (Exception e)
            {
                Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {relative}", $"ERROR: {e.Message}");
            }
        }
Exemplo n.º 2
0
        public static bool ParseInitialSettings(NpgsqlConnection connection, bool haveArguments, string pgroutinerSettingsFile)
        {
            var pgroutinerFile = Path.Join(Program.CurrentDir, pgroutinerSettingsFile);
            var exists         = File.Exists(Path.Join(pgroutinerFile));

            if (!exists && Program.Config.GetSection("PgRoutiner").GetChildren().Count() == 0 && !haveArguments)
            {
                Program.WriteLine(ConsoleColor.Yellow, "",
                                  "You don't seem to be using any available command-line commands and PgRoutiner configuration seems to be missing.");
                Program.Write(ConsoleColor.Yellow,
                              $"Would you like to create a custom settings file \"");
                Program.Write(ConsoleColor.Cyan, pgroutinerSettingsFile);
                Program.WriteLine(ConsoleColor.Yellow, "\" with your current values?",
                                  "This settings configuration file can be used to change settings for this directory without using a command-line.");
                Program.Write(ConsoleColor.Yellow,
                              $"Create \"");
                Program.Write(ConsoleColor.Cyan, pgroutinerSettingsFile);
                Program.WriteLine(ConsoleColor.Yellow, "\" in this dir [Y/N]?");

                var answer = Program.Ask(null, ConsoleKey.Y, ConsoleKey.N);
                if (answer == ConsoleKey.Y)
                {
                    BuildSettingsFile(pgroutinerFile, connection, pgroutinerSettingsFile);
                }
            }

            var routineCount = connection.GetRoutineCount(Value);
            var crudCount    = connection.GetTableDefintionsCount(Value);

            if ((Value.Routines && Value.OutputDir != null && routineCount > 0) ||
                (Value.Crud && Value.CrudOutputDir != null && crudCount > 0) ||
                (Value.UnitTests && Value.UnitTestsDir != null))
            {
                ProjectInfo = ParseProjectFile();
                if (ProjectInfo == null)
                {
                    return(false);
                }

                if (Value.Routines)
                {
                    UpdateProjectReferences(ProjectInfo);
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        private static void BuilMd(NpgsqlConnection connection)
        {
            if (string.IsNullOrEmpty(Settings.Value.MdFile))
            {
                return;
            }
            var file          = string.Format(Path.GetFullPath(Path.Combine(Program.CurrentDir, Settings.Value.MdFile)), ConnectionName);
            var relative      = file.GetRelativePath();
            var shortFilename = Path.GetFileName(file);
            var dir           = Path.GetFullPath(Path.GetDirectoryName(Path.GetFullPath(file)));
            var exists        = File.Exists(file);

            if (!Settings.Value.Dump && !Directory.Exists(dir))
            {
                DumpRelativePath("Creating dir: {0} ...", dir);
                Directory.CreateDirectory(dir);
            }

            if (!Settings.Value.Dump && exists && Settings.Value.MdOverwrite == false)
            {
                DumpFormat("File {0} exists, overwrite is set to false, skipping ...", relative);
                return;
            }
            if (!Settings.Value.Dump && exists && Settings.Value.SkipIfExists != null && (
                    Settings.Value.SkipIfExists.Contains(shortFilename) || Settings.Value.SkipIfExists.Contains(relative))
                )
            {
                DumpFormat("Skipping {0}, already exists ...", relative);
                return;
            }
            if (!Settings.Value.Dump && exists && Settings.Value.MdAskOverwrite &&
                Program.Ask($"File {relative} already exists, overwrite? [Y/N]", ConsoleKey.Y, ConsoleKey.N) == ConsoleKey.N)
            {
                DumpFormat("Skipping {0} ...", relative);
                return;
            }

            DumpFormat("Creating markdown file {0} ...", relative);
            var builder = new MarkdownDocument(Settings.Value, connection);

            WriteFile(file, builder.Build());
        }
Exemplo n.º 4
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());
            }
        }
Exemplo n.º 5
0
        private static void UpdateProjectReferences(Project project)
        {
            if (project.NpgsqlIncluded == false)
            {
                if (!Value.SkipUpdateReferences)
                {
                    Program.DumpError($"Npgsql package package is required.");
                    if (Program.Ask("Add Npgsql reference? [Y/N]", ConsoleKey.Y, ConsoleKey.N) == ConsoleKey.Y)
                    {
                        Program.RunProcess("dotnet", "add package Npgsql");
                    }
                }
            }

            if (!Value.SkipAsyncMethods && project.AsyncLinqIncluded == false)
            {
                if (!Value.SkipUpdateReferences)
                {
                    Program.DumpError($"To be able to use async methods, System.Linq.Async package is required.");
                    if (Program.Ask("Add System.Linq.Async package reference? [Y/N]", ConsoleKey.Y, ConsoleKey.N) == ConsoleKey.Y)
                    {
                        Program.RunProcess("dotnet", "add package System.Linq.Async");
                    }
                }
            }

            if (string.IsNullOrEmpty(project.NormVersion))
            {
                if (!Value.SkipUpdateReferences)
                {
                    Program.DumpError($"Norm.net package package is required.");
                    if (Program.Ask("Add Norm.net reference? [Y/N]", ConsoleKey.Y, ConsoleKey.N) == ConsoleKey.Y)
                    {
                        Program.RunProcess("dotnet", "add package Norm.net");
                        project.NormVersion = Value.MinNormVersion;
                    }
                }
            }

            var minNormVersion = Convert.ToInt32(Value.MinNormVersion.Replace(".", ""));

            try
            {
                var version = Convert.ToInt32(project.NormVersion.Replace(".", ""));
                if (version < minNormVersion)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                if (!Value.SkipUpdateReferences)
                {
                    Program.DumpError($"Minimum version for Norm.net package is {Settings.Value.MinNormVersion}. Current version in project is {project.NormVersion}.");
                    if (Program.Ask("Update Norm.net package? [Y/N]", ConsoleKey.Y, ConsoleKey.N) == ConsoleKey.Y)
                    {
                        Program.RunProcess("dotnet", "add package Norm.net");
                    }
                }
            }
        }