mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Refactoring PackageManager
--HG-- branch : nuget
This commit is contained in:
@@ -1 +1 @@
|
|||||||
299e6c980fd683c0a9dba451dcd07d36b7db537a external/nuget
|
105bc28c6cf151bac00949bd15a06256a7971693 external/nuget
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
using System;
|
using System.IO;
|
||||||
using System.IO;
|
|
||||||
using System.Web.Hosting;
|
using System.Web.Hosting;
|
||||||
using Orchard.Commands;
|
using Orchard.Commands;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.Packaging.Services;
|
using Orchard.Packaging.Services;
|
||||||
using Orchard.UI.Notify;
|
|
||||||
|
|
||||||
namespace Orchard.Packaging.Commands {
|
namespace Orchard.Packaging.Commands {
|
||||||
[OrchardFeature("Orchard.Packaging")]
|
[OrchardFeature("Orchard.Packaging")]
|
||||||
@@ -18,6 +16,9 @@ namespace Orchard.Packaging.Commands {
|
|||||||
_packageManager = packageManager;
|
_packageManager = packageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[OrchardSwitch]
|
||||||
|
public string Version { get; set; }
|
||||||
|
|
||||||
[CommandHelp("package create <moduleName>\r\n\t" + "Create a package for the module <moduleName>. The default filename is Orchard.<extension>.<moduleName>.<moduleVersion>.nupkg.")]
|
[CommandHelp("package create <moduleName>\r\n\t" + "Create a package for the module <moduleName>. The default filename is Orchard.<extension>.<moduleName>.<moduleVersion>.nupkg.")]
|
||||||
[CommandName("package create")]
|
[CommandName("package create")]
|
||||||
public void CreatePackage(string moduleName) {
|
public void CreatePackage(string moduleName) {
|
||||||
@@ -47,30 +48,23 @@ namespace Orchard.Packaging.Commands {
|
|||||||
Context.Output.WriteLine(T("Package \"{0}\" successfully created", fileInfo.FullName));
|
Context.Output.WriteLine(T("Package \"{0}\" successfully created", fileInfo.FullName));
|
||||||
}
|
}
|
||||||
|
|
||||||
[CommandHelp("package install <filename>\r\n\t" + "Install a module from a package <filename>.")]
|
[CommandHelp("package install <packageId> <location> /Version:<version> \r\n\t" + "Install a module or a theme from a package file.")]
|
||||||
[CommandName("package install")]
|
[CommandName("package install")]
|
||||||
public void InstallPackage(string filename) {
|
[OrchardSwitches("Version")]
|
||||||
if (!File.Exists(filename)) {
|
public void InstallPackage(string packageId, string location) {
|
||||||
Context.Output.WriteLine(T("File \"{0}\" does not exist.", filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
var solutionFolder = GetSolutionFolder();
|
var solutionFolder = GetSolutionFolder();
|
||||||
|
|
||||||
if(solutionFolder == null) {
|
if(solutionFolder == null) {
|
||||||
Context.Output.WriteLine(T("The project's location is not supported"));
|
Context.Output.WriteLine(T("The project's location is not supported"));
|
||||||
}
|
}
|
||||||
|
|
||||||
var packageInfo = _packageManager.Install(filename, solutionFolder);
|
var packageInfo = _packageManager.Install(packageId, Version, location, solutionFolder);
|
||||||
Context.Output.WriteLine(T("Package \"{0}\" successfully installed at \"{1}\"", packageInfo.ExtensionName, packageInfo.ExtensionPath));
|
Context.Output.WriteLine(T("Package \"{0}\" successfully installed at \"{1}\"", packageInfo.ExtensionName, packageInfo.ExtensionPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetSolutionFolder() {
|
private static string GetSolutionFolder() {
|
||||||
var orchardDirectory = Directory.GetParent(OrchardWebProj);
|
var orchardDirectory = Directory.GetParent(OrchardWebProj);
|
||||||
if(orchardDirectory.Parent == null) {
|
return orchardDirectory.Parent == null ? null : orchardDirectory.Parent.FullName;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return orchardDirectory.Parent.FullName;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ namespace Orchard.Packaging.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public interface IPackageExpander : IDependency {
|
public interface IPackageExpander : IDependency {
|
||||||
PackageInfo ExpandPackage(string packageId, string version, string location, string destination);
|
PackageInfo ExpandPackage(string packageId, string version, string location, string solutionFolder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,6 @@ namespace Orchard.Packaging.Services {
|
|||||||
PackageData Download(string feedItemId);
|
PackageData Download(string feedItemId);
|
||||||
|
|
||||||
void Push(PackageData packageData, string feedUrl, string login, string password);
|
void Push(PackageData packageData, string feedUrl, string login, string password);
|
||||||
PackageInfo Install(string filename, string destination);
|
PackageInfo Install(string packageId, string version, string location, string solutionFolder);
|
||||||
PackageInfo Install(Uri uri, string destination);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Xml.Linq;
|
|
||||||
using NuGet;
|
using NuGet;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.FileSystems.VirtualPath;
|
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
|
|
||||||
using NuGetPackageManager = NuGet.PackageManager;
|
using NuGetPackageManager = NuGet.PackageManager;
|
||||||
@@ -14,239 +9,59 @@ using NuGetPackageManager = NuGet.PackageManager;
|
|||||||
namespace Orchard.Packaging.Services {
|
namespace Orchard.Packaging.Services {
|
||||||
[OrchardFeature("PackagingServices")]
|
[OrchardFeature("PackagingServices")]
|
||||||
public class PackageExpander : IPackageExpander {
|
public class PackageExpander : IPackageExpander {
|
||||||
private const string ContentTypePrefix = "Orchard ";
|
|
||||||
private readonly IVirtualPathProvider _virtualPathProvider;
|
|
||||||
|
|
||||||
public PackageExpander(IVirtualPathProvider virtualPathProvider) {
|
|
||||||
_virtualPathProvider = virtualPathProvider;
|
|
||||||
|
|
||||||
|
public PackageExpander() {
|
||||||
T = NullLocalizer.Instance;
|
T = NullLocalizer.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Localizer T { get; set; }
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public PackageInfo ExpandPackage(string packageId, string version, string location, string destination) {
|
public PackageInfo ExpandPackage(string packageId, string version, string location, string solutionFolder) {
|
||||||
var context = new ExpandContext();
|
|
||||||
|
|
||||||
var packagesPath = Path.Combine(destination, "packages");
|
var packagesPath = Path.Combine(solutionFolder, "packages"); // where to download/uncompress packages
|
||||||
var projectPath = Path.Combine(destination, "Orchard.Web");
|
var projectPath = Path.Combine(solutionFolder, "Orchard.Web"); // where to install packages (in the project's folder)
|
||||||
|
|
||||||
BeginPackage(context, packageId, version, location, packagesPath);
|
|
||||||
try {
|
|
||||||
|
|
||||||
|
// instantiates the appropriate package repository
|
||||||
var packageRepository = Uri.IsWellFormedUriString(location, UriKind.Absolute)
|
var packageRepository = Uri.IsWellFormedUriString(location, UriKind.Absolute)
|
||||||
? new DataServicePackageRepository(new Uri(location))
|
? new DataServicePackageRepository(new Uri(location))
|
||||||
: new LocalPackageRepository(location) as IPackageRepository;
|
: new LocalPackageRepository(location) as IPackageRepository;
|
||||||
|
|
||||||
var package = packageRepository.FindPackage(packageId, exactVersion: new Version(version));
|
// gets an IPackage instance from the repository
|
||||||
|
var packageVersion = String.IsNullOrEmpty(version) ? null : new Version(version);
|
||||||
|
var package = packageRepository.FindPackage(packageId, packageVersion);
|
||||||
|
|
||||||
if ( package == null ) {
|
if ( package == null ) {
|
||||||
throw new ArgumentException(T("The specified package could not be found: {0}.{1}", packageId, version).Text);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.ExtensionName = package.Title;
|
var packageManager = new NuGetPackageManager(
|
||||||
context.ExtensionVersion = package.Version.ToString();
|
packageRepository,
|
||||||
context.TargetPath = projectPath;
|
new DefaultPackagePathResolver(location),
|
||||||
|
new FileBasedProjectSystem(packagesPath)
|
||||||
|
);
|
||||||
|
|
||||||
// packageManager.InstallPackage(package, ignoreDependencies: true);
|
// specifically tells to ignore dependencies
|
||||||
|
packageManager.InstallPackage(package, ignoreDependencies: true);
|
||||||
//var packageManager = new NuGetPackageManager(
|
|
||||||
// packageRepository,
|
|
||||||
// new DefaultPackagePathResolver(location),
|
|
||||||
// new FileBasedProjectSystem(packagesPath)
|
|
||||||
//);
|
|
||||||
|
|
||||||
var projectManager = new ProjectManager(
|
var projectManager = new ProjectManager(
|
||||||
packageRepository, // source repository for the package to install
|
new LocalPackageRepository(packagesPath), // source repository for the package to install
|
||||||
new DefaultPackagePathResolver(location),
|
new DefaultPackagePathResolver(location),
|
||||||
new FileBasedProjectSystem(projectPath), // the location of the project (where to copy the content files)
|
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
|
new LocalPackageRepository(packagesPath) // the location of the uncompressed package, used to check if the package is already installed
|
||||||
);
|
);
|
||||||
|
|
||||||
// add the package to the project
|
// add the package to the project
|
||||||
projectManager.AddPackageReference(packageId, new Version(version));
|
projectManager.AddPackageReference(packageId, packageVersion);
|
||||||
|
|
||||||
|
|
||||||
#if REFACTORING
|
|
||||||
GetCoreProperties(context);
|
|
||||||
EstablishPaths(context, _virtualPathProvider);
|
|
||||||
|
|
||||||
string projectFile = context.ExtensionName + ".csproj";
|
|
||||||
if (LoadProject(context, projectFile)) {
|
|
||||||
ExtractFile(context, projectFile);
|
|
||||||
ExtractProjectFiles(context, "Compile", "Content", "None", "EmbeddedResource");
|
|
||||||
ExtractReferenceFiles(context);
|
|
||||||
}
|
|
||||||
else if (context.ExtensionType == "Theme") {
|
|
||||||
// this is a simple theme with no csproj
|
|
||||||
ExtractThemeFiles(context);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
EndPackage(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new PackageInfo {
|
return new PackageInfo {
|
||||||
ExtensionName = context.ExtensionName,
|
ExtensionName = package.Title,
|
||||||
ExtensionVersion = context.ExtensionVersion,
|
ExtensionVersion = package.Version.ToString(),
|
||||||
ExtensionType = context.ExtensionType,
|
ExtensionType = String.Empty, // todo: assign value
|
||||||
ExtensionPath = context.TargetPath
|
ExtensionPath = projectPath
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ExtractFile(ExpandContext context, string relativePath) {
|
|
||||||
#if REFACTORING
|
|
||||||
Uri partUri = PackUriHelper.CreatePartUri(new Uri(context.SourcePath + relativePath, UriKind.Relative));
|
|
||||||
PackagePart packagePart = context.Package.GetPart(partUri);
|
|
||||||
using (Stream packageStream = packagePart.GetStream(FileMode.Open, FileAccess.Read)) {
|
|
||||||
string filePath = _virtualPathProvider.Combine(context.TargetPath, relativePath);
|
|
||||||
string folderPath = _virtualPathProvider.GetDirectoryName(filePath);
|
|
||||||
if (!_virtualPathProvider.DirectoryExists(folderPath)) {
|
|
||||||
_virtualPathProvider.CreateDirectory(folderPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
using (Stream fileStream = _virtualPathProvider.CreateFile(filePath)) {
|
|
||||||
packageStream.CopyTo(fileStream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ExtractProjectFiles(ExpandContext context, params string[] itemGroupTypes) {
|
|
||||||
IEnumerable<XElement> itemGroups = context.Project
|
|
||||||
.Elements(Ns("Project"))
|
|
||||||
.Elements(Ns("ItemGroup"));
|
|
||||||
|
|
||||||
foreach (string itemGroupType in itemGroupTypes) {
|
|
||||||
IEnumerable<string> includePaths = itemGroups
|
|
||||||
.Elements(Ns(itemGroupType))
|
|
||||||
.Attributes("Include")
|
|
||||||
.Select(x => x.Value);
|
|
||||||
foreach (string includePath in includePaths) {
|
|
||||||
ExtractFile(context, includePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ExtractReferenceFiles(ExpandContext context) {
|
|
||||||
var entries = context.Project
|
|
||||||
.Elements(Ns("Project"))
|
|
||||||
.Elements(Ns("ItemGroup"))
|
|
||||||
.Elements(Ns("Reference"))
|
|
||||||
.Select(reference => new {
|
|
||||||
Include = reference.Attribute("Include"),
|
|
||||||
HintPath = reference.Element(Ns("HintPath"))
|
|
||||||
})
|
|
||||||
.Where(entry => entry.Include != null);
|
|
||||||
|
|
||||||
foreach (var entry in entries) {
|
|
||||||
var assemblyName = new AssemblyName(entry.Include.Value);
|
|
||||||
string hintPath = entry.HintPath != null ? entry.HintPath.Value : null;
|
|
||||||
|
|
||||||
string virtualPath = "bin/" + assemblyName.Name + ".dll";
|
|
||||||
if (PartExists(context, virtualPath)) {
|
|
||||||
ExtractFile(context, virtualPath);
|
|
||||||
}
|
|
||||||
else if (hintPath != null) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ExtractThemeFiles(ExpandContext context) {
|
|
||||||
#if REFACTORING
|
|
||||||
foreach (var relativePath in from p in context.Package.GetParts()
|
|
||||||
where p.Uri.ToString().StartsWith("/" + context.ExtensionName + "/", StringComparison.OrdinalIgnoreCase)
|
|
||||||
select p.Uri.ToString().Substring(("/" + context.ExtensionName + "/").Length)) {
|
|
||||||
ExtractFile(context, relativePath);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool PartExists(ExpandContext context, string relativePath) {
|
|
||||||
#if REFACTORING
|
|
||||||
Uri projectUri = PackUriHelper.CreatePartUri(new Uri(context.SourcePath + relativePath, UriKind.Relative));
|
|
||||||
return context.Package.PartExists(projectUri);
|
|
||||||
#else
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private XName Ns(string localName) {
|
|
||||||
return XName.Get(localName, "http://schemas.microsoft.com/developer/msbuild/2003");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool LoadProject(ExpandContext context, string relativePath) {
|
|
||||||
#if REFACTORING
|
|
||||||
Uri projectUri = PackUriHelper.CreatePartUri(new Uri(context.SourcePath + relativePath, UriKind.Relative));
|
|
||||||
if (!context.Package.PartExists(projectUri)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
PackagePart part = context.Package.GetPart(projectUri);
|
|
||||||
using (Stream stream = part.GetStream(FileMode.Open, FileAccess.Read)) {
|
|
||||||
context.Project = XDocument.Load(stream);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EndPackage(ExpandContext context) {
|
|
||||||
#if REFACTORING
|
|
||||||
context.Package.Close();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GetCoreProperties(ExpandContext context) {
|
|
||||||
#if REFACTORING
|
|
||||||
context.ExtensionName = context.Package.PackageProperties.Identifier;
|
|
||||||
context.ExtensionVersion = context.Package.PackageProperties.Version;
|
|
||||||
|
|
||||||
string contentType = context.Package.PackageProperties.ContentType;
|
|
||||||
if (contentType.StartsWith(ContentTypePrefix)) {
|
|
||||||
context.ExtensionType = contentType.Substring(ContentTypePrefix.Length);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EstablishPaths(ExpandContext context, IVirtualPathProvider virtualPathProvider) {
|
|
||||||
context.SourcePath = "\\" + context.ExtensionName + "\\";
|
|
||||||
switch (context.ExtensionType) {
|
|
||||||
case "Theme":
|
|
||||||
context.TargetPath = virtualPathProvider.Combine("~/Themes/" + context.ExtensionName);
|
|
||||||
break;
|
|
||||||
case "Module":
|
|
||||||
context.TargetPath = virtualPathProvider.Combine("~/Modules/" + context.ExtensionName);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new OrchardCoreException(T("Unknown extension type \"{0}\"", context.ExtensionType));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Nested type: ExpandContext
|
|
||||||
|
|
||||||
private class ExpandContext {
|
|
||||||
public Stream Stream { get; set; }
|
|
||||||
//public Package Package { get; set; }
|
|
||||||
|
|
||||||
public string ExtensionName { get; set; }
|
|
||||||
public string ExtensionVersion { get; set; }
|
|
||||||
public string ExtensionType { get; set; }
|
|
||||||
|
|
||||||
public string TargetPath { get; set; }
|
|
||||||
|
|
||||||
public string SourcePath { get; set; }
|
|
||||||
|
|
||||||
public XDocument Project { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,9 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using NuGet;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.Environment.Extensions.Models;
|
using Orchard.Environment.Extensions.Models;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace Orchard.Packaging.Services {
|
namespace Orchard.Packaging.Services {
|
||||||
[OrchardFeature("PackagingServices")]
|
[OrchardFeature("PackagingServices")]
|
||||||
@@ -83,21 +83,10 @@ namespace Orchard.Packaging.Services {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public PackageInfo Install(string filename, string destination) {
|
public PackageInfo Install(string packageId, string version, string location, string solutionFolder) {
|
||||||
var name = Path.GetFileNameWithoutExtension(filename);
|
return _packageExpander.ExpandPackage(packageId, version, location, solutionFolder);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PackageInfo Install(Uri uri, string destination) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user