Updating packaging commands to handle solution-less applications

If the solution is not found, don't NuGet-Install the package, but still NuGet-AddReference
Uninstallation deletes the module if there is no solution folder
Creating a specific ReferenceRepository implementation to remove the packages.config dependency

--HG--
branch : dev
This commit is contained in:
Sébastien Ros
2010-11-11 23:19:23 -08:00
parent 1f3ddc98ed
commit 686a9e6f4d
5 changed files with 158 additions and 58 deletions

View File

@@ -5,26 +5,30 @@ using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.UI.Notify;
using NuGetPackageManager = NuGet.PackageManager;
using System.Security.Permissions;
using System.Security;
using System.Web.Hosting;
namespace Orchard.Packaging.Services {
[OrchardFeature("PackagingServices")]
public class PackageInstaller : IPackageInstaller {
private const string PackagesPath = "packages";
private const string ProjectPath = "Orchard.Web";
private const string SolutionFilename = "Orchard.sln";
private readonly INotifier _notifier;
private readonly IExtensionManager _extensionManager;
public PackageInstaller(INotifier notifier) {
public PackageInstaller(INotifier notifier, IExtensionManager extensionManager) {
_notifier = notifier;
_extensionManager = extensionManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public PackageInfo Install(string packageId, string version, string location, string solutionFolder) {
var packagesPath = Path.Combine(solutionFolder, PackagesPath); // where to download/uncompress packages
var projectPath = Path.Combine(solutionFolder, ProjectPath); // where to install packages (in the project's folder)
public PackageInfo Install(string packageId, string version, string location, string applicationPath) {
// this logger is used to render NuGet's log on the notifier
var logger = new NugetLogger(_notifier);
// instantiates the appropriate package repository
@@ -34,23 +38,43 @@ namespace Orchard.Packaging.Services {
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, id:{0} version:{1}", packageId, String.IsNullOrEmpty(version) ? T("No version").Text : version).Text);
}
var packageManager = new NuGetPackageManager(
packageRepository,
new DefaultPackagePathResolver(location),
new PhysicalFileSystem(packagesPath) { Logger = logger }
) {Logger = logger};
bool installed = false;
// specifically tells to ignore dependencies
packageManager.InstallPackage(package, ignoreDependencies: true);
// if we can access the parent directory, and the solution is inside, NuGet-install the package here
string solutionPath;
var installedPackagesPath = String.Empty;
if (TryGetSolutionPath(applicationPath, out solutionPath)) {
installedPackagesPath = Path.Combine(solutionPath, PackagesPath);
try {
var packageManager = new NuGetPackageManager(
packageRepository,
new DefaultPackagePathResolver(location),
new PhysicalFileSystem(installedPackagesPath) { Logger = logger }
) { Logger = logger };
packageManager.InstallPackage(package, ignoreDependencies: true);
installed = true;
}
catch {
// installing the package at the solution level failed
}
}
// if the package got installed successfully, use it, otherwise use the previous repository
var sourceRepository = installed
? new LocalPackageRepository(installedPackagesPath)
: packageRepository;
var project = new FileBasedProjectSystem(applicationPath) { Logger = logger };
var projectManager = new ProjectManager(
new LocalPackageRepository(packagesPath), // source repository for the package to install
sourceRepository, // source repository for the package to install
new DefaultPackagePathResolver(location),
new FileBasedProjectSystem(projectPath) { Logger = logger } // the location of the project (where to copy the content files)
project,
new ExtensionReferenceRepository(project, sourceRepository, _extensionManager)
) {Logger = logger};
// add the package to the project
@@ -59,38 +83,68 @@ namespace Orchard.Packaging.Services {
return new PackageInfo {
ExtensionName = package.Title ?? package.Id,
ExtensionVersion = package.Version.ToString(),
ExtensionType = String.Empty, // todo: assign value
ExtensionPath = projectPath
ExtensionType = package.Id.StartsWith("Orchard.Theme") ? "Theme" : "Module",
ExtensionPath = applicationPath
};
}
public void Uninstall(string packageId, string solutionFolder) {
var packagesPath = Path.Combine(solutionFolder, PackagesPath); // where to download/uncompress packages
var projectPath = Path.Combine(solutionFolder, ProjectPath); // where to install packages (in the project's folder)
public void Uninstall(string packageId, string applicationPath) {
// this logger is used to render NuGet's log on the notifier
var logger = new NugetLogger(_notifier);
// instantiates the appropriate package repository
var packageRepository = PackageRepositoryFactory.Default.CreateRepository(new PackageSource("Default", packagesPath));
string solutionPath;
// if we can access the parent directory, and the solution is inside, NuGet-uninstall the package here
if (TryGetSolutionPath(applicationPath, out solutionPath)) {
var installedPackagesPath = Path.Combine(solutionPath, PackagesPath);
var projectManager = new ProjectManager(
new LocalPackageRepository(packagesPath),
new DefaultPackagePathResolver(packagesPath),
new FileBasedProjectSystem(projectPath) { Logger = logger }
) { Logger = logger };
var sourcePackageRepository = new LocalPackageRepository(installedPackagesPath);
var project = new FileBasedProjectSystem(applicationPath) { Logger = logger };
var projectManager = new ProjectManager(
sourcePackageRepository,
new DefaultPackagePathResolver(installedPackagesPath),
project,
new ExtensionReferenceRepository(project, sourcePackageRepository, _extensionManager)
) { Logger = logger };
// removes the package from the project
projectManager.RemovePackageReference(packageId);
// add the package to the project
projectManager.RemovePackageReference(packageId);
var packageManager = new NuGetPackageManager(
packageRepository,
new DefaultPackagePathResolver(packagesPath),
new PhysicalFileSystem(packagesPath) { Logger = logger }
) { Logger = logger };
var packageManager = new NuGetPackageManager(
sourcePackageRepository,
new DefaultPackagePathResolver(applicationPath),
new PhysicalFileSystem(installedPackagesPath) { Logger = logger }
) { Logger = logger };
packageManager.UninstallPackage(packageId);
}
else {
// otherwise delete the folder
packageManager.UninstallPackage(packageId);
string extensionPath = packageId.StartsWith("Orchard.Themes.")
? "~/Themes/" + packageId.Substring("Orchard.Theme.".Length)
: "~/Modules/" + packageId.Substring("Orchard.Module.".Length);
string extensionFullPath = HostingEnvironment.MapPath(extensionPath);
if (Directory.Exists(extensionFullPath)) {
Directory.Delete(extensionFullPath, true);
}
else {
throw new OrchardException(T("Package not found: ", packageId));
}
}
}
private bool TryGetSolutionPath(string applicationPath, out string parentPath) {
try {
parentPath = Directory.GetParent(applicationPath).Parent.FullName;
var solutionPath = Path.Combine(parentPath, SolutionFilename);
return File.Exists(solutionPath);
}
catch {
parentPath = null;
return false;
}
}
}
}