-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathAppHostBuilder.cs
87 lines (68 loc) · 3 KB
/
AppHostBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
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;
}
}