Exemple #1
0
        /// <summary>
        /// Validate that the CSDL metadata defined for a service matches the documentation.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task <bool> CheckServiceMetadataAsync(CheckMetadataOptions options)
        {
            List <Schema> schemas = await TryGetMetadataSchemasAsync(options);

            if (null == schemas)
            {
                return(false);
            }

            FancyConsole.WriteLine(FancyConsole.ConsoleSuccessColor, "  found {0} schema definitions: {1}", schemas.Count, (from s in schemas select s.Namespace).ComponentsJoinedByString(", "));

            var docSet = await GetDocSetAsync(options);

            if (null == docSet)
            {
                return(false);
            }

            const string testname = "validate-service-metadata";

            TestReport.StartTest(testname);

            List <ResourceDefinition> foundResources = ODataParser.GenerateResourcesFromSchemas(schemas);
            CheckResults results = new CheckResults();

            List <ValidationError> collectedErrors = new List <ValidationError>();

            foreach (var resource in foundResources)
            {
                FancyConsole.WriteLine();
                FancyConsole.Write(FancyConsole.ConsoleHeaderColor, "Checking resource: {0}...", resource.Metadata.ResourceType);

                FancyConsole.VerboseWriteLine();
                FancyConsole.VerboseWriteLine(resource.JsonExample);
                FancyConsole.VerboseWriteLine();

                // Verify that this resource matches the documentation
                ValidationError[] errors;
                docSet.ResourceCollection.ValidateJsonExample(resource.Metadata, resource.JsonExample, out errors, new ValidationOptions {
                    RelaxedStringValidation = true
                });
                results.IncrementResultCount(errors);

                collectedErrors.AddRange(errors);

                await WriteOutErrorsAndFinishTestAsync(errors, options.SilenceWarnings, successMessage : " no errors.");
            }

            if (options.IgnoreWarnings)
            {
                results.ConvertWarningsToSuccess();
            }

            var output = (from e in collectedErrors select e.ErrorText).ComponentsJoinedByString("\r\n");

            await TestReport.FinishTestAsync(testname, results.WereFailures?TestOutcome.Failed : TestOutcome.Passed, stdOut : output);

            results.PrintToConsole();
            return(!results.WereFailures);
        }
Exemple #2
0
        /// <summary>
        /// Create and return a document set based on input options
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task <DocSet> GetDocSetAsync(DocSetOptions options)
        {
            FancyConsole.VerboseWriteLine("Opening documentation from {0}", options.DocumentationSetPath);
            DocSet set = new DocSet(options.DocumentationSetPath);

            FancyConsole.VerboseWriteLine("Scanning documentation files...");
            ValidationError[] loadErrors;
            if (!set.ScanDocumentation(out loadErrors))
            {
                await WriteOutErrorsAndFinishTestAsync(loadErrors, options.SilenceWarnings);

                return(null);
            }

            return(set);
        }
Exemple #3
0
        private static async Task <bool> PublishDocumentationAsync(PublishOptions options)
        {
            var outputPath = options.OutputDirectory;

            FancyConsole.WriteLine("Publishing documentation to {0}", outputPath);

            DocSet docs = await GetDocSetAsync(options);

            if (null == docs)
            {
                return(false);
            }

            DocumentPublisher publisher = null;

            switch (options.Format)
            {
            case PublishOptions.PublishFormat.Markdown:
                publisher = new MarkdownPublisher(docs);
                break;

            case PublishOptions.PublishFormat.Html:
                publisher = new DocumentPublisherHtml(docs, options);
                break;

            case PublishOptions.PublishFormat.Mustache:
                publisher = new HtmlMustacheWriter(docs, options);
                break;

            case PublishOptions.PublishFormat.Swagger2:
                publisher = new SwaggerWriter(docs, "https://service.org")      // TODO: Plumb in the base URL.
                {
                    Title       = options.Title,
                    Description = options.Description,
                    Version     = options.Version
                };
                break;

            case PublishOptions.PublishFormat.Outline:
                publisher = new OutlinePublisher(docs);
                break;

            default:
                FancyConsole.WriteLine(
                    FancyConsole.ConsoleErrorColor,
                    "Unsupported publishing format: {0}",
                    options.Format);
                return(false);
            }

            FancyConsole.WriteLineIndented("  ", "Format: {0}", publisher.GetType().Name);
            publisher.VerboseLogging = options.EnableVerboseOutput;
            FancyConsole.WriteLine();

            FancyConsole.WriteLine("Publishing content...");
            publisher.NewMessage += publisher_NewMessage;

            try
            {
                await publisher.PublishToFolderAsync(outputPath);

                FancyConsole.WriteLine(FancyConsole.ConsoleSuccessColor, "Finished publishing documentation to: {0}", outputPath);
            }
            catch (Exception ex)
            {
                FancyConsole.WriteLine(
                    FancyConsole.ConsoleErrorColor,
                    "An error occured while publishing: {0}",
                    ex.Message);
                FancyConsole.VerboseWriteLine(ex.ToString());
                return(false);
            }

            return(true);
        }