From 4fdc01a6ca201d2cd7c2e20bc9a452a2d673ae2b Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 13 May 2019 21:45:22 +0300 Subject: [PATCH] Always download the latest template. --- .../Volo/Abp/Cli/CliPaths.cs | 2 +- .../Volo/Abp/Cli/CliService.cs | 12 +- .../Cli/ProjectBuilding/AbpIoTemplateStore.cs | 117 ++++++++++++++---- .../Building/ProjectBuildContext.cs | 10 +- .../Steps/NugetReferenceReplaceStep.cs | 2 +- .../Abp/Cli/ProjectBuilding/ITemplateStore.cs | 5 +- .../Abp/Cli/ProjectBuilding/ProjectBuilder.cs | 6 +- .../Abp/Cli/ProjectBuilding/TemplateFile.cs | 5 +- .../Volo/Abp/Cli/Utils/VersionHelper.cs | 15 --- 9 files changed, 113 insertions(+), 61 deletions(-) delete mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/VersionHelper.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs index 9c3f5a458c..eb6339b9da 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs @@ -12,4 +12,4 @@ namespace Volo.Abp.Cli private static readonly string AbpRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".abp"); } -} +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs index 40629a17c7..abcd641d07 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs @@ -1,11 +1,11 @@ using System; +using System.Reflection; using Microsoft.Extensions.DependencyInjection; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Commands; -using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli @@ -32,7 +32,7 @@ namespace Volo.Abp.Cli public async Task RunAsync(string[] args) { - Logger.LogInformation($"ABP CLI, version {VersionHelper.Version}."); + Logger.LogInformation($"ABP CLI, version {GetCliVersion()}."); Logger.LogInformation("https://abp.io"); var commandLineArgs = CommandLineArgumentParser.Parse(args); @@ -56,5 +56,13 @@ namespace Volo.Abp.Cli } } } + + private static string GetCliVersion() + { + return typeof(CliService) + .Assembly + .GetFileVersion() + .RemovePostFix(".0"); + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs index 74683197d5..adfa4e044a 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs @@ -37,47 +37,80 @@ namespace Volo.Abp.Cli.ProjectBuilding public async Task GetAsync( string name, - string version, DatabaseProvider databaseProvider, - string projectName) + string projectName, + string version = null) { - var localCacheFolder = Path.Combine(CliPaths.TemplateCache, version); - DirectoryHelper.CreateIfNotExists(localCacheFolder); + if (version == null) + { + version = await GetLatestTemplateVersionAsync(name); + } + + DirectoryHelper.CreateIfNotExists(CliPaths.TemplateCache); - var localCacheFile = Path.Combine(localCacheFolder, name + ".zip"); + var localCacheFile = Path.Combine(CliPaths.TemplateCache, name + "-" + version + ".zip"); if (File.Exists(localCacheFile)) { Logger.LogInformation("Using cached template: " + name + ", version: " + version); - return new TemplateFile(File.ReadAllBytes(localCacheFile)); + return new TemplateFile(File.ReadAllBytes(localCacheFile), version); } Logger.LogInformation("Downloading template: " + name + ", version: " + version); - using (var client = new System.Net.Http.HttpClient()) + var fileContent = await DownloadTemplateFileContentAsync( + new TemplateDownloadInputDto + { + Name = name, + Version = version, + DatabaseProvider = databaseProvider.ToString(), + ProjectName = projectName + } + ); + + File.WriteAllBytes(localCacheFile, fileContent); + + return new TemplateFile(fileContent, version); + } + + private async Task GetLatestTemplateVersionAsync(string name) + { + var postData = JsonSerializer.Serialize(new GetLatestTemplateVersionDto { Name = name }); + + using (var client = new HttpClient()) { - client.Timeout = TimeSpan.FromMinutes(3); + client.Timeout = TimeSpan.FromSeconds(30); - if (File.Exists(CliPaths.AccessToken)) + AddAuthentication(client); + + var responseMessage = await client.PostAsync( + Options.AbpIoWwwUrlRoot + "api/download/template/get-version/", + new StringContent(postData, Encoding.UTF8, MimeTypes.Application.Json), + CancellationTokenProvider.Token + ); + + if (!responseMessage.IsSuccessStatusCode) { - var accessToken = File.ReadAllText(CliPaths.AccessToken, Encoding.UTF8); - if (!accessToken.IsNullOrEmpty()) - { - client.SetBearerToken(accessToken); - } + throw new Exception("Remote server returns error! HTTP status code: " + responseMessage.StatusCode); } - var serializedPostDataAsString = JsonSerializer.Serialize(new - { - name = name, - version = version, - databaseProvider = databaseProvider, - projectName = projectName - }); + var result = await responseMessage.Content.ReadAsStringAsync(); + return JsonSerializer.Deserialize(result).Version; + } + } + + private async Task DownloadTemplateFileContentAsync(TemplateDownloadInputDto input) + { + var postData = JsonSerializer.Serialize(input); + + using (var client = new HttpClient()) + { + client.Timeout = TimeSpan.FromMinutes(3); + + AddAuthentication(client); - var downloadUrl = Options.AbpIoWwwUrlRoot + "api/download/template/"; var responseMessage = await client.PostAsync( - downloadUrl, - new StringContent(serializedPostDataAsString, Encoding.UTF8, MimeTypes.Application.Json), + Options.AbpIoWwwUrlRoot + "api/download/template/", + new StringContent(postData, Encoding.UTF8, MimeTypes.Application.Json), CancellationTokenProvider.Token ); @@ -86,11 +119,41 @@ namespace Volo.Abp.Cli.ProjectBuilding throw new Exception("Remote server returns error! HTTP status code: " + responseMessage.StatusCode); } - var fileContent = await responseMessage.Content.ReadAsByteArrayAsync(); + return await responseMessage.Content.ReadAsByteArrayAsync(); + } + } - File.WriteAllBytes(localCacheFile, fileContent); - return new TemplateFile(fileContent); + private static void AddAuthentication(HttpClient client) + { + if (File.Exists(CliPaths.AccessToken)) + { + var accessToken = File.ReadAllText(CliPaths.AccessToken, Encoding.UTF8); + if (!accessToken.IsNullOrEmpty()) + { + client.SetBearerToken(accessToken); + } } } + + public class TemplateDownloadInputDto + { + public string Name { get; set; } + + public string Version { get; set; } + + public string DatabaseProvider { get; set; } + + public string ProjectName { get; set; } + } + + public class GetLatestTemplateVersionDto + { + public string Name { get; set; } + } + + public class GetLatestTemplateVersionResultDto + { + public string Version { get; set; } + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ProjectBuildContext.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ProjectBuildContext.cs index 256d9ec003..1ea040022f 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ProjectBuildContext.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ProjectBuildContext.cs @@ -8,9 +8,6 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building [NotNull] public TemplateFile TemplateFile { get; } - [NotNull] - public string Version { get; } - [NotNull] public ProjectBuildArgs BuildArgs { get; } @@ -21,15 +18,14 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building public ProjectResult Result { get; set; } - public ProjectBuildContext([NotNull] TemplateInfo template, + public ProjectBuildContext( + [NotNull] TemplateInfo template, [NotNull] TemplateFile templateFile, - [NotNull] ProjectBuildArgs buildArgs, - [NotNull] string version) + [NotNull] ProjectBuildArgs buildArgs) { Template = Check.NotNull(template, nameof(template)); TemplateFile = Check.NotNull(templateFile, nameof(templateFile)); BuildArgs = Check.NotNull(buildArgs, nameof(buildArgs)); - Version = Check.NotNullOrWhiteSpace(version, nameof(version)); Result = new ProjectResult(); } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs index 7c2c80a48c..49ccb308c8 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs @@ -14,7 +14,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps context.Files, "MyCompanyName", "MyProjectName", - context.Version + context.TemplateFile.Version ).Run(); } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ITemplateStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ITemplateStore.cs index d8206b9200..064c4eb81b 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ITemplateStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ITemplateStore.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using JetBrains.Annotations; using Volo.Abp.Cli.ProjectBuilding.Building; namespace Volo.Abp.Cli.ProjectBuilding @@ -7,9 +8,9 @@ namespace Volo.Abp.Cli.ProjectBuilding { Task GetAsync( string name, - string version, DatabaseProvider databaseProvider, - string projectName + string projectName, + [CanBeNull] string version = null ); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuilder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuilder.cs index 0d8d96c9d1..0b66ea375d 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuilder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/ProjectBuilder.cs @@ -31,11 +31,8 @@ namespace Volo.Abp.Cli.ProjectBuilding } } - var version = VersionHelper.Version; - var templateFile = await TemplateStore.GetAsync( args.TemplateName, - version, args.DatabaseProvider, args.SolutionName.FullName ); @@ -43,8 +40,7 @@ namespace Volo.Abp.Cli.ProjectBuilding var context = new ProjectBuildContext( templateInfo, templateFile, - args, - version + args ); ProjectBuildPipelineBuilder.Build(context).Execute(context); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateFile.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateFile.cs index 1abe65044e..234a7b509a 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateFile.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateFile.cs @@ -2,11 +2,14 @@ { public class TemplateFile { + public string Version { get; } + public byte[] FileBytes { get; } - public TemplateFile(byte[] fileBytes) + public TemplateFile(byte[] fileBytes, string version) { FileBytes = fileBytes; + Version = version; } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/VersionHelper.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/VersionHelper.cs deleted file mode 100644 index c57c447822..0000000000 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/VersionHelper.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; - -namespace Volo.Abp.Cli.Utils -{ - public static class VersionHelper - { - //public static string Version => typeof(AbpCliCoreModule).Assembly - // .GetFileVersion() - // .RemovePostFix(".0"); - public static string Version => "0.17.0.0"; - } -}