|
| 1 | +# score_sync_toml |
| 2 | + |
| 3 | +A Sphinx extension that configures and drives |
| 4 | +[`needs-config-writer`](https://needs-config-writer.useblocks.com) to produce a |
| 5 | +`ubproject.toml` file at build time. This file is consumed **statically** by the |
| 6 | +[ubCode VS Code extension](https://ubcode.useblocks.com) to power the language server |
| 7 | +(need indexing, autocompletion, hover, schema validation, etc.). |
| 8 | + |
| 9 | +**The problem it solves:** ubCode cannot run Sphinx. It needs a static |
| 10 | +`ubproject.toml` to know which RST directives are valid need types, what link types |
| 11 | +exist, what schemas apply, and so on. `score_sync_toml` bridges this gap by |
| 12 | +serialising the live Sphinx-Needs configuration into that file after every build. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Files |
| 17 | + |
| 18 | +| File | Purpose | |
| 19 | +|---|---| |
| 20 | +| `__init__.py` | Sphinx extension — configures `needs_config_writer` and generates helper TOML | |
| 21 | +| `shared.toml` | Static TOML fragment always merged into the output `ubproject.toml` | |
| 22 | +| `BUILD` | Bazel build definition | |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## How it works |
| 27 | + |
| 28 | +### 1. `setup(app)` configures `needs_config_writer` |
| 29 | + |
| 30 | +| Setting | Value | Reason | |
| 31 | +|---|---|---| |
| 32 | +| `needscfg_outpath` | `ubproject.toml` | Where to write the final file (relative to `confdir`) | |
| 33 | +| `needscfg_overwrite` | `True` | Always regenerate on each build | |
| 34 | +| `needscfg_write_all` | `True` | Write full config, not just diffs, so the file is self-contained | |
| 35 | +| `needscfg_exclude_defaults` | `True` | Omit default values to keep the file readable | |
| 36 | +| `needscfg_warn_on_diff` | `False` | Don't fail CI when the file changes | |
| 37 | + |
| 38 | +### 2. Excluded variables |
| 39 | + |
| 40 | +Some Sphinx-Needs config values are excluded from serialisation because they either |
| 41 | +cannot be represented in TOML or are managed elsewhere: |
| 42 | + |
| 43 | +| Variable | Reason for exclusion | |
| 44 | +|---|---| |
| 45 | +| `needs_from_toml` / `needs_from_toml_table` | Meta-config consumed by `needs_config_writer` itself | |
| 46 | +| `needs_schema_definitions_from_json` | Pointer handled via `shared.toml` | |
| 47 | +| `needs_schema_definitions` | Generated schema content — managed via `schemas.json` / `score_metamodel` | |
| 48 | +| `needs_render_context` | Contains Python callables (`draw_full_interface` etc.) — not serialisable | |
| 49 | + |
| 50 | +### 3. Merging `shared.toml` |
| 51 | + |
| 52 | +The static `shared.toml` is always merged into the output. It contributes: |
| 53 | + |
| 54 | +- **`[parse.extend_directives]`** — teaches ubCode to parse third-party RST |
| 55 | + directives (`grid`, `grid-item-card`, `uml`) without treating them as errors. |
| 56 | +- **`schema_definitions_from_json = "schemas.json"`** — points ubCode to the |
| 57 | + JSON Schema validation file generated by `score_metamodel`. |
| 58 | +- **`index_on_save = true`** — tells ubCode to re-index whenever a file is saved. |
| 59 | + |
| 60 | +### 4. `_write_needs_types_toml(app)` — generating `[[needs.types]]` |
| 61 | + |
| 62 | +`needs_config_writer` cannot serialise the project's custom `ScoreNeedType` dicts |
| 63 | +(they contain Python-specific data) and silently skips them. Without |
| 64 | +`[[needs.types]]` entries in `ubproject.toml`, **ubCode does not recognise any |
| 65 | +directives as needs** and the index is empty. |
| 66 | + |
| 67 | +`_write_needs_types_toml` works around this by: |
| 68 | + |
| 69 | +1. Reading `app.config.needs_types` — already populated by `score_metamodel` from |
| 70 | + `metamodel.yaml` before `score_sync_toml` runs. |
| 71 | +2. Writing `needs_types_generated.toml` next to `conf.py` containing clean |
| 72 | + `[[needs.types]]` entries with only the fields ubCode requires: |
| 73 | + `directive`, `title`, `prefix`, and optionally `color` / `style`. |
| 74 | +3. Appending that file to `needscfg_merge_toml_files` so it is merged into the |
| 75 | + final `ubproject.toml`. |
| 76 | + |
| 77 | +### 5. Relative path handling |
| 78 | + |
| 79 | +Fields such as `needs_external_needs[*].json_path` contain **absolute paths** |
| 80 | +injected by Bazel. `needscfg_relative_path_fields` converts these to paths |
| 81 | +relative to `confdir` in the output TOML so the file remains portable. |
| 82 | + |
| 83 | +### 6. Suppressed warnings |
| 84 | + |
| 85 | +``` |
| 86 | +needs_config_writer.path_conversion |
| 87 | +needs_config_writer.unsupported_type |
| 88 | +``` |
| 89 | + |
| 90 | +Suppressed so that Bazel builds running with `-W` (warnings-as-errors) do not fail |
| 91 | +due to known, handled edge cases in serialisation. |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## Data flow |
| 96 | + |
| 97 | +``` |
| 98 | +metamodel.yaml |
| 99 | + │ |
| 100 | + ▼ |
| 101 | +score_metamodel.setup() ──► app.config.needs_types (Python objects) |
| 102 | + │ |
| 103 | + ▼ |
| 104 | + _write_needs_types_toml() |
| 105 | + │ |
| 106 | + ▼ |
| 107 | + needs_types_generated.toml ──┐ |
| 108 | + │ |
| 109 | +shared.toml ────────────────────────────────────────────┤ merge via |
| 110 | + │ needs_config_writer |
| 111 | +app.config (all other needs_* settings) ────────────────┤ |
| 112 | + ▼ |
| 113 | + ubproject.toml |
| 114 | + │ |
| 115 | + ▼ |
| 116 | + Sphinx-Needs |
| 117 | + ubCode language server |
| 118 | +``` |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## Generated files |
| 123 | + |
| 124 | +Both files are written to `confdir` (the `docs/` directory) on every Sphinx build |
| 125 | +and are committed to the repository so that ubCode works without needing to run a |
| 126 | +build first. |
| 127 | + |
| 128 | +| File | Generated by | Used by | |
| 129 | +|---|---|---| |
| 130 | +| `ubproject.toml` | `needs_config_writer` + merges above | ubCode language server | |
| 131 | +| `schemas.json` | `score_metamodel.sn_schemas.write_sn_schemas` | ubCode + sphinx-needs schema validation | |
| 132 | +| `needs_types_generated.toml` | `_write_needs_types_toml` | Merged into `ubproject.toml`, not committed | |
0 commit comments