diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index bf2bd71fc0..53d23cb508 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -29,6 +29,11 @@ public class AbpCliCoreModule : AbpModule context.Services.AddHttpClient(CliConsts.HttpClientName) .ConfigurePrimaryHttpMessageHandler(() => new CliHttpClientHandler()); + context.Services.AddHttpClient(CliConsts.GithubHttpClientName, client => + { + client.DefaultRequestHeaders.UserAgent.ParseAdd("MyAgent/1.0"); + }); + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Configure(options => diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs index 1c0cd3ace4..93cd5f3f98 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs +++ b/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 GithubHttpClientName = "GithubHttpClient"; + public const string LogoutUrl = CliUrls.WwwAbpIo + "api/license/logout"; public const string LicenseCodePlaceHolder = @""; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs index 4c1351ffba..ae54752746 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs +++ b/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 AccountAbpIo = AccountAbpIoProduction; 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 AccountAbpIoProduction = "https://account.abp.io/"; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientFactory.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientFactory.cs index 1d21dd7be5..32ba3e1d06 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientFactory.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientFactory.cs @@ -20,9 +20,9 @@ public class CliHttpClientFactory : ISingletonDependency _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; if (needsAuthentication) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs index 8800d0f05a..b3c72bea4b 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using NuGet.Versioning; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -60,24 +61,26 @@ public class NuGetService : ITransientDependency public async Task 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) { return null; } List versions; - - 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) + + if (!includeNightly && includeReleaseCandidates) { versions = versionList .Where(v => !v.Contains("-preview")) @@ -148,15 +151,13 @@ public class NuGetService : ITransientDependency logger: Logger )) { - if (responseMessage.StatusCode == System.Net.HttpStatusCode.NotFound) + if (responseMessage.StatusCode == HttpStatusCode.NotFound) { //the package doesn't exist... return null; } - else - { - await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(responseMessage); - } + + await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(responseMessage); var responseContent = await responseMessage.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(responseContent).Versions; @@ -183,9 +184,38 @@ public class NuGetService : ITransientDependency return CliUrls.GetNuGetPackageInfoUrl(_apiKeyResult.ApiKey, packageId); } + private async Task 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(content).Version; + } + } + catch + { + return null; + } + } + public class NuGetVersionResultDto { [JsonProperty("versions")] public List Versions { get; set; } } + + public class LatestVersionResultDto + { + public string Version { get; set; } + } } diff --git a/latest-version.json b/latest-version.json new file mode 100644 index 0000000000..6a970e7e88 --- /dev/null +++ b/latest-version.json @@ -0,0 +1,3 @@ +{ + "version": "7.0.2" +} \ No newline at end of file