|
2 | 2 | using Microsoft.Extensions.Logging.Abstractions;
|
3 | 3 | using Microsoft.Extensions.Options;
|
4 | 4 | using System;
|
| 5 | +using System.Collections.Generic; |
5 | 6 | using System.IO;
|
| 7 | +using System.Linq; |
6 | 8 | using System.Net.Http;
|
7 | 9 | using System.Text;
|
| 10 | +using System.Text.RegularExpressions; |
8 | 11 | using System.Threading.Tasks;
|
9 | 12 | using Volo.Abp.Cli.Http;
|
| 13 | +using Volo.Abp.Cli.ProjectBuilding.Templates.App; |
| 14 | +using Volo.Abp.Cli.ProjectBuilding.Templates.MvcModule; |
10 | 15 | using Volo.Abp.DependencyInjection;
|
11 | 16 | using Volo.Abp.Http;
|
12 | 17 | using Volo.Abp.IO;
|
@@ -48,16 +53,33 @@ public async Task<TemplateFile> GetAsync(
|
48 | 53 | string templateSource = null)
|
49 | 54 | {
|
50 | 55 |
|
| 56 | + DirectoryHelper.CreateIfNotExists(CliPaths.TemplateCache); |
| 57 | + |
51 | 58 | var latestVersion = await GetLatestSourceCodeVersionAsync(name, type);
|
52 | 59 | if (version == null)
|
53 | 60 | {
|
| 61 | + if (latestVersion == null) |
| 62 | + { |
| 63 | + Logger.LogWarning("The remote service is currently unavailable, please specify the version."); |
| 64 | + Logger.LogWarning(string.Empty); |
| 65 | + Logger.LogWarning("Find the following template in your cache directory: "); |
| 66 | + Logger.LogWarning("\t Template Name\tVersion"); |
| 67 | + |
| 68 | + var templateList = GetLocalTemplates(); |
| 69 | + foreach (var cacheFile in templateList) |
| 70 | + { |
| 71 | + Logger.LogWarning($"\t {cacheFile.TemplateName}\t\t{cacheFile.Version}"); |
| 72 | + } |
| 73 | + |
| 74 | + Logger.LogWarning(string.Empty); |
| 75 | + throw new CliUsageException("Use command: abp new Acme.BookStore -v version"); |
| 76 | + } |
| 77 | + |
54 | 78 | version = latestVersion;
|
55 | 79 | }
|
56 | 80 |
|
57 | 81 | var nugetVersion = (await GetTemplateNugetVersionAsync(name, type, version)) ?? version;
|
58 |
| - |
59 |
| - DirectoryHelper.CreateIfNotExists(CliPaths.TemplateCache); |
60 |
| - |
| 82 | + |
61 | 83 | if (!string.IsNullOrWhiteSpace(templateSource) && !IsNetworkSource(templateSource))
|
62 | 84 | {
|
63 | 85 | Logger.LogInformation("Using local " + type + ": " + name + ", version: " + version);
|
@@ -122,7 +144,7 @@ private async Task<string> GetLatestSourceCodeVersionAsync(string name, string t
|
122 | 144 | catch (Exception ex)
|
123 | 145 | {
|
124 | 146 | Console.WriteLine("Error occured while getting the latest version from {0} : {1}", url, ex.Message);
|
125 |
| - throw; |
| 147 | + return null; |
126 | 148 | }
|
127 | 149 | }
|
128 | 150 |
|
@@ -195,11 +217,30 @@ private async Task<byte[]> DownloadSourceCodeContentAsync(SourceCodeDownloadInpu
|
195 | 217 | }
|
196 | 218 | }
|
197 | 219 |
|
198 |
| - private static bool IsNetworkSource(string source) |
| 220 | + private bool IsNetworkSource(string source) |
199 | 221 | {
|
200 | 222 | return source.ToLower().StartsWith("http");
|
201 | 223 | }
|
202 | 224 |
|
| 225 | + private List<(string TemplateName, string Version)> GetLocalTemplates() |
| 226 | + { |
| 227 | + var templateList = new List<(string TemplateName, string Version)>(); |
| 228 | + |
| 229 | + var stringBuilder = new StringBuilder(); |
| 230 | + foreach (var cacheFile in Directory.GetFiles(CliPaths.TemplateCache)) |
| 231 | + { |
| 232 | + stringBuilder.AppendLine(cacheFile); |
| 233 | + } |
| 234 | + |
| 235 | + var matches = Regex.Matches(stringBuilder.ToString(),$"({AppTemplate.TemplateName}|{AppProTemplate.TemplateName}|{ModuleTemplate.TemplateName}|{ModuleProTemplate.TemplateName})-(.+).zip"); |
| 236 | + foreach (Match match in matches) |
| 237 | + { |
| 238 | + templateList.Add((match.Groups[1].Value, match.Groups[2].Value)); |
| 239 | + } |
| 240 | + |
| 241 | + return templateList; |
| 242 | + } |
| 243 | + |
203 | 244 | public class SourceCodeDownloadInputDto
|
204 | 245 | {
|
205 | 246 | public string Name { get; set; }
|
|
0 commit comments