public virtual async Task <int> Execute(InstrumentationResult result, string output, string repoToken, string serviceJobId, string serviceName, string commitMessage, string rootFolder, string commit, string commitAuthorName, string commitAuthorEmail, string commitCommitterName, string commitCommitterEmail, string branch, string remoteName, string remoteUrl) { var hits = _hitsReader.TryReadFromDirectory(result.HitsPath); var files = result.GetSourceFiles(); var coverallsJob = new CoverallsJobModel { ServiceJobId = serviceJobId, ServiceName = serviceName, RepoToken = repoToken, CoverallsGitModel = !string.IsNullOrWhiteSpace(branch) ? new CoverallsGitModel { Head = !string.IsNullOrWhiteSpace(commit) ? new CoverallsCommitModel { Id = commit, AuthorName = commitAuthorName, AuthorEmail = commitAuthorEmail, CommitterName = commitCommitterName, CommitterEmail = commitCommitterEmail, Message = commitMessage } : null, Branch = branch, Remotes = !string.IsNullOrWhiteSpace(remoteUrl) ? new List <CoverallsRemote> { new CoverallsRemote { Name = remoteName, Url = remoteUrl } } : null } : null, SourceFiles = new List <CoverallsSourceFileModel>() }; foreach (var file in files) { var sourceFile = Path.Combine(result.SourcePath, file.Path); if (!_fileSystem.File.Exists(sourceFile)) { System.Console.WriteLine($"File not found: {sourceFile}"); continue; } var sourceLines = _fileSystem.File.ReadAllLines(sourceFile); var hitsPerLine = file.Sequences .GroupByMany(f => f.GetLines()) .ToDictionary(g => g.Key, g => g.Sum(i => hits.GetHitCount(i.HitId))); var fileName = PathUtils.GetRelativePath(rootFolder, sourceFile).Replace("\\", "/"); var coverallsSourceFileModel = new CoverallsSourceFileModel { Name = fileName, SourceDigest = ComputeSourceDigest(sourceFile), Coverage = Enumerable.Range(1, sourceLines.Length).Select(line => { return(hitsPerLine.ContainsKey(line) ? hitsPerLine[line] : default(int?)); }).ToArray() }; coverallsJob.SourceFiles.Add(coverallsSourceFileModel); } var coverallsJson = JsonConvert.SerializeObject(coverallsJob, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (!string.IsNullOrWhiteSpace(output)) { _fileSystem.File.WriteAllText(output, coverallsJson); } return(await Post(coverallsJson)); }
public virtual async Task <int> Execute(InstrumentationResult result) { var hits = HitsInfo.TryReadFromDirectory(result.HitsPath); var files = result.GetSourceFiles(); var coverallsJob = new CoverallsJobModel { ServiceJobId = _serviceJobId, ServiceName = _serviceName, RepoToken = _repoToken, CoverallsGitModel = !string.IsNullOrWhiteSpace(_branch) ? new CoverallsGitModel { Head = !string.IsNullOrWhiteSpace(_commit) ? new CoverallsCommitModel { Id = _commit, AuthorName = _commitAuthorName, AuthorEmail = _commitAuthorEmail, CommitterName = _commitCommitterName, CommitterEmail = _commitCommitterEmail, Message = _commitMessage } : null, Branch = _branch, Remotes = !string.IsNullOrWhiteSpace(_remoteUrl) ? new List <CoverallsRemote> { new CoverallsRemote { Name = _remoteName, Url = _remoteUrl } } : null } : null, SourceFiles = new List <CoverallsSourceFileModel>() }; foreach (var kvFile in files) { var sourceFile = Path.Combine(result.SourcePath, kvFile.Key); if (!File.Exists(sourceFile)) { Console.WriteLine($"File not found: {sourceFile}"); continue; } var sourceLines = File.ReadAllLines(sourceFile); var hitsPerLine = kvFile.Value.Instructions .SelectMany(i => i.GetLines(), (instruction, line) => new { instruction, line }) .GroupBy(i => i.line) .ToDictionary(g => g.Key, g => g.Sum(j => hits.GetInstructionHitCount(j.instruction.Id))); var fileName = Path.GetRelativePath(_rootFolder, sourceFile).Replace("\\", "/"); var coverallsSourceFileModel = new CoverallsSourceFileModel { Name = fileName, SourceDigest = ComputeSourceDigest(sourceFile), Coverage = Enumerable.Range(1, sourceLines.Length).Select(line => { return(hitsPerLine.ContainsKey(line) ? hitsPerLine[line] : default(int?)); }).ToArray() }; coverallsJob.SourceFiles.Add(coverallsSourceFileModel); } var coverallsJson = JsonConvert.SerializeObject(coverallsJob, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (!string.IsNullOrWhiteSpace(_output)) { File.WriteAllText(_output, coverallsJson); } return(await Post(coverallsJson)); }