public Task <ICollection <AttachmentSet> > ProcessAttachmentSetsAsync(XmlElement configurationElement, ICollection <AttachmentSet> attachments, IProgress <int> progressReporter, IMessageLogger logger, CancellationToken cancellationToken) { string finalFileName = configurationElement.FirstChild.InnerText; StringBuilder stringBuilder = new StringBuilder(); string finalFolder = null; foreach (var attachmentSet in attachments) { foreach (var attachment in attachmentSet.Attachments.OrderBy(f => f.Uri.AbsolutePath)) { if (finalFolder is null) { finalFolder = Path.GetDirectoryName(attachment.Uri.AbsolutePath); } stringBuilder.AppendLine(File.ReadAllText(attachment.Uri.AbsolutePath).Trim()); } } File.WriteAllText(Path.Combine(finalFolder, finalFileName), stringBuilder.ToString()); List <AttachmentSet> mergedAttachment = new List <AttachmentSet>(); var mergedAttachmentSet = new AttachmentSet(new Uri("my://sample/datacollector"), "SampleDataCollector"); mergedAttachmentSet.Attachments.Add(UriDataAttachment.CreateFrom(Path.Combine(finalFolder, finalFileName), string.Empty)); mergedAttachment.Add(mergedAttachmentSet); return(Task.FromResult((ICollection <AttachmentSet>) new Collection <AttachmentSet>(mergedAttachment))); }
public async Task <ICollection <AttachmentSet> > ProcessAttachmentSetsAsync(ICollection <AttachmentSet> attachments, IProgress <int> progressReporter, IMessageLogger logger, CancellationToken cancellationToken) { if (attachments != null && attachments.Any()) { var coverageReportFilePaths = new List <string>(); var coverageOtherFilePaths = new List <string>(); foreach (var attachmentSet in attachments) { foreach (var attachment in attachmentSet.Attachments) { if (attachment.Uri.LocalPath.EndsWith(CoverageFileExtension, StringComparison.OrdinalIgnoreCase)) { coverageReportFilePaths.Add(attachment.Uri.LocalPath); } else { coverageOtherFilePaths.Add(attachment.Uri.LocalPath); } } } if (coverageReportFilePaths.Count > 1) { var mergedCoverageReportFilePath = await this.MergeCodeCoverageFilesAsync(coverageReportFilePaths, progressReporter, cancellationToken).ConfigureAwait(false); if (!string.IsNullOrEmpty(mergedCoverageReportFilePath)) { var resultAttachmentSet = new AttachmentSet(CodeCoverageDataCollectorUri, CoverageFriendlyName); resultAttachmentSet.Attachments.Add(UriDataAttachment.CreateFrom(mergedCoverageReportFilePath, CoverageFriendlyName)); foreach (var coverageOtherFilePath in coverageOtherFilePaths) { resultAttachmentSet.Attachments.Add(UriDataAttachment.CreateFrom(coverageOtherFilePath, string.Empty)); } return(new Collection <AttachmentSet> { resultAttachmentSet }); } } return(attachments); } return(new Collection <AttachmentSet>()); }
/// <summary> /// Add a new file transfer (either copy/move) request. /// </summary> /// <param name="fileTransferInfo"> /// The file Transfer Info. /// </param> /// <param name="sendFileCompletedCallback"> /// The send File Completed Callback. /// </param> /// <param name="uri"> /// The uri. /// </param> /// <param name="friendlyName"> /// The friendly Name. /// </param> private void AddNewFileTransfer(FileTransferInformation fileTransferInfo, AsyncCompletedEventHandler sendFileCompletedCallback, Uri uri, string friendlyName) { var context = fileTransferInfo.Context; Debug.Assert( context != null, "DataCollectionManager.AddNewFileTransfer: FileDataHeaderMessage with null context."); var testCaseId = fileTransferInfo.Context.HasTestCase ? fileTransferInfo.Context.TestExecId.Id.ToString() : string.Empty; var directoryPath = Path.Combine( this.SessionOutputDirectory, testCaseId); var localFilePath = Path.Combine(directoryPath, Path.GetFileName(fileTransferInfo.FileName)); var task = Task.Factory.StartNew( () => { Validate(fileTransferInfo, localFilePath); if (this.cancellationTokenSource.Token.IsCancellationRequested) { this.cancellationTokenSource.Token.ThrowIfCancellationRequested(); } try { if (fileTransferInfo.PerformCleanup) { if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Moving file {0} to {1}", fileTransferInfo.FileName, localFilePath); } this.fileHelper.MoveFile(fileTransferInfo.FileName, localFilePath); if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Moved file {0} to {1}", fileTransferInfo.FileName, localFilePath); } } else { if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Copying file {0} to {1}", fileTransferInfo.FileName, localFilePath); } this.fileHelper.CopyFile(fileTransferInfo.FileName, localFilePath); if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Copied file {0} to {1}", fileTransferInfo.FileName, localFilePath); } } } catch (Exception ex) { this.LogError( ex.ToString(), uri, friendlyName, Guid.Parse(testCaseId)); throw; } }, this.cancellationTokenSource.Token); var continuationTask = task.ContinueWith( (t) => { try { if (t.Exception == null) { lock (attachmentTaskLock) { this.AttachmentSets[fileTransferInfo.Context][uri].Attachments.Add(UriDataAttachment.CreateFrom(localFilePath, fileTransferInfo.Description)); } } sendFileCompletedCallback?.Invoke(this, new AsyncCompletedEventArgs(t.Exception, false, fileTransferInfo.UserToken)); } catch (Exception e) { if (EqtTrace.IsErrorEnabled) { EqtTrace.Error( "DataCollectionAttachmentManager.TriggerCallBack: Error occurred while raising the file transfer completed callback for {0}. Error: {1}", localFilePath, e.ToString()); } } }, this.cancellationTokenSource.Token); this.attachmentTasks[fileTransferInfo.Context].Add(continuationTask); }