private static ParseResult TryParseYamlMetadataFile(string metadataFileName, out MetadataItem projectMetadata) { projectMetadata = null; try { using (StreamReader reader = new StreamReader(metadataFileName)) { projectMetadata = YamlUtility.Deserialize <MetadataItem>(reader); return(new ParseResult(ResultLevel.Success)); } } catch (Exception e) { return(new ParseResult(ResultLevel.Error, e.Message)); } }
private static ParseResult TryExportYamlMetadataFile(MetadataItem doc, string folder, UriKind uriKind, out string filePath) { filePath = Path.Combine(folder, doc.Name).FormatPath(uriKind, folder); try { using (StreamWriter writer = new StreamWriter(filePath)) { YamlUtility.Serialize(writer, doc); return(new ParseResult(ResultLevel.Success, "Successfully generated metadata {0} for {1}", filePath, doc.Name)); } } catch (Exception e) { return(new ParseResult(ResultLevel.Error, e.Message)); } }
private static ParseResult ResolveAndExportYamlMetadata(Dictionary <string, MetadataItem> allMembers, string folder, string indexFileName, string tocFileName, string apiFolder) { var model = YamlMetadataResolver.ResolveMetadata(allMembers, apiFolder); // 1. generate toc.yml model.TocYamlViewModel.Href = tocFileName; model.TocYamlViewModel.Type = MemberType.Toc; // TOC do not change var tocViewModel = TocViewModel.Convert(model.TocYamlViewModel); string tocFilePath = Path.Combine(folder, tocFileName); using (StreamWriter sw = new StreamWriter(tocFilePath)) { YamlUtility.Serialize(sw, tocViewModel); } // 2. generate api.yml string indexFilePath = Path.Combine(folder, indexFileName); using (StreamWriter sw = new StreamWriter(indexFilePath)) { YamlUtility.Serialize(sw, model.Indexer); } // 3. generate each item's yaml var members = model.Members; foreach (var memberModel in members) { string itemFilepath = Path.Combine(folder, apiFolder, memberModel.Href); Directory.CreateDirectory(Path.GetDirectoryName(itemFilepath)); using (StreamWriter sw = new StreamWriter(itemFilepath)) { var memberViewModel = OnePageViewModel.Convert(memberModel); YamlUtility.Serialize(sw, memberViewModel); ParseResult.WriteToConsole(ResultLevel.Success, "Metadata file for {0} is saved to {1}", memberModel.Name, itemFilepath); } } return(new ParseResult(ResultLevel.Success)); }
/// <summary> /// Extract YAML format content from yaml header /// </summary> /// <param name="content">the whole matched yaml header</param> /// <param name="requiredProperties">The properties that should be specified</param> /// <param name="properties"></param> /// <param name="message"></param> /// <returns></returns> private static bool TryExtractProperties(string content, IEnumerable <string> requiredProperties, out Dictionary <string, object> properties, out string message) { properties = new Dictionary <string, object>(); message = string.Empty; if (string.IsNullOrEmpty(content)) { return(false); } try { using (StringReader reader = new StringReader(content)) { properties = YamlUtility.Deserialize <Dictionary <string, object> >(reader); string checkPropertyMessage; bool checkPropertyStatus = CheckRequiredProperties(properties, requiredProperties, out checkPropertyMessage); if (!checkPropertyStatus) { throw new InvalidDataException(checkPropertyMessage); } if (!string.IsNullOrEmpty(checkPropertyMessage)) { message += checkPropertyMessage; } } return(true); } catch (Exception e) { message += string.Format(@"yaml header --- {0} --- is not in a valid YAML format: {1}.", content, e.Message); return(false); } }