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