Refactoring CopyExtension to support source file copy

For SpecFlow tests, we used to support deploying extensions (modules
and themes) only by deploying their compiled assemblies, and never
copying their source files.

We want to add SpecFlow test for dynamic compilation, so this refactoring
was a necessary step to get there.

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-11-23 13:52:59 -08:00
parent 35c2a6b6ac
commit a86db32577
4 changed files with 47 additions and 12 deletions

View File

@@ -105,17 +105,17 @@ namespace Orchard.Specs.Bindings {
[Given(@"I have module ""(.*)""")]
public void GivenIHaveModule(string moduleName) {
Host.CopyExtension("Modules", moduleName);
Host.CopyExtension("Modules", moduleName, ExtensionDeploymentOptions.CompiledAssembly);
}
[Given(@"I have theme ""(.*)""")]
public void GivenIHaveTheme(string themeName) {
Host.CopyExtension("Themes", themeName);
Host.CopyExtension("Themes", themeName, ExtensionDeploymentOptions.CompiledAssembly);
}
[Given(@"I have core ""(.*)""")]
public void GivenIHaveCore(string moduleName) {
Host.CopyExtension("Core", moduleName);
Host.CopyExtension("Core", moduleName, ExtensionDeploymentOptions.CompiledAssembly);
}
[Given(@"I have a clean site with")]

View File

@@ -0,0 +1,9 @@
using System;
namespace Orchard.Specs.Hosting {
[Flags]
public enum ExtensionDeploymentOptions {
CompiledAssembly = 0x01,
SourceCode = 0x02,
}
}

View File

@@ -19,7 +19,8 @@ namespace Orchard.Specs.Hosting {
var baseDir = Path.Get(AppDomain.CurrentDomain.BaseDirectory);
_tempSite = Path.Get(_orchardTemp).Combine(System.IO.Path.GetRandomFileName());
try { _tempSite.Delete(); } catch {}
try { _tempSite.Delete(); }
catch { }
// Trying the two known relative paths to the Orchard.Web directory.
// The second one is for the target "spec" in orchard.proj.
@@ -76,30 +77,47 @@ namespace Orchard.Specs.Hosting {
catch { }
}
public void CopyExtension(string extensionFolder, string extensionName) {
public void CopyExtension(string extensionFolder, string extensionName, ExtensionDeploymentOptions deploymentOptions) {
var sourceModule = _orchardWebPath.Combine(extensionFolder).Combine(extensionName);
var targetModule = _tempSite.Combine(extensionFolder).Combine(extensionName);
sourceModule.ShallowCopy("*.txt", targetModule);
sourceModule.ShallowCopy("*.info", targetModule);
//sourceModule.ShallowCopy("*.csproj", targetModule);
//sourceModule.DeepCopy("*.cs", targetModule);)
if ((deploymentOptions & ExtensionDeploymentOptions.SourceCode) == ExtensionDeploymentOptions.SourceCode) {
sourceModule.ShallowCopy("*.csproj", targetModule);
sourceModule.DeepCopy("*.cs", targetModule);
}
if (sourceModule.Combine("bin").IsDirectory) {
sourceModule.Combine("bin").ShallowCopy("*.dll", targetModule.Combine("bin"));
sourceModule.Combine("bin").ShallowCopy("*.exe", targetModule.Combine("bin"));
sourceModule.Combine("bin").ShallowCopy("*.pdb", targetModule.Combine("bin"));
sourceModule.Combine("bin").ShallowCopy(path => IsExtensionBinaryFile(path, extensionName, deploymentOptions), targetModule.Combine("bin"));
}
if (sourceModule.Combine("Views").IsDirectory)
sourceModule.Combine("Views").DeepCopy(targetModule.Combine("Views"));
}
private bool IsExtensionBinaryFile(Path path, string extensionName, ExtensionDeploymentOptions deploymentOptions) {
bool isValidExtension =
StringComparer.OrdinalIgnoreCase.Equals(path.Extension, ".exe") ||
StringComparer.OrdinalIgnoreCase.Equals(path.Extension, ".dll") ||
StringComparer.OrdinalIgnoreCase.Equals(path.Extension, ".pdb");
if (!isValidExtension)
return false;
bool isExtensionAssembly = StringComparer.OrdinalIgnoreCase.Equals(path.FileNameWithoutExtension, extensionName);
bool copyExtensionAssembly = (deploymentOptions & ExtensionDeploymentOptions.CompiledAssembly) == ExtensionDeploymentOptions.CompiledAssembly;
if (isExtensionAssembly && !copyExtensionAssembly)
return false;
return true;
}
public string HostName { get; set; }
public string PhysicalDirectory { get; private set; }
public string VirtualDirectory { get; private set; }
public string Cookies { get; set; }

View File

@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using Path = Bleroy.FluentPath.Path;
namespace Orchard.Specs.Util {
@@ -35,6 +36,13 @@ namespace Orchard.Specs.Util {
return sourcePath;
}
public static Path ShallowCopy(this Path sourcePath, Predicate<Path> predicatePath, Path targetPath) {
sourcePath
.GetFiles(predicatePath, 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();