예제 #1
0
        private static List <CoverageSource> ParseCoverageSources(MainArgs args)
        {
            List <CoverageSource> results = new List <CoverageSource>();

            if (args.OptMultiple)
            {
                var modes = args.OptInput.Split(';');
                foreach (var modekeyvalue in modes)
                {
                    var split   = modekeyvalue.Split('=');
                    var rawMode = split[0];
                    var input   = split[1];
                    var mode    = GetMode(rawMode);
                    if (!mode.HasValue)
                    {
                        ExitWithError("Unknown mode provided");
                    }

                    results.Add(new CoverageSource((CoverageMode)mode, input));
                }
            }
            else
            {
                var mode = GetMode(args);
                if (!mode.HasValue)
                {
                    ExitWithError("Unknown mode provided");
                }

                results.Add(new CoverageSource((CoverageMode)mode, args.OptInput));
            }

            return(results);
        }
예제 #2
0
        private string ResolveRepoToken(MainArgs args)
        {
            string?repoToken = null;

            if (args.IsProvided("--repoToken"))
            {
                repoToken = args.OptRepotoken;
                if (repoToken.IsNullOrWhitespace())
                {
                    ExitWithError("parameter repoToken is required.");
                }
            }
            else
            {
                var variable = args.OptRepotokenvariable;
                if (variable.IsNullOrWhitespace())
                {
                    ExitWithError("parameter repoTokenVariable is required.");
                }
                else
                {
                    repoToken = _environmentVariables.GetEnvironmentVariable(variable);
                }

                if (repoToken.IsNullOrWhitespace())
                {
                    ExitWithError($"No token found in Environment Variable '{variable}'.");
                }
            }

            return(repoToken);
        }
예제 #3
0
        private Either <GitData, CommitSha>?ResolveGitData(IConsole console, MainArgs args)
        {
            var providers = new List <IGitDataResolver>
            {
                new CommandLineGitDataResolver(args),
                new AppVeyorGitDataResolver(_environmentVariables),
                new TeamCityGitDataResolver(_environmentVariables, _console)
            };

            var provider = providers.FirstOrDefault(p => p.CanProvideData());

            if (provider is null)
            {
                console.WriteLine("No git data available");
            }
            else
            {
                console.WriteLine($"Using Git Data Provider '{provider.DisplayName}'");
                var data = provider.GenerateData();
                if (data.HasValue)
                {
                    return(data);
                }
            }

            return(null);
        }
예제 #4
0
        public int?Run(string[] argv)
        {
            try
            {
                var args = new MainArgs(argv, version: _version);
                if (args.Failed)
                {
                    _console.WriteLine(args.FailMessage);
                    return(args.FailErrorCode);
                }

                var gitData = ResolveGitData(_console, args);

                var settings = LoadSettings(args);

                var metadata = CoverageMetadataResolver.Resolve(args);

                var app    = new CoverallsPublisher(_console, _fileSystem);
                var result = app.Run(settings, gitData.ValueOrDefault(), metadata);
                if (!result.Successful)
                {
                    ExitWithError(result.Error);
                }

                return(null);
            }
            catch (ExitException ex)
            {
                _console.WriteErrorLine(ex.Message);
                return(1);
            }
        }
예제 #5
0
        private static bool ResolveParallel(MainArgs args, IEnvironmentVariables variables)
        {
            if (args.IsProvided("--parallel"))
            {
                return(args.OptParallel);
            }

            return(variables.GetBooleanVariable("COVERALLS_PARALLEL"));
        }
예제 #6
0
        private static Option <GitData> ResolveGitData(MainArgs args)
        {
            var providers = new List <IGitDataResolver>
            {
                new CommandLineGitDataResolver(args),
                new AppVeyorGitDataResolver(new EnvironmentVariables())
            };

            return(providers.Where(p => p.CanProvideData()).Select(p => p.GenerateData()).FirstOrDefault());
        }
예제 #7
0
 private static List <IMetaDataResolver> CreateResolvers(MainArgs args, IEnvironmentVariables variables)
 {
     return(new List <IMetaDataResolver>
     {
         new CommandLineMetaDataResolver(args),
         new AppVeyorMetaDataResolver(variables),
         new TravisMetaDataResolver(variables),
         new TeamCityMetaDataResolver(variables)
     });
 }
예제 #8
0
        private ConfigurationSettings LoadSettings(MainArgs args)
        {
            var settings = new ConfigurationSettings(
                repoToken: ResolveRepoToken(args),
                outputFile: args.IsProvided("--output") ? args.OptOutput : null,
                dryRun: args.OptDryrun,
                treatUploadErrorsAsWarnings: args.OptTreatuploaderrorsaswarnings,
                useRelativePaths: args.OptUserelativepaths,
                basePath: args.IsProvided("--basePath") ? args.OptBasepath : null,
                ParseCoverageSources(args));

            return(settings);
        }
예제 #9
0
        private static Option <string> ResolvePullRequestId(MainArgs args)
        {
            if (args.IsProvided("--pullRequest"))
            {
                return(args.OptPullrequest);
            }
            var prId = new EnvironmentVariables().GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER");

            if (prId.IsNotNullOrWhitespace())
            {
                return(prId);
            }
            return(null);
        }
예제 #10
0
        private static Option <string> ResolveServiceJobId(MainArgs args)
        {
            if (args.IsProvided("--jobId"))
            {
                return(args.OptJobid);
            }
            var jobId = new EnvironmentVariables().GetEnvironmentVariable("APPVEYOR_JOB_ID");

            if (jobId.IsNotNullOrWhitespace())
            {
                return(jobId);
            }
            return(null);
        }
예제 #11
0
        private static ConfigurationSettings LoadSettings(MainArgs args)
        {
            var settings = new ConfigurationSettings
            {
                RepoToken  = ResolveRepoToken(args),
                OutputFile = args.IsProvided("--output") ? args.OptOutput : string.Empty,
                DryRun     = args.OptDryrun,
                TreatUploadErrorsAsWarnings = args.OptTreatuploaderrorsaswarnings,
                UseRelativePaths            = args.OptUserelativepaths,
                BasePath = args.IsProvided("--basePath") ? args.OptBasepath : null
            };

            settings.CoverageSources.AddRange(ParseCoverageSources(args));
            return(settings);
        }
예제 #12
0
        private static Option <string> ResolveServiceName(MainArgs args)
        {
            if (args.IsProvided("--serviceName"))
            {
                return(args.OptServicename);
            }

            var isAppVeyor = new EnvironmentVariables().GetBooleanVariable("APPVEYOR");

            if (isAppVeyor)
            {
                return("appveyor");
            }

            return(null);
        }
예제 #13
0
        private static Option <string> ResolveServiceNumber(MainArgs args)
        {
            if (args.IsProvided("--serviceNumber"))
            {
                return(args.OptServicenumber);
            }

            var jobId = new EnvironmentVariables().GetEnvironmentVariable("APPVEYOR_BUILD_NUMBER");

            if (jobId.IsNotNullOrWhitespace())
            {
                return(jobId);
            }

            return(null);
        }
예제 #14
0
        public static CoverageMetadata Resolve(MainArgs args)
        {
            var serviceName   = ResolveServiceName(args);
            var serviceJobId  = ResolveServiceJobId(args);
            var serviceNumber = ResolveServiceNumber(args);
            var pullRequestId = ResolvePullRequestId(args);
            var parallel      = args.OptParallel;

            return(new CoverageMetadata
            {
                ServiceJobId = serviceJobId.ValueOr("0"),
                ServiceName = serviceName.ValueOr("coveralls.net"),
                ServiceNumber = serviceNumber.ValueOr(null),
                PullRequestId = pullRequestId.ValueOr(null),
                Parallel = parallel
            });
        }
예제 #15
0
        public int?Run(string[] argv)
        {
            try
            {
                var args = new MainArgs(argv, version: _version);
                if (args.Failed)
                {
                    _console.WriteLine(args.FailMessage !);
                    return(args.FailErrorCode);
                }

                var metadata = CoverageMetadataResolver.Resolve(args, _environmentVariables);

                if (args.OptCompleteParallelWork)
                {
                    var repoToken = ResolveRepoToken(args);

                    var pushResult = _coverallsService.PushParallelCompleteWebhook(repoToken, metadata.ServiceBuildNumber);
                    if (!pushResult.Successful)
                    {
                        ExitWithError(pushResult.Error);
                    }

                    return(null);
                }

                var settings = LoadSettings(args);

                var gitData = ResolveGitData(_console, args);

                var app    = new CoverallsPublisher(_console, _fileSystem, _coverallsService);
                var result = app.Run(settings, gitData, metadata);
                if (!result.Successful)
                {
                    ExitWithError(result.Error);
                }

                return(null);
            }
            catch (ExitException ex)
            {
                _console.WriteErrorLine(ex.Message);
                return(1);
            }
        }
예제 #16
0
        public static CoverageMetadata Resolve(MainArgs args, IEnvironmentVariables variables)
        {
            _ = args ?? throw new ArgumentNullException(paramName: nameof(args));

            var resolvers          = CreateResolvers(args, variables);
            var serviceName        = Resolve(resolvers, r => r.ResolveServiceName());
            var serviceJobId       = Resolve(resolvers, r => r.ResolveServiceJobId());
            var serviceBuildNumber = Resolve(resolvers, r => r.ResolveServiceBuildNumber());
            var pullRequestId      = Resolve(resolvers, r => r.ResolvePullRequestId());
            var parallel           = ResolveParallel(args, variables);

            return(new CoverageMetadata(
                       serviceName: serviceName.ValueOr("coveralls.net"),
                       serviceJobId: serviceJobId.ValueOr("0"),
                       serviceBuildNumber: serviceBuildNumber.ValueOrDefault(),
                       pullRequestId: pullRequestId.ValueOrDefault(),
                       parallel: parallel));
        }
예제 #17
0
        private static CoverageMode?GetMode(MainArgs args)
        {
            if (args.OptMonocov)
            {
                return(CoverageMode.MonoCov);
            }

            if (args.OptChutzpah)
            {
                return(CoverageMode.Chutzpah);
            }

            if (args.OptDynamiccodecoverage)
            {
                return(CoverageMode.DynamicCodeCoverage);
            }

            if (args.OptExportcodecoverage)
            {
                return(CoverageMode.ExportCodeCoverage);
            }

            if (args.OptOpencover)
            {
                return(CoverageMode.OpenCover);
            }

            if (args.OptLcov)
            {
                return(CoverageMode.LCov);
            }

            if (args.OptNCover)
            {
                return(CoverageMode.NCover);
            }

            if (args.OptReportGenerator)
            {
                return(CoverageMode.ReportGenerator);
            }

            return(null); // Unreachable
        }
예제 #18
0
        private static Option <GitData> ResolveGitData(IConsole console, MainArgs args)
        {
            var providers = new List <IGitDataResolver>
            {
                new CommandLineGitDataResolver(args),
                new AppVeyorGitDataResolver(new EnvironmentVariables())
            };

            var provider = providers.FirstOrDefault(p => p.CanProvideData());

            if (provider is null)
            {
                console.WriteLine("No git data available");
                return(Option <GitData> .None);
            }

            console.WriteLine($"Using Git Data Provider '{provider.DisplayName}'");
            return(provider.GenerateData());
        }
예제 #19
0
        private static List <CoverageSource> ParseCoverageSources(MainArgs args)
        {
            var optInput = args.OptInput ?? throw new ArgumentException(
                                     message: "Parsing Sources mode requires input",
                                     paramName: nameof(args));

            List <CoverageSource> results = new List <CoverageSource>();

            if (args.OptMultiple)
            {
                var modes = optInput.Split(';');
                foreach (var modekeyvalue in modes)
                {
                    var split   = modekeyvalue.Split('=');
                    var rawMode = split[0];
                    var input   = split[1];
                    var mode    = GetMode(rawMode);
                    if (!mode.HasValue)
                    {
                        ExitWithError("Unknown mode provided");
                    }
                    else
                    {
                        results.Add(new CoverageSource(mode.Value, input));
                    }
                }
            }
            else
            {
                var mode = GetMode(args);
                if (!mode.HasValue)
                {
                    ExitWithError("Unknown mode provided");
                }
                else
                {
                    results.Add(new CoverageSource(mode.Value, optInput));
                }
            }

            return(results);
        }
예제 #20
0
        public static void Main(string[] argv)
        {
            var args      = new MainArgs(argv, exit: true, version: Assembly.GetEntryAssembly().GetName().Version);
            var repoToken = args.OptRepotoken;

            if (string.IsNullOrWhiteSpace(repoToken))
            {
                Console.Error.WriteLine("parameter repoToken is required.");
                Console.WriteLine(MainArgs.Usage);
                Environment.Exit(1);
            }

            var outputFile = args.IsProvided("--output") ? args.OptOutput : string.Empty;

            if (!string.IsNullOrWhiteSpace(outputFile) && File.Exists(outputFile))
            {
                Console.WriteLine("output file '{0}' already exists and will be overwritten.", outputFile);
            }

            var pathProcessor = new PathProcessor(args.IsProvided("--basePath") ? args.OptBasepath : null);

            List <CoverageFile> files;

            if (args.IsProvided("--monocov") && args.OptMonocov)
            {
                var fileName = args.OptInput;
                if (!Directory.Exists(fileName))
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }
                Dictionary <string, XDocument> documents = new DirectoryInfo(fileName).GetFiles().Where(f => f.Name.EndsWith(".xml")).ToDictionary(f => f.Name, f => XDocument.Load(f.FullName));

                files = new MonoCoverParser(pathProcessor).GenerateSourceFiles(documents, args.OptUserelativepaths);
            }
            else if (args.IsProvided("--dynamiccodecoverage") && args.OptDynamiccodecoverage)
            {
                var fileName = args.OptInput;
                if (!File.Exists(fileName))
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }

                var document = XDocument.Load(fileName);

                files = new DynamicCodeCoverageParser(new FileSystem(), pathProcessor).GenerateSourceFiles(document, args.OptUserelativepaths);
            }
            else if (args.IsProvided("--mprof") && args.OptMprof)
            {
                var fileName = args.OptInput;
                files = null;

                if (File.Exists(fileName))
                {
                    var document = XDocument.Load(fileName);

                    files = new MonoProfParser(new FileSystem(), pathProcessor).GenerateSourceFiles(document, args.OptUserelativepaths);
                }
                else if (Directory.Exists(fileName))
                {
                    Dictionary <string, XDocument> documents = new DirectoryInfo(fileName).GetFiles().Where(f => f.Name.EndsWith(".xml")).ToDictionary(f => f.Name, f => XDocument.Load(f.FullName));

                    files = new MonoProfParser(new FileSystem(), pathProcessor).GenerateSourceFiles(documents, args.OptUserelativepaths);
                }
                else
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }
            }
            else
            {
                var fileName = args.OptInput;
                if (!File.Exists(fileName))
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }

                var document = XDocument.Load(fileName);

                files = new OpenCoverParser(new FileSystem(), pathProcessor).GenerateSourceFiles(document, args.OptUserelativepaths);
            }

            GitData gitData  = null;
            var     commitId = args.IsProvided("--commitId") ? args.OptCommitid : string.Empty;

            if (commitId.IsNotNullOrWhitespace())
            {
                var committerName = args.OptCommitauthor ?? string.Empty;
                var comitterEmail = args.OptCommitemail ?? string.Empty;
                var commitMessage = args.OptCommitmessage ?? string.Empty;
                gitData = new GitData
                {
                    Head = new GitHead
                    {
                        Id            = commitId,
                        AuthorName    = committerName,
                        AuthorEmail   = comitterEmail,
                        CommitterName = committerName,
                        ComitterEmail = comitterEmail,
                        Message       = commitMessage
                    },
                    Branch = args.OptCommitbranch ?? string.Empty
                };
            }

            var serviceJobId = args.IsProvided("--jobId") ? args.OptJobid : "0";

            string serviceName = args.IsProvided("--serviceName") ? args.OptServicename : "coveralls.net";
            var    data        = new CoverallData
            {
                RepoToken    = repoToken,
                ServiceJobId = serviceJobId,
                ServiceName  = serviceName,
                SourceFiles  = files.ToArray(),
                Git          = gitData
            };

            var fileData = JsonConvert.SerializeObject(data);

            if (!string.IsNullOrWhiteSpace(outputFile))
            {
                WriteFileData(fileData, outputFile);
            }
            if (!args.OptDryrun)
            {
                var uploaded = Upload(@"https://coveralls.io/api/v1/jobs", fileData);
                if (!uploaded)
                {
                    Console.Error.WriteLine("Failed to upload to coveralls");
                    Environment.Exit(1);
                }
            }
        }
예제 #21
0
        public static void Main(string[] argv)
        {
            var args = new MainArgs(argv, exit: true, version: Assembly.GetEntryAssembly().GetName().Version);
            var repoToken = args.OptRepotoken;
            if (string.IsNullOrWhiteSpace(repoToken))
            {
                Console.Error.WriteLine("parameter repoToken is required.");
                Console.WriteLine(MainArgs.Usage);
                Environment.Exit(1);
            }

            var outputFile = args.IsProvided("--output") ? args.OptOutput : string.Empty;
            if (!string.IsNullOrWhiteSpace(outputFile) && File.Exists(outputFile))
            {
                Console.WriteLine("output file '{0}' already exists and will be overwritten.", outputFile);
            }

            var pathProcessor = new PathProcessor(args.IsProvided("--basePath") ? args.OptBasepath : null);

            List<CoverageFile> files;
            if (args.IsProvided("--monocov") && args.OptMonocov)
            {
                var fileName = args.OptInput;
                if (!Directory.Exists(fileName))
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }
                Dictionary<string,XDocument> documents = new DirectoryInfo(fileName).GetFiles().Where(f => f.Name.EndsWith(".xml")).ToDictionary(f=>f.Name, f=>XDocument.Load(f.FullName));

                files = new MonoCoverParser(pathProcessor).GenerateSourceFiles(documents, args.OptUserelativepaths);
            }
            else if (args.IsProvided("--dynamiccodecoverage") && args.OptDynamiccodecoverage)
            {
                var fileName = args.OptInput;
                if (!File.Exists(fileName))
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }

                var document = XDocument.Load(fileName);

                files = new DynamicCodeCoverageParser(new FileSystem(), pathProcessor).GenerateSourceFiles(document, args.OptUserelativepaths);
            }
            else if (args.IsProvided("--mprof") && args.OptMprof)
            {
                var fileName = args.OptInput;
                files = null;

                if (File.Exists(fileName)) 
                {
                    var document = XDocument.Load (fileName);

                    files = new MonoProfParser (new FileSystem (), pathProcessor).GenerateSourceFiles (document, args.OptUserelativepaths);
                }
                else if (Directory.Exists(fileName))
                {
                    Dictionary<string,XDocument> documents = new DirectoryInfo(fileName).GetFiles().Where(f => f.Name.EndsWith(".xml")).ToDictionary(f=>f.Name, f=>XDocument.Load(f.FullName));

					files = new MonoProfParser(new FileSystem(), pathProcessor).GenerateSourceFiles(documents, args.OptUserelativepaths);
                }
                else
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }
            }
            else
            {
                var fileName = args.OptInput;
                if (!File.Exists(fileName))
                {
                    Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                    Environment.Exit(1);
                }

                var document = XDocument.Load(fileName);

                files = new OpenCoverParser(new FileSystem(), pathProcessor).GenerateSourceFiles(document, args.OptUserelativepaths);
            }

            GitData gitData = null;
            var commitId = args.IsProvided("--commitId") ? args.OptCommitid : string.Empty;
            if (commitId.IsNotNullOrWhitespace())
            {
                var committerName = args.OptCommitauthor ?? string.Empty;
                var comitterEmail = args.OptCommitemail ?? string.Empty;
                var commitMessage = args.OptCommitmessage ?? string.Empty;
                gitData = new GitData
                {
                    Head = new GitHead
                    {
                        Id = commitId,
                        AuthorName = committerName,
                        AuthorEmail = comitterEmail,
                        CommitterName = committerName,
                        ComitterEmail = comitterEmail,
                        Message = commitMessage
                    },
                    Branch = args.OptCommitbranch ?? string.Empty
                };
            }

            var serviceJobId = args.IsProvided("--jobId") ? args.OptJobid : "0";

            string serviceName = args.IsProvided("--serviceName") ? args.OptServicename : "coveralls.net";
            var data = new CoverallData
            {
                RepoToken = repoToken,
                ServiceJobId = serviceJobId,
                ServiceName = serviceName,
                SourceFiles = files.ToArray(),
                Git = gitData
            };

            var fileData = JsonConvert.SerializeObject(data);
            if (!string.IsNullOrWhiteSpace(outputFile))
            {
                WriteFileData(fileData, outputFile);
            }
            if (!args.OptDryrun)
            {
                var uploaded = Upload(@"https://coveralls.io/api/v1/jobs", fileData);
                if (!uploaded)
                {
                    Console.Error.WriteLine("Failed to upload to coveralls");
                    Environment.Exit(1);
                }
            }
        }
 public CommandLineGitDataResolver(MainArgs args)
 {
     _args = args;
 }
예제 #23
0
        public static void Main(string[] argv)
        {
            var args      = new MainArgs(argv, exit: true, version: Assembly.GetEntryAssembly().GetName().Version);
            var repoToken = args.OptRepotoken;

            if (string.IsNullOrWhiteSpace(repoToken))
            {
                Console.Error.WriteLine("parameter repoToken is required.");
                Console.WriteLine(MainArgs.Usage);
                Environment.Exit(1);
            }
            var fileName = args.OptInput;

            if (!File.Exists(fileName))
            {
                Console.Error.WriteLine("Input file '" + fileName + "' cannot be found");
                Environment.Exit(1);
            }
            var document = XDocument.Load(fileName);

            List <CoverageFile> files = new OpenCoverParser(new FileSystem()).GenerateSourceFiles(document);

            GitData gitData  = null;
            var     commitId = args.OptCommitid ?? string.Empty;

            if (!string.IsNullOrWhiteSpace(commitId))
            {
                var committerName = args.OptCommitauthor ?? string.Empty;
                var comitterEmail = args.OptCommitemail ?? string.Empty;
                var commitMessage = args.OptCommitmessage ?? string.Empty;
                gitData = new GitData
                {
                    Head = new GitHead
                    {
                        Id            = commitId,
                        AuthorName    = committerName,
                        AuthorEmail   = comitterEmail,
                        CommitterName = committerName,
                        ComitterEmail = comitterEmail,
                        Message       = commitMessage
                    },
                    Branch = args.OptCommitbranch ?? string.Empty
                };
            }

            var serviceJobId = args.OptJobid ?? "0";

            var data = new CoverallData
            {
                RepoToken    = repoToken,
                ServiceJobId = serviceJobId,
                ServiceName  = "coveralls.net",
                SourceFiles  = files.ToArray(),
                Git          = gitData
            };

            var fileData = JsonConvert.SerializeObject(data);
            var uploaded = Upload(@"https://coveralls.io/api/v1/jobs", fileData);

            if (!uploaded)
            {
                Console.Error.WriteLine("Failed to upload to coveralls");
                Environment.Exit(1);
            }
        }
 public CommandLineGitDataResolver(MainArgs args)
 {
     _args = args;
 }
예제 #25
0
        public static void Main(string[] argv)
        {
            var    args = new MainArgs(argv, exit: true, version: (string)GetDisplayVersion());
            string repoToken;

            if (args.IsProvided("--repoToken"))
            {
                repoToken = args.OptRepotoken;
                if (repoToken.IsNullOrWhitespace())
                {
                    ExitWithError("parameter repoToken is required.");
                }
            }
            else
            {
                var variable = args.OptRepotokenvariable;
                if (variable.IsNullOrWhitespace())
                {
                    ExitWithError("parameter repoTokenVariable is required.");
                }

                repoToken = Environment.GetEnvironmentVariable(variable);
                if (repoToken.IsNullOrWhitespace())
                {
                    ExitWithError("No token found in Environment Variable '{0}'.".FormatWith(variable));
                }
            }
            var outputFile = args.IsProvided("--output") ? args.OptOutput : string.Empty;

            if (!string.IsNullOrWhiteSpace(outputFile) && File.Exists(outputFile))
            {
                Console.WriteLine("output file '{0}' already exists and will be overwritten.", outputFile);
            }

            var pathProcessor = new PathProcessor(args.IsProvided("--basePath") ? args.OptBasepath : null);

            List <CoverageFile> files;

            if (args.IsProvided("--monocov") && args.OptMonocov)
            {
                var fileName = args.OptInput;
                if (!Directory.Exists(fileName))
                {
                    ExitWithError("Input file '" + fileName + "' cannot be found");
                }
                Dictionary <string, XDocument> documents =
                    new DirectoryInfo(fileName).GetFiles()
                    .Where(f => f.Name.EndsWith(".xml"))
                    .ToDictionary(f => f.Name, f => XDocument.Load(f.FullName));

                files = new MonoCoverParser(pathProcessor).GenerateSourceFiles(documents, args.OptUserelativepaths);
            }
            else
            {
                List <FileCoverageData> coverageData;
                if (args.IsProvided("--dynamiccodecoverage") && args.OptDynamiccodecoverage)
                {
                    var fileName = args.OptInput;
                    if (!File.Exists(fileName))
                    {
                        ExitWithError("Input file '" + fileName + "' cannot be found");
                    }

                    var document = XDocument.Load(fileName);

                    coverageData = new DynamicCodeCoverageParser().GenerateSourceFiles(document);
                }
                else if (args.IsProvided("--exportcodecoverage") && args.OptExportcodecoverage)
                {
                    var fileName = args.OptInput;
                    if (!File.Exists(fileName))
                    {
                        ExitWithError("Input file '" + fileName + "' cannot be found");
                    }

                    var document = XDocument.Load(fileName);

                    coverageData = new ExportCodeCoverageParser().GenerateSourceFiles(document);
                }
                else
                {
                    var fileName = args.OptInput;
                    if (!File.Exists(fileName))
                    {
                        ExitWithError("Input file '" + fileName + "' cannot be found");
                    }

                    var document = XDocument.Load(fileName);

                    coverageData = new OpenCoverParser().GenerateSourceFiles(document);
                }

                files = coverageData.Select(coverageFileData =>
                {
                    var coverageBuilder = new CoverageFileBuilder(coverageFileData);

                    var path = coverageFileData.FullPath;
                    if (args.OptUserelativepaths)
                    {
                        path = pathProcessor.ConvertPath(coverageFileData.FullPath);
                    }
                    path = pathProcessor.UnixifyPath(path);
                    coverageBuilder.SetPath(path);

                    var readAllText = new FileSystem().TryLoadFile(coverageFileData.FullPath);
                    if (readAllText.HasValue)
                    {
                        coverageBuilder.AddSource((string)readAllText);
                    }

                    var coverageFile = coverageBuilder.CreateFile();
                    return(coverageFile);
                }).ToList();
            }

            var gitData = ResolveGitData(args);

            var serviceJobId  = ResolveServiceJobId(args);
            var pullRequestId = ResolvePullRequestId(args);

            string serviceName = args.IsProvided("--serviceName") ? args.OptServicename : "coveralls.net";
            var    data        = new CoverallData
            {
                RepoToken     = repoToken,
                ServiceJobId  = serviceJobId.ValueOr("0"),
                ServiceName   = serviceName,
                PullRequestId = pullRequestId.ValueOr(null),
                SourceFiles   = files.ToArray(),
                Git           = gitData.ValueOrDefault()
            };

            var fileData = JsonConvert.SerializeObject(data);

            if (!string.IsNullOrWhiteSpace(outputFile))
            {
                WriteFileData(fileData, outputFile);
            }
            if (!args.OptDryrun)
            {
                var uploadResult = new CoverallsService().Upload(fileData);
                if (!uploadResult.Successful)
                {
                    var message = string.Format("Failed to upload to coveralls\n{0}", uploadResult.Error);
                    if (args.OptTreatuploaderrorsaswarnings)
                    {
                        Console.WriteLine(message);
                    }
                    else
                    {
                        ExitWithError(message);
                    }
                }
                else
                {
                    Console.WriteLine("Coverage data uploaded to coveralls.");
                }
            }
        }
예제 #26
0
        private static Option<GitData> ResolveGitData(MainArgs args)
        {
            var providers = new List<IGitDataResolver>
            {
                new CommandLineGitDataResolver(args),
                new AppVeyorGitDataResolver(new EnvironmentVariables())
            };

            return providers.Where(p=>p.CanProvideData()).Select(p=>p.GenerateData()).FirstOrDefault();
        }
예제 #27
0
 private static Option<string> ResolveServiceJobId(MainArgs args)
 {
     if (args.IsProvided("--jobId")) return args.OptJobid;
     var jobId = new EnvironmentVariables().GetEnvironmentVariable("APPVEYOR_JOB_ID");
     if (jobId.IsNotNullOrWhitespace()) return jobId;
     return null;
 }
예제 #28
0
        public static void Main(string[] argv)
        {
            var args = new MainArgs(argv, exit: true, version: (string)GetDisplayVersion());
            string repoToken;
            if (args.IsProvided("--repoToken"))
            {
                repoToken = args.OptRepotoken;
                if (repoToken.IsNullOrWhitespace())
                {
                    ExitWithError("parameter repoToken is required.");
                }
            }
            else
            {
                var variable = args.OptRepotokenvariable;
                if (variable.IsNullOrWhitespace())
                {
                    ExitWithError("parameter repoTokenVariable is required.");
                }

                repoToken = Environment.GetEnvironmentVariable(variable);
                if (repoToken.IsNullOrWhitespace())
                {
                    ExitWithError("No token found in Environment Variable '{0}'.".FormatWith(variable));
                }

            }
            var outputFile = args.IsProvided("--output") ? args.OptOutput : string.Empty;
            if (!string.IsNullOrWhiteSpace(outputFile) && File.Exists(outputFile))
            {
                Console.WriteLine("output file '{0}' already exists and will be overwritten.", outputFile);
            }

            var pathProcessor = new PathProcessor(args.IsProvided("--basePath") ? args.OptBasepath : null);

            List<CoverageFile> files;
            if (args.IsProvided("--monocov") && args.OptMonocov)
            {
                var fileName = args.OptInput;
                if (!Directory.Exists(fileName))
                {
                    ExitWithError("Input file '" + fileName + "' cannot be found");
                }
                Dictionary<string, XDocument> documents =
                    new DirectoryInfo(fileName).GetFiles()
                        .Where(f => f.Name.EndsWith(".xml"))
                        .ToDictionary(f => f.Name, f => XDocument.Load(f.FullName));

                files = new MonoCoverParser(pathProcessor).GenerateSourceFiles(documents, args.OptUserelativepaths);
            }
            else
            {
                List<FileCoverageData> coverageData;
                if (args.IsProvided("--dynamiccodecoverage") && args.OptDynamiccodecoverage)
                {
                    var fileName = args.OptInput;
                    if (!File.Exists(fileName))
                    {
                        ExitWithError("Input file '" + fileName + "' cannot be found");
                    }

                    var document = XDocument.Load(fileName);

                    coverageData = new DynamicCodeCoverageParser().GenerateSourceFiles(document);
                }
                else
                {
                    var fileName = args.OptInput;
                    if (!File.Exists(fileName))
                    {
                        ExitWithError("Input file '" + fileName + "' cannot be found");
                    }

                    var document = XDocument.Load(fileName);

                    coverageData = new OpenCoverParser().GenerateSourceFiles(document);
                }

                files = coverageData.Select(coverageFileData =>
                {
                    var coverageBuilder = new CoverageFileBuilder(coverageFileData);

                    var path = coverageFileData.FullPath;
                    if (args.OptUserelativepaths)
                    {
                        path = pathProcessor.ConvertPath(coverageFileData.FullPath);
                    }
                    path = pathProcessor.UnixifyPath(path);
                    coverageBuilder.SetPath(path);

                    var readAllText = new FileSystem().TryLoadFile(coverageFileData.FullPath);
                    if (readAllText.HasValue)
                    {
                        coverageBuilder.AddSource((string)readAllText);
                    }

                    var coverageFile = coverageBuilder.CreateFile();
                    return coverageFile;
                }).ToList();
            }

            var gitData = ResolveGitData(args);

            var serviceJobId = ResolveServiceJobId(args);

            string serviceName = args.IsProvided("--serviceName") ? args.OptServicename : "coveralls.net";
            var data = new CoverallData
            {
                RepoToken = repoToken,
                ServiceJobId = serviceJobId.ValueOr("0"),
                ServiceName = serviceName,
                SourceFiles = files.ToArray(),
                Git = gitData.ValueOrDefault()
            };

            var fileData = JsonConvert.SerializeObject(data);
            if (!string.IsNullOrWhiteSpace(outputFile))
            {
                WriteFileData(fileData, outputFile);
            }
            if (!args.OptDryrun)
            {
                var uploadResult = new CoverallsService().Upload(fileData);
                if (!uploadResult.Successful)
                {
                    var message = string.Format("Failed to upload to coveralls\n{0}", uploadResult.Error);
                    if (args.OptTreatuploaderrorsaswarnings)
                    {
                        Console.WriteLine(message);
                    }
                    else
                    {
                        ExitWithError(message);
                    }
                }
            }
        }