-
Notifications
You must be signed in to change notification settings - Fork 609
Basic implementation of build cache. #8754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Aspire.Cli.Builds; | ||
|
||
internal interface IAppHostBuilder | ||
{ | ||
Task<int> BuildAppHostAsync(FileInfo projectFile, bool useCache, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken); | ||
} | ||
|
||
internal sealed class AppHostBuilder(ILogger<AppHostBuilder> logger, IDotNetCliRunner runner) : IAppHostBuilder | ||
{ | ||
private readonly ActivitySource _activitySource = new ActivitySource(nameof(AppHostBuilder)); | ||
private readonly SHA256 _sha256 = SHA256.Create(); | ||
|
||
private async Task<string> GetBuildFingerprintAsync(FileInfo projectFile, CancellationToken cancellationToken) | ||
{ | ||
using var activity = _activitySource.StartActivity(); | ||
|
||
_ = logger; | ||
|
||
var msBuildResult = await runner.GetProjectItemsAndPropertiesAsync( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are environment variables and global properties already ingested at this point? |
||
projectFile, | ||
["ProjectReference", "PackageReference", "Compile"], | ||
["OutputPath"], | ||
new DotNetCliRunnerInvocationOptions(), | ||
cancellationToken | ||
); | ||
|
||
var json = msBuildResult.Output?.RootElement.ToString(); | ||
|
||
var jsonBytes = Encoding.UTF8.GetBytes(json!); | ||
var hash = _sha256.ComputeHash(jsonBytes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, Sha256.HashData is static these days |
||
var hashString = Convert.ToHexString(hash); | ||
|
||
return hashString; | ||
} | ||
|
||
private string GetAppHostStateBasePath(FileInfo projectFile) | ||
{ | ||
var fullPath = projectFile.FullName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowercase this? |
||
var fullPathBytes = Encoding.UTF8.GetBytes(fullPath); | ||
var hash = _sha256.ComputeHash(fullPathBytes); | ||
var hashString = Convert.ToHexString(hash); | ||
|
||
var homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
var appHostStatePath = Path.Combine(homeDirectory, ".aspire", "apphosts", hashString); | ||
|
||
if (Directory.Exists(appHostStatePath)) | ||
{ | ||
return appHostStatePath; | ||
} | ||
else | ||
{ | ||
Directory.CreateDirectory(appHostStatePath); | ||
return appHostStatePath; | ||
} | ||
} | ||
|
||
public async Task<int> BuildAppHostAsync(FileInfo projectFile, bool useCache, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken) | ||
{ | ||
using var activity = _activitySource.StartActivity(); | ||
|
||
var currentFingerprint = await GetBuildFingerprintAsync(projectFile, cancellationToken); | ||
var appHostStatePath = GetAppHostStateBasePath(projectFile); | ||
var buildFingerprintFile = Path.Combine(appHostStatePath, "fingerprint.txt"); | ||
|
||
if (File.Exists(buildFingerprintFile) && useCache) | ||
{ | ||
var lastFingerprint = await File.ReadAllTextAsync(buildFingerprintFile, cancellationToken); | ||
if (lastFingerprint == currentFingerprint) | ||
{ | ||
return 0; | ||
} | ||
} | ||
|
||
var exitCode = await runner.BuildAsync(projectFile, options, cancellationToken); | ||
|
||
await File.WriteAllTextAsync(buildFingerprintFile, currentFingerprint, cancellationToken); | ||
|
||
return exitCode; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,7 +331,7 @@ public async Task<int> NewProjectAsync(string templateName, string name, string | |
internal static string GetBackchannelSocketPath() | ||
{ | ||
var homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
var dotnetCliPath = Path.Combine(homeDirectory, ".dotnet", "aspire", "cli", "backchannels"); | ||
var dotnetCliPath = Path.Combine(homeDirectory, ".aspire", "cli", "backchannels"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏾 |
||
|
||
if (!Directory.Exists(dotnetCliPath)) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we feel about transitivity here? You might need the same finger print for the closure of project refs (that aren't aspire projects).