|
23 | 23 | import subprocess |
24 | 24 | import sys |
25 | 25 | import tempfile |
| 26 | +import textwrap |
26 | 27 | import yaml |
27 | 28 | from datetime import datetime |
28 | 29 | from pathlib import Path |
@@ -223,12 +224,68 @@ def _prepare_new_library_config(library_config: Dict) -> Dict: |
223 | 224 | return library_config |
224 | 225 |
|
225 | 226 |
|
| 227 | +def _update_docs_index_rst(output: str, new_library_config: Dict): |
| 228 | + """Creates or updates the docs/index.rst file for a library. |
| 229 | +
|
| 230 | + If the file does not exist, it will be created. If it exists, a new |
| 231 | + API reference section will be appended. |
| 232 | +
|
| 233 | + Args: |
| 234 | + output(str): Path to the directory where code should be generated. |
| 235 | + new_library_config(Dict): The configuration of the new library. |
| 236 | + """ |
| 237 | + library_id = _get_library_id(new_library_config) |
| 238 | + # a library can have multiple apis, but we'll use the first one to get the version |
| 239 | + api_version = Path(new_library_config["apis"][0]["path"]).name.replace("_", "-") |
| 240 | + docs_dir = Path(output) / "packages" / library_id / "docs" |
| 241 | + os.makedirs(docs_dir, exist_ok=True) |
| 242 | + index_rst_path = docs_dir / "index.rst" |
| 243 | + |
| 244 | + new_api_reference = textwrap.dedent(f"""\ |
| 245 | + API Reference |
| 246 | + ------------- |
| 247 | + .. toctree:: |
| 248 | + :maxdepth: 2 |
| 249 | +
|
| 250 | + {api_version}/services |
| 251 | + {api_version}/types |
| 252 | + """) |
| 253 | + |
| 254 | + if not index_rst_path.exists(): |
| 255 | + content = textwrap.dedent(f"""\ |
| 256 | + .. include:: README.rst |
| 257 | +
|
| 258 | + .. include:: multiprocessing.rst |
| 259 | +
|
| 260 | + {new_api_reference} |
| 261 | +
|
| 262 | + Changelog |
| 263 | + --------- |
| 264 | +
|
| 265 | + For a list of all ``{library_id}`` releases: |
| 266 | +
|
| 267 | + .. toctree:: |
| 268 | + :maxdepth: 2 |
| 269 | +
|
| 270 | + CHANGELOG |
| 271 | + """) |
| 272 | + _write_text_file(str(index_rst_path), content) |
| 273 | + else: |
| 274 | + content = _read_text_file(str(index_rst_path)) |
| 275 | + # a changelog is guaranteed to exist |
| 276 | + changelog_section = "Changelog\n---------" |
| 277 | + parts = content.split(changelog_section) |
| 278 | + # insert api reference before the changelog |
| 279 | + # The prepended newline is important for formatting. |
| 280 | + content = parts[0] + "\n" + new_api_reference + changelog_section + parts[1] |
| 281 | + _write_text_file(str(index_rst_path), content) |
| 282 | + |
226 | 283 | def handle_configure( |
227 | 284 | librarian: str = LIBRARIAN_DIR, |
228 | 285 | source: str = SOURCE_DIR, |
229 | 286 | repo: str = REPO_DIR, |
230 | 287 | input: str = INPUT_DIR, |
231 | | - output: str = OUTPUT_DIR |
| 288 | + output: str = OUTPUT_DIR, |
232 | 289 | ): |
233 | 290 | """Onboards a new library by completing its configuration. |
234 | 291 |
|
@@ -259,13 +316,14 @@ def handle_configure( |
259 | 316 | # configure-request.json contains the library definitions. |
260 | 317 | request_data = _read_json_file(f"{librarian}/{CONFIGURE_REQUEST_FILE}") |
261 | 318 | new_library_config = _get_new_library_config(request_data) |
262 | | - |
| 319 | + |
263 | 320 | _update_global_changelog( |
264 | 321 | f"{repo}/CHANGELOG.md", |
265 | 322 | f"{output}/CHANGELOG.md", |
266 | 323 | [new_library_config], |
267 | 324 | ) |
268 | 325 | prepared_config = _prepare_new_library_config(new_library_config) |
| 326 | + _update_docs_index_rst(output, new_library_config) |
269 | 327 |
|
270 | 328 | # Write the new library configuration to configure-response.json. |
271 | 329 | _write_json_file(f"{librarian}/configure-response.json", prepared_config) |
|
0 commit comments