public async Task <(bool Ok, List <string> Value)> GetPackageVersionsAsync(string packageName, bool preRelease = false, CancellationToken cancelationToken = default) { try { if (PackagePreReleaseVersions is null) { var autocompleted = await $"{Uri.Scheme}://{s_apiSearch}.{Uri.Host}/autocomplete?id={packageName}&PreRelease=true" .GetJsonAsync <Classes.AutoComplete>(cancelationToken); PackageVersions = autocompleted.Data.Where(d => !d.Contains("-")).ToList(); PackagePreReleaseVersions = autocompleted.Data.Where(d => d.Contains("-")).ToList(); } if (preRelease) { return(PackagePreReleaseVersions.Count() > 0, PackagePreReleaseVersions); } else { return(PackageVersions.Count() > 0, PackageVersions); } } catch (Exception ex) { ex.LogException(); } return(false, null); }
// Making this method protected virtual for tests. protected virtual async Task <string> GetLatestStableVersionForEraAsync(string packageId, int eraMajorVersion, int eraMinorVersion) { string latestPatchVersion = string.Empty; foreach (var versionEndpoint in NugetPackageVersionsEndpoints) { string allPackageVersionsUrl = string.Concat(versionEndpoint.ItemSpec, packageId, "/index.json"); string versionsJson = string.Empty; using (HttpClient httpClient = new HttpClient()) { using (HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(allPackageVersionsUrl)) { if (httpResponseMessage.StatusCode != HttpStatusCode.OK) { Log.LogError($"Unable to reach the package versions url at {allPackageVersionsUrl}. Recieved status code {httpResponseMessage.StatusCode}."); return(null); } versionsJson = await httpResponseMessage.Content.ReadAsStringAsync(); } } PackageVersions packageVersions = JsonSerializer.Deserialize <PackageVersions>(versionsJson); string latestPatchFromFeed = packageVersions.GetLatestPatchStableVersionForEra(eraMajorVersion, eraMinorVersion, Log); // CompareTo method will return 1 if latestPatchVersion is empty so no need to add check. if (latestPatchFromFeed.CompareTo(latestPatchVersion) > 0) { latestPatchVersion = latestPatchFromFeed; } } return(latestPatchVersion); }
/// <summary> /// This function returns the package version corresponding to the specified package id. /// </summary> /// <param name="context">The <see cref="CodeGenerationContext"/> provided by core scaffolding.</param> /// <param name="id">The package id.</param> /// <returns>The package version if the package id is present in the package versions file, else returns null.</returns> public string GetPackageVersion(CodeGenerationContext context, string id) { if (context == null) { throw new ArgumentNullException("context"); } if (id == null) { throw new ArgumentNullException("id"); } IDictionary <string, string> packageVersions = PackageVersions.GetPackageVersions(context); string value; packageVersions.TryGetValue(id, out value); return(value); }
private async void UpdateVersions(string name) { string[] latest = new[] { Properties.Resources.LatestVersion }; if (string.IsNullOrWhiteSpace(name)) { PackageVersions = latest; } IEnumerable <string> versions = await VSPackage.Manager.Provider.GetVersionsAsync(name) ?? latest; PackageVersions = versions.ToList(); foreach (string version in PackageVersions) { Version v; if (Version.TryParse(version, out v)) { SelectedPackageVersion = version; return; } } SelectedPackageVersion = PackageVersions.FirstOrDefault(); }