Always download the latest template.

pull/1120/head
Halil ibrahim Kalkan 7 years ago
parent 545390034f
commit 4fdc01a6ca

@ -12,4 +12,4 @@ namespace Volo.Abp.Cli
private static readonly string AbpRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".abp");
}
}
}

@ -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");
}
}
}

@ -37,47 +37,80 @@ namespace Volo.Abp.Cli.ProjectBuilding
public async Task<TemplateFile> 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<string> 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<GetLatestTemplateVersionResultDto>(result).Version;
}
}
private async Task<byte[]> 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; }
}
}
}

@ -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();
}

@ -14,7 +14,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps
context.Files,
"MyCompanyName",
"MyProjectName",
context.Version
context.TemplateFile.Version
).Run();
}

@ -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<TemplateFile> GetAsync(
string name,
string version,
DatabaseProvider databaseProvider,
string projectName
string projectName,
[CanBeNull] string version = null
);
}
}

@ -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);

@ -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;
}
}
}

@ -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";
}
}
Loading…
Cancel
Save