Example #1
0
        public static void PreStart()
        {
            using (DisposableTimer.StartNew("PRE_START"))
            {
                var cfg = BootstrapperSection.Initialize();

                //cria um text logger somente para o startup
                //remove no post start
                try
                {
                    _traceFileName = HostingEnvironment.MapPath(cfg.TraceOutput);
                    if (File.Exists(_traceFileName))
                    {
                        File.Delete(_traceFileName);
                    }

                    var listener = new TextWriterTraceListener(_traceFileName, "StartupListener");

                    Trace.Listeners.Add(listener);
                }
                catch { }


                var executingAssembly = Assembly.GetExecutingAssembly();
                Trace.TraceInformation("Entry Assembly: {0}", executingAssembly.GetName().Name);


                if (cfg.HttpModules.Trace.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(TracerHttpModule));
                }

                if (cfg.StopMonitoring)
                {
                    HttpInternals.StopFileMonitoring();
                }

                if (cfg.HttpModules.CustomError.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(CustomErrorHttpModule));
                }

                if (cfg.HttpModules.WhiteSpace.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(WhitespaceModule));
                }

                using (DisposableTimer.StartNew("DbFileContext"))
                {
                    DbFileContext.Initialize();
                }

                if (cfg.PluginLoader.Enabled)
                {
                    using (DisposableTimer.StartNew("PluginLoader"))
                    {
                        PluginLoaderEntryPoint.Initialize();
                    }
                }

                if (cfg.VirtualPathProviders.SubFolderVpp.Enabled)
                {
                    SubfolderVpp.SelfRegister();
                }

                if (cfg.DumpToLocal.Enabled)
                {
                    using (DisposableTimer.StartNew("DumpToLocal"))
                    {
                        DbToLocal.Execute();
                    }
                }

                //todo: Dependency Injection
                if (cfg.VirtualPathProviders.DbFileSystemVpp.Enabled)
                {
                    var customvpp = new CustomVirtualPathProvider()
                                    .AddImpl(new CachedDbServiceFileSystemProvider(new DefaultDbService(), new WebCacheWrapper()));
                    HostingEnvironment.RegisterVirtualPathProvider(customvpp);
                }

                KompilerEntryPoint.AddReferences(
                    typeof(Controller),
                    typeof(WebPageRenderingBase),
                    typeof(WebCacheWrapper),
                    typeof(ViewRenderer),
                    typeof(DbToLocal),
                    typeof(ErrorModel));

                if (cfg.Kompiler.Enabled)
                {
                    using (DisposableTimer.StartNew("Kompiler"))
                    {
                        KompilerEntryPoint.Execute();
                    }
                }

                if (cfg.InsertRoutes)
                {
                    var routes = RouteTable.Routes;

                    routes.RouteExistingFiles  = false;
                    routes.LowercaseUrls       = true;
                    routes.AppendTrailingSlash = true;

                    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
                    routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(css|js|txt|png|gif|jpg|jpeg|bmp)(/.*)?" });

                    routes.IgnoreRoute("Content/{*pathInfo}");
                    routes.IgnoreRoute("Scripts/{*pathInfo}");
                    routes.IgnoreRoute("Bundles/{*pathInfo}");

                    //routes.MapRoute("MvcLib", "{controller}/{action}", new string[] { "" });


                    if (cfg.TraceOutput.IsNotNullOrWhiteSpace())
                    {
                        //routes.MapHttpHandler<WebPagesRouteHandler>("~/dump.cshtml");
                    }
                }
            }
        }
Example #2
0
        public static void Execute()
        {
            if (_initialized)
            {
                throw new InvalidOperationException("Compilador só pode ser executado no Pre-Start!");
            }

            _initialized = true;

            //plugin loader
            PluginLoaderEntryPoint.Initialize();

            if (BootstrapperSection.Instance.Kompiler.ForceRecompilation)
            {
                //se forçar a recompilação, remove o assembly existente.
                KompilerDbService.RemoveExistingCompiledAssemblyFromDb();
            }

            AddReferences(PluginStorage.GetAssemblies().ToArray());

            byte[] buffer = new byte[0];
            string msg;

            try
            {
                //todo: usar depdendency injection
                IKompiler kompiler;

                if (BootstrapperSection.Instance.Kompiler.Roslyn)
                {
                    kompiler = new RoslynKompiler();
                }
                else
                {
                    kompiler = new CodeDomWrapper();
                }

                if (BootstrapperSection.Instance.Kompiler.LoadFromDb)
                {
                    Trace.TraceInformation("Compiling from DB...");
                    var source = KompilerDbService.LoadSourceCodeFromDb();
                    msg = kompiler.CompileFromSource(source, out buffer);
                }
                else
                {
                    var localRootFolder = BootstrapperSection.Instance.DumpToLocal.Folder;
                    Trace.TraceInformation("Compiling from Local File System: {0}", localRootFolder);
                    msg = kompiler.CompileFromFolder(localRootFolder, out buffer);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                Trace.TraceInformation("Erro durante a compilação do projeto no banco de dados. \r\n" + ex.Message);
            }

            if (string.IsNullOrWhiteSpace(msg) && buffer.Length > 0)
            {
                Trace.TraceInformation("[Kompiler]: DB Compilation Result: SUCCESS");

                if (!BootstrapperSection.Instance.Kompiler.ForceRecompilation)
                {
                    //só salva no banco se compilação forçada for False
                    KompilerDbService.SaveCompiledCustomAssembly(buffer);
                }

                PluginLoaderEntryPoint.SaveAndLoadAssembly(CompiledAssemblyName + ".dll", buffer);
            }
            else
            {
                Trace.TraceInformation("[Kompiler]: DB Compilation Result: Bytes:{0}, Msg:{1}",
                                       buffer.Length, msg);
            }
        }
Example #3
0
        public static void PreStart()
        {
            var cfg = BootstrapperSection.Initialize();

            //cria um text logger somente para o startup
            //remove no post start
            try
            {
                _traceFileName = HostingEnvironment.MapPath(cfg.TraceOutput);
                if (!string.IsNullOrWhiteSpace(_traceFileName))
                {
                    if (File.Exists(_traceFileName))
                    {
                        try
                        {
                            File.Delete(_traceFileName);
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError(ex.ToString());
                        }
                    }

                    var listener = new TextWriterTraceListener(_traceFileName, "StartupListener");

                    Trace.Listeners.Add(listener);

                    Trace.TraceInformation("[Bootstrapper]: StartupLog added: {0}", listener);
                }
            }
            catch
            {
            }

            using (DisposableTimer.StartNew("Frankstein PRE_START"))
            {
                var executingAssembly = Assembly.GetExecutingAssembly();
                Trace.TraceInformation("[Bootstrapper]:Entry Assembly: {0}", executingAssembly.GetName().Name);

                DynamicModuleUtility.RegisterModule(typeof(FranksteinHttpModule));

                Trace.TraceInformation("[Bootstrapper]:cfg.HttpModules.Trace.Enabled = {0}", cfg.HttpModules.Trace.Enabled);
                if (cfg.HttpModules.Trace.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(TracerHttpModule));
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.HttpModules.TransactionScope.Enabled = {0}", cfg.HttpModules.TransactionScope.Enabled);
                if (cfg.HttpModules.TransactionScope.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(TransactionScopeHttpModule));
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.StopMonitoring = {0}", cfg.StopMonitoring);
                if (cfg.StopMonitoring)
                {
                    HttpInternals.StopFileMonitoring();
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.HttpModules.CustomError.Enabled = {0}", cfg.HttpModules.CustomError.Enabled);
                if (cfg.HttpModules.CustomError.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(CustomErrorHttpModule));
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.HttpModules.WhiteSpace.Enabled = {0}", cfg.HttpModules.WhiteSpace.Enabled);
                if (cfg.HttpModules.WhiteSpace.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(WhitespaceModule));
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.HttpModules.PathRewriter.Enabled = {0}", cfg.HttpModules.PathRewriter.Enabled);
                if (cfg.HttpModules.PathRewriter.Enabled)
                {
                    DynamicModuleUtility.RegisterModule(typeof(PathRewriterHttpModule));
                }

                using (DisposableTimer.StartNew("DbFileContext"))
                {
                    DbFileContext.Initialize();
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.PluginLoader.Enabled = {0}", cfg.PluginLoader.Enabled);
                if (cfg.PluginLoader.Enabled)
                {
                    using (DisposableTimer.StartNew("PluginLoader"))
                    {
                        PluginLoaderEntryPoint.Initialize();
                    }
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.VirtualPathProviders.SubFolderVpp.Enabled = {0}", cfg.VirtualPathProviders.SubFolderVpp.Enabled);
                if (cfg.VirtualPathProviders.SubFolderVpp.Enabled)
                {
                    SubfolderVpp.SelfRegister();
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.VirtualPathProviders.DbFileSystemVpp.Enabled = {0}", cfg.VirtualPathProviders.DbFileSystemVpp.Enabled);
                if (cfg.VirtualPathProviders.DbFileSystemVpp.Enabled)
                {
                    var customvpp = new CustomVirtualPathProvider()
                                    .AddImpl(new CachedDbServiceFileSystemProvider(new DefaultDbService(), new WebCacheWrapper()));
                    HostingEnvironment.RegisterVirtualPathProvider(customvpp);
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.DumpToLocal.Enabled = {0}", cfg.DumpToLocal.Enabled);
                if (cfg.DumpToLocal.Enabled)
                {
                    using (DisposableTimer.StartNew("DumpToLocal"))
                    {
                        DbToLocal.Execute();
                    }
                }

                Trace.TraceInformation("[Bootstrapper]:cfg.Kompiler.Enabled = {0}", cfg.Kompiler.Enabled);
                var bin = new DirectoryInfo(HttpRuntime.BinDirectory);

                foreach (var fileInfo in bin.EnumerateFiles("*.dll", SearchOption.AllDirectories))
                {
                    if (cfg.Verbose)
                    {
                        Trace.Indent();
                        Trace.TraceInformation("[BinFolder]: {0}", fileInfo.FullName);
                        Trace.Unindent();
                    }
                    KompilerEntryPoint.AddReferences(fileInfo.FullName);
                }

                foreach (var keyValuePair in CustomConfig.RefreshConfig())
                {
                    KompilerEntryPoint.AddReferences(keyValuePair);
                }


                if (cfg.Kompiler.Enabled)
                {
                    using (DisposableTimer.StartNew("Kompiler"))
                    {
                        KompilerEntryPoint.Execute();
                    }
                }
            }
        }