Ver Fonte

Merge pull request #1608 from Gillibald/feature/IAssetLoader-GetAssets

AssetLoader GetAssets
Jeremy Koritzinsky há 7 anos atrás
pai
commit
bf045bbea8

+ 18 - 10
src/Avalonia.Base/Platform/IAssetLoader.cs

@@ -2,6 +2,7 @@
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
 using System;
+using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
 
@@ -18,8 +19,8 @@ namespace Avalonia.Platform
         /// AssetLoader needs a refactor cause right now it lives in 3+ platforms which 
         /// can all be loaded on Windows. 
         /// </summary>
-        /// <param name="asm"></param>
-        void SetDefaultAssembly(Assembly asm);
+        /// <param name="assembly"></param>
+        void SetDefaultAssembly(Assembly assembly);
 
         /// <summary>
         /// Checks if an asset with the specified URI exists.
@@ -32,32 +33,39 @@ namespace Avalonia.Platform
         bool Exists(Uri uri, Uri baseUri = null);
 
         /// <summary>
-        /// Opens the resource with the requested URI.
+        /// Opens the asset with the requested URI.
         /// </summary>
         /// <param name="uri">The URI.</param>
         /// <param name="baseUri">
         /// A base URI to use if <paramref name="uri"/> is relative.
         /// </param>
-        /// <returns>A stream containing the resource contents.</returns>
+        /// <returns>A stream containing the asset contents.</returns>
         /// <exception cref="FileNotFoundException">
-        /// The resource was not found.
+        /// The asset could not be found.
         /// </exception>
         Stream Open(Uri uri, Uri baseUri = null);
 
         /// <summary>
-        /// Opens the resource with the requested URI and returns the resource string and the
-        /// assembly containing the resource.
+        /// Opens the asset with the requested URI and returns the asset stream and the
+        /// assembly containing the asset.
         /// </summary>
         /// <param name="uri">The URI.</param>
         /// <param name="baseUri">
         /// A base URI to use if <paramref name="uri"/> is relative.
         /// </param>
         /// <returns>
-        /// The stream containing the resource contents together with the assembly.
+        /// The stream containing the asset contents together with the assembly.
         /// </returns>
         /// <exception cref="FileNotFoundException">
-        /// The resource was not found.
+        /// The asset could not be found.
         /// </exception>
-        Tuple<Stream, Assembly> OpenAndGetAssembly(Uri uri, Uri baseUri = null);
+        (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null);
+
+        /// <summary>
+        /// Gets all assets of a folder and subfolders that match specified uri.
+        /// </summary>
+        /// <param name="uri">The URI.</param>
+        /// <returns>All matching assets as a tuple of the absolute path to the asset and the assembly containing the asset</returns>
+        IEnumerable<(string absolutePath, Assembly assembly)> GetAssets(Uri uri);
     }
 }

+ 24 - 10
src/Shared/PlatformSupport/AssetLoader.cs

@@ -57,21 +57,21 @@ namespace Avalonia.Shared.PlatformSupport
         }
 
         /// <summary>
-        /// Opens the resource with the requested URI.
+        /// Opens the asset with the requested URI.
         /// </summary>
         /// <param name="uri">The URI.</param>
         /// <param name="baseUri">
         /// A base URI to use if <paramref name="uri"/> is relative.
         /// </param>
-        /// <returns>A stream containing the resource contents.</returns>
+        /// <returns>A stream containing the asset contents.</returns>
         /// <exception cref="FileNotFoundException">
-        /// The resource was not found.
+        /// The asset could not be found.
         /// </exception>
         public Stream Open(Uri uri, Uri baseUri = null) => OpenAndGetAssembly(uri, baseUri).Item1;
-        
+
         /// <summary>
-        /// Opens the resource with the requested URI and returns the resource string and the
-        /// assembly containing the resource.
+        /// Opens the asset with the requested URI and returns the asset stream and the
+        /// assembly containing the asset.
         /// </summary>
         /// <param name="uri">The URI.</param>
         /// <param name="baseUri">
@@ -81,9 +81,9 @@ namespace Avalonia.Shared.PlatformSupport
         /// The stream containing the resource contents together with the assembly.
         /// </returns>
         /// <exception cref="FileNotFoundException">
-        /// The resource was not found.
+        /// The asset could not be found.
         /// </exception>
-        public Tuple<Stream, Assembly> OpenAndGetAssembly(Uri uri, Uri baseUri = null)
+        public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null)
         {
             var asset = GetAsset(uri, baseUri);
 
@@ -92,7 +92,21 @@ namespace Avalonia.Shared.PlatformSupport
                 throw new FileNotFoundException($"The resource {uri} could not be found.");
             }
 
-            return Tuple.Create(asset.GetStream(), asset.Assembly);
+            return (asset.GetStream(), asset.Assembly);
+        }
+
+        /// <summary>
+        /// Gets all assets of a folder and subfolders that match specified uri.
+        /// </summary>
+        /// <param name="uri">The URI.</param>
+        /// <returns>All matching assets as a tuple of the absolute path to the asset and the assembly containing the asset</returns>
+        public IEnumerable<(string absolutePath, Assembly assembly)> GetAssets(Uri uri)
+        {
+            var assembly = GetAssembly(uri);
+
+            return assembly?.Resources.Where(x => x.Key.Contains(uri.AbsolutePath))
+                       .Select(x => (x.Key, x.Value.Assembly)) ??
+                   Enumerable.Empty<(string AbsolutePath, Assembly Assembly)>();
         }
 
         private IAssetDescriptor GetAsset(Uri uri, Uri baseUri)
@@ -219,4 +233,4 @@ namespace Avalonia.Shared.PlatformSupport
             public string Name { get; }
         }
     }
-}
+}

+ 9 - 4
tests/Avalonia.UnitTests/MockAssetLoader.cs

@@ -4,7 +4,6 @@ using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Text;
-using System.Threading.Tasks;
 using Avalonia.Platform;
 
 namespace Avalonia.UnitTests
@@ -27,10 +26,16 @@ namespace Avalonia.UnitTests
         {
             return new MemoryStream(Encoding.UTF8.GetBytes(_assets[uri]));
         }
-        
-        public Tuple<Stream, Assembly> OpenAndGetAssembly(Uri uri, Uri baseUri = null)
+
+        public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null)
+        {
+            return (Open(uri, baseUri), (Assembly)null);
+        }
+
+        public IEnumerable<(string absolutePath, Assembly assembly)> GetAssets(Uri uri)
         {
-            return Tuple.Create(Open(uri, baseUri), (Assembly)null);
+            return _assets.Keys.Where(x => x.AbsolutePath.Contains(uri.AbsolutePath))
+                .Select(x => (x.AbsolutePath, Assembly.GetEntryAssembly()));
         }
 
         public void SetDefaultAssembly(Assembly asm)