Slightly refactor IAppDataFolder

Remove the "SetBasePath" method, introduce IAppDataFolderRoot as replacement.

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-06-12 12:18:08 -07:00
parent c566e6599d
commit 767f19a4e3
13 changed files with 117 additions and 98 deletions

View File

@@ -10,6 +10,7 @@ using Orchard.FileSystems.VirtualPath;
using Orchard.Indexing;
using Orchard.Core.Indexing.Lucene;
using Orchard.Services;
using Orchard.Tests.Environment.Configuration;
namespace Orchard.Tests.Indexing {
public class DefaultIndexProviderTests {
@@ -31,8 +32,7 @@ namespace Orchard.Tests.Indexing {
}
Directory.CreateDirectory(_basePath);
_appDataFolder = new AppDataFolder(new DefaultVirtualPathMonitor(new Clock()));
_appDataFolder.SetBasePath(_basePath);
_appDataFolder = AppDataFolderTests.CreateAppDataFolder(_basePath);
var builder = new ContainerBuilder();
builder.RegisterType<DefaultIndexProvider>().As<IIndexProvider>();

View File

@@ -10,6 +10,7 @@ using Orchard.FileSystems.VirtualPath;
using Orchard.Indexing;
using Orchard.Core.Indexing.Lucene;
using Orchard.Services;
using Orchard.Tests.Environment.Configuration;
namespace Orchard.Tests.Indexing {
public class DefaultSearchBuilderTests {
@@ -31,9 +32,7 @@ namespace Orchard.Tests.Indexing {
}
Directory.CreateDirectory(_basePath);
_appDataFolder = new AppDataFolder(new DefaultVirtualPathMonitor(new Clock()));
_appDataFolder.SetBasePath(_basePath);
_appDataFolder = AppDataFolderTests.CreateAppDataFolder(_basePath);
var builder = new ContainerBuilder();
builder.RegisterType<DefaultIndexProvider>().As<IIndexProvider>();

View File

@@ -8,6 +8,7 @@ using Orchard.Environment.Descriptor.Models;
using Orchard.FileSystems.AppData;
using Orchard.FileSystems.VirtualPath;
using Orchard.Services;
using Orchard.Tests.Environment.Configuration;
namespace Orchard.Tests.Environment.Blueprint {
[TestFixture]
@@ -21,8 +22,9 @@ namespace Orchard.Tests.Environment.Blueprint {
_tempFolder = Path.GetTempFileName();
File.Delete(_tempFolder);
Directory.CreateDirectory(_tempFolder);
_appDataFolder = new AppDataFolder(new DefaultVirtualPathMonitor(new Clock()));
_appDataFolder.SetBasePath(_tempFolder);
_appDataFolder = AppDataFolderTests.CreateAppDataFolder(_tempFolder);
var builder = new ContainerBuilder();
builder.RegisterInstance(_appDataFolder).As<IAppDataFolder>();
builder.RegisterType<ShellDescriptorCache>().As<IShellDescriptorCache>();

View File

@@ -1,7 +1,6 @@
using System.IO;
using System.Linq;
using NUnit.Framework;
using Orchard.Environment;
using Orchard.FileSystems.AppData;
using Orchard.FileSystems.VirtualPath;
using Orchard.Services;
@@ -12,6 +11,16 @@ namespace Orchard.Tests.Environment.Configuration {
private string _tempFolder;
private IAppDataFolder _appDataFolder;
public class StubAppDataFolderRoot : IAppDataFolderRoot {
public string RootPath { get; set; }
public string RootFolder { get; set; }
}
public static IAppDataFolder CreateAppDataFolder(string tempFolder) {
var folderRoot = new StubAppDataFolderRoot {RootPath = "~/App_Data", RootFolder = tempFolder};
return new AppDataFolder(folderRoot, new DefaultVirtualPathMonitor(new Clock()));
}
[SetUp]
public void Init() {
_tempFolder = Path.GetTempFileName();
@@ -21,8 +30,7 @@ namespace Orchard.Tests.Environment.Configuration {
File.WriteAllText(Path.Combine(_tempFolder, "alpha\\gamma.txt"), "gamma-content");
Directory.CreateDirectory(Path.Combine(_tempFolder, "alpha\\omega"));
_appDataFolder = new AppDataFolder(new DefaultVirtualPathMonitor(new Clock()));
_appDataFolder.SetBasePath(_tempFolder);
_appDataFolder = CreateAppDataFolder(_tempFolder);
}
[TearDown]

View File

@@ -12,14 +12,14 @@ namespace Orchard.Tests.Environment.Configuration {
[TestFixture]
public class DefaultTenantManagerTests {
private string _tempFolder;
private AppDataFolder _appData;
private IAppDataFolder _appData;
[SetUp]
public void Init() {
_appData = new AppDataFolder(new DefaultVirtualPathMonitor(new Clock()));
_tempFolder = Path.GetTempFileName();
File.Delete(_tempFolder);
_appData.SetBasePath(_tempFolder);
_appData = AppDataFolderTests.CreateAppDataFolder(_tempFolder);
}
[TearDown]
public void Term() {

View File

@@ -9,6 +9,7 @@ using Autofac;
using Autofac.Integration.Web;
using Moq;
using NUnit.Framework;
using Orchard.Caching;
using Orchard.Environment;
using Orchard.Environment.AutofacUtil;
using Orchard.Environment.Configuration;
@@ -22,6 +23,7 @@ using Orchard.FileSystems.AppData;
using Orchard.Mvc;
using Orchard.Mvc.ModelBinders;
using Orchard.Mvc.Routes;
using Orchard.Tests.Environment.Configuration;
using Orchard.Tests.Environment.TestDependencies;
using Orchard.Tests.Stubs;
using Orchard.Tests.Utility;
@@ -38,6 +40,12 @@ namespace Orchard.Tests.Environment {
[SetUp]
public void Init() {
var temp = Path.GetTempFileName();
File.Delete(temp);
Directory.CreateDirectory(temp);
var appDataFolder = AppDataFolderTests.CreateAppDataFolder(temp);
_controllerBuilder = new ControllerBuilder();
_routeCollection = new RouteCollection();
_modelBinderDictionary = new ModelBinderDictionary();
@@ -51,6 +59,7 @@ namespace Orchard.Tests.Environment {
builder.RegisterType<ModelBinderPublisher>().As<IModelBinderPublisher>();
builder.RegisterType<ShellContextFactory>().As<IShellContextFactory>();
builder.RegisterType<StubExtensionManager>().As<IExtensionManager>();
builder.RegisterInstance(appDataFolder);
builder.RegisterInstance(_controllerBuilder);
builder.RegisterInstance(_routeCollection);
builder.RegisterInstance(_modelBinderDictionary);
@@ -75,13 +84,6 @@ namespace Orchard.Tests.Environment {
_container.Mock<IOrchardShellEvents>()
.Setup(e=>e.Activated());
var temp = Path.GetTempFileName();
File.Delete(temp);
Directory.CreateDirectory(temp);
_container.Resolve<IAppDataFolder>()
.SetBasePath(temp);
var updater = new ContainerUpdater();
updater.RegisterInstance(_container).SingleInstance();
updater.Update(_lifetime);
@@ -120,6 +122,10 @@ namespace Orchard.Tests.Environment {
public void UninstallExtension(string extensionType, string extensionName) {
throw new NotImplementedException();
}
public void Monitor(Action<IVolatileToken> monitor) {
throw new NotImplementedException();
}
}
public class StubShellSettingsLoader : IShellSettingsManager {

View File

@@ -72,11 +72,14 @@ namespace Orchard.Data {
private ISessionFactory BuildSessionFactory(bool createDatabase, bool updateSchema) {
Logger.Debug("Building session factory");
var shellPath = _appDataFolder.CreateDirectory(Path.Combine("Sites", _shellSettings.Name));
var shellPath = _appDataFolder.Combine("Sites", _shellSettings.Name);
_appDataFolder.CreateDirectory(shellPath);
var shellFolder = _appDataFolder.MapPath(shellPath);
var sessionFactory = _sessionFactoryBuilder.BuildSessionFactory(new SessionFactoryParameters {
Provider = _shellSettings.DataProvider,
DataFolder = shellPath,
DataFolder = shellFolder,
ConnectionString = _shellSettings.DataConnectionString,
CreateDatabase = createDatabase,
UpdateSchema = updateSchema,

View File

@@ -36,6 +36,7 @@ namespace Orchard.Environment {
builder.RegisterType<DefaultHostEnvironment>().As<IHostEnvironment>().SingleInstance();
builder.RegisterType<DefaultBuildManager>().As<IBuildManager>().SingleInstance();
builder.RegisterType<WebFormsExtensionsVirtualPathProvider>().As<ICustomVirtualPathProvider>().SingleInstance();
builder.RegisterType<AppDataFolderRoot>().As<IAppDataFolderRoot>().SingleInstance();
RegisterVolatileProvider<WebSiteFolder, IWebSiteFolder>(builder);
RegisterVolatileProvider<AppDataFolder, IAppDataFolder>(builder);

View File

@@ -1,32 +1,50 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Hosting;
using Orchard.Caching;
using Orchard.Environment;
using Orchard.FileSystems.VirtualPath;
namespace Orchard.FileSystems.AppData {
public class AppDataFolder : IAppDataFolder {
private readonly IAppDataFolderRoot _root;
private readonly IVirtualPathMonitor _virtualPathMonitor;
protected string _basePath;
public AppDataFolder(IVirtualPathMonitor virtualPathMonitor) {
public AppDataFolder(IAppDataFolderRoot root, IVirtualPathMonitor virtualPathMonitor) {
_root = root;
_virtualPathMonitor = virtualPathMonitor;
_basePath = HostingEnvironment.MapPath("~/App_Data");
}
public string RootFolder {
get {
return _root.RootFolder;
}
}
public string _appDataPath {
get {
return _root.RootPath;
}
}
/// <summary>
/// Combine a set of virtual paths into a physical path based at "_basePath"
/// Combine a set of virtual paths relative to "~/App_Data" into an absolute physical path
/// starting with "_basePath".
/// </summary>
private string CombineToPhysicalPath(params string[] paths) {
return Path.Combine(_basePath, Path.Combine(paths))
return Path.Combine(RootFolder, Path.Combine(paths))
.Replace('/', Path.DirectorySeparatorChar);
}
/// <summary>
/// Combine a set of virtual paths into a virtual path relative to "~/App_Data"
/// </summary>
public string Combine(params string[] paths) {
return Path.Combine(paths).Replace(Path.DirectorySeparatorChar, '/');
}
public IVolatileToken WhenPathChanges(string path) {
var replace = Path.Combine("~/App_Data", path).Replace(Path.DirectorySeparatorChar, '/');
var replace = Combine(_appDataPath, path);
return _virtualPathMonitor.WhenPathChanges(replace);
}
@@ -59,12 +77,7 @@ namespace Orchard.FileSystems.AppData {
}
public bool FileExists(string path) {
var filePath = CombineToPhysicalPath(path);
return File.Exists(filePath);
}
public string Combine(params string[] paths) {
return Path.Combine(paths).Replace(Path.DirectorySeparatorChar, '/');
return File.Exists(CombineToPhysicalPath(path));
}
public IEnumerable<string> ListFiles(string path) {
@@ -93,15 +106,8 @@ namespace Orchard.FileSystems.AppData {
});
}
public string CreateDirectory(string path) {
var directory = CombineToPhysicalPath(path);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
return directory;
}
public void SetBasePath(string basePath) {
_basePath = basePath;
public void CreateDirectory(string path) {
Directory.CreateDirectory(CombineToPhysicalPath(path));
}
public string MapPath(string path) {

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
@@ -5,9 +6,8 @@ using Orchard.Caching;
namespace Orchard.FileSystems.AppData {
/// <summary>
/// Abstraction of App_Data folder
/// Expected to work on physical filesystem, but decouples core
/// system from web hosting apis
/// Abstraction of App_Data folder. All virtual paths passed in or returned are relative to "~/App_Data".
/// Expected to work on physical filesystem, but decouples core system from web hosting apis
/// </summary>
public interface IAppDataFolder : IVolatileProvider {
IEnumerable<string> ListFiles(string path);
@@ -24,15 +24,10 @@ namespace Orchard.FileSystems.AppData {
void DeleteFile(string path);
string CreateDirectory(string path);
void CreateDirectory(string path);
IVolatileToken WhenPathChanges(string path);
/// <summary>
/// May be called to initialize component when not in a hosting environment
/// app domain
/// </summary>
void SetBasePath(string basePath);
string MapPath(string path);
}
}

View File

@@ -0,0 +1,29 @@
using System;
using System.Web.Hosting;
namespace Orchard.FileSystems.AppData {
/// <summary>
/// Abstraction over the root location of "~/App_Data", mainly to enable
/// unit testing of AppDataFolder.
/// </summary>
public interface IAppDataFolderRoot : ISingletonDependency {
/// <summary>
/// Virtual path of root ("~/App_Data")
/// </summary>
string RootPath { get; }
/// <summary>
/// Physical path of root (typically: MapPath(RootPath))
/// </summary>
string RootFolder { get; }
}
public class AppDataFolderRoot : IAppDataFolderRoot {
public string RootPath {
get { return "~/App_Data"; }
}
public string RootFolder {
get { return HostingEnvironment.MapPath(RootPath); }
}
}
}

View File

@@ -5,36 +5,25 @@ using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Orchard.Caching;
using Orchard.Environment.Extensions;
using Orchard.FileSystems.AppData;
using Orchard.FileSystems.WebSite;
namespace Orchard.FileSystems.Dependencies {
public class DefaultDependenciesFolder : IDependenciesFolder {
private readonly string _basePath = "Dependencies";
private readonly string _persistanceFileName = "dependencies.xml";
private readonly ICacheManager _cacheManager;
private readonly IWebSiteFolder _webSiteFolder;
private readonly IAppDataFolder _appDataFolder;
private readonly IExtensionManagerEvents _events;
private readonly InvalidationToken _writeThroughToken;
public DefaultDependenciesFolder(ICacheManager cacheManager, IWebSiteFolder webSiteFolder, IAppDataFolder appDataFolder, IExtensionManagerEvents events) {
public DefaultDependenciesFolder(ICacheManager cacheManager, IAppDataFolder appDataFolder) {
_cacheManager = cacheManager;
_webSiteFolder = webSiteFolder;
_appDataFolder = appDataFolder;
_events = events;
_writeThroughToken = new InvalidationToken();
}
private string BasePath {
get {
return _basePath;
}
}
private string PersistencePath {
get {
return _appDataFolder.Combine(BasePath, "dependencies.xml");
return _appDataFolder.Combine(_basePath, _persistanceFileName);
}
}
@@ -42,10 +31,12 @@ namespace Orchard.FileSystems.Dependencies {
get {
return _cacheManager.Get(PersistencePath,
ctx => {
_appDataFolder.CreateDirectory(_basePath);
ctx.Monitor(_appDataFolder.WhenPathChanges(ctx.Key));
ctx.Monitor(_writeThroughToken);
_appDataFolder.CreateDirectory(BasePath);
_appDataFolder.CreateDirectory(_basePath);
return ReadDependencies(ctx.Key).ToList();
});
}
@@ -55,36 +46,14 @@ namespace Orchard.FileSystems.Dependencies {
public bool IsCurrent { get; set; }
}
//public void StoreReferencedAssembly(string moduleName) {
// if (Descriptors.Any(d => d.ModuleName == moduleName)) {
// // Remove the moduleName from the list of assemblies in the dependency folder
// var newDescriptors = Descriptors.Where(d => d.ModuleName != moduleName);
// WriteDependencies(PersistencePath, newDescriptors);
// }
//}
//public void StoreBuildProviderAssembly(string moduleName, string virtualPath, Assembly assembly) {
// var descriptor = new DependencyDescriptor {
// ModuleName = moduleName,
// IsFromBuildProvider = true,
// VirtualPath = virtualPath,
// FileName = assembly.Location
// };
// StoreDepencyInformation(descriptor);
// _webSiteFolder.WhenPathChanges(virtualPath, () => _events.ModuleChanged(moduleName));
//}
public void StorePrecompiledAssembly(string moduleModuleName, string virtualPath, string loaderName) {
_appDataFolder.CreateDirectory(BasePath);
_appDataFolder.CreateDirectory(_basePath);
// Only store assembly if it's more recent that what we have stored already (if anything)
var assemblyFileName = _appDataFolder.MapPath(virtualPath);
if (IsNewerAssembly(moduleModuleName, assemblyFileName)) {
var destinationFileName = Path.GetFileName(assemblyFileName);
var destinationPath = _appDataFolder.MapPath(_appDataFolder.Combine(BasePath, destinationFileName));
var destinationPath = _appDataFolder.MapPath(_appDataFolder.Combine(_basePath, destinationFileName));
File.Copy(assemblyFileName, destinationPath, true);
StoreDepencyInformation(new DependencyDescriptor {
@@ -119,7 +88,7 @@ namespace Orchard.FileSystems.Dependencies {
return true;
}
var existingFileName = _appDataFolder.MapPath(_appDataFolder.Combine(BasePath, dependency.FileName));
var existingFileName = _appDataFolder.MapPath(_appDataFolder.Combine(_basePath, dependency.FileName));
if (!File.Exists(existingFileName)) {
return true;
}
@@ -141,13 +110,13 @@ namespace Orchard.FileSystems.Dependencies {
}
public Assembly LoadAssembly(string moduleName) {
_appDataFolder.CreateDirectory(BasePath);
_appDataFolder.CreateDirectory(_basePath);
var dependency = Descriptors.SingleOrDefault(d => d.ModuleName == moduleName);
if (dependency == null)
return null;
if (!_appDataFolder.FileExists(_appDataFolder.Combine(BasePath, dependency.FileName)))
if (!_appDataFolder.FileExists(_appDataFolder.Combine(_basePath, dependency.FileName)))
return null;
return Assembly.Load(Path.GetFileNameWithoutExtension(dependency.FileName));
@@ -158,7 +127,7 @@ namespace Orchard.FileSystems.Dependencies {
if (dependency == null)
return false;
if (!_appDataFolder.FileExists(_appDataFolder.Combine(BasePath, dependency.FileName)))
if (!_appDataFolder.FileExists(_appDataFolder.Combine(_basePath, dependency.FileName)))
return false;
return true;

View File

@@ -353,6 +353,7 @@
<Compile Include="Data\DataModule.cs" />
<Compile Include="Data\Orderable.cs" />
<Compile Include="Environment\DefaultOrchardShell.cs" />
<Compile Include="FileSystems\AppData\IAppDataFolderRoot.cs" />
<Compile Include="FileSystems\Dependencies\DefaultDependenciesFolder.cs" />
<Compile Include="FileSystems\VirtualPath\DefaultVirtualPathMonitor.cs" />
<Compile Include="FileSystems\VirtualPath\DefaultVirtualPathProvider.cs" />