mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Using FluentPath assembly
Deprecates PathUtil utility class --HG-- branch : dev
This commit is contained in:
@@ -13,14 +13,13 @@ namespace Orchard.Specs.Hosting
|
||||
{
|
||||
public static RequestDetails SendRequest(this WebHost webHost, string urlPath)
|
||||
{
|
||||
var physicalPath = new PathUtil(webHost.PhysicalDirectory);
|
||||
var physicalPath = Bleroy.FluentPath.Path.Get(webHost.PhysicalDirectory);
|
||||
|
||||
var details = new RequestDetails
|
||||
{
|
||||
Page = physicalPath
|
||||
.Combine(urlPath.TrimStart('/', '\\'))
|
||||
.GetRelativePath(physicalPath)
|
||||
.ToString()
|
||||
};
|
||||
|
||||
webHost.Execute(() =>
|
||||
|
@@ -3,33 +3,33 @@ using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Web.Hosting;
|
||||
using Orchard.Specs.Util;
|
||||
using Path = Bleroy.FluentPath.Path;
|
||||
|
||||
namespace Orchard.Specs.Hosting
|
||||
{
|
||||
public class WebHost
|
||||
{
|
||||
namespace Orchard.Specs.Hosting {
|
||||
public class WebHost {
|
||||
private WebHostAgent _webHostAgent;
|
||||
private PathUtil _tempSite;
|
||||
private PathUtil _orchardWebPath;
|
||||
private Path _tempSite;
|
||||
private Path _orchardWebPath;
|
||||
|
||||
public void Initialize(string templateName, string virtualDirectory)
|
||||
{
|
||||
_tempSite = PathUtil.GetTempFolder();
|
||||
_orchardWebPath = PathUtil.BaseDirectory.Parent.Parent.Parent.Combine("Orchard.Web");
|
||||
|
||||
var baseDir = PathUtil.BaseDirectory;
|
||||
|
||||
PathUtil.BaseDirectory
|
||||
.Combine("Hosting")
|
||||
.Combine(templateName)
|
||||
.CopyAll(SearchOption.AllDirectories, _tempSite);
|
||||
public void Initialize(string templateName, string virtualDirectory) {
|
||||
var baseDir = Path.Get(AppDomain.CurrentDomain.BaseDirectory);
|
||||
|
||||
_orchardWebPath.Combine("bin").CopyAll("*.dll", _tempSite.Combine("bin"));
|
||||
_orchardWebPath.Combine("bin").CopyAll("*.pdb", _tempSite.Combine("bin"));
|
||||
baseDir.CopyAll("*.dll", _tempSite.Combine("bin"));
|
||||
baseDir.CopyAll("*.pdb", _tempSite.Combine("bin"));
|
||||
_tempSite = Path.Get(System.IO.Path.GetTempFileName()).Delete().CreateDirectory();
|
||||
|
||||
PhysicalDirectory = _tempSite.ToString();
|
||||
_orchardWebPath = baseDir.Parent.Parent.Parent.Combine("Orchard.Web");
|
||||
|
||||
baseDir.Combine("Hosting").Combine(templateName)
|
||||
.DeepCopy(_tempSite);
|
||||
|
||||
_orchardWebPath.Combine("bin")
|
||||
.ShallowCopy("*.dll", _tempSite.Combine("bin"))
|
||||
.ShallowCopy("*.pdb", _tempSite.Combine("bin"));
|
||||
|
||||
baseDir
|
||||
.ShallowCopy("*.dll", _tempSite.Combine("bin"))
|
||||
.ShallowCopy("*.pdb", _tempSite.Combine("bin"));
|
||||
|
||||
PhysicalDirectory = _tempSite;
|
||||
VirtualDirectory = virtualDirectory;
|
||||
|
||||
_webHostAgent = (WebHostAgent)ApplicationHost.CreateApplicationHost(typeof(WebHostAgent), VirtualDirectory, PhysicalDirectory);
|
||||
@@ -38,28 +38,24 @@ namespace Orchard.Specs.Hosting
|
||||
public void CopyExtension(string extensionFolder, string extensionName) {
|
||||
var sourceModule = _orchardWebPath.Combine(extensionFolder).Combine(extensionName);
|
||||
var targetModule = _tempSite.Combine(extensionFolder).Combine(extensionName);
|
||||
sourceModule.CopyAll("*.txt", targetModule);
|
||||
if (sourceModule.Combine("Views").DirectoryExists)
|
||||
sourceModule.Combine("Views").CopyAll(SearchOption.AllDirectories, targetModule.Combine("Views"));
|
||||
|
||||
sourceModule.ShallowCopy("*.txt", targetModule);
|
||||
if (sourceModule.Combine("Views").IsDirectory)
|
||||
sourceModule.Combine("Views").DeepCopy(targetModule.Combine("Views"));
|
||||
}
|
||||
|
||||
public string PhysicalDirectory { get; private set; }
|
||||
public string VirtualDirectory { get; private set; }
|
||||
|
||||
public void Execute(Action action)
|
||||
{
|
||||
public void Execute(Action action) {
|
||||
var shuttleSend = new SerializableDelegate<Action>(action);
|
||||
var shuttleRecv = _webHostAgent.Execute(shuttleSend);
|
||||
CopyFields(shuttleRecv.Delegate.Target, shuttleSend.Delegate.Target);
|
||||
}
|
||||
|
||||
private static void CopyFields<T>(T from, T to) where T : class
|
||||
{
|
||||
private static void CopyFields<T>(T from, T to) where T : class {
|
||||
if (from == null || to == null)
|
||||
return;
|
||||
foreach (FieldInfo fieldInfo in from.GetType().GetFields())
|
||||
{
|
||||
foreach (FieldInfo fieldInfo in from.GetType().GetFields()) {
|
||||
var value = fieldInfo.GetValue(from);
|
||||
fieldInfo.SetValue(to, value);
|
||||
}
|
||||
|
@@ -43,6 +43,10 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\autofac\Autofac.Integration.Web.Mvc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="FluentPath, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\fluentpath\FluentPath.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="HtmlAgilityPack, Version=1.3.9.1, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\htmlagilitypack\HtmlAgilityPack.dll</HintPath>
|
||||
@@ -85,6 +89,7 @@
|
||||
<Compile Include="Hosting\Orchard.Web\HelloYetAgainHandler.cs" />
|
||||
<Compile Include="Hosting\RequestExtensions.cs" />
|
||||
<Compile Include="Hosting\RequestDetails.cs" />
|
||||
<Compile Include="Util\PathExtensions.cs" />
|
||||
<Compile Include="WebHosting.feature.cs">
|
||||
<DependentUpon>WebHosting.feature</DependentUpon>
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -110,7 +115,6 @@
|
||||
<Compile Include="Hosting\Orchard.Web\_Default.aspx.cs">
|
||||
<SubType>ASPXCodeBehind</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Util\PathUtil.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Hosting\Orchard.Web\Web.config">
|
||||
|
37
src/Orchard.Specs/Util/PathExtensions.cs
Normal file
37
src/Orchard.Specs/Util/PathExtensions.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.IO;
|
||||
using Path = Bleroy.FluentPath.Path;
|
||||
|
||||
namespace Orchard.Specs.Util {
|
||||
public static class PathExtensions {
|
||||
public static Path GetRelativePath(this Path path, Path basePath) {
|
||||
if (path.Equals(basePath))
|
||||
return Path.Get(".");
|
||||
|
||||
if (path.Parent.Equals(basePath))
|
||||
return path.FileName;
|
||||
|
||||
return path.Parent.GetRelativePath(basePath).Combine(path.FileName);
|
||||
}
|
||||
|
||||
|
||||
public static Path DeepCopy(this Path sourcePath, Path targetPath) {
|
||||
sourcePath
|
||||
.GetFiles("*", true /*recursive*/)
|
||||
.ForEach(file => FileCopy(sourcePath, targetPath, file));
|
||||
return sourcePath;
|
||||
}
|
||||
|
||||
public static Path ShallowCopy(this Path sourcePath, string pattern, Path targetPath) {
|
||||
sourcePath
|
||||
.GetFiles(pattern, false /*recursive*/)
|
||||
.ForEach(file => FileCopy(sourcePath, targetPath, file));
|
||||
return sourcePath;
|
||||
}
|
||||
|
||||
private static void FileCopy(Path sourcePath, Path targetPath, Path sourceFile) {
|
||||
var targetFile = targetPath.Combine(sourceFile.GetRelativePath(sourcePath));
|
||||
targetFile.Parent.CreateDirectory();
|
||||
File.Copy(sourceFile, targetFile, true /*overwrite*/);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,117 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Orchard.Specs.Util {
|
||||
public class PathUtil {
|
||||
private readonly string _path;
|
||||
|
||||
public PathUtil(string path) {
|
||||
_path = path;
|
||||
}
|
||||
|
||||
public static PathUtil GetTempFile() {
|
||||
return new PathUtil(Path.GetTempFileName());
|
||||
}
|
||||
|
||||
public static PathUtil GetTempFolder() {
|
||||
var path = GetTempFile().DeleteFile().CreateDirectory();
|
||||
return path;
|
||||
}
|
||||
|
||||
public static PathUtil BaseDirectory {
|
||||
get { return new PathUtil(AppDomain.CurrentDomain.BaseDirectory); }
|
||||
}
|
||||
|
||||
public PathUtil FullPath {
|
||||
get { return new PathUtil(Path.GetFullPath(_path)); }
|
||||
}
|
||||
|
||||
public PathUtil CreateDirectory() {
|
||||
Directory.CreateDirectory(_path);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PathUtil DeleteFile() {
|
||||
File.Delete(_path);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PathUtil Combine(PathUtil path) {
|
||||
return Combine(path.ToString());
|
||||
}
|
||||
public PathUtil Combine(string path) {
|
||||
return new PathUtil(Path.Combine(_path, path));
|
||||
}
|
||||
|
||||
public IEnumerable<PathUtil> GetFiles(string pattern) {
|
||||
return GetFiles(pattern, SearchOption.TopDirectoryOnly);
|
||||
}
|
||||
|
||||
public IEnumerable<PathUtil> GetFiles(SearchOption searchOptions) {
|
||||
return GetFiles("*", searchOptions);
|
||||
}
|
||||
|
||||
public IEnumerable<PathUtil> GetFiles(string pattern, SearchOption searchOptions) {
|
||||
return Directory.GetFiles(_path, pattern, searchOptions).Select(sz => new PathUtil(sz));
|
||||
}
|
||||
|
||||
public void CopyAll(string pattern, PathUtil target) {
|
||||
CopyAll(pattern, SearchOption.TopDirectoryOnly, target);
|
||||
}
|
||||
|
||||
public void CopyAll(SearchOption searchOptions, PathUtil target) {
|
||||
CopyAll("*", searchOptions, target);
|
||||
}
|
||||
|
||||
public void CopyAll(string pattern, SearchOption searchOptions, PathUtil target) {
|
||||
foreach (var file in GetFiles(pattern, searchOptions)) {
|
||||
var targetFile = target.Combine(file.GetRelativePath(this));
|
||||
targetFile.Parent.CreateDirectory();
|
||||
file.Copy(targetFile);
|
||||
}
|
||||
}
|
||||
|
||||
public override int GetHashCode() {
|
||||
return (_path ?? string.Empty).GetHashCode();
|
||||
}
|
||||
public override bool Equals(object obj) {
|
||||
if (obj == null || obj.GetType() != GetType())
|
||||
return false;
|
||||
return ((PathUtil)obj)._path == _path;
|
||||
}
|
||||
public override string ToString() {
|
||||
return _path;
|
||||
}
|
||||
|
||||
public PathUtil GetRelativePath(PathUtil basePath) {
|
||||
if (this.Equals(basePath))
|
||||
return new PathUtil(".");
|
||||
|
||||
if (Parent.Equals(basePath))
|
||||
return FileName;
|
||||
|
||||
return Parent.GetRelativePath(basePath).Combine(FileName);
|
||||
}
|
||||
|
||||
|
||||
public PathUtil Copy(PathUtil target) {
|
||||
File.Copy(_path, target._path, true);
|
||||
return target;
|
||||
}
|
||||
|
||||
public PathUtil Parent {
|
||||
get { return new PathUtil(Path.GetDirectoryName(_path)); }
|
||||
}
|
||||
|
||||
public PathUtil FileName {
|
||||
get { return new PathUtil(Path.GetFileName(_path)); }
|
||||
}
|
||||
|
||||
public bool DirectoryExists {
|
||||
get { return Directory.Exists(_path); }
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user