public override FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata) { switch (file.Type) { case DocumentType.Article: var filePath = Path.Combine(file.BaseDir, file.File); var swaggerContent = File.ReadAllText(filePath); var swagger = SwaggerJsonParser.Parse(swaggerContent); swagger.Metadata[DocumentTypeKey] = RestApiDocumentType; swagger.Raw = swaggerContent; var repoInfo = GitUtility.GetGitDetail(filePath); if (repoInfo != null) { swagger.Metadata["source"] = new SourceDetail() { Remote = repoInfo }; } swagger.Metadata = MergeMetadata(swagger.Metadata, metadata); var vm = SwaggerModelConverter.FromSwaggerModel(swagger); var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath); return new FileModel(file, vm, serializer: Environment.Is64BitProcess ? null : new BinaryFormatter()) { Uids = new[] { new UidDefinition(vm.Uid, displayLocalPath) } .Concat(from item in vm.Children select new UidDefinition(item.Uid, displayLocalPath)) .Concat(from tag in vm.Tags select new UidDefinition(tag.Uid, displayLocalPath)).ToImmutableArray(), LocalPathFromRepoRoot = repoInfo?.RelativePath ?? filePath.ToDisplayPath(), LocalPathFromRoot = displayLocalPath }; case DocumentType.Overwrite: // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors return OverwriteDocumentReader.Read(file); default: throw new NotSupportedException(); } }
private static OverwriteDocumentModel TransformModel(string filePath, YamlHtmlPart part) { if (part == null) { return(null); } var properties = part.YamlHeader; string checkPropertyMessage; var checkPropertyStatus = CheckRequiredProperties(properties, RequiredProperties, out checkPropertyMessage); if (!checkPropertyStatus) { throw new InvalidDataException(checkPropertyMessage); } var overriden = RemoveRequiredProperties(properties, RequiredProperties); var repoInfo = GitUtility.GetGitDetail(filePath); return(new OverwriteDocumentModel { Uid = properties[Constants.PropertyName.Uid].ToString(), Metadata = overriden, Conceptual = part.Conceptual, Documentation = new SourceDetail { Remote = repoInfo, StartLine = part.StartLine, EndLine = part.EndLine, Path = part.SourceFile } }); }
public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata) { var filePath = Path.Combine(file.BaseDir, file.File); TocViewModel toc = LoadSingleToc(filePath); var repoDetail = GitUtility.GetGitDetail(filePath); // todo : metadata. return(new FileModel(file, toc) { Uids = new[] { file.File }.ToImmutableArray(), LocalPathFromRepoRoot = repoDetail?.RelativePath }); }
private static void MergeNewFileRepositoryToConfig(BuildJsonConfig config) { GitDetail repoInfoFromBaseDirectory = GitUtility.GetGitDetail(Path.Combine(Environment.CurrentDirectory, config.BaseDirectory)); if (repoInfoFromBaseDirectory?.LocalWorkingDirectory != null) { config.GlobalMetadata["baseRepositoryDirectory"] = repoInfoFromBaseDirectory.LocalWorkingDirectory; } if (repoInfoFromBaseDirectory != null && repoInfoFromBaseDirectory.RelativePath != null) { repoInfoFromBaseDirectory.RelativePath = Path.Combine(repoInfoFromBaseDirectory.RelativePath, DocAsCode.Constants.DefaultOverwriteFolderName); } object newFileRepository; if (config.GlobalMetadata.TryGetValue("newFileRepository", out newFileRepository)) { GitDetail repoInfo = null; try { repoInfo = JObject.FromObject(newFileRepository).ToObject <GitDetail>(); } catch (Exception e) { throw new DocumentException($"Unable to convert newFileRepository to GitDetail in globalMetadata: {e.Message}", e); } if (repoInfoFromBaseDirectory != null) { if (repoInfo.RelativePath == null) { repoInfo.RelativePath = repoInfoFromBaseDirectory.RelativePath; } if (repoInfo.RemoteBranch == null) { repoInfo.RemoteBranch = repoInfoFromBaseDirectory.RemoteBranch; } if (repoInfo.RemoteRepositoryUrl == null) { repoInfo.RemoteRepositoryUrl = repoInfoFromBaseDirectory.RemoteRepositoryUrl; } } config.GlobalMetadata["newFileRepository"] = repoInfo; } else { config.GlobalMetadata["newFileRepository"] = repoInfoFromBaseDirectory; } }
public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata) { switch (file.Type) { case DocumentType.Article: var page = YamlUtility.Deserialize <PageViewModel>(Path.Combine(file.BaseDir, file.File)); if (page.Items == null || page.Items.Count == 0) { return(null); } if (page.Metadata == null) { page.Metadata = metadata.ToDictionary(p => p.Key, p => p.Value); } else { foreach (var item in metadata) { if (!page.Metadata.ContainsKey(item.Key)) { page.Metadata[item.Key] = item.Value; } } } var filePath = Path.Combine(file.BaseDir, file.File); var repoInfo = GitUtility.GetGitDetail(filePath); // Item's source is the path for the original code, should not be used here var displayLocalPath = repoInfo?.RelativePath ?? filePath.ToDisplayPath(); return(new FileModel(file, page, serializer: new BinaryFormatter()) { Uids = (from item in page.Items select new UidDefinition(item.Uid, displayLocalPath)).ToImmutableArray(), Properties = { LinkToFiles = new HashSet <string>(), LinkToUids = new HashSet <string>(), }, LocalPathFromRepoRoot = displayLocalPath, }); case DocumentType.Overwrite: // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors return(OverwriteDocumentReader.Read(file)); default: throw new NotSupportedException(); } }
public static Dictionary <string, object> ReadMarkdownAsConceptual(string baseDir, string file) { var filePath = Path.Combine(baseDir, file); var repoInfo = GitUtility.GetGitDetail(filePath); return(new Dictionary <string, object> { ["conceptual"] = File.ReadAllText(filePath), ["type"] = "Conceptual", ["source"] = new SourceDetail() { Remote = repoInfo }, ["path"] = file, }); }
public static Dictionary <string, object> ReadMarkdownAsConceptual(string baseDir, string file) { var filePath = Path.Combine(baseDir, file); var repoInfo = GitUtility.GetGitDetail(filePath); return(new Dictionary <string, object> { [Constants.PropertyName.Conceptual] = File.ReadAllText(filePath), [Constants.PropertyName.Type] = "Conceptual", [Constants.PropertyName.Source] = new SourceDetail() { Remote = repoInfo }, [Constants.PropertyName.Path] = file, }); }
private static IEnumerable <T> ReadMarkDownCore <T>(string file) where T : IOverrideDocumentViewModel { var content = File.ReadAllText(file); var repoInfo = GitUtility.GetGitDetail(file); var lineIndex = GetLineIndex(content).ToList(); var yamlDetails = YamlHeaderParser.Select(content); if (yamlDetails == null) { yield break; } var sections = from detail in yamlDetails let id = detail.Id from ms in detail.MatchedSections from location in ms.Value.Locations orderby location.StartLocation descending select new { Detail = detail, Id = id, Location = location }; var currentEnd = Coordinate.GetCoordinate(content); foreach (var item in sections) { if (!string.IsNullOrEmpty(item.Id)) { int start = lineIndex[item.Location.EndLocation.Line] + item.Location.EndLocation.Column + 1; int end = lineIndex[currentEnd.Line] + currentEnd.Column + 1; using (var sw = new StringWriter()) { YamlUtility.Serialize(sw, item.Detail.Properties); using (var sr = new StringReader(sw.ToString())) { var vm = YamlUtility.Deserialize <T>(sr); vm.Conceptual = content.Substring(start, end - start + 1); vm.Documentation = new SourceDetail { Remote = repoInfo, StartLine = item.Location.EndLocation.Line, EndLine = currentEnd.Line }; vm.Uid = item.Id; yield return(vm); } } } currentEnd = item.Location.StartLocation; } }
public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata) { var filePath = file.FullPath; var tocViewModel = Utility.LoadSingleToc(filePath); var toc = new TocItemViewModel { Items = tocViewModel }; var repoDetail = GitUtility.GetGitDetail(filePath); var displayLocalPath = repoDetail?.RelativePath ?? filePath; // todo : metadata. return(new FileModel(file, toc) { Uids = new[] { new UidDefinition(file.File, displayLocalPath) }.ToImmutableArray(), LocalPathFromRepoRoot = displayLocalPath, }); }
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)); }
private static void MergeGitContributeToConfig(BuildJsonConfig config) { GitDetail repoInfoFromBaseDirectory = GitUtility.GetGitDetail(Path.Combine(Directory.GetCurrentDirectory(), config.BaseDirectory)); if (repoInfoFromBaseDirectory?.RelativePath != null) { repoInfoFromBaseDirectory.RelativePath = Path.Combine(repoInfoFromBaseDirectory.RelativePath, DocAsCode.Constants.DefaultOverwriteFolderName); } object gitRespositoryOpenToPublicContributors; if (config.GlobalMetadata.TryGetValue("_gitContribute", out gitRespositoryOpenToPublicContributors)) { GitDetail repoInfo; try { repoInfo = JObject.FromObject(gitRespositoryOpenToPublicContributors).ToObject <GitDetail>(); } catch (Exception e) { throw new DocumentException($"Unable to convert _gitContribute to GitDetail in globalMetadata: {e.Message}", e); } if (repoInfoFromBaseDirectory != null) { if (repoInfo.RelativePath == null) { repoInfo.RelativePath = repoInfoFromBaseDirectory.RelativePath; } if (repoInfo.RemoteBranch == null) { repoInfo.RemoteBranch = repoInfoFromBaseDirectory.RemoteBranch; } if (repoInfo.RemoteRepositoryUrl == null) { repoInfo.RemoteRepositoryUrl = repoInfoFromBaseDirectory.RemoteRepositoryUrl; } } config.GlobalMetadata["_gitContribute"] = repoInfo; } else { config.GlobalMetadata["_gitContribute"] = repoInfoFromBaseDirectory; } }
public static SourceDetail GetSourceDetail(ISymbol symbol) { // For namespace, definition is meaningless if (symbol == null || symbol.Kind == SymbolKind.Namespace) { return(null); } var syntaxRef = symbol.DeclaringSyntaxReferences.LastOrDefault(); if (symbol.IsExtern || syntaxRef == null) { return(new SourceDetail { IsExternalPath = true, Path = symbol.ContainingAssembly?.Name, }); } var syntaxNode = syntaxRef.GetSyntax(); Debug.Assert(syntaxNode != null); if (syntaxNode != null) { var source = new SourceDetail { StartLine = syntaxNode.SyntaxTree.GetLineSpan(syntaxNode.Span).StartLinePosition.Line, Path = syntaxNode.SyntaxTree.FilePath, Name = symbol.Name }; source.Remote = GitUtility.GetGitDetail(source.Path); if (source.Remote != null) { source.Path = source.Path.FormatPath(UriKind.Relative, source.Remote.LocalWorkingDirectory); } return(source); } return(null); }
public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata) { string uid = null; Dictionary <string, object> content = null; var metafile = Path.Combine(file.BaseDir, file.File.TrimEnd('.') + ".meta"); if (File.Exists(metafile)) { content = YamlUtility.Deserialize <Dictionary <string, object> >(metafile); if (content != null) { foreach (var item in metadata) { if (!content.ContainsKey(item.Key)) { content[item.Key] = item.Value; } if (item.Key == Constants.PropertyName.Uid) { uid = item.Value as string; } } } } if (content == null) { content = metadata.ToDictionary(p => p.Key, p => p.Value); } var filePath = Path.Combine(file.BaseDir, file.File); var repoDetail = GitUtility.GetGitDetail(filePath); var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath); return(new FileModel(file, content) { Uids = string.IsNullOrEmpty(uid) ? ImmutableArray <UidDefinition> .Empty : ImmutableArray <UidDefinition> .Empty.Add(new UidDefinition(uid, displayLocalPath)), LocalPathFromRepoRoot = repoDetail?.RelativePath ?? Path.Combine(file.BaseDir, file.File).ToDisplayPath(), LocalPathFromRoot = displayLocalPath }); }
public FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata) { string uid = null; Dictionary <string, object> content = null; var metafile = Path.Combine(file.BaseDir, file.File.TrimEnd('.') + ".meta"); if (File.Exists(metafile)) { content = YamlUtility.Deserialize <Dictionary <string, object> >(metafile); if (content != null) { foreach (var item in metadata) { if (!content.ContainsKey(item.Key)) { content[item.Key] = item.Value; } if (item.Key == "uid") { uid = item.Value as string; } } } } if (content == null) { content = metadata.ToDictionary(p => p.Key, p => p.Value); } var filePath = Path.Combine(file.BaseDir, file.File); var repoDetail = GitUtility.GetGitDetail(filePath); return(new FileModel(file, content) { Uids = string.IsNullOrEmpty(uid) ? ImmutableArray <string> .Empty : ImmutableArray <string> .Empty.Add(uid), LocalPathFromRepoRoot = repoDetail?.RelativePath }); }
private static IEnumerable <OverwriteDocumentModel> ReadMarkDownCore(string file) { var content = File.ReadAllText(file); var repoInfo = GitUtility.GetGitDetail(file); var lineIndex = GetLineIndex(content).ToList(); var yamlDetails = YamlHeaderParser.Select(content); var sections = from detail in yamlDetails let id = detail.Id from location in detail.MatchedSection.Locations orderby location.StartLocation descending select new { Detail = detail, Id = id, Location = location }; var currentEnd = Coordinate.GetCoordinate(content); foreach (var item in sections) { if (!string.IsNullOrEmpty(item.Id)) { int start = lineIndex[item.Location.EndLocation.Line] + item.Location.EndLocation.Column + 1; int end = lineIndex[currentEnd.Line] + currentEnd.Column + 1; yield return(new OverwriteDocumentModel { Uid = item.Id, Metadata = item.Detail.Properties, Conceptual = content.Substring(start, end - start), Documentation = new SourceDetail { Remote = repoInfo, StartLine = item.Location.EndLocation.Line, EndLine = currentEnd.Line, Path = Path.GetFullPath(file).ToDisplayPath() } }); } currentEnd = item.Location.StartLocation; } }
/// <summary> /// Not doing duplication check here, do it outside /// </summary> /// <param name="markdownFilePath"></param> /// <param name="resolvedContent"></param> /// <param name="referenceFolder"></param> /// <param name="yamlHandler"></param> /// <param name="markdown"></param> /// <returns></returns> public static ParseResult TryParseCustomizedMarkdown(string markdownFilePath, string resolvedContent, string referenceFolder, Func <MetadataItem, ParseResult> yamlHandler, out List <MarkdownIndex> markdown) { var gitDetail = GitUtility.GetGitDetail(markdownFilePath); if (string.IsNullOrEmpty(resolvedContent)) { resolvedContent = File.ReadAllText(markdownFilePath); } string markdownFile = resolvedContent; int length = markdownFile.Length; var yamlRegex = new Regex(@"\-\-\-((?!\n)\s)*\n((?!\n)\s)*(?<content>.*)((?!\n)\s)*\n\-\-\-((?!\n)\s)*\n", RegexOptions.Compiled | RegexOptions.Multiline); MatchCollection matches = yamlRegex.Matches(markdownFile); if (matches.Count == 0) { markdown = new List <MarkdownIndex>(); return(new ParseResult(ResultLevel.Warn, "no valid yaml header is found in {0}", markdownFilePath)); } int startIndex = 0; MarkdownIndex lastSection = null; List <MarkdownIndex> sections = new List <MarkdownIndex>(); StringBuilder error = new StringBuilder(); for (int i = 0; i < matches.Count; i++) { var match = matches[i]; string content = match.Groups["content"].Value; MetadataItem viewModel; // Content to yaml try { using (StringReader reader = new StringReader(content)) { viewModel = YamlUtility.Deserialize <MetadataItem>(reader); if (string.IsNullOrEmpty(viewModel.Name)) { throw new ArgumentException("Name for yaml header is required"); } // TODO: override metadata, merge viewmodel? if (yamlHandler != null) { ParseResult result = yamlHandler(viewModel); if (result.ResultLevel != ResultLevel.Success) { throw new ArgumentException(result.Message); } } } } catch (Exception e) { error.AppendFormat("{0} in {1} line {2} is not in a valid yaml format {3}", match.Value, markdownFilePath, markdownFile.Substring(0, startIndex).Split('\n').Length + 2, e.Message); continue; } startIndex = match.Index + match.Length; if (lastSection != null) { lastSection.ContentEndIndex = match.Index - 1; if (lastSection.ContentEndIndex > lastSection.ContentStartIndex) { lastSection.MarkdownContent = markdownFile.Substring(lastSection.ContentStartIndex, lastSection.ContentEndIndex - lastSection.ContentStartIndex + 1); lastSection.Path = lastSection.Path.FormatPath(UriKind.Relative, lastSection.Remote.LocalWorkingDirectory); ExtractReferenceFromMdSection(ref lastSection, referenceFolder); sections.Add(lastSection); } } lastSection = new MarkdownIndex { ApiName = viewModel.Name, ContentStartIndex = startIndex, ContentEndIndex = length - 1, Remote = gitDetail, Path = markdownFilePath }; // endIndex should be set from next match if there is next match if (lastSection.ContentEndIndex > lastSection.ContentStartIndex) { lastSection.MarkdownContent = markdownFile.Substring(lastSection.ContentStartIndex); } } if (lastSection != null) { if (lastSection.Remote != null && !string.IsNullOrEmpty(lastSection.Remote.LocalWorkingDirectory)) { lastSection.Path = lastSection.Path.FormatPath(UriKind.Relative, lastSection.Remote.LocalWorkingDirectory); } ExtractReferenceFromMdSection(ref lastSection, referenceFolder); sections.Add(lastSection); } markdown = sections; if (error.Length > 0) { return(new ParseResult(ResultLevel.Warn, error.ToString())); } return(new ParseResult(ResultLevel.Success)); }
public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata) { switch (file.Type) { case DocumentType.Article: var filePath = Path.Combine(file.BaseDir, file.File); var swaggerContent = File.ReadAllText(filePath); var swagger = GetModelWithoutRef <SwaggerModel>(swaggerContent); swagger.Metadata[DocumentTypeKey] = RestApiDocumentType; swagger.Raw = swaggerContent; var repoInfo = GitUtility.GetGitDetail(filePath); if (repoInfo != null) { swagger.Metadata["source"] = new SourceDetail() { Remote = repoInfo }; } swagger.Metadata = MergeMetadata(swagger.Metadata, metadata); var vm = RestApiItemViewModel.FromSwaggerModel(swagger); var displayLocalPath = repoInfo?.RelativePath ?? Path.Combine(file.BaseDir, file.File).ToDisplayPath(); return(new FileModel(file, vm, serializer: new BinaryFormatter()) { Uids = new UidDefinition[] { new UidDefinition(vm.Uid, displayLocalPath) }.Concat(from item in vm.Children select new UidDefinition(item.Uid, displayLocalPath)).ToImmutableArray(), LocalPathFromRepoRoot = displayLocalPath, Properties = { LinkToFiles = new HashSet <string>(), LinkToUids = new HashSet <string>(), }, }); case DocumentType.Override: var overrides = MarkdownReader.ReadMarkdownAsOverride <RestApiItemViewModel>(file.BaseDir, file.File); if (overrides == null || overrides.Count == 0) { return(null); } displayLocalPath = overrides[0].Documentation?.Remote?.RelativePath ?? Path.Combine(file.BaseDir, file.File).ToDisplayPath(); return(new FileModel(file, overrides, serializer: new BinaryFormatter()) { Uids = (from item in overrides select new UidDefinition( item.Uid, displayLocalPath, item.Documentation.StartLine + 1 )).ToImmutableArray(), Properties = { LinkToFiles = new HashSet <string>(), LinkToUids = new HashSet <string>(), }, LocalPathFromRepoRoot = displayLocalPath, }); default: throw new NotSupportedException(); } }
public void TestGitUtility() { var output = GitUtility.GetGitDetail(Environment.CurrentDirectory); Assert.AreEqual("https://capservice.visualstudio.com/DefaultCollection/CAPS/_git/DocAsCode", output.RemoteRepositoryUrl); }