- 
                Notifications
    You must be signed in to change notification settings 
- Fork 715
Refactor AspireStore APIs #7626
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      37173fa
              
                Refactor AspireStore APIs
              
              
                sebastienros 8ec1d1d
              
                Pleasing Copilot
              
              
                sebastienros f38e0b3
              
                Retrieve IAspireStore from DI
              
              
                sebastienros 4c118d9
              
                Rename file
              
              
                sebastienros 34513ae
              
                Ensure dead code in HashDigestStream is not used
              
              
                sebastienros 88321f6
              
                Update src/Aspire.Hosting/Utils/HashDigestStream.cs
              
              
                sebastienros b5c43bb
              
                Update src/Aspire.Hosting/Utils/HashDigestStream.cs
              
              
                sebastienros b13f32f
              
                Feedback
              
              
                sebastienros 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 hidden or 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 hidden or 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 hidden or 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
    
  
  
    
              
        
          
          
            45 changes: 15 additions & 30 deletions
          
          45 
        
  src/Aspire.Hosting/ApplicationModel/AspireStoreExtensions.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -1,49 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|  | ||
| using System.Reflection; | ||
|  | ||
| namespace Aspire.Hosting.ApplicationModel; | ||
|  | ||
| /// <summary> | ||
| /// Provides extension methods for <see cref="IDistributedApplicationBuilder"/> to create an <see cref="IAspireStore"/> instance. | ||
| /// </summary> | ||
| public static class AspireStoreExtensions | ||
| { | ||
| internal const string AspireStorePathKeyName = "Aspire:Store:Path"; | ||
|  | ||
| /// <summary> | ||
| /// Creates a new App Host store using the provided <paramref name="builder"/>. | ||
| /// Gets a deterministic file path that is a copy of the <paramref name="sourceFilename"/>. | ||
| /// The resulting file name will depend on the content of the file. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> | ||
| /// <returns>The <see cref="IAspireStore"/>.</returns> | ||
| public static IAspireStore CreateStore(this IDistributedApplicationBuilder builder) | ||
| /// <param name="aspireStore">The <see cref="IAspireStore"/> instance.</param> | ||
| /// <param name="filenameTemplate">A file name to base the result on.</param> | ||
| /// <param name="sourceFilename">An existing file.</param> | ||
| /// <returns>A deterministic file path with the same content as <paramref name="sourceFilename"/>.</returns> | ||
| /// <exception cref="FileNotFoundException">Thrown when the source file does not exist.</exception> | ||
| public static string GetFileNameWithContent(this IAspireStore aspireStore, string filenameTemplate, string sourceFilename) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
|  | ||
| var aspireDir = builder.Configuration[AspireStorePathKeyName]; | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(filenameTemplate); | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(sourceFilename); | ||
|  | ||
| if (string.IsNullOrWhiteSpace(aspireDir)) | ||
| if (!File.Exists(sourceFilename)) | ||
| { | ||
| var assemblyMetadata = builder.AppHostAssembly?.GetCustomAttributes<AssemblyMetadataAttribute>(); | ||
| aspireDir = GetMetadataValue(assemblyMetadata, "AppHostProjectBaseIntermediateOutputPath"); | ||
|  | ||
| if (string.IsNullOrWhiteSpace(aspireDir)) | ||
| { | ||
| throw new InvalidOperationException($"Could not determine an appropriate location for local storage. Set the {AspireStorePathKeyName} setting to a folder where the App Host content should be stored."); | ||
| } | ||
| throw new FileNotFoundException("The source file does not exist.", sourceFilename); | ||
| } | ||
|  | ||
| return new AspireStore(Path.Combine(aspireDir, ".aspire")); | ||
| } | ||
|  | ||
| /// <summary> | ||
| /// Gets the metadata value for the specified key from the assembly metadata. | ||
| /// </summary> | ||
| /// <param name="assemblyMetadata">The assembly metadata.</param> | ||
| /// <param name="key">The key to look for.</param> | ||
| /// <returns>The metadata value if found; otherwise, null.</returns> | ||
| private static string? GetMetadataValue(IEnumerable<AssemblyMetadataAttribute>? assemblyMetadata, string key) => | ||
| assemblyMetadata?.FirstOrDefault(a => string.Equals(a.Key, key, StringComparison.OrdinalIgnoreCase))?.Value; | ||
| using var sourceStream = File.OpenRead(sourceFilename); | ||
|  | ||
| return aspireStore.GetFileNameWithContent(filenameTemplate, sourceStream); | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,53 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|  | ||
| using System.IO.Hashing; | ||
|  | ||
| namespace Aspire.Hosting.Utils; | ||
|  | ||
| /// <summary> | ||
| /// A stream capable of computing the hash digest of raw data while also copying it. | ||
| /// </summary> | ||
| internal sealed class HashDigestStream : Stream | ||
| { | ||
| private readonly Stream _writeStream; | ||
| private readonly NonCryptographicHashAlgorithm _hashAlgorithm; | ||
|  | ||
| public HashDigestStream(Stream writeStream, NonCryptographicHashAlgorithm hashAlgorithm) | ||
| { | ||
| _writeStream = writeStream; | ||
| _hashAlgorithm = hashAlgorithm; | ||
| } | ||
|  | ||
| public override bool CanWrite => true; | ||
|  | ||
| public override void Write(byte[] buffer, int offset, int count) | ||
| { | ||
| _hashAlgorithm.Append(buffer.AsSpan(offset, count)); | ||
| _writeStream.Write(buffer, offset, count); | ||
| } | ||
|  | ||
| public override void Flush() | ||
| { | ||
| _writeStream.Flush(); | ||
| } | ||
|  | ||
| // This should not be used by Stream.CopyTo(Stream) | ||
| public override void Write(ReadOnlySpan<byte> buffer) | ||
| => throw new NotImplementedException(); | ||
|  | ||
| // This class is never used with async writes, but if it ever is, implement these overrides | ||
| public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
| => throw new NotImplementedException(); | ||
| public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken) | ||
| => throw new NotImplementedException(); | ||
|  | ||
| public override bool CanRead => false; | ||
| public override bool CanSeek => false; | ||
| public override long Length => throw new NotImplementedException(); | ||
| public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|  | ||
| public override int Read(byte[] buffer, int offset, int count) => throw new NotImplementedException(); | ||
| public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException(); | ||
| public override void SetLength(long value) => throw new NotImplementedException(); | ||
| } | 
  
    
      This file contains hidden or 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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.