Пример #1
0
        public void ValidateCompleteModules(bool suppressUnusedBindingsErrors)
        {
            foreach (var moduleGenerator in moduleGenerators) {
                if (!moduleGenerator.IsComplete) {
                    continue;
                }

                GraphVerifier graphVerifier;
                try
                {
                    var moduleBindings = ProcessCompleteModule(moduleGenerator, false);
                    if (moduleBindings == null)
                    {
                        // No bindings means that bindings could not be resolved and
                        // errors were reported.
                        continue;
                    }

                    graphVerifier = new GraphVerifier();
                    graphVerifier.DetectCircularDependencies(moduleBindings.Values, new Stack<Binding>());
                }
                catch (InvalidOperationException ex) {
                    errorReporter.LogError(ex.Message);
                    continue;
                }
                catch (ValidationException ex) {
                    errorReporter.LogError(ex.Message);
                    continue;
                }

                // XXX ben: Write graphviz file here.

                // TODO: This analysis is broken for entry points that are satisfied by [Provides] methods.
                if (!moduleGenerator.IsLibrary)
                {
                    try
                    {
                        var moduleBindings = ProcessCompleteModule(moduleGenerator, true);
                        graphVerifier.DetectUnusedBindings(moduleBindings.Values);
                    }
                    catch (InvalidOperationException ex)
                    {
                        if (suppressUnusedBindingsErrors)
                        {
                            errorReporter.LogWarning(ex.Message);
                        }
                        else
                        {
                            errorReporter.LogError(ex.Message);
                        }
                    }
                    catch (ValidationException ex)
                    {
                        errorReporter.LogError(ex.Message);
                    }
                }
            }
        }
Пример #2
0
        public void ValidateCompleteModules(bool suppressUnusedBindingsErrors, bool suppressGraphviz, string outputDirectory)
        {
            string graphvizDirectory = null;
            if (!suppressGraphviz)
            {
                graphvizDirectory = PrepareGraphvizDirectory(outputDirectory);
            }

            var invalidModules = new HashSet<ModuleGenerator>();
            foreach (var moduleGenerator in moduleGenerators)
            {
                if (!moduleGenerator.IsComplete)
                {
                    continue;
                }

                try
                {
                    var modules = new Dictionary<string, ModuleGenerator>();
                    GatherIncludedModules(moduleGenerator, modules, new Stack<string>());

                    var injectTypesByModule = new Dictionary<TypeReference, IList<ModuleGenerator>>(new TypeReferenceComparer());
                    foreach (var module in modules.Values)
                    {
                        foreach (var injectType in module.Injects)
                        {
                            IList<ModuleGenerator> providingModules;
                            if (!injectTypesByModule.TryGetValue(injectType, out providingModules))
                            {
                                injectTypesByModule[injectType] = providingModules = new List<ModuleGenerator>();
                            }

                            providingModules.Add(module);
                        }
                    }

                    foreach (var kvp in injectTypesByModule)
                    {
                        var type = kvp.Key;
                        var providingModules = kvp.Value;

                        if (providingModules.Count == 1)
                        {
                            continue;
                        }

                        var sb = new StringBuilder();
                        sb.Append("The type ")
                            .Append(type.FullName)
                            .AppendLine(" is provided multiple times in one complete module network:");

                        for (var i = 0; i < providingModules.Count; ++i)
                        {
                            sb.Append("  ")
                                .Append(i + 1)
                                .Append(": ")
                                .AppendLine(providingModules[i].ModuleType.FullName);
                        }

                        errorReporter.LogError(sb.ToString());
                        invalidModules.Add(moduleGenerator);
                    }
                }
                catch (ValidationException ex)
                {
                    errorReporter.LogError(ex.Message);
                    invalidModules.Add(moduleGenerator);
                }
            }

            if (invalidModules.Count > 0)
            {
                return;
            }

            loader = new CompilerLoader(injectBindings, lazyBindings, providerBindings, errorReporter);

            if (loader.HasError)
            {
                return;
            }

            foreach (var moduleGenerator in moduleGenerators)
            {
                if (!moduleGenerator.IsComplete)
                {
                    continue;
                }

                GraphVerifier graphVerifier;
                IDictionary<string, Binding> moduleBindings;

                try
                {
                    moduleBindings = ProcessCompleteModule(moduleGenerator, false);
                    if (moduleBindings == null)
                    {
                        // No bindings means that bindings could not be resolved and
                        // errors were reported.
                        continue;
                    }

                    graphVerifier = new GraphVerifier();
                    graphVerifier.DetectCircularDependencies(moduleBindings.Values, new Stack<Binding>());
                }
                catch (InvalidOperationException ex)
                {
                    errorReporter.LogError(ex.Message);
                    continue;
                }
                catch (ValidationException ex)
                {
                    errorReporter.LogError(ex.Message);
                    continue;
                }

                try
                {
                    if (!suppressGraphviz)
                    {
                        WriteModuleGraph(moduleGenerator, moduleBindings, graphvizDirectory);
                    }
                }
                catch (IOException ex)
                {
                    errorReporter.LogWarning("Graph visualization failed: " + ex.Message);
                }
                catch (Exception ex)
                {
                    errorReporter.LogWarning("Graph visualization failed, please report this as a bug: " + Environment.NewLine + ex);
                }

                // TODO: This analysis is broken for entry points that are satisfied by [Provides] methods.
                if (!moduleGenerator.IsLibrary)
                {
                    try
                    {
                        moduleBindings = ProcessCompleteModule(moduleGenerator, true);
                        graphVerifier.DetectUnusedBindings(moduleBindings.Values);
                    }
                    catch (InvalidOperationException ex)
                    {
                        if (suppressUnusedBindingsErrors)
                        {
                            errorReporter.LogWarning(ex.Message);
                        }
                        else
                        {
                            errorReporter.LogError(ex.Message);
                        }
                    }
                    catch (ValidationException ex)
                    {
                        errorReporter.LogError(ex.Message);
                    }
                }
            }
        }