From 63b764da8d4b46b8182ea63c46bf5d871f4148cc Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 24 Mar 2020 20:52:09 +0800 Subject: [PATCH 1/5] Make CLI available when abp.io is offline --- .../ProjectBuilding/AbpIoSourceCodeStore.cs | 5 ++- .../Analyticses/CliAnalyticsCollect.cs | 41 ++++++++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs index 1ffdefbe61..23d8a8b5ff 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs @@ -51,7 +51,8 @@ namespace Volo.Abp.Cli.ProjectBuilding var latestVersion = await GetLatestSourceCodeVersionAsync(name, type); if (version == null) { - version = latestVersion; + version = latestVersion ?? throw new CliUsageException( + "The remote service is currently unavailable, please specify the version (like abp new Acme.BookStore -v 2.3.0(Make sure you have a template cache locally))!"); } var nugetVersion = (await GetTemplateNugetVersionAsync(name, type, version)) ?? version; @@ -122,7 +123,7 @@ namespace Volo.Abp.Cli.ProjectBuilding catch (Exception ex) { Console.WriteLine("Error occured while getting the latest version from {0} : {1}", url, ex.Message); - throw; + return null; } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs index fdfbb8b5d3..eb5d8686d8 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs @@ -1,4 +1,5 @@ -using System.Net.Http; +using System; +using System.Net.Http; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -32,27 +33,37 @@ namespace Volo.Abp.Cli.ProjectBuilding.Analyticses public async Task CollectAsync(CliAnalyticsCollectInputDto input) { var postData = _jsonSerializer.Serialize(input); - using (var client = new CliHttpClient()) + var url = $"{CliUrls.WwwAbpIo}api/clianalytics/collect"; + + try { - var responseMessage = await client.PostAsync( - $"{CliUrls.WwwAbpIo}api/clianalytics/collect", - new StringContent(postData, Encoding.UTF8, MimeTypes.Application.Json), - _cancellationTokenProvider.Token - ); - - if (!responseMessage.IsSuccessStatusCode) + using (var client = new CliHttpClient()) { - var exceptionMessage = "Remote server returns '" + (int)responseMessage.StatusCode + "-" + responseMessage.ReasonPhrase + "'. "; - var remoteServiceErrorMessage = await _remoteServiceExceptionHandler.GetAbpRemoteServiceErrorAsync(responseMessage); + var responseMessage = await client.PostAsync( + url, + new StringContent(postData, Encoding.UTF8, MimeTypes.Application.Json), + _cancellationTokenProvider.Token + ); - if (remoteServiceErrorMessage != null) + if (!responseMessage.IsSuccessStatusCode) { - exceptionMessage += remoteServiceErrorMessage; - } + var exceptionMessage = "Remote server returns '" + (int)responseMessage.StatusCode + "-" + responseMessage.ReasonPhrase + "'. "; + var remoteServiceErrorMessage = await _remoteServiceExceptionHandler.GetAbpRemoteServiceErrorAsync(responseMessage); + + if (remoteServiceErrorMessage != null) + { + exceptionMessage += remoteServiceErrorMessage; + } - _logger.LogInformation(exceptionMessage); + _logger.LogInformation(exceptionMessage); + } } } + catch (Exception ex) + { + Console.WriteLine("Error occured while cli analytics from {0} : {1}", url, ex.Message); + } + } } } \ No newline at end of file From 1ba052cba54e7ea77e627057d195663a98960db5 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 25 Mar 2020 14:21:47 +0800 Subject: [PATCH 2/5] Output cache template info, ignore CLI analysis exceptions --- .../ProjectBuilding/AbpIoSourceCodeStore.cs | 24 +++++++++++++++---- .../Analyticses/CliAnalyticsCollect.cs | 3 +-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs index 23d8a8b5ff..170bf25dda 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; using System.IO; +using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -48,17 +49,30 @@ namespace Volo.Abp.Cli.ProjectBuilding string templateSource = null) { + DirectoryHelper.CreateIfNotExists(CliPaths.TemplateCache); + var latestVersion = await GetLatestSourceCodeVersionAsync(name, type); if (version == null) { - version = latestVersion ?? throw new CliUsageException( - "The remote service is currently unavailable, please specify the version (like abp new Acme.BookStore -v 2.3.0(Make sure you have a template cache locally))!"); + if (latestVersion == null) + { + Logger.LogWarning("The remote service is currently unavailable, please specify the version."); + Logger.LogWarning(string.Empty); + Logger.LogWarning("Find the following template in your cache directory: "); + + foreach (var cacheFile in Directory.GetFiles(CliPaths.TemplateCache)) + { + Logger.LogWarning($" {cacheFile}"); + } + + Logger.LogWarning(string.Empty); + throw new CliUsageException("Use command: abp new Acme.BookStore -v version"); + } + version = latestVersion; } var nugetVersion = (await GetTemplateNugetVersionAsync(name, type, version)) ?? version; - - DirectoryHelper.CreateIfNotExists(CliPaths.TemplateCache); - + if (!string.IsNullOrWhiteSpace(templateSource) && !IsNetworkSource(templateSource)) { Logger.LogInformation("Using local " + type + ": " + name + ", version: " + version); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs index eb5d8686d8..621631af2a 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs @@ -61,9 +61,8 @@ namespace Volo.Abp.Cli.ProjectBuilding.Analyticses } catch (Exception ex) { - Console.WriteLine("Error occured while cli analytics from {0} : {1}", url, ex.Message); + // ignored } - } } } \ No newline at end of file From 6ce7f3bb863165e12393a391b9a5cf95367e0236 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 25 Mar 2020 16:33:08 +0800 Subject: [PATCH 3/5] Formatted output --- .../ProjectBuilding/AbpIoSourceCodeStore.cs | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs index 170bf25dda..a2fe686692 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs @@ -2,10 +2,12 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Volo.Abp.Cli.Http; using Volo.Abp.DependencyInjection; @@ -60,14 +62,16 @@ namespace Volo.Abp.Cli.ProjectBuilding Logger.LogWarning(string.Empty); Logger.LogWarning("Find the following template in your cache directory: "); - foreach (var cacheFile in Directory.GetFiles(CliPaths.TemplateCache)) + var templateList = GetLocalTemplates(); + foreach (var cacheFile in templateList) { - Logger.LogWarning($" {cacheFile}"); + Logger.LogWarning($" {cacheFile.TemplateName}: {cacheFile.Version}"); } Logger.LogWarning(string.Empty); throw new CliUsageException("Use command: abp new Acme.BookStore -v version"); } + version = latestVersion; } @@ -210,11 +214,30 @@ namespace Volo.Abp.Cli.ProjectBuilding } } - private static bool IsNetworkSource(string source) + private bool IsNetworkSource(string source) { return source.ToLower().StartsWith("http"); } + private List<(string TemplateName, string Version)> GetLocalTemplates() + { + var templateList = new List<(string TemplateName, string Version)>(); + + var stringBuilder = new StringBuilder(); + foreach (var cacheFile in Directory.GetFiles(CliPaths.TemplateCache)) + { + stringBuilder.AppendLine(cacheFile); + } + + var matches = Regex.Matches(stringBuilder.ToString(),"(app|app-pro|module|module-pro)-(.+).zip"); + foreach (Match match in matches) + { + templateList.Add((match.Groups[1].Value, match.Groups[2].Value)); + } + + return templateList; + } + public class SourceCodeDownloadInputDto { public string Name { get; set; } From 7192c224a0cea2103d31fa3fca64ae1171d04633 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 25 Mar 2020 16:53:02 +0800 Subject: [PATCH 4/5] Formatted output --- .../Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs index a2fe686692..2285c3cb86 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs @@ -61,11 +61,12 @@ namespace Volo.Abp.Cli.ProjectBuilding Logger.LogWarning("The remote service is currently unavailable, please specify the version."); Logger.LogWarning(string.Empty); Logger.LogWarning("Find the following template in your cache directory: "); + Logger.LogWarning("\t Template Name\tVersion"); var templateList = GetLocalTemplates(); foreach (var cacheFile in templateList) { - Logger.LogWarning($" {cacheFile.TemplateName}: {cacheFile.Version}"); + Logger.LogWarning($"\t {cacheFile.TemplateName}\t\t{cacheFile.Version}"); } Logger.LogWarning(string.Empty); From 60ce0e96650194191f324bf0bc77a4ab8711cc55 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 25 Mar 2020 20:19:59 +0800 Subject: [PATCH 5/5] Match template regex use constant --- .../Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs index 2285c3cb86..0aa2587e20 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs @@ -10,6 +10,8 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Volo.Abp.Cli.Http; +using Volo.Abp.Cli.ProjectBuilding.Templates.App; +using Volo.Abp.Cli.ProjectBuilding.Templates.MvcModule; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; using Volo.Abp.IO; @@ -230,7 +232,7 @@ namespace Volo.Abp.Cli.ProjectBuilding stringBuilder.AppendLine(cacheFile); } - var matches = Regex.Matches(stringBuilder.ToString(),"(app|app-pro|module|module-pro)-(.+).zip"); + var matches = Regex.Matches(stringBuilder.ToString(),$"({AppTemplate.TemplateName}|{AppProTemplate.TemplateName}|{ModuleTemplate.TemplateName}|{ModuleProTemplate.TemplateName})-(.+).zip"); foreach (Match match in matches) { templateList.Add((match.Groups[1].Value, match.Groups[2].Value));