Esempio n. 1
0
        /// <summary>
        /// Run the command-line application.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        /// <returns>The exit code.</returns>
        public static int Run(IReadOnlyList <string> args)
        {
            try
            {
                var argsReader = new ArgsReader(args);
                if (argsReader.ReadHelpFlag())
                {
                    WriteUsage(Console.Out);
                    return(0);
                }

                var isVerify = argsReader.ReadVerifyFlag();

                var settings = new XmlDocMarkdownSettings
                {
                    NewLine         = argsReader.ReadNewLineOption(),
                    SourceCodePath  = argsReader.ReadSourceOption(),
                    RootNamespace   = argsReader.ReadNamespaceOption(),
                    IncludeObsolete = argsReader.ReadObsoleteFlag(),
                    SkipUnbrowsable = argsReader.ReadSkipUnbrowsableFlag(),
                    VisibilityLevel = argsReader.ReadVisibilityOption(),
                    ShouldClean     = argsReader.ReadCleanFlag(),
                    IsQuiet         = argsReader.ReadQuietFlag(),
                    IsDryRun        = isVerify || argsReader.ReadDryRunFlag(),
                    FrontMatter     = argsReader.ReadFrontMatter(),
                    PermalinkStyle  = argsReader.ReadPermalinkStyle(),
                    GenerateToc     = argsReader.ReadTocFlag(),
                    TocPrefix       = argsReader.ReadTocPrefix(),
                    NamespacePages  = argsReader.ReadNamespacePagesFlag(),
                };

                var    externalDocs = new List <ExternalDocumentation>();
                string?externalOption;
                while ((externalOption = argsReader.ReadExternalOption()) != null)
                {
                    externalDocs.Add(new ExternalDocumentation {
                        Namespace = externalOption
                    });
                }
                if (externalDocs.Count != 0)
                {
                    settings.ExternalDocs = externalDocs;
                }

                var inputPath = argsReader.ReadArgument();
                if (inputPath == null)
                {
                    throw new ArgsReaderException("Missing input path.");
                }

                var input = File.Exists(inputPath)
                                        ? new XmlDocInput {
                    AssemblyPath = inputPath
                }
                                        : new XmlDocInput {
                    Assembly = Assembly.Load(inputPath)
                };

                var outputPath = argsReader.ReadArgument();
                if (outputPath == null)
                {
                    throw new ArgsReaderException("Missing output path.");
                }

                argsReader.VerifyComplete();

                var result = XmlDocMarkdownGenerator.Generate(input, outputPath, settings);

                foreach (var message in result.Messages)
                {
                    Console.WriteLine(message);
                }

                return(isVerify && result.Added.Count + result.Changed.Count + result.Removed.Count != 0 ? 1 : 0);
            }
            catch (Exception exception)
            {
                if (exception is ArgsReaderException)
                {
                    Console.Error.WriteLine(exception.Message);
                    Console.Error.WriteLine();
                    WriteUsage(Console.Error);
                    return(2);
                }
                else if (exception is ApplicationException || exception is IOException || exception is UnauthorizedAccessException)
                {
                    Console.Error.WriteLine(exception.Message);
                    return(3);
                }
                else
                {
                    Console.Error.WriteLine(exception.ToString());
                    return(3);
                }
            }
        }