/Rhino.Etl.Cmd/RhinoEtlSetup.cs

http://github.com/ayende/rhino-etl · C# · 115 lines · 100 code · 11 blank · 4 comment · 4 complexity · 7113891d364c4ac3c049a078348b6eb7 MD5 · raw file

  1. namespace Rhino.Etl.Cmd
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using Boo.Lang.Useful.CommandLine;
  7. using Core;
  8. using log4net;
  9. using log4net.Config;
  10. using Dsl;
  11. public class RhinoEtlSetup
  12. {
  13. private readonly ILog log = LogManager.GetLogger(typeof(RhinoEtlSetup));
  14. private static void Main(string[] args)
  15. {
  16. try
  17. {
  18. RhinoEtlCommandLineOptions options = new RhinoEtlCommandLineOptions();
  19. try
  20. {
  21. options.Parse(args);
  22. }
  23. catch (CommandLineException e)
  24. {
  25. Console.WriteLine(e.Message);
  26. options.PrintOptions();
  27. return;
  28. }
  29. new RhinoEtlSetup().Execute(options);
  30. }
  31. catch (Exception e)
  32. {
  33. Console.WriteLine(e);
  34. }
  35. }
  36. private void Execute(RhinoEtlCommandLineOptions options)
  37. {
  38. RhinoEtlRunner.SetupLogging(options.Verbose);
  39. log.DebugFormat("Starting with {0}", options.File);
  40. try
  41. {
  42. string ext = Path.GetExtension(options.File).ToLower();
  43. Type processType;
  44. if(ext==".exe" || ext==".dll")
  45. {
  46. processType = GetFromAssembly(options);
  47. }
  48. else
  49. {
  50. processType = GetFromDslFile(options.File);
  51. }
  52. ExecuteProcessInSeparateAppDomain(processType, options);
  53. }
  54. catch (Exception e)
  55. {
  56. log.Debug(e);
  57. log.Error(e.Message);
  58. }
  59. }
  60. private static Type GetFromAssembly(RhinoEtlCommandLineOptions options)
  61. {
  62. FileInfo _assemblyInfo = new FileInfo(options.File);
  63. Assembly asm = Assembly.LoadFile(_assemblyInfo.FullName);
  64. //Assembly asm = Assembly.Load(options.File);
  65. foreach (Type type in asm.GetTypes())
  66. {
  67. if(typeof(EtlProcess).IsAssignableFrom(type) && type.Name.Equals(options.Process, StringComparison.InvariantCultureIgnoreCase))
  68. return type;
  69. }
  70. throw new InvalidOperationException("Could not find type nameed '" + options.Process + "' on: " +
  71. options.File);
  72. }
  73. private static Type GetFromDslFile(string filename)
  74. {
  75. Type processType;
  76. EtlProcess process = EtlDslEngine.Factory.Create<EtlProcess>(filename);
  77. processType = process.GetType();
  78. return processType;
  79. }
  80. private void ExecuteProcessInSeparateAppDomain(Type processType, RhinoEtlCommandLineOptions options)
  81. {
  82. try
  83. {
  84. FileInfo _assemblyInfo = new FileInfo(options.File);
  85. //we have to run the code in another appdomain, because we want to
  86. //setup our own app.config for it
  87. AppDomainSetup appDomainSetup = new AppDomainSetup();
  88. //setting this to the current executing directory because that's where the dsl's dll gets created.
  89. appDomainSetup.ApplicationBase = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
  90. appDomainSetup.ConfigurationFile = string.IsNullOrEmpty(options.Configuration) ? options.File + ".config" : options.Configuration;
  91. AppDomain appDomain = AppDomain.CreateDomain("etl.domain", null, appDomainSetup);
  92. appDomain.Load(processType.Assembly.GetName());
  93. RhinoEtlRunner runner = (RhinoEtlRunner)appDomain.CreateInstanceAndUnwrap(typeof (RhinoEtlRunner).Assembly.GetName().FullName,
  94. typeof (RhinoEtlRunner).FullName);
  95. runner.Start(processType, options.Verbose);
  96. }
  97. catch (Exception e)
  98. {
  99. log.Debug(e);
  100. log.Error(e.Message);
  101. }
  102. }
  103. }
  104. }