<commit_before>using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Web;
using System.Web.Hosting;
using Orchard.Specs.Util;
using Path = Bleroy.FluentPath.Path;

namespace Orchard.Specs.Hosting {
    public class WebHost {
        private readonly Path _orchardTemp;
        private WebHostAgent _webHostAgent;
        private Path _tempSite;
        private Path _orchardWebPath;
        private Path _codeGenDir;
        private IEnumerable<string> _knownModules;
        private IEnumerable<string> _knownThemes;
        private IEnumerable<string> _knownBinAssemblies;

        public WebHost(Path orchardTemp) {
            _orchardTemp = orchardTemp;
        }

        public void Initialize(string templateName, string virtualDirectory, DynamicCompilationOption dynamicCompilationOption) {
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            var baseDir = Path.Get(AppDomain.CurrentDomain.BaseDirectory);

            _tempSite = _orchardTemp.Combine(System.IO.Path.GetRandomFileName());
            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.
            
            _orchardWebPath = baseDir;

            while (!_orchardWebPath.Combine("Orchard.proj").Exists && _orchardWebPath.Parent != null) {
                _orchardWebPath = _orchardWebPath.Parent;
            }

            _orchardWebPath = _orchardWebPath.Combine("src").Combine("Orchard.Web");

            Log("Initialization of ASP.NET host for template web site \"{0}\":", templateName);
            Log(" Source location: \"{0}\"", _orchardWebPath);
            Log(" Temporary location: \"{0}\"", _tempSite);

            _knownModules = _orchardWebPath.Combine("Modules").Directories.Where(d => d.Combine("module.txt").Exists).Select(d => d.FileName).ToList();
            //foreach (var filename in _knownModules)
            //    Log("Available Module: \"{0}\"", filename);

            _knownThemes = _orchardWebPath.Combine("Themes").Directories.Where(d => d.Combine("theme.txt").Exists).Select(d => d.FileName).ToList();
            //foreach (var filename in _knownThemes)
            //    Log("Available Theme: \"{0}\"", filename);

            _knownBinAssemblies = _orchardWebPath.Combine("bin").GetFiles("*.dll").Select(f => f.FileNameWithoutExtension);
            //foreach (var filename in _knownBinAssemblies)
            //    Log("Assembly in ~/bin: \"{0}\"", filename);

            Log("Copy files from template \"{0}\"", templateName);
            baseDir.Combine("Hosting").Combine(templateName)
                .DeepCopy(_tempSite);

            if (dynamicCompilationOption != DynamicCompilationOption.Enabled) {
                var sourceConfig = baseDir.Combine("Hosting").Combine("TemplateConfigs");
                var siteConfig = _tempSite.Combine("Config");
                switch (dynamicCompilationOption) {
                    case DynamicCompilationOption.Disabled:
                        File.Copy(sourceConfig.Combine("DisableDynamicCompilation.HostComponents.config"), siteConfig.Combine("HostComponents.config"));
                        break;
                    case DynamicCompilationOption.Force:
                        File.Copy(sourceConfig.Combine("ForceDynamicCompilation.HostComponents.config"), siteConfig.Combine("HostComponents.config"));
                        break;
                }
            }

            Log("Copy binaries of the \"Orchard.Web\" project");
            _orchardWebPath.Combine("bin")
                .ShallowCopy("*.dll", _tempSite.Combine("bin"))
                .ShallowCopy("*.pdb", _tempSite.Combine("bin"));

            Log("Copy SqlCe native binaries");
            if (_orchardWebPath.Combine("bin").Combine("x86").IsDirectory) {
                _orchardWebPath.Combine("bin").Combine("x86")
                    .DeepCopy("*.*", _tempSite.Combine("bin").Combine("x86"));
            }

            if (_orchardWebPath.Combine("bin").Combine("amd64").IsDirectory) {
                _orchardWebPath.Combine("bin").Combine("amd64")
                    .DeepCopy("*.*", _tempSite.Combine("bin").Combine("amd64"));
            }

            // Copy binaries of this project, so that remote execution of lambda
            // can be achieved through serialization to the ASP.NET appdomain
            // (see Execute(Action) method)
            Log("Copy Orchard.Specflow test project binaries");
            baseDir.ShallowCopy(
                path => IsSpecFlowTestAssembly(path) && !_tempSite.Combine("bin").Combine(path.FileName).Exists, 
                _tempSite.Combine("bin"));

            Log("Copy Orchard recipes");
            _orchardWebPath.Combine("Modules").Combine("Orchard.Setup").Combine("Recipes").DeepCopy("*.xml", _tempSite.Combine("Modules").Combine("Orchard.Setup").Combine("Recipes"));

            StartAspNetHost(virtualDirectory);

            Log("ASP.NET host initialization completed in {0} sec", stopwatch.Elapsed.TotalSeconds);
        }

        private void StartAspNetHost(string virtualDirectory) {
            Log("Starting up ASP.NET host");
            HostName = "localhost";
            PhysicalDirectory = _tempSite;
            VirtualDirectory = virtualDirectory;

            _webHostAgent = (WebHostAgent)ApplicationHost.CreateApplicationHost(typeof(WebHostAgent), VirtualDirectory, PhysicalDirectory);<commit_msg>Copying roslyn binaries before running Specflow tests<commit_after>using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Web;
using System.Web.Hosting;
using Orchard.Specs.Util;
using Path = Bleroy.FluentPath.Path;

namespace Orchard.Specs.Hosting {
    public class WebHost {
        private readonly Path _orchardTemp;
        private WebHostAgent _webHostAgent;
        private Path _tempSite;
        private Path _orchardWebPath;
        private Path _codeGenDir;
        private IEnumerable<string> _knownModules;
        private IEnumerable<string> _knownThemes;
        private IEnumerable<string> _knownBinAssemblies;

        public WebHost(Path orchardTemp) {
            _orchardTemp = orchardTemp;
        }

        public void Initialize(string templateName, string virtualDirectory, DynamicCompilationOption dynamicCompilationOption) {
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            var baseDir = Path.Get(AppDomain.CurrentDomain.BaseDirectory);

            _tempSite = _orchardTemp.Combine(System.IO.Path.GetRandomFileName());
            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.
            
            _orchardWebPath = baseDir;

            while (!_orchardWebPath.Combine("Orchard.proj").Exists && _orchardWebPath.Parent != null) {
                _orchardWebPath = _orchardWebPath.Parent;
            }

            _orchardWebPath = _orchardWebPath.Combine("src").Combine("Orchard.Web");

            Log("Initialization of ASP.NET host for template web site \"{0}\":", templateName);
            Log(" Source location: \"{0}\"", _orchardWebPath);
            Log(" Temporary location: \"{0}\"", _tempSite);

            _knownModules = _orchardWebPath.Combine("Modules").Directories.Where(d => d.Combine("module.txt").Exists).Select(d => d.FileName).ToList();
            //foreach (var filename in _knownModules)
            //    Log("Available Module: \"{0}\"", filename);

            _knownThemes = _orchardWebPath.Combine("Themes").Directories.Where(d => d.Combine("theme.txt").Exists).Select(d => d.FileName).ToList();
            //foreach (var filename in _knownThemes)
            //    Log("Available Theme: \"{0}\"", filename);

            _knownBinAssemblies = _orchardWebPath.Combine("bin").GetFiles("*.dll").Select(f => f.FileNameWithoutExtension);
            //foreach (var filename in _knownBinAssemblies)
            //    Log("Assembly in ~/bin: \"{0}\"", filename);

            Log("Copy files from template \"{0}\"", templateName);
            baseDir.Combine("Hosting").Combine(templateName)
                .DeepCopy(_tempSite);

            if (dynamicCompilationOption != DynamicCompilationOption.Enabled) {
                var sourceConfig = baseDir.Combine("Hosting").Combine("TemplateConfigs");
                var siteConfig = _tempSite.Combine("Config");
                switch (dynamicCompilationOption) {
                    case DynamicCompilationOption.Disabled:
                        File.Copy(sourceConfig.Combine("DisableDynamicCompilation.HostComponents.config"), siteConfig.Combine("HostComponents.config"));
                        break;
                    case DynamicCompilationOption.Force:
                        File.Copy(sourceConfig.Combine("ForceDynamicCompilation.HostComponents.config"), siteConfig.Combine("HostComponents.config"));
                        break;
                }
            }

            Log("Copy binaries of the \"Orchard.Web\" project");
            _orchardWebPath.Combine("bin")
                .ShallowCopy("*.dll", _tempSite.Combine("bin"))
                .ShallowCopy("*.pdb", _tempSite.Combine("bin"));

            Log("Copy SqlCe native binaries");
            if (_orchardWebPath.Combine("bin").Combine("x86").IsDirectory) {
                _orchardWebPath.Combine("bin").Combine("x86")
                    .DeepCopy("*.*", _tempSite.Combine("bin").Combine("x86"));
            }

            if (_orchardWebPath.Combine("bin").Combine("amd64").IsDirectory) {
                _orchardWebPath.Combine("bin").Combine("amd64")
                    .DeepCopy("*.*", _tempSite.Combine("bin").Combine("amd64"));
            }

            Log("Copy roslyn binaries");
            if (_orchardWebPath.Combine("bin").Combine("roslyn").IsDirectory) {
                _orchardWebPath.Combine("bin").Combine("roslyn")
                    .DeepCopy("*.*", _tempSite.Combine("bin").Combine("roslyn"));
            }

            // Copy binaries of this project, so that remote execution of lambda
            // can be achieved through serialization to the ASP.NET appdomain
            // (see Execute(Action) method)
            Log("Copy Orchard.Specflow test project binaries");
            baseDir.ShallowCopy(
                path => IsSpecFlowTestAssembly(path) && !_tempSite.Combine("bin").Combine(path.FileName).Exists, 
                _tempSite.Combine("bin"));

            Log("Copy Orchard recipes");
            _orchardWebPath.Combine("Modules").Combine("Orchard.Setup").Combine("Recipes").DeepCopy("*.xml", _tempSite.Combine("Modules").Combine("Orchard.Setup").Combine("Recipes"));

            StartAspNetHost(virtualDirectory);

            Log("ASP.NET host initialization completed in {0} sec", stopwatch.Elapsed.TotalSeconds);
        }

        private void StartAspNetHost(string virtualDirectory) {
            Log("Starting up ASP.NET host");
            HostName = "localhost";
            PhysicalDirectory = _tempSite;
            VirtualDirectory = virtualDirectory;

            _webHostAgent = (WebHostAgent)ApplicationHost.CreateApplicationHost(typeof(WebHostAgent), VirtualDirectory, PhysicalDirectory);
{'fraction_non_alphanumeric': 0.12096843185901275, 'fraction_numerical': 0.0034220207032252545, 'mean_word_length': 2.4412717103326464, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4107044', 'n_tokens_mistral': 3212, 'n_tokens_neox': 3019, 'n_words': 591}