public async Task <IReadOnlyList <(ITemplatePackageInfo PackageInfo, IReadOnlyList <ITemplateInfo> MatchedTemplates)> > SearchForTemplatePackagesAsync( Func <TemplatePackageSearchData, bool> packFilter, Func <TemplatePackageSearchData, IReadOnlyList <ITemplateInfo> > matchingTemplatesFilter, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (_searchCache == null) { _logger.LogDebug("Initializing search cache..."); string metadataLocation = await GetSearchFileAsync(cancellationToken).ConfigureAwait(false); _searchCache = TemplateSearchCache.FromJObject(_environmentSettings.Host.FileSystem.ReadObject(metadataLocation), _logger, _additionalDataReaders); _logger.LogDebug("Search cache was successfully setup."); } IEnumerable <TemplatePackageSearchData> filteredPackages = _searchCache.TemplatePackages.Where(package => packFilter(package)); _logger.LogDebug("Retrieved {0} packages matching package search criteria.", filteredPackages.Count()); List <(ITemplatePackageInfo PackageInfo, IReadOnlyList <ITemplateInfo> MatchedTemplates)> matchingTemplates = filteredPackages .Select <TemplatePackageSearchData, (ITemplatePackageInfo PackageInfo, IReadOnlyList <ITemplateInfo> MatchedTemplates)>(package => (package, matchingTemplatesFilter(package))) .Where(result => result.MatchedTemplates.Any()) .ToList(); _logger.LogDebug("Retrieved {0} packages matching template search criteria.", matchingTemplates.Count); return(matchingTemplates); }
public void CanSkipInvalidEntriesSearchMetadata() { var environmentSettings = _environmentSettingsHelper.CreateEnvironment(virtualize: true); string content = File.ReadAllText("NuGetTemplateSearchInfoWithInvalidData.json"); JObject cache = JObject.Parse(content); var parsedCache = TemplateSearchCache.FromJObject(cache, environmentSettings.Host.Logger); Assert.Equal(1, parsedCache.TemplatePackages.Count); Assert.Equal(1, parsedCache.TemplatePackages.Sum(p => p.Templates.Count)); Assert.IsAssignableFrom <ITemplateInfo>(parsedCache.TemplatePackages[0].Templates[0]); Assert.Equal("Microsoft.AzureFunctions.ProjectTemplate.CSharp.3.x", ((ITemplateInfo)parsedCache.TemplatePackages[0].Templates[0]).Identity); }
public void CanReadSearchMetadata() { var environmentSettings = _environmentSettingsHelper.CreateEnvironment(virtualize: true); string content = File.ReadAllText("NuGetTemplateSearchInfo.json"); JObject cache = JObject.Parse(content); var parsedCache = TemplateSearchCache.FromJObject(cache, environmentSettings.Host.Logger); Assert.Equal(1, parsedCache.TemplatePackages.Count); Assert.Equal(2, parsedCache.TemplatePackages.Sum(p => p.Templates.Count)); Assert.IsAssignableFrom <ITemplateInfo>(parsedCache.TemplatePackages[0].Templates[0]); //can read tags Assert.Equal(2, ((ITemplateInfo)parsedCache.TemplatePackages[0].Templates[0]).TagsCollection.Count); //can read parameters Assert.Equal(5, ((ITemplateInfo)parsedCache.TemplatePackages[0].Templates[0]).Parameters.Count); }
private static TemplateSearchCache?LoadExistingCache(CommandArgs config) { if (!config.DiffMode || config.DiffOverrideSearchCacheLocation == null) { return(null); } FileInfo?cacheFileLocation = config.DiffOverrideSearchCacheLocation; Verbose.WriteLine($"Opening {cacheFileLocation.FullName}"); using (var fileStream = cacheFileLocation.OpenRead()) using (var textReader = new StreamReader(fileStream, System.Text.Encoding.UTF8, true)) using (var jsonReader = new JsonTextReader(textReader)) { return(TemplateSearchCache.FromJObject(JObject.Load(jsonReader), NullLogger.Instance, new Dictionary <string, Func <object, object> >() { { CliHostSearchCacheData.DataName, CliHostSearchCacheData.Reader } })); } }
private static async Task <TemplateSearchCache?> LoadExistingCacheAsync(CommandArgs config, CancellationToken cancellationToken) { Verbose.WriteLine($"Loading existing cache information."); const string uri = "https://dotnet-templating.azureedge.net/search/NuGetTemplateSearchInfoVer2.json"; FileInfo?cacheFileLocation = config.DiffOverrideSearchCacheLocation; if (cacheFileLocation == null) { await DownloadUriToFileAsync(uri, "currentSearchCache.json", cancellationToken).ConfigureAwait(false); cacheFileLocation = new FileInfo("currentSearchCache.json"); } Verbose.WriteLine($"Opening {cacheFileLocation.FullName}"); using (var fileStream = cacheFileLocation.OpenRead()) using (var textReader = new StreamReader(fileStream, System.Text.Encoding.UTF8, true)) using (var jsonReader = new JsonTextReader(textReader)) { return(TemplateSearchCache.FromJObject(JObject.Load(jsonReader), NullLogger.Instance, new Dictionary <string, Func <object, object> >() { { CliHostSearchCacheData.DataName, CliHostSearchCacheData.Reader } })); } }