@ -4,6 +4,7 @@ using System.IO;
using System.Linq ;
using System.Net ;
using System.Threading.Tasks ;
using System.Xml ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Logging.Abstractions ;
using Volo.Abp.Cli.Http ;
@ -40,26 +41,36 @@ namespace Volo.Abp.Cli.ProjectModification
Logger = NullLogger < ProjectNugetPackageAdder > . Instance ;
}
public async Task AddAsync ( string projectFile , string packageName )
public async Task AddAsync ( string projectFile , string packageName , string version = null )
{
await AddAsync (
projectFile ,
await FindNugetPackageInfoAsync ( packageName )
await FindNugetPackageInfoAsync ( packageName ) ,
version
) ;
}
public Task AddAsync ( string projectFile , NugetPackageInfo package )
public Task AddAsync ( string projectFile , NugetPackageInfo package , string version = null )
{
if ( File . ReadAllText ( projectFile ) . Contains ( $"\" { package . Name } \ "" ) )
var projectFileContent = File . ReadAllText ( projectFile ) ;
if ( projectFileContent . Contains ( $"\" { package . Name } \ "" ) )
{
return Task . CompletedTask ;
}
if ( version = = null )
{
version = GetAbpVersionOrNull ( projectFileContent ) ;
}
using ( DirectoryHelper . ChangeCurrentDirectory ( Path . GetDirectoryName ( projectFile ) ) )
{
Logger . LogInformation ( $"Installing '{package.Name}' package to the project '{Path.GetFileNameWithoutExtension(projectFile)}'..." ) ;
CmdHelper . Run ( "dotnet" , "add package " + package . Name ) ;
var versionOption = version = = null ? "" : $" -v {version}" ;
CmdHelper . Run ( "dotnet" , $"add package {package.Name}{versionOption}" ) ;
var moduleFiles = ModuleClassFinder . Find ( projectFile , "AbpModule" ) ;
if ( moduleFiles . Count = = 0 )
@ -80,11 +91,22 @@ namespace Volo.Abp.Cli.ProjectModification
return Task . CompletedTask ;
}
protected virtual async Task < NugetPackageInfo > FindNugetPackageInfoAsync ( string moduleName )
private string GetAbpVersionOrNull ( string projectFileContent )
{
var doc = new XmlDocument ( ) { PreserveWhitespace = true } ;
doc . Load ( StreamHelper . GenerateStreamFromString ( projectFileContent ) ) ;
var nodes = doc . SelectNodes ( "/Project/ItemGroup/PackageReference[starts-with(@Include, 'Volo.')]" ) ;
return nodes ? [ 0 ] ? . Attributes ? [ "Version" ] ? . Value ;
}
protected virtual async Task < NugetPackageInfo > FindNugetPackageInfoAsync ( string packageName )
{
using ( var client = new CliHttpClient ( ) )
{
var url = $"{CliUrls.WwwAbpIo}api/app/nugetPackage/byName/?name=" + moduleName ;
var url = $"{CliUrls.WwwAbpIo}api/app/nugetPackage/byName/?name=" + packag eName;
var response = await client . GetAsync ( url ) ;
@ -92,7 +114,7 @@ namespace Volo.Abp.Cli.ProjectModification
{
if ( response . StatusCode = = HttpStatusCode . NotFound )
{
throw new CliUsageException ( $"'{modul eName}' nuget package could not be found!" ) ;
throw new CliUsageException ( $"'{packag eName}' nuget package could not be found!" ) ;
}
await RemoteServiceExceptionHandler . EnsureSuccessfulHttpResponseAsync ( response ) ;
@ -103,4 +125,4 @@ namespace Volo.Abp.Cli.ProjectModification
}
}
}
}
}