smollerGraph gets API docs, without changing a comment. Doxygen makes a page for each class and file from the comments in the headers, and since ours start with two slashes, as Google's style guide writes them, a filter gives each one the third slash Doxygen reads. A Docker stage builds the docs on a clean Ubuntu, as CI does, and CI publishes them on GitHub Pages from the public repository, on each push to main.
Topics: Tooling and deployment
The code
Each step on screen is a tag in the database's repository. The code at the end of the episode is ep016/end.
Transcript
Intro
0:00 Welcome to episode sixteen, in which there's nothing new in the database: instead, it gets docs, a page for each class and file, made from the comments we've been writing all along. Here's where we'll be by the end of the episode.
0:12 By the end, our code is going to have API docs, on a website of their own. Each class has a page, such as the graph's, with its public functions, and what each one does, in the words of the comment over it, where it has one of its own.
0:28 The series' website is the place to start, if you'd like to know more about the project. The code is open source, on GitHub, with a tag at each step of each episode, so you can pick it up at any point. And each episode has a page of its own, with its chapters and its code.
Last time
0:45 Last time, the graph went out to a graph neural network and came back. sg export wrote it as JSON Lines and CSV, for a script to read. A GraphSAGE in PyTorch learnt which species are threatened, from their relatives. And its risks went back into the graph, for Cypher to ask about. That's fifteen episodes of code, and to find out what a function does, you still have to open its header. Today, the comments in the headers become docs, on a website of their own.
Today
1:17 Here's today's work, in three steps, from the headers to a website. Doxygen, a tool that makes docs from the comments in code, is going to make ours, with a target in the CMake file. A stage in the Dockerfile is going to build them on a clean Ubuntu, the way CI is going to. And CI is going to publish them on GitHub Pages, a website GitHub serves for a repository, each time the public repository's main changes.
Docs from comments
1:45 Doxygen parses the code, so it knows each class, function and field, and it takes the comment just before each one as its documentation. There's a catch, though, with comments like ours. Ours start with two slashes, as Google's style guide writes them, while Doxygen reads comments written its own ways, such as with three slashes, or a slash and two stars, and leaves plain comments out. One way round it would be to add a slash to each of the three hundred or so lines of comments in our headers. The other is a filter, a command Doxygen runs on each header before it reads it, which adds the third slash for it, so the headers stay as they are. Doxygen then reads the comments, and the graph's page says what Open does, in the comment's own words.
A docs target
2:31 In the CMake file, at the end, find package looks for Doxygen. The comment over it says what the block is for: the API docs, from the comments in the headers, when Doxygen is installed, which building the docs target writes to build, docs, html. Without Doxygen, the rest is skipped, so building the database doesn't need it. When it's found, fetch content declares a theme, doxygen-awesome, from a tag's archive on GitHub, the way Abseil and GoogleTest come, and its comment says it restyles Doxygen's pages, sidebar and all. Then make available downloads it, as it does the others. Then the settings, each a variable whose name starts with DOXYGEN. The project's name is smollerGraph, and its brief is a small graph database, from scratch. The pages go in the build folder, under docs, and the README is the front page. Extract all puts each class, and its public functions and fields, in the docs, with a comment or without one. Javadoc autobrief takes the first sentence of a comment as its brief, the line that lists show. Include graph turns off a drawing, and the comment over it says which: a file's page draws the files that include it, but not all it includes, which is most of the standard library. Tree view, the index, a sidebar that isn't full height and the light colour style are the settings the theme asks for, and its two stylesheets go in as extras. Filter patterns says that each header goes through sed first, a command line tool that edits text. Its expression finds the start of a line, any spaces, and two slashes, and puts back what it found, the ampersand, with a third slash after it. The comment over it says why: our comments start with two slashes, as Google's style guide writes them, and Doxygen reads comments written its own ways, such as with three. Last, doxygen add docs makes the docs target, from the README and the source folder.
4:23 Building the docs target runs Doxygen, which reads the README and each file in the source folder, through the filter for the headers, and writes out each page as it goes. It's all done in under a second, on this Mac. That's about two hundred and fifty files, the pages, with their scripts, diagrams and the search's index, and about three and a half megabytes in all.
4:45 The front page is the README, with its sections in the sidebar, such as building and running, and the UI. Files lists the source files, and the query header's page lists what's in it. Path is a struct, and its brief is the first sentence of its comment: a path through the graph, its nodes, from its start to its end, and the relationships between them. And Cheapest path, from episode thirteen, has the first sentence of its comment: it returns a cheapest path, whose weights add up to as little as any other's, found with Dijkstra's algorithm.
In Docker
5:18 CI is going to build the docs on a fresh machine, with whatever Doxygen it can install, so the Dockerfile builds them first, the way CI is going to. At the end, the Dockerfile gets two new stages. Docs build starts from the toolchain stage, where CMake, Ninja and a compiler are, and the comment over it says how to use it: docker build, with the docs target and output site, writes their pages to the site folder. And configuring is enough, as building the docs reads the sources and compiles nothing. The stage installs Doxygen, and Graphviz, which draws its diagrams, from Ubuntu's packages, then clears apt's lists, as the toolchain does. It copies the source in, configures the project, and builds the docs target. Docs is the last stage: from scratch, an empty image, with the pages copied in from the stage before, so the output flag copies out the pages and nothing else. The site folder goes in the Docker ignore file, so a build doesn't send the last one's pages along with the source, and in the git ignore file too.
6:21 Docker builds the stages the docs need, one after another. The toolchain comes from its cache, from the other builds, so it installs Doxygen and Graphviz on top, in about fifteen seconds. Then configuring downloads the theme with the other dependencies, and Doxygen runs, Ubuntu's version of it, three years older than the one on this Mac. The site folder has those pages, and some thirty more files, mostly icons, which the older Doxygen writes as files of their own. The graph's page is there too, named a little differently, as Linux tells upper and lower case apart in file names.
On GitHub Pages
6:56 In the CI workflow, the test job, which checks the style, runs the tests and runs clang-tidy, gets two more steps. Build the API docs is the first of the two. The comment over it says why it's there: the API docs go up from the public repository, where each episode arrives once it's out, and the private one, whose Actions minutes count, skips them. So it runs in a public repository and nowhere else, and it runs the docs stage, into the site folder. Upload pages artifact packs the site folder up for GitHub Pages, as an artifact, a file the workflow keeps. A second job, called docs, deploys the artifact, and the comment over it says where to: on each push to the public repository's main, to nenuadrian dot github dot io, slash smollerGraph. Its condition is a run on main, in a public repository, and it needs the test job, so docs go up from code whose tests pass, and no other. Its permissions let it write to Pages, and give it an ID token, which Pages checks the deployment with. Its environment is GitHub Pages, whose URL is the one the deploy step reports, and that step, deploy pages, is all the job does.
8:09 Here's the path from a push to the docs. An episode's code arrives in the public repository's main once the episode is out. The test job checks it, and builds the docs in Docker. It hands the pages to GitHub as an artifact, and the docs job deploys them, so the docs online match the last code in the public repository that passed, and a step that fails stops the docs from changing.
What's missing
8:33 Here's some of what our docs don't do yet. They're the docs of the latest code, where a library with releases keeps the docs of each, as each episode's tag would be here. They list what's in the code, where a guide to how the parts fit, how a query becomes a plan, say, would be pages written for the purpose. And Doxygen warns about what it can't make sense of, which CI could treat as a failure, so the docs can't quietly break.
Wrap-up
8:58 So, today smollerGraph got API docs, without changing a comment. Doxygen made a page for each class and file, from the comments in the headers, with a filter that gives each one the third slash Doxygen reads. A stage in the Dockerfile builds them on a clean Ubuntu, the way CI does. And CI publishes them on GitHub Pages, from the public repository, each time its main changes.
9:24 This is going to be an evolving project, so I'd love to hear from you. Let me know in the comments how you document your own code, and if you'd like to follow along, subscribe to the channel. I hope you find this useful, and thanks for watching.