From a86db3257742bdfcaaced9cf9e79a5b39b3d476d Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Tue, 23 Nov 2010 13:52:59 -0800 Subject: [PATCH] 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 --- src/Orchard.Specs/Bindings/WebAppHosting.cs | 6 ++-- .../Hosting/ExtensionDeploymentOptions.cs | 9 +++++ src/Orchard.Specs/Hosting/WebHost.cs | 34 ++++++++++++++----- src/Orchard.Specs/Util/PathExtensions.cs | 10 +++++- 4 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs diff --git a/src/Orchard.Specs/Bindings/WebAppHosting.cs b/src/Orchard.Specs/Bindings/WebAppHosting.cs index cb5637d63..26cc03dc1 100644 --- a/src/Orchard.Specs/Bindings/WebAppHosting.cs +++ b/src/Orchard.Specs/Bindings/WebAppHosting.cs @@ -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")] diff --git a/src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs b/src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs new file mode 100644 index 000000000..09ef7bcd3 --- /dev/null +++ b/src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs @@ -0,0 +1,9 @@ +using System; + +namespace Orchard.Specs.Hosting { + [Flags] + public enum ExtensionDeploymentOptions { + CompiledAssembly = 0x01, + SourceCode = 0x02, + } +} \ No newline at end of file diff --git a/src/Orchard.Specs/Hosting/WebHost.cs b/src/Orchard.Specs/Hosting/WebHost.cs index 220d2f2c7..a601b83c3 100644 --- a/src/Orchard.Specs/Hosting/WebHost.cs +++ b/src/Orchard.Specs/Hosting/WebHost.cs @@ -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; } diff --git a/src/Orchard.Specs/Util/PathExtensions.cs b/src/Orchard.Specs/Util/PathExtensions.cs index df369adfd..92222a075 100644 --- a/src/Orchard.Specs/Util/PathExtensions.cs +++ b/src/Orchard.Specs/Util/PathExtensions.cs @@ -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 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();