public async Task <bool> InitializeAsync() { using (await _mutex.LockAsync()) { try { IEnumerable <JsDelivrPackage> packages; if (IsCachedVersionOld(out packages)) { Directory.CreateDirectory(new FileInfo(_localPath).DirectoryName); using (WebClient client = new WebClient()) { await client.DownloadFileTaskAsync(_remoteApiUrl, _localPath).ConfigureAwait(false); } } if (packages == null) { using (StreamReader reader = new StreamReader(_localPath)) { string json = await reader.ReadToEndAsync().ConfigureAwait(false); packages = JsonConvert.DeserializeObject <IEnumerable <JsDelivrPackage> >(json); } } var comparer = new PackageNameComparer(); _packages = packages.OrderBy(p => p.Name, comparer); return(true); } catch (Exception) { return(false); } } }
public async Task<bool> InitializeAsync() { using (await _mutex.LockAsync()) { try { IEnumerable<JsDelivrPackage> packages; if (IsCachedVersionOld(out packages)) { Directory.CreateDirectory(new FileInfo(_localPath).DirectoryName); using (WebClient client = new WebClient()) { await client.DownloadFileTaskAsync(_remoteApiUrl, _localPath).ConfigureAwait(false); } } if (packages == null) { using (StreamReader reader = new StreamReader(_localPath)) { string json = await reader.ReadToEndAsync().ConfigureAwait(false); packages = JsonConvert.DeserializeObject<IEnumerable<JsDelivrPackage>>(json); } } var comparer = new PackageNameComparer(); _packages = packages.OrderBy(p => p.Name, comparer); return true; } catch (Exception) { return false; } } }
public async Task <bool> InitializeAsync() { using (await _mutex.LockAsync().ConfigureAwait(false)) { try { IEnumerable <CdnjsPackage> packages; if (IsCachedVersionOld(out packages)) { Directory.CreateDirectory(new FileInfo(_localPath).DirectoryName); using (WebClient client = new WebClient()) { await client.DownloadFileTaskAsync(_remoteApiUrl, _localPath).ConfigureAwait(false); } } if (packages == null) { using (StreamReader reader = new StreamReader(_localPath)) { string json = await reader.ReadToEndAsync(); var obj = (JObject)JsonConvert.DeserializeObject(json); packages = JsonConvert.DeserializeObject <IEnumerable <CdnjsPackage> >(obj["results"].ToString()); } } var comparer = new PackageNameComparer(); var distinct = new List <CdnjsPackage>(); // For some reason, the Cdnjs api returns duplicate entries foreach (var package in packages.OrderBy(p => p.Name, comparer)) { if (!distinct.Any(p => p.Name == package.Name)) { distinct.Add(package); } } _packages = distinct; Dictionary <string, Lazy <Task <IPackageInfo> > > infos = new Dictionary <string, Lazy <Task <IPackageInfo> > >(); foreach (CdnjsPackage package in distinct) { CdnjsPackage local = package; infos[local.Name] = new Lazy <Task <IPackageInfo> >(async() => { IPackageMetaData metadata = await local.GetPackageMetaData(Name); return(new PackageInfo(local.Name, metadata.Description) { Homepage = metadata.Homepage }); }); } _searchPackages = infos; return(true); } catch (Exception) { return(false); } } }
public async Task<bool> InitializeAsync() { using (await _mutex.LockAsync().ConfigureAwait(false)) { try { IEnumerable<CdnjsPackage> packages; if (IsCachedVersionOld(out packages)) { Directory.CreateDirectory(new FileInfo(_localPath).DirectoryName); using (WebClient client = new WebClient()) { await client.DownloadFileTaskAsync(_remoteApiUrl, _localPath).ConfigureAwait(false); } } if (packages == null) { using (StreamReader reader = new StreamReader(_localPath)) { string json = await reader.ReadToEndAsync(); var obj = (JObject)JsonConvert.DeserializeObject(json); packages = JsonConvert.DeserializeObject<IEnumerable<CdnjsPackage>>(obj["results"].ToString()); } } var comparer = new PackageNameComparer(); var distinct = new List<CdnjsPackage>(); // For some reason, the Cdnjs api returns duplicate entries foreach (var package in packages.OrderBy(p => p.Name, comparer)) { if (!distinct.Any(p => p.Name == package.Name)) { distinct.Add(package); } } _packages = distinct; return true; } catch (Exception) { return false; } } }
public async Task<bool> InitializeAsync() { using (await _mutex.LockAsync().ConfigureAwait(false)) { try { IEnumerable<CdnjsPackage> packages; if (IsCachedVersionOld(out packages)) { Directory.CreateDirectory(new FileInfo(_localPath).DirectoryName); using (WebClient client = new WebClient()) { await client.DownloadFileTaskAsync(_remoteApiUrl, _localPath).ConfigureAwait(false); } } if (packages == null) { using (StreamReader reader = new StreamReader(_localPath)) { string json = await reader.ReadToEndAsync(); var obj = (JObject)JsonConvert.DeserializeObject(json); packages = JsonConvert.DeserializeObject<IEnumerable<CdnjsPackage>>(obj["results"].ToString()); } } var comparer = new PackageNameComparer(); var distinct = new List<CdnjsPackage>(); // For some reason, the Cdnjs api returns duplicate entries foreach (var package in packages.OrderBy(p => p.Name, comparer)) { if (!distinct.Any(p => p.Name == package.Name)) { distinct.Add(package); } } _packages = distinct; Dictionary<string, Lazy<Task<IPackageInfo>>> infos = new Dictionary<string, Lazy<Task<IPackageInfo>>>(); foreach (CdnjsPackage package in distinct) { CdnjsPackage local = package; infos[local.Name] = new Lazy<Task<IPackageInfo>>(async () => { IPackageMetaData metadata = await local.GetPackageMetaData(Name); return new PackageInfo(local.Name, metadata.Description) { Homepage = metadata.Homepage }; }); } _searchPackages = infos; return true; } catch (Exception) { return false; } } }