Skip to content

Commit 6a8b18f

Browse files
Source code linker partial re-write (#125)
* Re-engineered working of source code linker * Adapted tests to conform to new working of the extension * Changed parsing & writing of json * Small adaptation in README --------- Co-authored-by: Alexander Lanin <Alexander.Lanin@bosch.com>
1 parent cddce74 commit 6a8b18f

14 files changed

Lines changed: 1439 additions & 662 deletions

File tree

docs.bzl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
4444
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
4545
load("@rules_python//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs")
4646
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
47-
load("@score_docs_as_code//src/extensions/score_source_code_linker:collect_source_files.bzl", "parse_source_files_for_needs_links")
4847
load("@score_python_basics//:defs.bzl", "score_virtualenv")
4948

5049
sphinx_requirements = all_requirements + [
@@ -66,13 +65,6 @@ def docs(source_files_to_scan_for_needs_links = None, source_dir = "docs", conf_
6665
* only callable from 'docs/BUILD'
6766
"""
6867

69-
# Parse source files for needs links
70-
# This needs to be created to generate a target, otherwise it won't execute as dependency for other macros
71-
parse_source_files_for_needs_links(
72-
name = "score_source_code_parser",
73-
srcs_and_deps = source_files_to_scan_for_needs_links if source_files_to_scan_for_needs_links else [],
74-
)
75-
7668
# We are iterating over all provided 'targets' in order to allow for automatic generation of them without
7769
# needing to modify the underlying 'docs.bzl' file.
7870
for target in docs_targets:
@@ -137,7 +129,7 @@ def _incremental(incremental_name = "incremental", live_name = "live_preview", s
137129
srcs = ["@score_docs_as_code//src:incremental.py"],
138130
deps = dependencies,
139131
# TODO: Figure out if we need all dependencies as data here or not.
140-
data = [":score_source_code_parser", "@score_docs_as_code//src:plantuml", "@score_docs_as_code//src:docs_assets"] + dependencies + external_needs_deps,
132+
data = ["@score_docs_as_code//src:plantuml", "@score_docs_as_code//src:docs_assets"] + dependencies + external_needs_deps,
141133
env = {
142134
"SOURCE_DIRECTORY": source_dir,
143135
"CONF_DIRECTORY": conf_dir,
@@ -205,7 +197,6 @@ def _docs(name = "docs", suffix = "", format = "html", external_needs_deps = lis
205197
"manual",
206198
],
207199
tools = [
208-
":score_source_code_parser",
209200
"@score_docs_as_code//src:plantuml",
210201
"@score_docs_as_code//src:docs_assets",
211202
] + external_needs_deps,

docs/BUILD

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,4 @@ docs(
4848
},
4949
],
5050
source_dir = "docs",
51-
source_files_to_scan_for_needs_links = [
52-
"//src:score_extension_files",
53-
],
5451
)

docs/product/extensions/source_code_linker.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The extension uses two main components to integrate with Bazel:
1616
- Handles dependency tracking for incremental builds
1717

1818
2. `parse_source_files.py`
19-
- Scans input files for template tags (e.g., `# req-traceability:`)
19+
- Scans input files for template tags (e.g., "#<!-- comment prevents parsing this occurance --> req-traceability:")
2020
- Retrieves git information (hash, file location)
2121
- Generates mapping file with requirement IDs and links
2222

@@ -37,16 +37,26 @@ The extension uses two main components to integrate with Bazel:
3737
- Gets current git hash for each file
3838
- Constructs GitHub URLs with format:
3939
`{base_url}/{repo}/blob/{hash}/{file}#L{line_nr}`
40-
**Note:** The base_url is defined in `parse_source_files.py`. Currently set to: `https://github.com/eclipse-score/score/blob/`
40+
**Note:** The base_url is defined in `parse_source_files.py`. Currently set to: `https://github.com/eclipse-score/score/blob/`
4141

4242
Produces JSON mapping file:
4343
```json
44-
{
45-
"REQ_ID": [
46-
"github_link1",
47-
"github_link2" // If multiple code-links exist
48-
]
49-
}
44+
[
45+
{
46+
"file": "src/implementation1.py",
47+
"line": 3,
48+
"tag":"# req-Id:",
49+
"need": "TREQ_ID_1",
50+
"full_line": "# req-Id: TREQ_ID_1"
51+
},
52+
{
53+
"file": "src/implementation2.py",
54+
"line": 3,
55+
"tag":"# req-Id:",
56+
"need": "TREQ_ID_1",
57+
"full_line": "# req-Id: TREQ_ID_1"
58+
},
59+
]
5060
```
5161

5262
<br>
@@ -86,7 +96,7 @@ The extension hooks into Sphinx's build process. It attaches to the `env-updated
8696

8797
### Adding Places to Search
8898

89-
You can easily add files to be searched by adding targets / files to the deps inside the
99+
You can easily add files to be searched by adding targets / files to the deps inside the
90100
`collect_source_files_for_score_source_code_linker` in `docs/BUILD`.
91101
See here:
92102

@@ -126,7 +136,7 @@ WARNING: Could not find TREQ_ID_200 in the needs id's. Found in file(s):['_tooli
126136

127137
### Quickly Finding Source Links
128138

129-
The easiest and quickest way to find source_code_link options is to just search for the option `source_code_link`. It should give you all rst files
139+
The easiest and quickest way to find source_code_link options is to just search for the option `source_code_link`. It should give you all rst files
130140
where the option is not empty.
131141

132142
### Executing Tests

src/extensions/score_metamodel/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
# SPDX-License-Identifier: Apache-2.0
1212
# *******************************************************************************
1313
import importlib
14-
import pkgutil
1514
import json
1615
import os
17-
16+
import pkgutil
1817
from collections.abc import Callable
1918
from pathlib import Path
2019

21-
2220
from ruamel.yaml import YAML
2321
from sphinx.application import Sphinx
2422
from sphinx_needs import logging
25-
from sphinx_needs.data import NeedsInfoType, SphinxNeedsData, NeedsView
23+
from sphinx_needs.data import NeedsInfoType, NeedsView, SphinxNeedsData
24+
2625
from .log import CheckLogger
2726

2827
logger = logging.get_logger(__name__)
@@ -81,7 +80,9 @@ def _run_checks(app: Sphinx, exception: Exception | None) -> None:
8180

8281
logger.debug(f"Running checks for {len(needs_all_needs)} needs")
8382

84-
prefix = str(Path(app.srcdir).relative_to(Path.cwd()))
83+
ws_root = os.environ.get("BUILD_WORKSPACE_DIRECTORY", None)
84+
cwd_or_ws_root = Path(ws_root) if ws_root else Path.cwd()
85+
prefix = str(Path(app.srcdir).relative_to(cwd_or_ws_root))
8586

8687
log = CheckLogger(logger, prefix)
8788

src/extensions/score_source_code_linker/BUILD

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ py_library(
2929
score_py_pytest(
3030
name = "score_source_code_linker_test",
3131
size = "small",
32-
srcs = glob(["tests/**/*.py"]),
32+
srcs = glob([
33+
"tests/**/*.py",
34+
"test/**/*.json",
35+
]),
36+
args = [
37+
"-s",
38+
"-vv",
39+
],
40+
data = glob(["**/*.json"]),
41+
imports = ["."],
3342
deps = [
3443
":score_source_code_linker",
44+
"@score_docs_as_code//src/extensions/score_metamodel",
3545
] + all_requirements,
3646
)
37-
38-
# Needed to make the file parser executeable and findable for the source_code_linker aspect
39-
py_binary(
40-
name = "parsed_source_files_for_source_code_linker",
41-
srcs = ["parse_source_files.py"],
42-
main = "parse_source_files.py",
43-
visibility = ["//visibility:public"],
44-
)

0 commit comments

Comments
 (0)