public static void ExecuteInjection(ResolvedInjectionConfiguration resolvedInjectionConfiguration)
        {
            MethodInlineInjector assemblyMethodInjector = new MethodInlineInjector(resolvedInjectionConfiguration);

            assemblyMethodInjector.Inject();
        }
Ejemplo n.º 2
0
        private void ExecuteInjection()
        {
#if !DEBUG
            try {
#endif
            string serializedInjectorConfiguration;
            if (_commandLineOptions.ReadConfigurationFromStandardInput)
            {
                serializedInjectorConfiguration = Console.In.ReadToEnd();
            }
            else
            {
                if (!File.Exists(_commandLineOptions.ConfigurationFilePath))
                {
                    throw new MethodInlineInjectorException(
                              "Injection configuration file doesn't exists",
                              new FileNotFoundException(_commandLineOptions.ConfigurationFilePath)
                              );
                }

                serializedInjectorConfiguration = File.ReadAllText(_commandLineOptions.ConfigurationFilePath);
            }

            Log.Debug($"Input configuration:{Environment.NewLine}{serializedInjectorConfiguration}");

            if (String.IsNullOrWhiteSpace(serializedInjectorConfiguration))
            {
                throw new MethodInlineInjectorException("Injector configuration is empty");
            }

            Log.Info("Parsing configuration file");
            ValidateConfiguration(serializedInjectorConfiguration);
            InjectionConfiguration injectionConfiguration =
                SimpleXmlSerializationUtility.XmlDeserializeFromString <InjectionConfiguration>(serializedInjectorConfiguration);

            Log.Info("Resolving configuration file");
            ResolvedInjectionConfiguration resolvedInjectionConfiguration =
                ResolvedInjectionConfigurationLoader.LoadFromInjectionConfiguration(injectionConfiguration);

            Log.Info("Starting injection");
            MethodInlineInjector assemblyMethodInjector = new MethodInlineInjector(resolvedInjectionConfiguration);

            int injectedMethodCount = 0;
            assemblyMethodInjector.BeforeMethodInjected += tuple => injectedMethodCount++;
            assemblyMethodInjector.Inject();

            Log.InfoFormat("Injected {0} methods", injectedMethodCount);

            Log.Info("Writing modified assemblies");
            foreach (ResolvedInjecteeAssembly injecteeAssembly in resolvedInjectionConfiguration.InjecteeAssemblies)
            {
                string path = injecteeAssembly.AssemblyDefinition.MainModule.FullyQualifiedName;
                Log.DebugFormat("Writing assembly {0} to '{1}'", injecteeAssembly.AssemblyDefinition.FullName, path);
                injecteeAssembly.AssemblyDefinition.Write(path);
            }
#if !DEBUG
        }

        catch (MethodInlineInjectorException e) {
            string message = "Fatal error: " + e;
            if (e.InnerException != null)
            {
                message += Environment.NewLine;
                message += "Error details: ";
                message += e.InnerException;
            }
            Log.Fatal(message);
            Environment.ExitCode = 1;
        }
#endif
        }