Browse Source

Get latest stable version from a new endpoint

pull/15783/head
Engincan VESKE 4 years ago
parent
commit
5a15994e5d
  1. 5
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  2. 2
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs
  3. 2
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs
  4. 4
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientFactory.cs
  5. 62
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs
  6. 3
      latest-version.json

5
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs

@ -29,6 +29,11 @@ public class AbpCliCoreModule : AbpModule
context.Services.AddHttpClient(CliConsts.HttpClientName) context.Services.AddHttpClient(CliConsts.HttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => new CliHttpClientHandler()); .ConfigurePrimaryHttpMessageHandler(() => new CliHttpClientHandler());
context.Services.AddHttpClient(CliConsts.GithubHttpClientName, client =>
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("MyAgent/1.0");
});
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Configure<AbpCliOptions>(options => Configure<AbpCliOptions>(options =>

2
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs

@ -10,6 +10,8 @@ public static class CliConsts
public const string HttpClientName = "AbpHttpClient"; public const string HttpClientName = "AbpHttpClient";
public const string GithubHttpClientName = "GithubHttpClient";
public const string LogoutUrl = CliUrls.WwwAbpIo + "api/license/logout"; public const string LogoutUrl = CliUrls.WwwAbpIo + "api/license/logout";
public const string LicenseCodePlaceHolder = @"<LICENSE_CODE/>"; public const string LicenseCodePlaceHolder = @"<LICENSE_CODE/>";

2
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs

@ -8,6 +8,8 @@ public static class CliUrls
public const string WwwAbpIo = WwwAbpIoProduction; public const string WwwAbpIo = WwwAbpIoProduction;
public const string AccountAbpIo = AccountAbpIoProduction; public const string AccountAbpIo = AccountAbpIoProduction;
public const string NuGetRootPath = NuGetRootPathProduction; public const string NuGetRootPath = NuGetRootPathProduction;
public const string LatestVersionCheckFullPath =
"https://raw.githubusercontent.com/abpframework/abp/dev/latest-version.json";
public const string WwwAbpIoProduction = "https://abp.io/"; public const string WwwAbpIoProduction = "https://abp.io/";
public const string AccountAbpIoProduction = "https://account.abp.io/"; public const string AccountAbpIoProduction = "https://account.abp.io/";

4
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientFactory.cs

@ -20,9 +20,9 @@ public class CliHttpClientFactory : ISingletonDependency
_cancellationTokenProvider = cancellationTokenProvider; _cancellationTokenProvider = cancellationTokenProvider;
} }
public HttpClient CreateClient(bool needsAuthentication = true, TimeSpan? timeout = null) public HttpClient CreateClient(string clientName = null, bool needsAuthentication = true, TimeSpan? timeout = null)
{ {
var httpClient = _clientFactory.CreateClient(CliConsts.HttpClientName); var httpClient = _clientFactory.CreateClient(clientName ?? CliConsts.HttpClientName);
httpClient.Timeout = timeout ?? DefaultTimeout; httpClient.Timeout = timeout ?? DefaultTimeout;
if (needsAuthentication) if (needsAuthentication)

62
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs

@ -3,6 +3,7 @@ using Newtonsoft.Json;
using NuGet.Versioning; using NuGet.Versioning;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
@ -60,24 +61,26 @@ public class NuGetService : ITransientDependency
public async Task<SemanticVersion> GetLatestVersionOrNullAsync(string packageId, bool includeNightly = false, bool includeReleaseCandidates = false) public async Task<SemanticVersion> GetLatestVersionOrNullAsync(string packageId, bool includeNightly = false, bool includeReleaseCandidates = false)
{ {
var versionList = await GetPackageVersionListAsync(packageId, includeNightly); if (!includeNightly && !includeReleaseCandidates)
{
var version = await GetLatestStableVersionOrNullAsync();
if (string.IsNullOrWhiteSpace(version))
{
return null;
}
return SemanticVersion.TryParse(version, out var semanticVersion) ? semanticVersion : null;
}
var versionList = await GetPackageVersionListAsync(packageId, includeNightly);
if (versionList == null) if (versionList == null)
{ {
return null; return null;
} }
List<SemanticVersion> versions; List<SemanticVersion> versions;
if (!includeNightly && !includeReleaseCandidates) if (!includeNightly && includeReleaseCandidates)
{
versions = versionList
.Select(SemanticVersion.Parse)
.OrderByDescending(v => v, new VersionComparer()).ToList();
versions = versions.Where(x => !x.IsPrerelease).ToList();
}
else if (!includeNightly && includeReleaseCandidates)
{ {
versions = versionList versions = versionList
.Where(v => !v.Contains("-preview")) .Where(v => !v.Contains("-preview"))
@ -148,15 +151,13 @@ public class NuGetService : ITransientDependency
logger: Logger logger: Logger
)) ))
{ {
if (responseMessage.StatusCode == System.Net.HttpStatusCode.NotFound) if (responseMessage.StatusCode == HttpStatusCode.NotFound)
{ {
//the package doesn't exist... //the package doesn't exist...
return null; return null;
} }
else
{ await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(responseMessage);
await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(responseMessage);
}
var responseContent = await responseMessage.Content.ReadAsStringAsync(); var responseContent = await responseMessage.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<NuGetVersionResultDto>(responseContent).Versions; return JsonSerializer.Deserialize<NuGetVersionResultDto>(responseContent).Versions;
@ -183,9 +184,38 @@ public class NuGetService : ITransientDependency
return CliUrls.GetNuGetPackageInfoUrl(_apiKeyResult.ApiKey, packageId); return CliUrls.GetNuGetPackageInfoUrl(_apiKeyResult.ApiKey, packageId);
} }
private async Task<string> GetLatestStableVersionOrNullAsync()
{
try
{
var client = _cliHttpClientFactory.CreateClient(clientName: CliConsts.GithubHttpClientName, needsAuthentication: false);
using (var responseMessage = await client.GetHttpResponseMessageWithRetryAsync(
CliUrls.LatestVersionCheckFullPath,
cancellationToken: CancellationTokenProvider.Token,
logger: Logger
))
{
await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(responseMessage);
var content = await responseMessage.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<LatestVersionResultDto>(content).Version;
}
}
catch
{
return null;
}
}
public class NuGetVersionResultDto public class NuGetVersionResultDto
{ {
[JsonProperty("versions")] [JsonProperty("versions")]
public List<string> Versions { get; set; } public List<string> Versions { get; set; }
} }
public class LatestVersionResultDto
{
public string Version { get; set; }
}
} }

3
latest-version.json

@ -0,0 +1,3 @@
{
"version": "7.0.2"
}
Loading…
Cancel
Save