-
Notifications
You must be signed in to change notification settings - Fork 597
DRAFT: 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
Draft
mitchdenny
wants to merge
3
commits into
main
Choose a base branch
from
mitchdenny/build-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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, 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( | ||
projectFile, | ||
["ProjectReference", "PackageReference", "Compile"], | ||
["OutputPath"], | ||
cancellationToken | ||
); | ||
|
||
var json = msBuildResult.Output?.RootElement.ToString(); | ||
|
||
var jsonBytes = Encoding.UTF8.GetBytes(json!); | ||
var hash = _sha256.ComputeHash(jsonBytes); | ||
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, 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, cancellationToken); | ||
|
||
await File.WriteAllTextAsync(buildFingerprintFile, currentFingerprint, cancellationToken); | ||
|
||
return exitCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.CommandLine; | ||
using System.Diagnostics; | ||
using Aspire.Cli.Backchannel; | ||
using Aspire.Cli.Builds; | ||
using Aspire.Cli.Interaction; | ||
using Aspire.Cli.Projects; | ||
using Aspire.Cli.Utils; | ||
|
@@ -18,17 +19,20 @@ internal sealed class PublishCommand : BaseCommand | |
private readonly IDotNetCliRunner _runner; | ||
private readonly IInteractionService _interactionService; | ||
private readonly IProjectLocator _projectLocator; | ||
private readonly IAppHostBuilder _appHostBuilder; | ||
|
||
public PublishCommand(IDotNetCliRunner runner, IInteractionService interactionService, IProjectLocator projectLocator) | ||
public PublishCommand(IDotNetCliRunner runner, IInteractionService interactionService, IProjectLocator projectLocator, IAppHostBuilder appHostBuilder) | ||
: base("publish", "Generates deployment artifacts for an Aspire app host project.") | ||
{ | ||
ArgumentNullException.ThrowIfNull(runner); | ||
ArgumentNullException.ThrowIfNull(interactionService); | ||
ArgumentNullException.ThrowIfNull(projectLocator); | ||
ArgumentNullException.ThrowIfNull(appHostBuilder); | ||
Comment on lines
27
to
+30
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. Ya know, this argument null checking isn't really necessary 😄. We not shipping a public API. |
||
|
||
_runner = runner; | ||
_interactionService = interactionService; | ||
_projectLocator = projectLocator; | ||
_appHostBuilder = appHostBuilder; | ||
|
||
var projectOption = new Option<FileInfo?>("--project"); | ||
projectOption.Description = "The path to the Aspire app host project file."; | ||
|
@@ -43,6 +47,10 @@ public PublishCommand(IDotNetCliRunner runner, IInteractionService interactionSe | |
outputPath.Description = "The output path for the generated artifacts."; | ||
outputPath.DefaultValueFactory = (result) => Path.Combine(Environment.CurrentDirectory); | ||
Options.Add(outputPath); | ||
|
||
var noCacheOption = new Option<bool>("--no-cache", "-nc"); | ||
noCacheOption.Description = "Do not use cached build of the app host."; | ||
Options.Add(noCacheOption); | ||
} | ||
|
||
protected override async Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken) | ||
|
@@ -75,7 +83,8 @@ protected override async Task<int> ExecuteAsync(ParseResult parseResult, Cancell | |
return ExitCodeConstants.FailedToDotnetRunAppHost; | ||
} | ||
|
||
var buildExitCode = await AppHostHelper.BuildAppHostAsync(_runner, _interactionService, effectiveAppHostProjectFile, cancellationToken); | ||
var useCache = !parseResult.GetValue<bool>("--no-cache"); | ||
var buildExitCode = await AppHostHelper.BuildAppHostAsync(_appHostBuilder, useCache, _interactionService, effectiveAppHostProjectFile, cancellationToken); | ||
|
||
if (buildExitCode != 0) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).