|
1 | 1 | # api |
2 | | -Stores and makes available API docs for all libhal packages |
| 2 | + |
| 3 | +Stores and makes available API docs for all libhal packages and their various |
| 4 | +versions. |
| 5 | + |
| 6 | +## 📂 File Layout |
| 7 | + |
| 8 | +```plaintext |
| 9 | +. |
| 10 | +├── LICENSE |
| 11 | +├── README.md |
| 12 | +├── libhal |
| 13 | +│ ├── 4.10.0 |
| 14 | +│ ├── main |
| 15 | +│ └── switcher.json |
| 16 | +├── <package> |
| 17 | +│ ├── <version 1> |
| 18 | +│ ├── <version 2> |
| 19 | +│ └── switcher.json |
| 20 | +└── libhal-util |
| 21 | + ├── 5.4.3 |
| 22 | + ├── main |
| 23 | + └── switcher.json |
| 24 | +``` |
| 25 | + |
| 26 | +Each directory corresponds to package or repo in the organization. The |
| 27 | +subdirectories are named for each version (or branch) that API docs are made |
| 28 | +for. Contained within are the static HTML, JS, and CSS files generated from |
| 29 | +Doxygen and Sphinx. The `switcher.json` file is used to provide a mapping from |
| 30 | +"version" to a URL which will appear on a drop down in the rendered |
| 31 | +documentation. The URL for the switcher MUST be absolute and is hard coded at |
| 32 | +documentation build time. |
| 33 | + |
| 34 | +## 🚀 Adding API doc support for a package |
| 35 | + |
| 36 | +### ⚙️ Deploy API Action |
| 37 | + |
| 38 | +> [!NOTE] |
| 39 | +> This is for libhal project only. At some later time I will document how |
| 40 | +> general C++ projects can use this same setup to deploy API docs for |
| 41 | +> themselves. |
| 42 | +
|
| 43 | +Add the following file to your github workflow `/.github/workflow/api.yml`: |
| 44 | + |
| 45 | +```yaml |
| 46 | +name: 📚 Deploy APIs |
| 47 | + |
| 48 | +on: |
| 49 | + release: |
| 50 | + types: |
| 51 | + - published |
| 52 | + push: |
| 53 | + branches: |
| 54 | + - main |
| 55 | + workflow_dispatch: |
| 56 | + |
| 57 | +permissions: |
| 58 | + contents: write |
| 59 | + |
| 60 | +jobs: |
| 61 | + deploy_api_docs: |
| 62 | + runs-on: ubuntu-latest |
| 63 | + steps: |
| 64 | + - uses: actions/checkout@v3 |
| 65 | + - uses: actions/setup-python@v4 |
| 66 | + with: |
| 67 | + python-version: 3.x |
| 68 | + - run: sudo apt update |
| 69 | + - run: sudo apt install -y doxygen |
| 70 | + - run: pip install -r docs/requirements.txt gitpython requests |
| 71 | + - run: wget https://raw.githubusercontent.com/libhal/ci/5.x.y/scripts/api.py |
| 72 | + - run: python api.py build --version ${{ github.ref_name }} |
| 73 | + - run: python api.py deploy --version ${{ github.ref_name }} --repo-name ${{ github.event.repository.name }} |
| 74 | + env: |
| 75 | + GITHUB_TOKEN: ${{ secrets.API_PUSH_TOKEN }} |
| 76 | +``` |
| 77 | +
|
| 78 | +> [!info] |
| 79 | +> Notice the `api.py` file. This file is the program that makes all of the |
| 80 | +> magic happen. The script above downloads the latest `5.x.y` version of the |
| 81 | +> script from the `libhal/ci` directory in order to build and deploy the API |
| 82 | +> docs. If there is something wrong, its likely to have to do with the `api.py` |
| 83 | +> script. |
| 84 | + |
| 85 | +### 🔑 Generating PAT token |
| 86 | + |
| 87 | +Notice the line `GITHUB_TOKEN: ${{ secrets.API_PUSH_TOKEN }}`. This is a PAT |
| 88 | +(Personal API Token). The default `GITHUB_TOKEN` only provides limited access |
| 89 | +to other repos. You cannot push to repos within an organization without a token |
| 90 | +with higher permissions. You need a token that has the permissions to update |
| 91 | +this repo and we'd like to limit that token to only being capable of updating |
| 92 | +this repo. |
| 93 | + |
| 94 | +1. To generate a PAT token follow the instructions in this page: |
| 95 | +[🔗 Creating a fine-grained personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token). |
| 96 | +1. When you get to the point where you can choose the **Repository access** |
| 97 | + Select: `Only select repositories`. |
| 98 | + 1. In the dropdown select: Select `<organization>/api` |
| 99 | +1. Set **Repository Permissions** access to: |
| 100 | + 1. Set `Contents` ➡️ Access: "Read and Write" |
| 101 | + 1. Set `Pull Requests` ➡️ Access: "Read and Write" |
| 102 | +1. Press **Save** |
| 103 | +1. You should be presented with a token, copy the token value and paste to a |
| 104 | + text editor for safe keeping. |
| 105 | +1. Go to your organization's secrets page using the URL: |
| 106 | + `https://github.com/organizations/<organization>/settings/secrets/actions`. |
| 107 | +1. Click the button `new organization secret` |
| 108 | +1. Set the name to `API_PUSH_TOKEN` and the value to the token you copied |
| 109 | + earlier. If you have no further use for the token, you can delete from your |
| 110 | + text editor. |
| 111 | + |
| 112 | +### 📑 Copy `docs/` from `libhal` |
| 113 | + |
| 114 | +The contents of `libhal`'s `docs/` directory has all of the doxygen and sphinx |
| 115 | +files needed to generate the API docs like `libhal`. Our docs use doxygen to |
| 116 | +generate XML docs based on the C++ code in the `include/` (the public API) |
| 117 | +directory. This is fed into `Sphinx` using the `Breathe` plugin to combine |
| 118 | +`doxygen` with `Sphinx`. The theme we use is the |
| 119 | +[`PyData` theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/). |
| 120 | + |
| 121 | +Within the `docs/libhal` directory you will find `.md` files and a `index.rst` |
| 122 | +file. The `index.rst` file organizes all of the markdown files and the markdown |
| 123 | +files are used to manually type out how you want the docs for a particular class |
| 124 | +or source file to be rendered. Change these to fit your package. |
| 125 | + |
| 126 | +Update the `json_url` for the switcher to the following and update the |
| 127 | +`<organization>` and `<package>` templates to the correct value: |
| 128 | + |
| 129 | +```python |
| 130 | +html_theme_options = { |
| 131 | + "switcher": { |
| 132 | + "json_url": "https://<organization>.github.io/<organization>/<package>/switcher.json", |
| 133 | + }, |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +Update these lines and replace `<package>` with the name of your package: |
| 138 | + |
| 139 | +```python |
| 140 | +breathe_projects = {"<package>": "../build/xml"} |
| 141 | +breathe_default_project = "<package>" |
| 142 | +breathe_default_members = ('members',) |
| 143 | +project = "<package>" |
| 144 | +``` |
| 145 | + |
| 146 | +Update `docs/index.rst` to fit what you'd like to show as the front page of your |
| 147 | +package. |
| 148 | + |
| 149 | +### 🎉 Done |
| 150 | + |
| 151 | +And you are done! The next time you push to main, make a release, OR manually |
| 152 | +run the `api.yml` workflow, your API docs will be generated, a new branch, |
| 153 | +commit, and PR will be made to `organization/api` that you can later decide to |
| 154 | +pull into the repo or decline. |
0 commit comments