56 std::unique_ptr<MustacheTemplateFile> &Template, StringRef TemplatePath,
57 std::vector<std::pair<StringRef, StringRef>> Partials) {
59 if (Error Err = T.takeError())
61 Template = std::move(T.get());
62 for (
const auto &[Name, FileName] : Partials)
63 if (
auto Err = Template->registerPartialFile(Name, FileName))
65 return Error::success();
69 StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
72 llvm::TimeTraceScope TS(
"Setup Templates");
78 llvm::TimeTraceScope TS(
"Generate JSON for Mustache");
81 RootDir, std::move(Infos), CDCtx))
87 SmallString<128> JSONDirPath(RootDir);
88 SmallString<128> DocsDirPath(RootDir);
90 TimeTraceScope TS(
"Create Output Directories");
91 sys::path::append(JSONDirPath,
"json");
92 if (
auto EC = sys::fs::create_directories(JSONDirPath))
93 return createFileError(JSONDirPath, EC);
94 sys::path::append(DocsDirPath, DirName);
95 if (
auto EC = sys::fs::create_directories(DocsDirPath))
96 return createFileError(DocsDirPath, EC);
100 llvm::TimeTraceScope TS(
"Iterate JSON files");
102 sys::fs::recursive_directory_iterator JSONIter(JSONDirPath, EC);
103 std::vector<json::Value> JSONFiles;
104 JSONFiles.reserve(Infos.size());
106 return createStringError(
"Failed to create directory iterator.");
108 while (JSONIter != sys::fs::recursive_directory_iterator()) {
110 if (JSONIter->type() == sys::fs::file_type::directory_file) {
111 SmallString<128> DocsClonedPath(JSONIter->path());
112 sys::path::replace_path_prefix(DocsClonedPath, JSONDirPath,
114 if (
auto EC = sys::fs::create_directories(DocsClonedPath)) {
115 return createFileError(DocsClonedPath, EC);
120 return createFileError(
"Failed to iterate: " + JSONIter->path(), EC);
122 auto Path = StringRef(JSONIter->path());
123 if (!Path.ends_with(
".json")) {
124 JSONIter.increment(EC);
128 auto File = MemoryBuffer::getFile(Path);
129 if (EC = File.getError(); EC) {
130 unsigned ID = CDCtx.
Diags.getCustomDiagID(DiagnosticsEngine::Warning,
131 "Failed to open file: %0 %1");
132 CDCtx.
Diags.Report(ID) << Path << EC.message();
133 JSONIter.increment(EC);
137 auto Parsed = json::parse((*File)->getBuffer());
139 return Parsed.takeError();
140 auto ValidJSON = Parsed.get();
142 std::error_code FileErr;
143 SmallString<128> DocsFilePath(JSONIter->path());
144 sys::path::replace_path_prefix(DocsFilePath, JSONDirPath, DocsDirPath);
145 sys::path::replace_extension(DocsFilePath, DirName);
146 raw_fd_ostream InfoOS(DocsFilePath, FileErr, sys::fs::OF_None);
152 getInfoTypeStr(Parsed->getAsObject(), sys::path::stem(DocsFilePath));
154 return InfoTypeStr.takeError();
156 InfoTypeStr.get(), RelativeRootPath))
158 JSONIter.increment(EC);
162 return Error::success();
clang::DiagnosticsEngine & Diags
llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< std::unique_ptr< doc::Info > > Infos, const clang::doc::ClangDocContext &CDCtx, std::string DirName) override
The main orchestrator for Mustache-based documentation.
virtual llvm::Error generateDocForJSON(llvm::json::Value &JSON, llvm::raw_fd_ostream &OS, const ClangDocContext &CDCtx, StringRef ObjectTypeStr, StringRef RelativeRootPath)=0
Populates templates with data from JSON and calls any specifics for the format.
llvm::Error setupTemplate(std::unique_ptr< MustacheTemplateFile > &Template, StringRef TemplatePath, std::vector< std::pair< StringRef, StringRef > > Partials)
Registers partials to templates.