Esempio n. 1
0
        public void Execute()
        {
            try
            {
#if DEBUG
                if (!Debugger.IsAttached)
                {
                    Debugger.Launch();

                    FodyEnvironment.LogDebug   = CreateLoggingCallback(LogDebug);
                    FodyEnvironment.LogInfo    = CreateLoggingCallback(LogInfo);
                    FodyEnvironment.LogWarning = CreateLoggingCallback(LogWarning);
                    FodyEnvironment.LogError   = CreateLoggingCallback(LogError);
                }
#endif

                // Clear cache because static members will be re-used over multiple builds over multiple systems
                CacheHelper.ClearAllCaches();

                var configuration = new Configuration(Config);

                InitializeEnvironment();

                LogInfo($"Catel.Fody v{GetType().Assembly.GetName().Version}");

                // 1st step: set up the basics
                var msCoreReferenceFinder = new MsCoreReferenceFinder(this, ModuleDefinition.AssemblyResolver);
                msCoreReferenceFinder.Execute();

                // Validate if Catel.Core is referenced
                var isRunningAgainstCatelCore = false;
                var catelCoreReference        = AssemblyResolver.Resolve("Catel.Core");
                if (catelCoreReference == null)
                {
                    if (!ModuleDefinition.Name.StartsWith("Catel.Core"))
                    {
                        LogWarning("No reference to Catel.Core found, this weaver is useless without referencing Catel");
                        return;
                    }

                    isRunningAgainstCatelCore = true;

                    LogInfo("No reference to Catel.Core found, but continuing because this is running against Catel.Core itself");
                }

                // Note: nested types not supported because we only list actual types (thus not nested)
                var types = ModuleDefinition.GetTypes().Where(x => x.IsClass && x.BaseType != null).ToList();

                var typeNodeBuilder = new CatelTypeNodeBuilder(types);
                typeNodeBuilder.Execute();

                // Remove any code generated types from the list of types to process
                var codeGenTypeCleaner = new CodeGenTypeCleaner(typeNodeBuilder);
                codeGenTypeCleaner.Execute();

                // 2nd step: Auto property weaving
                if (!isRunningAgainstCatelCore && configuration.WeaveProperties)
                {
                    FodyEnvironment.LogInfo("Weaving properties");

                    var propertyWeaverService = new AutoPropertiesWeaverService(configuration, typeNodeBuilder, msCoreReferenceFinder);
                    propertyWeaverService.Execute();
                }
                else
                {
                    FodyEnvironment.LogInfo("Weaving properties is disabled");
                }

                // 3rd step: Exposed properties weaving
                if (!isRunningAgainstCatelCore && configuration.WeaveExposedProperties)
                {
                    FodyEnvironment.LogInfo("Weaving exposed properties");

                    var exposedPropertiesWeaverService = new ExposedPropertiesWeaverService(typeNodeBuilder, msCoreReferenceFinder);
                    exposedPropertiesWeaverService.Execute();
                }
                else
                {
                    FodyEnvironment.LogInfo("Weaving exposed properties is disabled");
                }

                // 4th step: Argument weaving
                if (!isRunningAgainstCatelCore && configuration.WeaveArguments)
                {
                    FodyEnvironment.LogInfo("Weaving arguments");

                    var argumentWeaverService = new ArgumentWeaverService(types, msCoreReferenceFinder);
                    argumentWeaverService.Execute();
                }
                else
                {
                    FodyEnvironment.LogInfo("Weaving arguments is disabled");
                }

                // 5th step: Logging weaving (we will run this against Catel.Core)
                if (configuration.WeaveLogging)
                {
                    FodyEnvironment.LogInfo("Weaving logging");

                    var loggingWeaver = new LoggingWeaverService(types);
                    loggingWeaver.Execute();
                }
                else
                {
                    FodyEnvironment.LogInfo("Weaving logging is disabled");
                }

                // 6th step: Xml schema weaving
                if (!isRunningAgainstCatelCore && configuration.GenerateXmlSchemas)
                {
                    FodyEnvironment.LogInfo("Weaving xml schemas");

                    var xmlSchemasWeaverService = new XmlSchemasWeaverService(msCoreReferenceFinder, typeNodeBuilder);
                    xmlSchemasWeaverService.Execute();
                }
                else
                {
                    FodyEnvironment.LogInfo("Weaving xml schemas is disabled");
                }

                // Last step: clean up
                var referenceCleaner = new ReferenceCleaner(this);
                referenceCleaner.Execute();
            }
            catch (Exception ex)
            {
                LogError(ex.Message);

#if DEBUG
                if (!Debugger.IsAttached)
                {
                    Debugger.Launch();
                }
#endif
            }
        }