Make use of async/await for getting file contents
This commit is contained in:
parent
ab660eaf09
commit
c5094a20a7
1 changed files with 26 additions and 21 deletions
|
@ -47,9 +47,11 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends HTTP requests and receives HTTP responses.
|
/// Sends HTTP requests and receives HTTP responses.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public HttpClient client = new HttpClient;
|
private readonly HttpClient client = new HttpClient();
|
||||||
|
|
||||||
public override bool Execute()
|
public override bool Execute() => ExecuteAsync().GetAwaiter().GetResult();
|
||||||
|
|
||||||
|
private async Task<bool> ExecuteAsync()
|
||||||
{
|
{
|
||||||
string baselineRelativeFileName = PrebuiltBaselineFile.Replace(RootDirectory, "");
|
string baselineRelativeFileName = PrebuiltBaselineFile.Replace(RootDirectory, "");
|
||||||
string gitLogCommand = $"log --first-parent --pretty=format:%H,%f,%ci -- {PrebuiltBaselineFile}";
|
string gitLogCommand = $"log --first-parent --pretty=format:%H,%f,%ci -- {PrebuiltBaselineFile}";
|
||||||
|
@ -57,22 +59,26 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport
|
||||||
DateTime startTime = DateTime.Now;
|
DateTime startTime = DateTime.Now;
|
||||||
Log.LogMessage(MessageImportance.High, "Generating summary usage burndown data...");
|
Log.LogMessage(MessageImportance.High, "Generating summary usage burndown data...");
|
||||||
|
|
||||||
ParallelQuery<string> data = ExecuteGitCommand(RootDirectory, gitLogCommand).AsParallel().Select(commitLine =>
|
|
||||||
{
|
IEnumerable<Task<Commit>> getCommitTasks = ExecuteGitCommand(RootDirectory, gitLogCommand)
|
||||||
var splitLine = commitLine.Split(',');
|
.Select(async commitLine =>
|
||||||
var commit = new Commit()
|
|
||||||
{
|
{
|
||||||
Sha = splitLine[0],
|
var splitLine = commitLine.Split(',');
|
||||||
Title = splitLine[1],
|
var commit = new Commit()
|
||||||
CommitDate = DateTime.Parse(splitLine[2])
|
{
|
||||||
};
|
Sha = splitLine[0],
|
||||||
string fileContents = GetFileContents(baselineRelativeFileName, commit.Sha);
|
Title = splitLine[1],
|
||||||
Usage[] usages = UsageData.Parse(XElement.Parse(fileContents)).Usages.NullAsEmpty().ToArray();
|
CommitDate = DateTime.Parse(splitLine[2])
|
||||||
commit.PackageVersionCount = usages.Count();
|
};
|
||||||
commit.PackageCount = usages.GroupBy(i => i.PackageIdentity.Id).Select(grp => grp.First()).Count();
|
string fileContents = await GetFileContentsAsync(baselineRelativeFileName, commit.Sha);
|
||||||
return commit;
|
Usage[] usages = UsageData.Parse(XElement.Parse(fileContents)).Usages.NullAsEmpty().ToArray();
|
||||||
})
|
commit.PackageVersionCount = usages.Count();
|
||||||
.Select(c => c.ToString());
|
commit.PackageCount = usages.GroupBy(i => i.PackageIdentity.Id).Select(grp => grp.First()).Count();
|
||||||
|
return commit;
|
||||||
|
});
|
||||||
|
|
||||||
|
Commit[] commits = await System.Threading.Tasks.Task.WhenAll(getCommitTasks);
|
||||||
|
IEnumerable<string> data = commits.Select(c => c.ToString());
|
||||||
|
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(OutputFilePath));
|
Directory.CreateDirectory(Path.GetDirectoryName(OutputFilePath));
|
||||||
|
|
||||||
|
@ -91,10 +97,9 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport
|
||||||
/// <param name="relativeFilePath">The relative path (from the git root) to the file.</param>
|
/// <param name="relativeFilePath">The relative path (from the git root) to the file.</param>
|
||||||
/// <param name="commitSha">The commit sha for the version of the file to get.</param>
|
/// <param name="commitSha">The commit sha for the version of the file to get.</param>
|
||||||
/// <returns>The contents of the specified file.</returns>
|
/// <returns>The contents of the specified file.</returns>
|
||||||
private string GetFileContents(string relativeFilePath, string commitSha)
|
private async Task<string> GetFileContentsAsync(string relativeFilePath, string commitSha)
|
||||||
{
|
{
|
||||||
string xmlString = client.GetStringAsync($"https://raw.githubusercontent.com/dotnet/source-build/{commitSha}/{relativeFilePath.Replace('\\', '/')}").Result;
|
return await client.GetStringAsync($"https://raw.githubusercontent.com/dotnet/source-build/{commitSha}/{relativeFilePath.Replace('\\', '/')}");
|
||||||
return xmlString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -132,4 +137,4 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue