@ -2,9 +2,7 @@ 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.Threading.Tasks ;
@ -46,7 +44,8 @@ namespace Volo.Abp.Cli.ProjectBuilding
public async Task < TemplateFile > GetAsync (
string name ,
string type ,
string version = null )
string version = null ,
string templateSource = null )
{
var latestVersion = await GetLatestSourceCodeVersionAsync ( name , type ) ;
@ -57,8 +56,14 @@ namespace Volo.Abp.Cli.ProjectBuilding
DirectoryHelper . CreateIfNotExists ( CliPaths . TemplateCache ) ;
if ( ! string . IsNullOrWhiteSpace ( templateSource ) & & ! IsNetworkSource ( templateSource ) )
{
Logger . LogInformation ( "Using local " + type + ": " + name + ", version: " + version ) ;
return new TemplateFile ( File . ReadAllBytes ( Path . Combine ( templateSource , name + "-" + version + ".zip" ) ) , version , latestVersion ) ;
}
var localCacheFile = Path . Combine ( CliPaths . TemplateCache , name + "-" + version + ".zip" ) ;
if ( Options . CacheTemplates & & File . Exists ( localCacheFile ) )
if ( Options . CacheTemplates & & File . Exists ( localCacheFile ) & & templateSource . IsNullOrWhiteSpace ( ) )
{
Logger . LogInformation ( "Using cached " + type + ": " + name + ", version: " + version ) ;
return new TemplateFile ( File . ReadAllBytes ( localCacheFile ) , version , latestVersion ) ;
@ -71,11 +76,12 @@ namespace Volo.Abp.Cli.ProjectBuilding
{
Name = name ,
Type = type ,
TemplateSource = templateSource ,
Version = version
}
) ;
if ( Options . CacheTemplates )
if ( Options . CacheTemplates & & templateSource . IsNullOrWhiteSpace ( ) )
{
File . WriteAllBytes ( localCacheFile , fileContent ) ;
}
@ -114,11 +120,20 @@ namespace Volo.Abp.Cli.ProjectBuilding
using ( var client = new CliHttpClient ( TimeSpan . FromMinutes ( 1 0 ) ) )
{
var responseMessage = await client . PostAsync (
$"{CliUrls.WwwAbpIo}api/download/{input.Type}/" ,
new StringContent ( postData , Encoding . UTF8 , MimeTypes . Application . Json ) ,
CancellationTokenProvider . Token
) ;
HttpResponseMessage responseMessage ;
if ( input . TemplateSource . IsNullOrWhiteSpace ( ) )
{
responseMessage = await client . PostAsync (
$"{CliUrls.WwwAbpIo}api/download/{input.Type}/" ,
new StringContent ( postData , Encoding . UTF8 , MimeTypes . Application . Json ) ,
CancellationTokenProvider . Token
) ;
}
else
{
responseMessage = await client . GetAsync ( input . TemplateSource , CancellationTokenProvider . Token ) ;
}
await RemoteServiceExceptionHandler . EnsureSuccessfulHttpResponseAsync ( responseMessage ) ;
@ -126,6 +141,11 @@ namespace Volo.Abp.Cli.ProjectBuilding
}
}
private bool IsNetworkSource ( string source )
{
return source . ToLower ( ) . StartsWith ( "http" ) ;
}
public class SourceCodeDownloadInputDto
{
public string Name { get ; set ; }
@ -133,6 +153,8 @@ namespace Volo.Abp.Cli.ProjectBuilding
public string Version { get ; set ; }
public string Type { get ; set ; }
public string TemplateSource { get ; set ; }
}
public class GetLatestSourceCodeVersionDto