Doxygen
Loading...
Searching...
No Matches
mermaid.cpp File Reference
#include "mermaid.h"
#include <fstream>
#include <mutex>
#include "config.h"
#include "debug.h"
#include "dir.h"
#include "doxygen.h"
#include "fileinfo.h"
#include "indexlist.h"
#include "message.h"
#include "portable.h"
#include "threadpool.h"
#include "util.h"
Include dependency graph for mermaid.cpp:

Go to the source code of this file.

Functions

static void runMermaid (const MermaidManager::DiagramList &diagrams)

Variables

static std::mutex g_mermaidMutex
static int g_mermaidIndex = 1

Function Documentation

◆ runMermaid()

void runMermaid ( const MermaidManager::DiagramList & diagrams)
static

Definition at line 134 of file mermaid.cpp.

135{
136 //printf("runMermaidContent for %zu images\n",contentList.size());
137 if (diagrams.empty()) return;
138
139 DString mmdc = Config_getString(MERMAID_PATH);
140 if (!mmdc.empty() && mmdc.at(mmdc.length()-1) != '/' && mmdc.at(mmdc.length()-1) != '\\')
141 {
142 mmdc += "/";
143 }
144 mmdc += "mmdc";
145
146 DString mermaidConfigFile = Config_getString(MERMAID_CONFIG_FILE);
147
148 struct MermaidCmd
149 {
150 MermaidCmd(const DString &mmdc_,const DString &args_,const DString &ext_,const DString &srcFile_,int srcLine_) :
151 mmdc(mmdc_), args(args_), ext(ext_), srcFile(srcFile_), srcLine(srcLine_) {}
152 DString mmdc;
153 DString args;
154 DString ext;
155 DString srcFile;
156 int srcLine;
157 };
158 std::vector<MermaidCmd> mermaidCmds;
159
160 for (const auto &diagram : diagrams)
161 {
162 //printf("content=%s\n",qPrint(mc.content));
163 if (diagram.info.content.empty()) continue;
164
165 DString ext = MermaidManager::imageExtension(diagram.imageFormat);
166
167 DString inputFile = diagram.info.baseName + ".mmd";
168 DString outputFile = diagram.info.baseName + "." + ext;
169
170 // Check if content has changed since last run (caching)
171 FileInfo fi(outputFile.str());
172 if (fi.exists())
173 {
174 DString cachedContent = fileToString(inputFile);
175 if (cachedContent == diagram.info.content)
176 {
177 continue;
178 }
179 }
180
181 // Build the mmdc command arguments
182 DString args;
183 args += "-q -i \"" + inputFile + "\" ";
184 args += "-o \"" + outputFile + "\" ";
185
186 if (!mermaidConfigFile.empty())
187 {
188 args += "-c \"" + mermaidConfigFile + "\" ";
189 }
190
191 mermaidCmds.emplace_back(mmdc, args,ext, diagram.info.srcFile, diagram.info.srcLine);
192 }
193
194 std::size_t numThreads = static_cast<std::size_t>(Config_getInt(DOT_NUM_THREADS));
195 size_t offset=0;
196 size_t total=mermaidCmds.size();
197 msg("Generating {} Mermaid files using {} threads\n", total, numThreads);
198 if (numThreads>1) // multi threaded version
199 {
200 ThreadPool threadPool(numThreads);
201 std::vector< std::future<int> > results;
202
203 // queue the work
204 for (const auto &cmd : mermaidCmds)
205 {
206 auto processFile = [&cmd]()
207 {
208 Debug::print(Debug::Mermaid, 0, "*** MermaidManager::run Running: {} {}\n", cmd.mmdc, cmd.args);
209 int exitCode = Portable::system(cmd.mmdc.data(), cmd.args.data(), true);
210 if (exitCode != 0)
211 {
212 err_full(cmd.srcFile, cmd.srcLine,
213 "Problems running Mermaid (mmdc). Verify that the command '{} {}' works from the command line. Exit code: {}.",
214 cmd.mmdc, cmd.args, exitCode);
215 }
216 return exitCode;
217 };
218 results.emplace_back(threadPool.queue(processFile));
219 }
220
221 // wait for the results
222 for (auto &f : results)
223 {
224 offset++;
225 msg("Generating Mermaid file {}/{}\n", offset, total);
226 f.get();
227 }
228 }
229 else // single threaded version
230 {
231 for (const auto &cmd : mermaidCmds)
232 {
233 offset++;
234 msg("Generating Mermaid file {}/{}\n", offset, total);
235 Debug::print(Debug::Mermaid, 0, "*** MermaidManager::run Running: {} {}\n", cmd.mmdc, cmd.args);
236
237 int exitCode = Portable::system(cmd.mmdc.data(), cmd.args.data(), true);
238 if (exitCode != 0)
239 {
240 err_full(cmd.srcFile, cmd.srcLine,
241 "Problems running Mermaid (mmdc). Verify that the command '{} {}' works from the command line. Exit code: {}.",
242 cmd.mmdc, cmd.args, exitCode);
243 }
244 }
245 }
246}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
char & at(size_t i)
Returns a reference to the character at index i.
Definition dstring.h:686
const std::string & str() const
Definition dstring.h:645
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
@ Mermaid
Definition debug.h:52
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:78
Minimal replacement for QFileInfo.
Definition fileinfo.h:26
static DString imageExtension(ImageFormat imageFormat)
Definition mermaid.cpp:48
Class managing a pool of worker threads.
Definition threadpool.h:48
#define Config_getInt(name)
Definition config.h:34
#define Config_getString(name)
Definition config.h:32
#define msg(fmt,...)
Definition message.h:94
#define err_full(file, line, fmt,...)
Definition message.h:132
int system(const DString &command, const DString &args, bool commandHasConsole=true)
Definition portable.cpp:121
DString fileToString(const DString &name, bool filter, bool isSourceCode)
Definition util.cpp:1053

References DString::at(), Config_getInt, Config_getString, DString::empty(), err_full, FileInfo::exists(), fileToString(), MermaidManager::imageExtension(), DString::length(), Debug::Mermaid, msg, Debug::print(), ThreadPool::queue(), DString::str(), and Portable::system().

Referenced by MermaidManager::run().

Variable Documentation

◆ g_mermaidIndex

int g_mermaidIndex = 1
static

Definition at line 36 of file mermaid.cpp.

Referenced by MermaidManager::writeMermaidSource().

◆ g_mermaidMutex

std::mutex g_mermaidMutex
static

Definition at line 35 of file mermaid.cpp.

Referenced by MermaidManager::writeMermaidSource().