Implementing "package install" command based on NuGet

--HG--
branch : nuget
This commit is contained in:
Sebastien Ros
2010-11-05 18:17:12 -07:00
parent 08eb862153
commit 70848ef2dd
7 changed files with 72 additions and 22 deletions

View File

@@ -63,10 +63,12 @@ namespace Orchard.Modules.Controllers {
foreach (string fileName in Request.Files) {
var file = Request.Files[fileName];
#if REFACTORING
var info = _packageManager.Install(file.InputStream);
Services.Notifier.Information(T("Installed package \"{0}\", version {1} of type \"{2}\" at location \"{3}\"",
info.ExtensionName, info.ExtensionVersion, info.ExtensionType, info.ExtensionPath));
#endif
}
return RedirectToAction("index");

View File

@@ -10,7 +10,7 @@ namespace Orchard.Packaging.Commands {
[OrchardFeature("Orchard.Packaging")]
public class PackagingCommands : DefaultOrchardCommandHandler {
private static readonly string OrchardWebProj = HostingEnvironment.MapPath("~/Orchard.Web.csproj");
private const string PackagePath = "Packages";
private const string CreatePackagePath = "CreatedPackages";
private readonly IPackageManager _packageManager;
@@ -29,7 +29,7 @@ namespace Orchard.Packaging.Commands {
// append "Orchard.[ExtensionType]" to prevent conflicts with other packages (e.g, TinyMce, jQuery, ...)
var filename = string.Format("Orchard.{0}.{1}.{2}.nupkg", packageData.ExtensionType, packageData.ExtensionName, packageData.ExtensionVersion);
var packagePath = Path.Combine(Directory.GetParent(OrchardWebProj).FullName, PackagePath);
var packagePath = Path.Combine(GetSolutionFolder(), CreatePackagePath);
if(!Directory.Exists(packagePath)) {
Directory.CreateDirectory(packagePath);
@@ -54,10 +54,12 @@ namespace Orchard.Packaging.Commands {
Context.Output.WriteLine(T("File \"{0}\" does not exist.", filename));
}
using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read)) {
var packageInfo = _packageManager.Install(stream);
var packageInfo = _packageManager.Install(filename, GetSolutionFolder());
Context.Output.WriteLine(T("Package \"{0}\" successfully installed at \"{1}\"", packageInfo.ExtensionName, packageInfo.ExtensionPath));
}
private string GetSolutionFolder() {
return Directory.GetParent(OrchardWebProj).Parent.FullName;
}
}
}

View File

@@ -167,9 +167,11 @@ namespace Orchard.Packaging.Controllers {
}
public ActionResult Install(string syndicationId, string cameFrom) {
#if REFACTORING
var packageData = _packageManager.Download(syndicationId);
_packageManager.Install(packageData.PackageStream);
_notifier.Information(T("Installed module"));
#endif
return RedirectToAction(cameFrom == "Themes" ? "ThemesIndex" : "ModulesIndex");
}
}

View File

@@ -10,6 +10,6 @@ namespace Orchard.Packaging.Services {
}
public interface IPackageExpander : IDependency {
PackageInfo ExpandPackage(Stream packageStream);
PackageInfo ExpandPackage(string packageId, string version, string location, string destination);
}
}

View File

@@ -6,6 +6,6 @@ namespace Orchard.Packaging.Services {
PackageData Download(string feedItemId);
void Push(PackageData packageData, string feedUrl, string login, string password);
PackageInfo Install(Stream packageStream);
PackageInfo Install(string filename, string destination);
}
}

View File

@@ -4,10 +4,13 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using NuGet;
using Orchard.Environment.Extensions;
using Orchard.FileSystems.VirtualPath;
using Orchard.Localization;
using NuGetPackageManager = NuGet.PackageManager;
namespace Orchard.Packaging.Services {
[OrchardFeature("PackagingServices")]
public class PackageExpander : IPackageExpander {
@@ -23,10 +26,49 @@ namespace Orchard.Packaging.Services {
public Localizer T { get; set; }
public PackageInfo ExpandPackage(Stream packageStream) {
public PackageInfo ExpandPackage(string packageId, string version, string location, string destination) {
var context = new ExpandContext();
BeginPackage(context, packageStream);
var packagesPath = Path.Combine(destination, "packages");
var projectPath = Path.Combine(destination, "Orchard.Web");
BeginPackage(context, packageId, version, location, packagesPath);
try {
var packageRepository = Uri.IsWellFormedUriString(location, UriKind.Absolute)
? new DataServicePackageRepository(new Uri(location))
: new LocalPackageRepository(location) as IPackageRepository;
var package = packageRepository.FindPackage(packageId, exactVersion: new Version(version));
if(package == null) {
throw new ArgumentException(T("The specified package could not be found: {0}.{1}", packageId, version).Text);
}
context.ExtensionName = package.Title;
context.ExtensionVersion = package.Version.ToString();
context.TargetPath = projectPath;
// packageManager.InstallPackage(package, ignoreDependencies: true);
//var packageManager = new NuGetPackageManager(
// packageRepository,
// new DefaultPackagePathResolver(location),
// new FileBasedProjectSystem(packagesPath)
//);
var projectManager = new ProjectManager(
packageRepository, // source repository for the package to install
new DefaultPackagePathResolver(location),
new FileBasedProjectSystem(projectPath), // the location of the project (where to copy the content files)
new LocalPackageRepository(packagesPath) // the location of the uncompressed package, used to check if the package is already installed
);
// add the package to the project
projectManager.AddPackageReference(packageId, new Version(version));
#if REFACTORING
GetCoreProperties(context);
EstablishPaths(context, _virtualPathProvider);
@@ -40,6 +82,7 @@ namespace Orchard.Packaging.Services {
// this is a simple theme with no csproj
ExtractThemeFiles(context);
}
#endif
}
finally {
EndPackage(context);
@@ -148,14 +191,8 @@ namespace Orchard.Packaging.Services {
return true;
}
private void BeginPackage(ExpandContext context, Stream packageStream) {
if (packageStream.CanSeek) {
context.Stream = packageStream;
}
else {
context.Stream = new MemoryStream();
packageStream.CopyTo(context.Stream);
}
private void BeginPackage(ExpandContext context, string packageId, string version, string location, string destination) {
#if REFACTORING
context.Package = Package.Open(context.Stream, FileMode.Open, FileAccess.Read);
#endif

View File

@@ -5,6 +5,7 @@ using System.Net;
using System.Text;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
using System.Text.RegularExpressions;
namespace Orchard.Packaging.Services {
[OrchardFeature("PackagingServices")]
@@ -82,8 +83,14 @@ namespace Orchard.Packaging.Services {
#endif
}
public PackageInfo Install(Stream packageStream) {
return _packageExpander.ExpandPackage(packageStream);
public PackageInfo Install(string filename, string destination) {
var name = Path.GetFileNameWithoutExtension(filename);
string version = String.Join(".", name.Split('.').Reverse().TakeWhile(part => part.All(Char.IsDigit)).Take(4).Reverse().ToArray());
string packageId = name.Substring(0, name.Length - version.Length -1);
string location = Path.GetDirectoryName(filename);
return _packageExpander.ExpandPackage(packageId, version, location, destination);
}
#endregion