//===================================================================== /// <summary> /// Main program entry point /// </summary> /// <param name="args">Command line arguments</param> /// <returns>Zero on success or non-zero on failure</returns> public static int Main(string[] args) { string path, version, framework = null, assemblyPath, typeName; // Write banner ConsoleApplication.WriteBanner(); // Specify options OptionCollection options = new OptionCollection(); options.Add(new SwitchOption("?", "Show this help page.")); options.Add(new StringOption("out", "Specify an output file. If unspecified, output goes to the " + "console.", "outputFilePath")); options.Add(new StringOption("config", "Specify a configuration file. If unspecified, " + "MRefBuilder.config is used", "configFilePath")); options.Add(new ListOption("dep", "Specify assemblies to load for dependencies.", "dependencyAssembly")); options.Add(new BooleanOption("internal", "Specify whether to document internal as well as " + "externally exposed APIs. *** DEPRECATED: Use the visibility settings in the MRefBuilder.config " + "file instead which provide finer grained control over the exposed API members.")); // Process options ParseArgumentsResult results = options.ParseArguments(args); if (results.Options["?"].IsPresent) { Console.WriteLine("MRefBuilder [options] assemblies"); options.WriteOptionSummary(Console.Out); return(1); } // Check for invalid options if (!results.Success) { results.WriteParseErrors(Console.Out); return(1); } // Check for missing or extra assembly directories if (results.UnusedArguments.Count < 1) { Console.WriteLine("Specify at least one assembly to reflect."); return(1); } // Load the configuration file XPathDocument config; string configDirectory = ComponentUtilities.ToolsFolder, configFile = Path.Combine(ComponentUtilities.ToolsFolder, "MRefBuilder.config"); if (results.Options["config"].IsPresent) { configFile = (string)results.Options["config"].Value; configDirectory = Path.GetDirectoryName(configFile); } try { config = new XPathDocument(configFile); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while attempting to read " + "the configuration file '{0}'. The error message is: {1}", configFile, e.Message); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while attempting to read " + "the configuration file '{0}'. The error message is: {1}", configFile, e.Message); return(1); } catch (XmlException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "The configuration file '{0}' is not " + "well-formed. The error message is: {1}", configFile, e.Message); return(1); } // Adjust the target platform XPathNodeIterator platformNodes = config.CreateNavigator().Select("/configuration/dduetools/platform"); if (platformNodes.MoveNext()) { XPathNavigator platformNode = platformNodes.Current; version = platformNode.GetAttribute("version", String.Empty); path = platformNode.GetAttribute("path", String.Empty); // !EFW - Added support for the new platform attributes and the framework XML file if (!String.IsNullOrEmpty(version) && !String.IsNullOrEmpty(path)) { // Set the framework using the legacy attributes. If set properly, they will document // other framework types but it uses the standard .NET assemblies which contain more // classes and methods that are not relevant to the other frameworks. path = Environment.ExpandEnvironmentVariables(path); if (!Directory.Exists(path)) { ConsoleApplication.WriteMessage(LogLevel.Error, "The specified target platform " + "directory '{0}' does not exist.", path); return(1); } if (version == "2.0") { TargetPlatform.SetToV2(path); } else if (version == "1.1") { TargetPlatform.SetToV1_1(path); } else if (version == "1.0") { TargetPlatform.SetToV1(path); } else { ConsoleApplication.WriteMessage(LogLevel.Error, "Unknown target platform " + "version '{0}'.", version); return(1); } } else { // Use the new framework definition file framework = platformNode.GetAttribute("framework", String.Empty); if (!String.IsNullOrEmpty(framework) && !String.IsNullOrEmpty(version)) { TargetPlatform.SetFrameworkInformation(framework, version); } else { ConsoleApplication.WriteMessage(LogLevel.Error, "Unknown target framework " + "version '{0} {1}'.", framework, version); return(1); } } } // Create an API member namer ApiNamer namer; // Apply a different naming method to assemblies using the Windows Store or Windows Phone frameworks if (framework == ".NETCore" || framework == ".NETPortable" || framework == "WindowsPhone" || framework == "WindowsPhoneApp") { namer = new WindowsStoreAndPhoneNamer(); } else { namer = new OrcasNamer(); } XPathNavigator namerNode = config.CreateNavigator().SelectSingleNode("/configuration/dduetools/namer"); if (namerNode != null) { assemblyPath = namerNode.GetAttribute("assembly", String.Empty); typeName = namerNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); namer = (ApiNamer)assembly.CreateInstance(typeName); if (namer == null) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' was not found in the " + "component assembly '{1}'.", typeName, assemblyPath); return(1); } } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "A file access error occurred while " + "attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "A file access error occurred while " + "attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message); return(1); } catch (BadImageFormatException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The component assembly '{0}' is not a " + "valid managed assembly.", assemblyPath); return(1); } catch (TypeLoadException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' was not found in the " + "component assembly '{1}'.", typeName, assemblyPath); return(1); } catch (MissingMethodException) { ConsoleApplication.WriteMessage(LogLevel.Error, "No appropriate constructor exists for " + "the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath); return(1); } catch (TargetInvocationException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while initializing the " + "type '{0}' in the component assembly '{1}'. The error message and stack trace " + "follows: {2}", typeName, assemblyPath, e.InnerException); return(1); } catch (InvalidCastException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' in the component assembly " + "'{1}' is not a component type.", typeName, assemblyPath); return(1); } } // Create a resolver AssemblyResolver resolver = new AssemblyResolver(); XPathNavigator resolverNode = config.CreateNavigator().SelectSingleNode("/configuration/dduetools/resolver"); if (resolverNode != null) { assemblyPath = resolverNode.GetAttribute("assembly", String.Empty); typeName = resolverNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); resolver = (AssemblyResolver)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new object[1] { resolverNode }, null, null); if (resolver == null) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' was not found in the " + "component assembly '{1}'.", typeName, assemblyPath); return(1); } } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "A file access error occurred while " + "attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "A file access error occurred while " + "attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message); return(1); } catch (BadImageFormatException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The component assembly '{0}' is not a " + "valid managed assembly.", assemblyPath); return(1); } catch (TypeLoadException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' was not found in the " + "component assembly '{1}'.", typeName, assemblyPath); return(1); } catch (MissingMethodException) { ConsoleApplication.WriteMessage(LogLevel.Error, "No appropriate constructor exists for " + "the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath); return(1); } catch (TargetInvocationException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while initializing the " + "type '{0}' in the component assembly '{1}'. The error message and stack trace " + "follows: {2}", typeName, assemblyPath, e.InnerException); return(1); } catch (InvalidCastException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' in the component assembly " + "'{1}' is not a component type.", typeName, assemblyPath); return(1); } } resolver.UnresolvedAssemblyReference += UnresolvedAssemblyReferenceHandler; // Get a text writer for output TextWriter output = Console.Out; if (results.Options["out"].IsPresent) { string file = (string)results.Options["out"].Value; try { output = new StreamWriter(file, false, Encoding.UTF8); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while attempting to " + "create an output file. The error message is: {0}", e.Message); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while attempting to " + "create an output file. The error message is: {0}", e.Message); return(1); } } // Dependency directory string[] dependencies = new string[0]; if (results.Options["dep"].IsPresent) { dependencies = (string[])results.Options["dep"].Value; } try { // Create a builder ApiVisitor = new ManagedReflectionWriter(output, namer, resolver, new ApiFilter(config.CreateNavigator().SelectSingleNode("/configuration/dduetools"))); // If the deprecated /internal+ option is used, expose everything via the filter to mimic the // behavior of prior versions. if (results.Options["internal"].IsPresent && (bool)results.Options["internal"].Value) { ApiVisitor.ApiFilter.IncludeAttributes = ApiVisitor.ApiFilter.IncludeExplicitInterfaceImplementations = ApiVisitor.ApiFilter.IncludePrivates = ApiVisitor.ApiFilter.IncludePrivateFields = ApiVisitor.ApiFilter.IncludeInternals = ApiVisitor.ApiFilter.IncludeProtected = ApiVisitor.ApiFilter.IncludeSealedProtected = ApiVisitor.ApiFilter.IncludeInheritedMembers = ApiVisitor.ApiFilter.IncludeInheritedFrameworkMembers = ApiVisitor.ApiFilter.IncludeInheritedFrameworkPrivateMembers = ApiVisitor.ApiFilter.IncludeInheritedFrameworkInternalMembers = ApiVisitor.ApiFilter.IncludeNoPIATypes = true; ApiVisitor.ApiFilter.IncludeProtectedInternalAsProtected = false; } // Register add-ins to the builder XPathNodeIterator addinNodes = config.CreateNavigator().Select("/configuration/dduetools/addins/addin"); foreach (XPathNavigator addinNode in addinNodes) { assemblyPath = addinNode.GetAttribute("assembly", String.Empty); typeName = addinNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); MRefBuilderAddIn addin = (MRefBuilderAddIn)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new object[2] { ApiVisitor, addinNode }, null, null); if (addin == null) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' was not found in " + "the add-in assembly '{1}'.", typeName, assemblyPath); return(1); } } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "A file access error occurred while " + "attempting to load the add-in assembly '{0}'. The error message is: {1}", assemblyPath, e.Message); return(1); } catch (BadImageFormatException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The add-in assembly '{0}' is not a " + "valid managed assembly.", assemblyPath); return(1); } catch (TypeLoadException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' was not found in the " + "add-in assembly '{1}'.", typeName, assemblyPath); return(1); } catch (MissingMethodException) { ConsoleApplication.WriteMessage(LogLevel.Error, "No appropriate constructor exists " + "for the type '{0}' in the add-in assembly '{1}'.", typeName, assemblyPath); return(1); } catch (TargetInvocationException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while initializing " + "the type '{0}' in the add-in assembly '{1}'. The error message and stack trace " + "follows: {2}", typeName, assemblyPath, e.InnerException); return(1); } catch (InvalidCastException) { ConsoleApplication.WriteMessage(LogLevel.Error, "The type '{0}' in the add-in " + "assembly '{1}' is not an MRefBuilderAddIn type.", typeName, assemblyPath); return(1); } } // Load dependencies foreach (string dependency in dependencies) { try { // Expand environment variables path = Environment.ExpandEnvironmentVariables(dependency); // If x86 but it didn't exist, assume it's a 32-bit system and change the name if (path.IndexOf("%ProgramFiles(x86)%", StringComparison.Ordinal) != -1) { path = Environment.ExpandEnvironmentVariables(path.Replace("(x86)", String.Empty)); } ApiVisitor.LoadAccessoryAssemblies(path); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while loading " + "dependency assemblies. The error message is: {0}", e.Message); return(1); } } // Parse the assemblies foreach (string dllPath in results.UnusedArguments) { try { // Expand environment variables path = Environment.ExpandEnvironmentVariables(dllPath); // If x86 but it didn't exist, assume it's a 32-bit system and change the name if (path.IndexOf("%ProgramFiles(x86)%", StringComparison.Ordinal) != -1) { path = Environment.ExpandEnvironmentVariables(path.Replace("(x86)", String.Empty)); } ApiVisitor.LoadAssemblies(path); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, "An error occurred while loading " + "assemblies for reflection. The error message is: {0}", e.Message); return(1); } } ConsoleApplication.WriteMessage(LogLevel.Info, "Loaded {0} assemblies for reflection and " + "{1} dependency assemblies.", ApiVisitor.Assemblies.Count(), ApiVisitor.AccessoryAssemblies.Count()); ApiVisitor.VisitApis(); if (ApiVisitor.Canceled) { ConsoleApplication.WriteMessage(LogLevel.Error, "MRefBuilder task canceled"); } else { ConsoleApplication.WriteMessage(LogLevel.Info, "Wrote information on {0} namespaces, " + "{1} types, and {2} members", ApiVisitor.Namespaces.Count(), ApiVisitor.Types.Count(), ApiVisitor.Members.Count()); } } finally { if (ApiVisitor != null) { ApiVisitor.Dispose(); } if (results.Options["out"].IsPresent) { output.Close(); } } return((ApiVisitor != null && ApiVisitor.Canceled) ? 2 : 0); }
private void LoadConfiguration(string configFile, bool includeInternalMembers) { // load the configuration file XPathDocument config; string configDirectory = Path.GetDirectoryName(configFile); try { config = new XPathDocument(configFile); } catch (IOException e) { throw new SandcastleBuildException(e, "An error occured while attempting to read the configuration file '{0}'", configFile); } catch (UnauthorizedAccessException e) { throw new SandcastleBuildException(e, "An error occured while attempting to read the configuration file '{0}'", configFile); } catch (XmlException e) { throw new SandcastleBuildException(e, "The configuration file '{0}' is not well-formed.", configFile); } ConfigurationContext configContext = new ConfigurationContext(); configContext.ConfigurationFile = configFile; configContext.Configuration = config; // adjust the target platform XPathNodeIterator platformNodes = config.CreateNavigator().Select("/configuration/dduetools/platform"); if (platformNodes.MoveNext()) { XPathNavigator platformNode = platformNodes.Current; string version = platformNode.GetAttribute("version", String.Empty); string path = platformNode.GetAttribute("path", String.Empty); path = Environment.ExpandEnvironmentVariables(path); if (!Directory.Exists(path)) { throw new SandcastleBuildException("The specifed target platform directory '{0}' does not exist. Specifie platform version '{1}'.", path, version); } if (version == "2.0") { TargetPlatform.SetToV2(path); } else if (version == "1.1") { TargetPlatform.SetToV1_1(path); } else if (version == "1.0") { TargetPlatform.SetToV1(path); } else { throw new SandcastleBuildException(null, "The specifed target platform '{0}' is not supported.", version); } this.FrameworkName = new FrameworkName(".NET Framework, Version=" + version); } // create a namer ApiNamer namer = TypeLoader.CreateType <ApiNamer>(configContext, "/configuration/dduetools/namer", () => new OrcasNamer()); // create a resolver AssemblyResolver resolver = TypeLoader.CreateType <AssemblyResolver>(configContext, "/configuration/dduetools/resolver", () => new AssemblyResolver()); XPathNodeIterator addinNodes = config.CreateNavigator().Select("/configuration/dduetools/addins/addin"); foreach (XPathNavigator addinNode in addinNodes) { MRefBuilderAddIn addin = TypeLoader.CreateType <MRefBuilderAddIn>(addinNode, configDirectory, addinNode); if (addin != null) { this.BuilderAddins.Add(addin); } } XPathNavigator filterConfig = config.CreateNavigator().SelectSingleNode("/configuration/dduetools"); if (includeInternalMembers) { this.ApiFilter = new AllDocumentedFilter(filterConfig); } else { this.ApiFilter = new ExternalDocumentedFilter(filterConfig); } this.ApiNamer = namer; this.Resolver = resolver; }
public static int Main(string[] args) { // write banner ConsoleApplication.WriteBanner(); // specify options OptionCollection options = new OptionCollection(); options.Add(new SwitchOption("?", "Show this help page.")); options.Add(new StringOption("out", "Specify an output file. If unspecified, output goes to the console.", "outputFilePath")); options.Add(new StringOption("config", "Specify a configuration file. If unspecified, MRefBuilder.config is used", "configFilePath")); options.Add(new ListOption("dep", "Speficy assemblies to load for dependencies.", "dependencyAssembly")); // options.Add( new BooleanOption("namespaces", "Control whether information on namespaces in provided.") ); options.Add(new BooleanOption("internal", "Specify whether to document internal as well as externally exposed APIs.")); // process options ParseArgumentsResult results = options.ParseArguments(args); if (results.Options["?"].IsPresent) { Console.WriteLine("MRefBuilder [options] assemblies"); options.WriteOptionSummary(Console.Out); return(0); } // check for invalid options if (!results.Success) { results.WriteParseErrors(Console.Out); return(1); } // check for missing or extra assembly directories if (results.UnusedArguments.Count < 1) { Console.WriteLine("Specify at least one assembly to reflect."); return(1); } // load the configuration file XPathDocument config; string configDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string configFile = Path.Combine(configDirectory, "MRefBuilder.config"); if (results.Options["config"].IsPresent) { configFile = (string)results.Options["config"].Value; configDirectory = Path.GetDirectoryName(configFile); } try { config = new XPathDocument(configFile); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to read the configuration file '{0}'. The error message is: {1}", configFile, e.Message)); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to read the configuration file '{0}'. The error message is: {1}", configFile, e.Message)); return(1); } catch (XmlException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The configuration file '{0}' is not well-formed. The error message is: {1}", configFile, e.Message)); return(1); } // adjust the target platform XPathNodeIterator platformNodes = config.CreateNavigator().Select("/configuration/dduetools/platform"); if (platformNodes.MoveNext()) { XPathNavigator platformNode = platformNodes.Current; string version = platformNode.GetAttribute("version", String.Empty); string path = platformNode.GetAttribute("path", String.Empty); path = Environment.ExpandEnvironmentVariables(path); if (!Directory.Exists(path)) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The specifed target platform directory '{0}' does not exist.", path)); return(1); } if (version == "2.0") { TargetPlatform.SetToV2(path); } else if (version == "1.1") { TargetPlatform.SetToV1_1(path); } else if (version == "1.0") { TargetPlatform.SetToV1(path); } else { Console.WriteLine("Unknown target platform version '{0}'.", version); return(1); } } // create a namer ApiNamer namer = new OrcasNamer(); XPathNavigator namerNode = config.CreateNavigator().SelectSingleNode("/configuration/dduetools/namer"); if (namerNode != null) { string assemblyPath = namerNode.GetAttribute("assembly", String.Empty); string typeName = namerNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); namer = (ApiNamer)assembly.CreateInstance(typeName); if (namer == null) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath)); return(1); } } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message)); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message)); return(1); } catch (BadImageFormatException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The component assembly '{0}' is not a valid managed assembly.", assemblyPath)); return(1); } catch (TypeLoadException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath)); return(1); } catch (MissingMethodException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("No appropriate constructor exists for the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath)); return(1); } catch (TargetInvocationException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while initializing the type '{0}' in the component assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString())); return(1); } catch (InvalidCastException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' in the component assembly '{1}' is not a component type.", typeName, assemblyPath)); return(1); } } // create a resolver AssemblyResolver resolver = new AssemblyResolver(); XPathNavigator resolverNode = config.CreateNavigator().SelectSingleNode("/configuration/dduetools/resolver"); if (resolverNode != null) { string assemblyPath = resolverNode.GetAttribute("assembly", String.Empty); string typeName = resolverNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); resolver = (AssemblyResolver)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new Object[1] { resolverNode }, null, null); if (resolver == null) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath)); return(1); } } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message)); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message)); return(1); } catch (BadImageFormatException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The component assembly '{0}' is not a valid managed assembly.", assemblyPath)); return(1); } catch (TypeLoadException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath)); return(1); } catch (MissingMethodException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("No appropriate constructor exists for the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath)); return(1); } catch (TargetInvocationException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while initializing the type '{0}' in the component assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString())); return(1); } catch (InvalidCastException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' in the component assembly '{1}' is not a component type.", typeName, assemblyPath)); return(1); } } resolver.UnresolvedAssemblyReference += new EventHandler <AssemblyReferenceEventArgs>(UnresolvedAssemblyReferenceHandler); // get a textwriter for output TextWriter output = Console.Out; if (results.Options["out"].IsPresent) { string file = (string)results.Options["out"].Value; try { output = new StreamWriter(file, false, Encoding.UTF8); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to create an output file. The error message is: {0}", e.Message)); return(1); } catch (UnauthorizedAccessException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to create an output file. The error message is: {0}", e.Message)); return(1); } } // dependency directory string[] dependencies = new string[0]; if (results.Options["dep"].IsPresent) { dependencies = (string[])results.Options["dep"].Value; } try { // create a builder ManagedReflectionWriter builder = new ManagedReflectionWriter(output, namer); // specify the resolver for the builder builder.Resolver = resolver; // builder.ApiFilter = new ExternalDocumentedFilter(config.CreateNavigator().SelectSingleNode("/configuration/dduetools")); // specify the filter for the builder if (results.Options["internal"].IsPresent && (bool)results.Options["internal"].Value) { builder.ApiFilter = new AllDocumentedFilter(config.CreateNavigator().SelectSingleNode("/configuration/dduetools")); } else { builder.ApiFilter = new ExternalDocumentedFilter(config.CreateNavigator().SelectSingleNode("/configuration/dduetools")); } // register add-ins to the builder XPathNodeIterator addinNodes = config.CreateNavigator().Select("/configuration/dduetools/addins/addin"); foreach (XPathNavigator addinNode in addinNodes) { string assemblyPath = addinNode.GetAttribute("assembly", String.Empty); string typeName = addinNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); MRefBuilderAddIn addin = (MRefBuilderAddIn)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new Object[2] { builder, addinNode }, null, null); if (namer == null) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the addin assembly '{1}'.", typeName, assemblyPath)); return(1); } } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the addin assembly '{0}'. The error message is: {1}", assemblyPath, e.Message)); return(1); } catch (BadImageFormatException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The addin assembly '{0}' is not a valid managed assembly.", assemblyPath)); return(1); } catch (TypeLoadException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the addin assembly '{1}'.", typeName, assemblyPath)); return(1); } catch (MissingMethodException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("No appropriate constructor exists for the type '{0}' in the addin assembly '{1}'.", typeName, assemblyPath)); return(1); } catch (TargetInvocationException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while initializing the type '{0}' in the addin assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString())); return(1); } catch (InvalidCastException) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' in the addin assembly '{1}' is not an MRefBuilderAddIn type.", typeName, assemblyPath)); return(1); } } try { // add a handler for unresolved assembly references //builder.UnresolvedModuleHandler = new System.Compiler.Module.AssemblyReferenceResolver(AssemblyNotFound); // load dependent bits foreach (string dependency in dependencies) { try { builder.LoadAccessoryAssemblies(dependency); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while loading dependency assemblies. The error message is: {0}", e.Message)); return(1); } } // parse the bits foreach (string dllPath in results.UnusedArguments) { try { builder.LoadAssemblies(dllPath); } catch (IOException e) { ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while loading assemblies for reflection. The error message is: {0}", e.Message)); return(1); } } ConsoleApplication.WriteMessage(LogLevel.Info, String.Format("Loaded {0} assemblies for reflection and {1} dependency assemblies.", builder.Assemblies.Length, builder.AccessoryAssemblies.Length)); // register callbacks //builder.RegisterStartTagCallback("apis", new MRefBuilderCallback(startTestCallback)); //MRefBuilderAddIn addin = new XamlAttachedMembersAddIn(builder, null); builder.VisitApis(); ConsoleApplication.WriteMessage(LogLevel.Info, String.Format("Wrote information on {0} namespaces, {1} types, and {2} members", builder.Namespaces.Length, builder.Types.Length, builder.Members.Length)); } finally { builder.Dispose(); } } finally { // output.Close(); } return(0); }
protected override bool OnRun(BuildLogger logger) { bool isSuccessful = false; _logger = logger; if (_assemblyFiles == null || _assemblyFiles.Count == 0) { _logger.WriteLine( "The assemblies files for reflections are required, but are not specified.", BuildLoggerLevel.Error); } // load the configuration file XPathDocument config; XPathNavigator documentNode; string configDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string configFile = Path.Combine(configDirectory, "Sandcastle.Reflection.config"); if (!String.IsNullOrEmpty(_configurationFile) && File.Exists(_configurationFile)) { configFile = _configurationFile; configDirectory = Path.GetDirectoryName(configFile); } try { config = new XPathDocument(configFile); documentNode = config.CreateNavigator(); } catch (IOException e) { _logger.WriteLine(String.Format( "An error occurred while attempting to read the configuration file '{0}'. The error message is: {1}", configFile, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (UnauthorizedAccessException e) { _logger.WriteLine(String.Format( "An error occurred while attempting to read the configuration file '{0}'. The error message is: {1}", configFile, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (XmlException e) { _logger.WriteLine(String.Format( "The configuration file '{0}' is not well-formed. The error message is: {1}", configFile, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } // adjust the target platform XPathNodeIterator platformNodes = documentNode.Select( "/configuration/dduetools/platform"); if (platformNodes.MoveNext()) { XPathNavigator platformNode = platformNodes.Current; string version = platformNode.GetAttribute("version", String.Empty); string path = platformNode.GetAttribute("path", String.Empty); path = Environment.ExpandEnvironmentVariables(path); if (!Directory.Exists(path)) { _logger.WriteLine(String.Format( "The specified target platform directory '{0}' does not exist.", path), BuildLoggerLevel.Error); return(isSuccessful); } if (version == "2.0") { TargetPlatform.SetToV2(path); } else if (version == "1.1") { TargetPlatform.SetToV1_1(path); } else if (version == "1.0") { TargetPlatform.SetToV1(path); } else { _logger.WriteLine(String.Format( "Unknown target platform version '{0}'.", version), BuildLoggerLevel.Error); return(isSuccessful); } } // create a namer ApiNamer namer = new OrcasNamer(); XPathNavigator namerNode = documentNode.SelectSingleNode( "/configuration/dduetools/namer"); if (namerNode != null) { string assemblyPath = namerNode.GetAttribute("assembly", String.Empty); string typeName = namerNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); namer = (ApiNamer)assembly.CreateInstance(typeName); if (namer == null) { _logger.WriteLine(String.Format( "The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } } catch (IOException e) { _logger.WriteLine(String.Format( "A file access error occurred while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (UnauthorizedAccessException e) { _logger.WriteLine(String.Format( "A file access error occurred while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (BadImageFormatException) { _logger.WriteLine(String.Format( "The component assembly '{0}' is not a valid managed assembly.", assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (TypeLoadException) { _logger.WriteLine(String.Format( "The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (MissingMethodException) { _logger.WriteLine(String.Format( "No appropriate constructor exists for the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (TargetInvocationException e) { _logger.WriteLine(String.Format( "An error occurred while initializing the type '{0}' in the component assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString()), BuildLoggerLevel.Error); return(isSuccessful); } catch (InvalidCastException) { _logger.WriteLine(String.Format( "The type '{0}' in the component assembly '{1}' is not a component type.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } } // create a resolver AssemblyResolver resolver = new AssemblyResolver(); XPathNavigator resolverNode = documentNode.SelectSingleNode( "/configuration/dduetools/resolver"); if (resolverNode != null) { string assemblyPath = resolverNode.GetAttribute("assembly", String.Empty); string typeName = resolverNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); resolver = (AssemblyResolver)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new Object[1] { resolverNode }, null, null); if (resolver == null) { _logger.WriteLine(String.Format( "The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } } catch (IOException e) { _logger.WriteLine(String.Format( "A file access error occurred while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (UnauthorizedAccessException e) { _logger.WriteLine(String.Format( "A file access error occurred while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (BadImageFormatException) { _logger.WriteLine(String.Format( "The component assembly '{0}' is not a valid managed assembly.", assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (TypeLoadException) { _logger.WriteLine(String.Format( "The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (MissingMethodException) { _logger.WriteLine(String.Format( "No appropriate constructor exists for the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (TargetInvocationException e) { _logger.WriteLine(String.Format( "An error occurred while initializing the type '{0}' in the component assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString()), BuildLoggerLevel.Error); return(isSuccessful); } catch (InvalidCastException) { _logger.WriteLine(String.Format( "The type '{0}' in the component assembly '{1}' is not a component type.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } } resolver.UnresolvedAssemblyReference += new EventHandler <AssemblyReferenceEventArgs>(OnUnresolvedAssemblyReferenceHandler); // Get a text-writer for output TextWriter output = null; if (!String.IsNullOrEmpty(_reflectionFile)) { try { output = new StreamWriter(_reflectionFile, false, Encoding.UTF8); } catch (IOException e) { _logger.WriteLine(String.Format( "An error occurred while attempting to create an output file. The error message is: {0}", e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (UnauthorizedAccessException e) { _logger.WriteLine(String.Format( "An error occurred while attempting to create an output file. The error message is: {0}", e.Message), BuildLoggerLevel.Error); return(isSuccessful); } } else { output = Console.Out; } // dependency directory IList <string> dependencies = new List <string>(); if (_dependencyFiles != null && _dependencyFiles.Count != 0) { for (int i = 0; i < _dependencyFiles.Count; i++) { string dependencyDir = _dependencyFiles[i]; if (dependencyDir != null) { dependencyDir = dependencyDir.Trim(); } if (!String.IsNullOrEmpty(dependencyDir)) { dependencies.Add(dependencyDir); } } } // create a builder ManagedReflectionWriter builder = new ManagedReflectionWriter( output, namer); // specify the resolver for the builder builder.Resolver = resolver; // builder.ApiFilter = new ExternalDocumentedFilter(documentNode.SelectSingleNode("/configuration/dduetools")); // specify the filter for the builder if (_documentInternals) { builder.ApiFilter = new AllDocumentedFilter( documentNode.SelectSingleNode("/configuration/dduetools")); } else { builder.ApiFilter = new ExternalDocumentedFilter( documentNode.SelectSingleNode("/configuration/dduetools")); } // register add-ins to the builder XPathNodeIterator addinNodes = documentNode.Select( "/configuration/dduetools/addins/addin"); foreach (XPathNavigator addinNode in addinNodes) { string assemblyPath = addinNode.GetAttribute("assembly", String.Empty); string typeName = addinNode.GetAttribute("type", String.Empty); assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath); if (!Path.IsPathRooted(assemblyPath)) { assemblyPath = Path.Combine(configDirectory, assemblyPath); } try { Assembly assembly = Assembly.LoadFrom(assemblyPath); MRefBuilderAddIn addin = (MRefBuilderAddIn)assembly.CreateInstance( typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new Object[2] { builder, addinNode }, null, null); if (namer == null) { _logger.WriteLine(String.Format( "The type '{0}' was not found in the addin assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } } catch (IOException e) { _logger.WriteLine(String.Format( "A file access error occurred while attempting to load the addin assembly '{0}'. The error message is: {1}", assemblyPath, e.Message), BuildLoggerLevel.Error); return(isSuccessful); } catch (BadImageFormatException) { _logger.WriteLine(String.Format( "The addin assembly '{0}' is not a valid managed assembly.", assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (TypeLoadException) { _logger.WriteLine(String.Format( "The type '{0}' was not found in the addin assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (MissingMethodException) { _logger.WriteLine(String.Format( "No appropriate constructor exists for the type '{0}' in the addin assembly '{1}'.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } catch (TargetInvocationException e) { _logger.WriteLine(String.Format( "An error occurred while initializing the type '{0}' in the addin assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString()), BuildLoggerLevel.Error); return(isSuccessful); } catch (InvalidCastException) { _logger.WriteLine(String.Format( "The type '{0}' in the addin assembly '{1}' is not an MRefBuilderAddIn type.", typeName, assemblyPath), BuildLoggerLevel.Error); return(isSuccessful); } } try { // add a handler for unresolved assembly references //builder.UnresolvedModuleHandler = new System.Compiler.Module.AssemblyReferenceResolver(AssemblyNotFound); // load dependent bits foreach (string dependency in dependencies) { try { builder.LoadAccessoryAssemblies(dependency); } catch (IOException e) { _logger.WriteLine(String.Format( "An error occurred while loading dependency assemblies. The error message is: {0}", e.Message), BuildLoggerLevel.Error); return(isSuccessful); } } // parse the bits foreach (string dllPath in _assemblyFiles) { try { builder.LoadAssemblies(dllPath); } catch (IOException e) { _logger.WriteLine(String.Format( "An error occurred while loading assemblies for reflection. The error message is: {0}", e.Message), BuildLoggerLevel.Error); return(isSuccessful); } } _logger.WriteLine(String.Format( "Loaded {0} assemblies for reflection and {1} dependency assemblies.", builder.Assemblies.Length, builder.AccessoryAssemblies.Length), BuildLoggerLevel.Info); // register callbacks //builder.RegisterStartTagCallback("apis", new MRefBuilderCallback(startTestCallback)); //MRefBuilderAddIn addin = new XamlAttachedMembersAddIn(builder, null); builder.VisitApis(); _logger.WriteLine(String.Format( "Wrote information on {0} namespaces, {1} types, and {2} members", builder.Namespaces.Length, builder.Types.Length, builder.Members.Length), BuildLoggerLevel.Info); isSuccessful = true; } finally { resolver.UnresolvedAssemblyReference -= new EventHandler <AssemblyReferenceEventArgs>(OnUnresolvedAssemblyReferenceHandler); builder.Dispose(); } return(isSuccessful); }