mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +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 Orchard.Commands;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Packaging.Services;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Packaging.Commands {
|
||||
[OrchardFeature("Orchard.Packaging")]
|
||||
@@ -18,6 +16,9 @@ namespace Orchard.Packaging.Commands {
|
||||
_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.")]
|
||||
[CommandName("package create")]
|
||||
public void CreatePackage(string moduleName) {
|
||||
@@ -47,30 +48,23 @@ namespace Orchard.Packaging.Commands {
|
||||
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")]
|
||||
public void InstallPackage(string filename) {
|
||||
if (!File.Exists(filename)) {
|
||||
Context.Output.WriteLine(T("File \"{0}\" does not exist.", filename));
|
||||
}
|
||||
|
||||
[OrchardSwitches("Version")]
|
||||
public void InstallPackage(string packageId, string location) {
|
||||
var solutionFolder = GetSolutionFolder();
|
||||
|
||||
if(solutionFolder == null) {
|
||||
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));
|
||||
}
|
||||
|
||||
private static string GetSolutionFolder() {
|
||||
var orchardDirectory = Directory.GetParent(OrchardWebProj);
|
||||
if(orchardDirectory.Parent == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return orchardDirectory.Parent.FullName;
|
||||
return orchardDirectory.Parent == null ? null : orchardDirectory.Parent.FullName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,6 +10,6 @@ namespace Orchard.Packaging.Services {
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
void Push(PackageData packageData, string feedUrl, string login, string password);
|
||||
PackageInfo Install(string filename, string destination);
|
||||
PackageInfo Install(Uri uri, string destination);
|
||||
PackageInfo Install(string packageId, string version, string location, string solutionFolder);
|
||||
}
|
||||
}
|
@@ -1,12 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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;
|
||||
@@ -14,239 +9,59 @@ using NuGetPackageManager = NuGet.PackageManager;
|
||||
namespace Orchard.Packaging.Services {
|
||||
[OrchardFeature("PackagingServices")]
|
||||
public class PackageExpander : IPackageExpander {
|
||||
private const string ContentTypePrefix = "Orchard ";
|
||||
private readonly IVirtualPathProvider _virtualPathProvider;
|
||||
|
||||
public PackageExpander(IVirtualPathProvider virtualPathProvider) {
|
||||
_virtualPathProvider = virtualPathProvider;
|
||||
|
||||
public PackageExpander() {
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
|
||||
public PackageInfo ExpandPackage(string packageId, string version, string location, string destination) {
|
||||
var context = new ExpandContext();
|
||||
public PackageInfo ExpandPackage(string packageId, string version, string location, string solutionFolder) {
|
||||
|
||||
var packagesPath = Path.Combine(destination, "packages");
|
||||
var projectPath = Path.Combine(destination, "Orchard.Web");
|
||||
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)
|
||||
|
||||
BeginPackage(context, packageId, version, location, packagesPath);
|
||||
try {
|
||||
// instantiates the appropriate package repository
|
||||
var packageRepository = Uri.IsWellFormedUriString(location, UriKind.Absolute)
|
||||
? new DataServicePackageRepository(new Uri(location))
|
||||
: new LocalPackageRepository(location) as IPackageRepository;
|
||||
|
||||
var packageRepository = Uri.IsWellFormedUriString(location, UriKind.Absolute)
|
||||
? new DataServicePackageRepository(new Uri(location))
|
||||
: new LocalPackageRepository(location) as IPackageRepository;
|
||||
// gets an IPackage instance from the repository
|
||||
var packageVersion = String.IsNullOrEmpty(version) ? null : new Version(version);
|
||||
var package = packageRepository.FindPackage(packageId, packageVersion);
|
||||
|
||||
var package = packageRepository.FindPackage(packageId, exactVersion: new Version(version));
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
var packageManager = new NuGetPackageManager(
|
||||
packageRepository,
|
||||
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
|
||||
);
|
||||
new FileBasedProjectSystem(packagesPath)
|
||||
);
|
||||
|
||||
// add the package to the project
|
||||
projectManager.AddPackageReference(packageId, new Version(version));
|
||||
// specifically tells to ignore dependencies
|
||||
packageManager.InstallPackage(package, ignoreDependencies: true);
|
||||
|
||||
var projectManager = new ProjectManager(
|
||||
new LocalPackageRepository(packagesPath), // 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
|
||||
);
|
||||
|
||||
#if REFACTORING
|
||||
GetCoreProperties(context);
|
||||
EstablishPaths(context, _virtualPathProvider);
|
||||
// add the package to the project
|
||||
projectManager.AddPackageReference(packageId, packageVersion);
|
||||
|
||||
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 {
|
||||
ExtensionName = context.ExtensionName,
|
||||
ExtensionVersion = context.ExtensionVersion,
|
||||
ExtensionType = context.ExtensionType,
|
||||
ExtensionPath = context.TargetPath
|
||||
ExtensionName = package.Title,
|
||||
ExtensionVersion = package.Version.ToString(),
|
||||
ExtensionType = String.Empty, // todo: assign value
|
||||
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.Net;
|
||||
using System.Text;
|
||||
using NuGet;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Environment.Extensions.Models;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Orchard.Packaging.Services {
|
||||
[OrchardFeature("PackagingServices")]
|
||||
@@ -83,21 +83,10 @@ namespace Orchard.Packaging.Services {
|
||||
#endif
|
||||
}
|
||||
|
||||
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);
|
||||
public PackageInfo Install(string packageId, string version, string location, string solutionFolder) {
|
||||
return _packageExpander.ExpandPackage(packageId, version, location, solutionFolder);
|
||||
}
|
||||
|
||||
public PackageInfo Install(Uri uri, string destination) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user