Ejemplo n.º 1
0
        /// <summary>
        /// should minic arguments from example\SQLCover.ps1
        /// run by `dotnet run -- -c Get-CoverTSql -r`
        /// `dotnet run -- -c Get-CoverTSql -e Export-OpenXml -k "Data Source=localhost;Initial Catalog=master;User ID=sa;Password=yourStrong(!)Password" -d DatabaseWithTests -q "tSQLt.runAll" -p TestCoverageOpenXml`
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(o =>
            {
                if (o.Verbose)
                {
                    Console.WriteLine($"Verbose output enabled. Current Arguments: -v {o.Verbose}");
                    Console.WriteLine("Current Arguments Serialized::" + JsonConvert.SerializeObject(o));
                    Console.WriteLine("SqlCoverCore! App is in Verbose mode!");
                }
                else
                {
                    Console.WriteLine($"Current Arguments: -v {o.Verbose} -c {o.Command}");
                    Console.WriteLine(":::Running SqlCoverCore:::");
                }
                var cType = CommandType.Unknown;
                var eType = CommandType.Unknown;
                string[] requiredParameters       = null;
                string[] requiredExportParameters = null;
                switch (o.Command)
                {
                case "Get-CoverTSql":
                    cType = CommandType.GetCoverTSql;
                    requiredParameters = new string[] {
                        "connectionString",
                        "databaseName",
                        "query"
                    };
                    break;

                case "Get-CoverExe":
                    cType = CommandType.GetCoverExe;
                    requiredParameters = new string[] {
                        "connectionString",
                        "databaseName",
                        "exeName",
                        "args"
                    };
                    break;

                case "Get-CoverRedgateCITest":
                    cType = CommandType.GetCoverRedgateCITest;
                    requiredParameters = new string[] {
                        "outputPath"
                    };
                    break;

                default:
                    Console.WriteLine("Command:" + o.Command + " is not supported");
                    break;
                }

                switch (o.ExportCommand)
                {
                case "Export-OpenXml":
                    eType = CommandType.ExportOpenXml;
                    requiredExportParameters = new string[] {
                        "outputPath"
                    };
                    break;

                case "Start-ReportGenerator":
                    eType = CommandType.StartReportGenerator;
                    requiredExportParameters = new string[] { "outputPath",
                                                              "reportGeneratorPath" };
                    break;

                case "Export-Html":
                    eType = CommandType.ExportHtml;
                    requiredExportParameters = new string[] {
                        "outputPath"
                    };
                    break;

                default:
                    Console.WriteLine("ExportCommand:" + o.ExportCommand + " is not supported");
                    break;
                }

                var validParams = eType != CommandType.Unknown && cType != CommandType.Unknown ? validateRequired(o, requiredParameters) : false;
                validParams     = validParams ? validateRequired(o, requiredExportParameters) : validParams;

                if (validParams)
                {
                    if (o.GetRequiredParameters)
                    {
                        Console.WriteLine(o.Command + " requiredParameters are:" + string.Join(',', requiredParameters));
                        Console.WriteLine(o.ExportCommand + " requiredParameters are:" + string.Join(',', requiredExportParameters));
                    }
                    else
                    {
                        Console.WriteLine(":::Running command" + cType.ToString() + ":::");
                        CodeCoverage coverage  = null;
                        CoverageResult results = null;
                        // run command
                        switch (cType)
                        {
                        case CommandType.GetCoverTSql:
                            coverage = new CodeCoverage(o.ConnectionString, o.databaseName, null, true, o.Debug);
                            results  = coverage.Cover(o.Query);
                            break;

                        case CommandType.GetCoverExe:
                            coverage = new CodeCoverage(o.ConnectionString, o.databaseName, null, true, o.Debug);
                            results  = coverage.CoverExe(o.ExeName, o.Args);
                            break;

                        case CommandType.GetCoverRedgateCITest:
                            //    coverage = new CodeCoverage(o.ConnectionString, o.databaseName, null, true, o.Debug);
                            //    coverage.Start();
                            // run Redgate SQLRelease Module

                            //    results = coverage.Stop();
                            Console.WriteLine(cType.ToString() + " is not YET supported");
                            break;
                        }
                        if (coverage != null && results != null)
                        {
                            Console.WriteLine(":::Running exportCommand" + eType.ToString() + ":::");
                            var resultString = "";
                            var outputPath   = "";
                            if (!string.IsNullOrWhiteSpace(o.OutputPath))
                            {
                                outputPath = o.OutputPath;
                                if (outputPath.Substring(outputPath.Length - 1, 1) != Path.DirectorySeparatorChar.ToString())
                                {
                                    outputPath += Path.DirectorySeparatorChar;
                                }
                            }
                            else
                            {
                                outputPath = "Coverage" + Path.DirectorySeparatorChar;
                                if (!Directory.Exists(outputPath))
                                {
                                    Directory.CreateDirectory(outputPath);
                                }
                            }
                            switch (eType)
                            {
                            case CommandType.ExportOpenXml:
                                resultString = results.OpenCoverXml();
                                results.SaveResult(outputPath + "Coverage.opencover.xml", resultString);
                                results.SaveSourceFiles(outputPath);
                                break;

                            case CommandType.ExportHtml:
                                resultString = results.Html();
                                results.SaveResult(outputPath + "Coverage.html", resultString);
                                results.SaveSourceFiles(outputPath);
                                break;

                            // thinking this should be separate from program, called directly from ci/cd
                            case CommandType.StartReportGenerator:
                                Console.WriteLine(eType.ToString() + " is not YET supported");
                                break;
                            }
                        }
                    }
                }
            });
        }