Exemplo n.º 1
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            if (string.IsNullOrEmpty(context.MarkdownContent) && string.IsNullOrEmpty(context.MarkdownFilePath))
            {
                throw new ArgumentException("Neither Markdown file content nor file path is specified!");
            }

            if (string.IsNullOrEmpty(context.MarkdownContent))
            {
                context.MarkdownContent = File.ReadAllText(context.MarkdownFilePath);
            }

            if (!string.IsNullOrEmpty(context.MarkdownFilePath))
            {
                item.Remote = GitUtility.GetGitDetail(context.MarkdownFilePath);
            }

            return(new ParseResult(ResultLevel.Success));
        }
Exemplo n.º 2
0
        private static ParseResult ExecutePipeline(MapFileItemViewModel index, IndexerContext context)
        {
            ParseResult result = new ParseResult(ResultLevel.Success);

            foreach (var pipeline in pipelines)
            {
                result = pipeline.Run(index, context);
                if (result.ResultLevel == ResultLevel.Error)
                {
                    return(result);
                }

                if (!string.IsNullOrEmpty(result.Message))
                {
                    result.WriteToConsole();
                }
            }

            return(result);
        }
        public void TestResolveYamlHeader()
        {
            MapFileItemViewModel item    = new MapFileItemViewModel();
            IndexerContext       context = new IndexerContext();

            context.ExternalApiIndex = new Dictionary <string, MetadataItem>
            {
                { "api1", new MetadataItem()
                  {
                      Name = "api1", Href = "api1.yml"
                  } },
                { "api2", new MetadataItem()
                  {
                      Name = "api2", Href = "api2.yml"
                  } }
            };
            ResolveYamlHeader resolver = new ResolveYamlHeader();
            var result = resolver.Run(item, context);

            Assert.AreEqual(result.ResultLevel, ResultLevel.Success);
        }
Exemplo n.º 4
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            var filePath = context.MarkdownFilePath;
            var apis     = context.ExternalApiIndex;
            var content  = context.MarkdownContent;
            var links    = LinkParser.Select(content);

            if (links == null || links.Count == 0)
            {
                return(new ParseResult(ResultLevel.Info, "No Api reference found for {0}", filePath));
            }
            if (item.References == null)
            {
                item.References = new ReferencesViewModel();
            }
            ReferencesViewModel references = item.References;

            foreach (var matchDetail in links)
            {
                var          referenceId = matchDetail.Id;
                var          apiId       = matchDetail.Id;
                MetadataItem api;
                if (apis.TryGetValue(apiId, out api))
                {
                    var reference = new MapFileItemViewModel
                    {
                        Id            = referenceId,
                        ReferenceKeys = matchDetail.MatchedSections,
                        Href          = FileExtensions.MakeRelativePath(Path.GetDirectoryName(filePath), api.Href),
                        MapFileType   = MapFileType.Link
                    };

                    // Api Index file only contains Id and Href
                    references.AddItem(reference);
                }
            }

            return(new ParseResult(ResultLevel.Success));
        }
Exemplo n.º 5
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            var    filePath = context.MarkdownFilePath;
            var    markdownMapFileOutputFolder = context.MarkdownMapFileOutputFolder;
            string markdownMapFileName         = Path.GetFileName(filePath) + Constants.MapFileExtension;
            string markdownFolder = Path.GetDirectoryName(filePath);

            // Use the same folder as api.yaml if the output folder is not set
            string markdownMapFileFolder = (string.IsNullOrEmpty(markdownMapFileOutputFolder) ? markdownFolder : markdownMapFileOutputFolder)
                                           ?? string.Empty;
            string markdownMapFileFullPath = Path.Combine(markdownMapFileFolder, markdownMapFileName);

            // Post-process item
            // if references'/overrides count is 0, set it to null
            if (item.References != null && item.References.Count == 0)
            {
                item.References = null;
            }
            if (item.CustomProperties != null && item.CustomProperties.Count == 0)
            {
                item.CustomProperties = null;
            }

            if (!item.HasUsefulInfo())
            {
                return(new ParseResult(ResultLevel.Info, "Finish processing {0}, Map file will not be generated as no neccessory info is contained", context.MarkdownFilePath));
            }

            MapFileViewModel mapFile = new MapFileViewModel();

            // For map file, always one key corresponding to the value
            mapFile.Add("default", item);
            JsonUtility.Serialize(markdownMapFileFullPath, mapFile);

            return(new ParseResult(ResultLevel.Success, "Finish processing {0}, successfully generated {0}", context.MarkdownFilePath, markdownMapFileFullPath));
        }
Exemplo n.º 6
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            Dictionary <string, MetadataItem> indexViewModel;
            var inputApiIndexFilePath = context.ApiIndexFilePath;

            // Read index
            if (string.IsNullOrEmpty(inputApiIndexFilePath) || !File.Exists(inputApiIndexFilePath))
            {
                context.ExternalApiIndex = new Dictionary <string, MetadataItem>();
                if (string.IsNullOrEmpty(inputApiIndexFilePath))
                {
                    return(new ParseResult(ResultLevel.Success));
                }
                return(new ParseResult(ResultLevel.Warn, "Index file {0} for API is not found", inputApiIndexFilePath));
            }

            using (StreamReader sr = new StreamReader(inputApiIndexFilePath))
            {
                indexViewModel = YamlUtility.Deserialize <Dictionary <string, MetadataItem> >(sr);
            }

            context.ExternalApiIndex = indexViewModel;
            return(new ParseResult(ResultLevel.Success));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Save to **.md.map
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static ParseResult Exec(IndexerContext context)
        {
            MapFileItemViewModel viewModel = new MapFileItemViewModel();

            return(ExecutePipeline(viewModel, context));
        }
Exemplo n.º 8
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            var filePath     = context.MarkdownFilePath;
            var content      = context.MarkdownContent;
            var codeSnippets = CodeSnippetParser.Select(content);

            if (codeSnippets == null || codeSnippets.Count == 0)
            {
                return(new ParseResult(ResultLevel.Info, "No code snippet reference found for {0}", filePath));
            }
            if (item.References == null)
            {
                item.References = new ReferencesViewModel();
            }
            ReferencesViewModel references = item.References;
            var defaultReferenceFolder     = Environment.CurrentDirectory;
            var referenceFolder            = string.IsNullOrEmpty(context.ReferenceOutputFolder)
                                    ? defaultReferenceFolder
                                    : context.ReferenceOutputFolder;

            foreach (var codeSnippet in codeSnippets)
            {
                var referenceId     = codeSnippet.Id;
                var codeSnippetPath = FileExtensions.GetFullPath(Path.GetDirectoryName(filePath), codeSnippet.Path);
                // As reference, copy file to local
                var targetFileName = FileExtensions.MakeRelativePath(referenceFolder, codeSnippetPath).ToValidFilePath();
                // Append ref incase the file name starts with ".", which means a hidden file in Linux
                targetFileName = "ref" + targetFileName;
                var targetPath = Path.Combine(referenceFolder, targetFileName);
                MapFileItemViewModel reference;
                if (!File.Exists(codeSnippetPath))
                {
                    reference = new MapFileItemViewModel
                    {
                        Id            = referenceId,
                        ReferenceKeys = codeSnippet.MatchedSections,
                        Message       = string.Format("{0} does not exist.", Path.GetFullPath(codeSnippetPath)),
                        MapFileType   = MapFileType.CodeSnippet
                    };

                    ParseResult.WriteToConsole(ResultLevel.Warn, reference.Message);
                }
                else
                {
                    FileExtensions.CopyFile(codeSnippetPath, targetPath);
                    reference = new MapFileItemViewModel
                    {
                        Id            = referenceId,
                        ReferenceKeys = codeSnippet.MatchedSections,
                        Href          = FileExtensions.MakeRelativePath(Path.GetDirectoryName(filePath), targetPath).BackSlashToForwardSlash(),
                        Startline     = codeSnippet.StartLine,
                        Endline       = codeSnippet.EndLine,
                        MapFileType   = MapFileType.CodeSnippet
                    };
                }

                // Api Index file only contains Id and Href
                references.AddItem(reference);
            }

            return(new ParseResult(ResultLevel.Success));
        }
Exemplo n.º 9
0
 public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
 {
     return(new ParseResult(ResultLevel.Success));
 }
Exemplo n.º 10
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            var filePath = context.MarkdownFilePath;
            var content  = context.MarkdownContent;
            var apis     = context.ExternalApiIndex;
            var apiMapFileOutputFolder = context.ApiMapFileOutputFolder;
            var yamlHeaders            = YamlHeaderParser.Select(content);

            if (yamlHeaders == null || yamlHeaders.Count == 0)
            {
                return(new ParseResult(ResultLevel.Info, "No valid yaml header reference found for {0}", filePath));
            }

            if (item.References == null)
            {
                item.References = new ReferencesViewModel();
            }
            ReferencesViewModel    references = item.References;
            List <MarkdownSection> sections   = SplitToSections(content, yamlHeaders);
            Dictionary <string, MarkdownSection> validMarkdownSections = new Dictionary <string, MarkdownSection>();

            foreach (var markdownSection in sections)
            {
                if (!string.IsNullOrEmpty(markdownSection.Id))
                {
                    validMarkdownSections[markdownSection.Id] = markdownSection;
                }
            }

            foreach (var yamlHeader in yamlHeaders)
            {
                var referenceId = yamlHeader.Id;
                var apiId       = yamlHeader.Id;

                MetadataItem api;
                if (apis.TryGetValue(apiId, out api))
                {
                    var reference = new MapFileItemViewModel
                    {
                        Id            = referenceId,
                        ReferenceKeys = yamlHeader.MatchedSections,
                        Href          = api.Href,
                    };
                    // *DONOT* Add references to Markdown file
                    // references.AddItem(reference);

                    // 2. Write api reference to API's map file
                    MarkdownSection markdownSection;
                    if (!validMarkdownSections.TryGetValue(apiId, out markdownSection))
                    {
                        continue;
                    }

                    var    apiPath        = api.Href;
                    var    apiIndexPath   = context.ApiIndexFilePath;
                    var    apiYamlPath    = FileExtensions.GetFullPath(Path.GetDirectoryName(apiIndexPath), apiPath);
                    string apiMapFileName = Path.GetFileName(apiPath) + Constants.MapFileExtension;
                    string apiFolder      = Path.GetDirectoryName(apiYamlPath);

                    // Use the same folder as api.yaml if the output folder is not set
                    string apiMapFileFolder   = (string.IsNullOrEmpty(apiMapFileOutputFolder) ? apiFolder : apiMapFileOutputFolder);
                    string apiMapFileFullPath = FileExtensions.GetFullPath(apiMapFileFolder, apiMapFileName);

                    // Path should be the relative path from .yml to .md
                    var markdownFilePath = context.MarkdownFilePath;
                    var indexFolder      = Path.GetDirectoryName(context.ApiIndexFilePath);
                    var apiYamlFilePath  = FileExtensions.GetFullPath(indexFolder, api.Href);
                    var relativePath     = FileExtensions.MakeRelativePath(Path.GetDirectoryName(apiYamlFilePath), markdownFilePath).BackSlashToForwardSlash();
                    MapFileItemViewModel apiMapFileSection = new MapFileItemViewModel
                    {
                        Id               = apiId,
                        Remote           = item.Remote,
                        Href             = relativePath,
                        Startline        = markdownSection.Location.StartLocation.Line + 1,
                        Endline          = markdownSection.Location.EndLocation.Line + 1, // Endline + 1 - 1, +1 for it starts from 0, -1 for it is actually the start line for next charactor, in code snippet, is always a \n
                        References       = SelectReferenceSection(references, markdownSection.Location),
                        CustomProperties = yamlHeader.Properties,
                        MapFileType      = MapFileType.Yaml
                    };
                    MapFileViewModel apiMapFile;
                    if (File.Exists(apiMapFileFullPath))
                    {
                        apiMapFile = JsonUtility.Deserialize <MapFileViewModel>(apiMapFileFullPath);
                    }
                    else
                    {
                        apiMapFile = new MapFileViewModel();
                    }

                    // Current behavior: Override existing one
                    apiMapFile[apiId] = apiMapFileSection;

                    // Post-process item
                    // if references'/overrides count is 0, set it to null
                    if (apiMapFileSection.References != null && apiMapFileSection.References.Count == 0)
                    {
                        apiMapFileSection.References = null;
                    }
                    if (apiMapFileSection.CustomProperties != null && apiMapFileSection.CustomProperties.Count == 0)
                    {
                        apiMapFileSection.CustomProperties = null;
                    }

                    JsonUtility.Serialize(apiMapFileFullPath, apiMapFile);
                    ParseResult.WriteToConsole(ResultLevel.Success, "Successfully generated {0}.", apiMapFileFullPath);
                }
            }

            // Select references to the indices where DefinedLine is between startline and endline
            return(new ParseResult(ResultLevel.Success));
        }