public static void Execute() { Trace.TraceInformation("[DbToLocal]: Starting... {0}", DirInfo.FullName); using (var ctx = new DbFileContext()) { var dbFiles = ctx.DbFiles .Where(x => !x.IsHidden && !x.IsDirectory && x.Extension != ".dll") .ToList(); foreach (var dbFile in dbFiles) { WriteToDisk(dbFile, false); } } }
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"); } } } }
public EFDbLockFactory(DbFileContext db, Guid srcGuid, string dir) { _db = db; _srcGuid = srcGuid; internalLockPrefix = dir; }
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(); } } } }
private static Dictionary<string, byte[]> LoadFromDb() { var assemblies = new Dictionary<string, byte[]>(); using (var ctx = new DbFileContext()) { var files = ctx.DbFiles .Where(x => !x.IsHidden && !x.IsDirectory && x.IsBinary && x.Extension.Equals(".dll")) .ToList(); foreach (var s in files) { Trace.TraceInformation("[PluginLoader]: Found assembly from Database: {0}", s.VirtualPath); assemblies.Add(s.Name, s.Bytes); } } return assemblies; }
private static void ExecutePlugin(Assembly assembly, bool force = false) { if (!BootstrapperSection.Instance.PluginLoader.Enabled && !force) { return; } var plugins = assembly.GetExportedTypes().Where(x => typeof(IPlugin).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract); foreach (var plugin in plugins) { if (_executedPlugins.Contains(plugin.AssemblyQualifiedName)) { continue; } _executedPlugins.Add(plugin.AssemblyQualifiedName); Trace.TraceInformation("[PluginLoader]: Found implementation of IPlugin '{0}'", plugin.FullName); IPlugin instance = null; using (DisposableTimer.StartNew(plugin.FullName)) { try { instance = Activator.CreateInstance(plugin) as IPlugin; } catch (Exception ex) { Trace.TraceError("[PluginLoader]:Error activating instance of type '{0}'. {1}", plugin, ex.Message); } finally { if (instance != null) { //verifica se o plugin está instalado using (var ctx = new DbFileContext()) { var module = ctx.DbModules.FirstOrDefault( x => x.Name.Equals(instance.PluginName, StringComparison.OrdinalIgnoreCase)); if (module == null) { module = new DbModule(); module.Name = instance.PluginName; ctx.DbModules.Add(module); ctx.SaveChanges(); } if (!module.Status) { //instalar using (DisposableTimer.StartNew("Installing {0}".Fmt(module.Name))) { try { instance.Install(); module.Status = true; ctx.SaveChanges(); } catch (Exception ex) { Trace.TraceError(ex.ToString()); } } } } Trace.TraceInformation("[PluginLoader]: Trying to Execute Plugin: {0}", instance.PluginName); try { instance.Start(); Trace.TraceInformation("[PluginLoader]: SUCCESS Executing Plugin: {0}", instance.PluginName); } catch (Exception ex) { Trace.TraceError(ex.Message); } } } } } }
static void Loop() { Console.Clear(); Console.WriteLine("Root Folder is: {0}", _dirInfo.FullName); while (true) { Console.WriteLine("1: Db To Local"); Console.WriteLine("2: Local To Db"); var option = Console.ReadKey(); Console.WriteLine("Opção escolhida: {0} ", option.Key); Console.WriteLine(); bool ok = false; switch (option.Key) { case ConsoleKey.D1: { using (var ctx = new DbFileContext()) { try { var dbFiles = ctx.DbFiles .Where(x => !x.IsHidden && !x.IsDirectory) .ToList(); foreach (var dbFile in dbFiles) { WriteToDisk(dbFile, false); } ok = true; } catch (Exception ex) { Console.WriteLine(ex); Console.Beep(880, 500); Console.ReadLine(); } } break; } case ConsoleKey.D2: { using (var db = new DbFileContext()) { try { db.Database.ExecuteSqlCommand(@"EXEC sp_msforeachtable ""ALTER TABLE ? NOCHECK CONSTRAINT all"""); db.Database.ExecuteSqlCommand("DELETE FROM DbFiles"); db.Database.ExecuteSqlCommand(@"EXEC sp_executesql ""DBCC CHECKIDENT('DbFiles', RESEED, 0)"" "); WriteFilesToDatabase(db, new Uri(_dirInfo.FullName), _dirInfo, null); db.Database.ExecuteSqlCommand(@"EXEC sp_msforeachtable ""ALTER TABLE ? CHECK CONSTRAINT all"""); ok = true; } catch (Exception ex) { Console.WriteLine(ex); Console.Beep(880, 500); Console.ReadLine(); } break; } } default: { break; } } if (ok) { break; } } }
public static void WriteFilesToDatabase(DbFileContext ctx, Uri initialUri, DirectoryInfo root, int?id) { string virtualPath; string dirName; if (id == null) { virtualPath = "/"; dirName = null; } else { var currentUri = new Uri(root.FullName); var tempRelative = initialUri.MakeRelativeUri(currentUri).ToString(); var iof = tempRelative.IndexOf('/'); virtualPath = tempRelative.Substring(iof); dirName = root.Name; } foreach (var ignoredDirectory in IgnoredDirectories) { if (virtualPath.StartsWith(ignoredDirectory, StringComparison.OrdinalIgnoreCase)) { return; } } var dbFile = new DbFile { IsDirectory = true, Name = dirName, VirtualPath = virtualPath, ParentId = id }; ctx.DbFiles.Add(dbFile); ctx.SaveChanges(); foreach (var fi in root.EnumerateFiles()) { bool ignore = IgnoredExtensions.Any(ignoredExtension => fi.Extension.StartsWith(ignoredExtension)) || IgnoredFiles.Any(x => x.Equals(fi.Name, StringComparison.OrdinalIgnoreCase)); if (ignore) { continue; } Console.WriteLine(fi.FullName); var dbFileFolder = new DbFile { IsDirectory = false, Name = Path.GetFileNameWithoutExtension(fi.Name), Extension = fi.Extension, VirtualPath = Path.Combine(virtualPath, fi.Name).Replace('\\', '/'), ParentId = dbFile.Id, }; if (IsTextFile(fi.Extension)) { var text = File.ReadAllText(fi.FullName, Encoding.UTF8); dbFileFolder.Texto = text; } else { var bytes = File.ReadAllBytes(fi.FullName); dbFileFolder.Bytes = bytes; dbFileFolder.IsBinary = true; } ctx.DbFiles.Add(dbFileFolder); ctx.SaveChanges(); } foreach (var di in root.EnumerateDirectories()) { WriteFilesToDatabase(ctx, initialUri, di, dbFile.Id); } }