2010-07-18 12:36:02 -07:00
using System ;
2010-07-06 18:56:55 -07:00
using System.IO ;
2010-11-05 18:17:12 -07:00
using NuGet ;
2010-07-22 22:09:34 -07:00
using Orchard.Environment.Extensions ;
2010-07-18 14:29:31 -07:00
using Orchard.Localization ;
2010-11-09 16:37:38 -08:00
using Orchard.UI.Notify ;
2010-11-05 18:17:12 -07:00
using NuGetPackageManager = NuGet . PackageManager ;
2010-07-22 22:09:34 -07:00
namespace Orchard.Packaging.Services {
[OrchardFeature("PackagingServices")]
2010-07-06 18:56:55 -07:00
public class PackageExpander : IPackageExpander {
2010-11-09 16:37:38 -08:00
private readonly INotifier _notifier ;
2010-07-18 14:29:31 -07:00
2010-11-09 16:37:38 -08:00
public PackageExpander ( INotifier notifier ) {
_notifier = notifier ;
2010-07-18 14:29:31 -07:00
T = NullLocalizer . Instance ;
2010-07-06 18:56:55 -07:00
}
2010-07-18 14:29:31 -07:00
public Localizer T { get ; set ; }
2010-11-09 15:32:58 -08:00
public PackageInfo ExpandPackage ( string packageId , string version , string location , string solutionFolder ) {
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
var packagesPath = Path . Combine ( solutionFolder , "packages" ) ; // where to download/uncompress packages
var projectPath = Path . Combine ( solutionFolder , "Orchard.Web" ) ; // where to install packages (in the project's folder)
2010-11-09 16:37:38 -08:00
var logger = new NugetLogger ( _notifier ) ;
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
// instantiates the appropriate package repository
2010-11-10 16:09:20 -08:00
var packageRepository = PackageRepositoryFactory . Default . CreateRepository ( new PackageSource ( "Default" , location ) ) ;
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
// gets an IPackage instance from the repository
var packageVersion = String . IsNullOrEmpty ( version ) ? null : new Version ( version ) ;
var package = packageRepository . FindPackage ( packageId , packageVersion ) ;
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
if ( package = = null ) {
throw new ArgumentException ( T ( "The specified package could not be found, id:{0} version:{1}" , packageId , String . IsNullOrEmpty ( version ) ? T ( "No version" ) . Text : version ) . Text ) ;
}
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
var packageManager = new NuGetPackageManager (
packageRepository ,
new DefaultPackagePathResolver ( location ) ,
2010-11-10 18:01:04 -08:00
new PhysicalFileSystem ( packagesPath ) { Logger = logger }
2010-11-09 16:37:38 -08:00
) { Logger = logger } ;
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
// specifically tells to ignore dependencies
packageManager . InstallPackage ( package , ignoreDependencies : true ) ;
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
var projectManager = new ProjectManager (
new LocalPackageRepository ( packagesPath ) , // source repository for the package to install
new DefaultPackagePathResolver ( location ) ,
2010-11-10 16:29:20 -08:00
new FileBasedProjectSystem ( projectPath ) { Logger = logger } // the location of the project (where to copy the content files)
2010-11-09 16:37:38 -08:00
) { Logger = logger } ;
2010-11-05 18:17:12 -07:00
2010-11-09 15:32:58 -08:00
// add the package to the project
projectManager . AddPackageReference ( packageId , packageVersion ) ;
2010-07-06 18:56:55 -07:00
2010-07-18 13:09:45 -07:00
return new PackageInfo {
2010-11-09 16:37:38 -08:00
ExtensionName = package . Title ? ? package . Id ,
2010-11-09 15:32:58 -08:00
ExtensionVersion = package . Version . ToString ( ) ,
ExtensionType = String . Empty , // todo: assign value
ExtensionPath = projectPath
2010-07-18 13:09:45 -07:00
} ;
2010-07-06 18:56:55 -07:00
}
}
}