1111# SPDX-License-Identifier: Apache-2.0
1212# *******************************************************************************
1313
14+ """
15+ Easy streamlined way for S-CORE docs-as-code.
16+ """
17+
1418# Multiple approaches are available to build the same documentation output:
1519#
1620# 1. **Esbonio via IDE support (`ide_support` target)**:
3741#
3842# For user-facing documentation, refer to `/README.md`.
3943
40- load ("@aspect_rules_py//py:defs.bzl" , "py_binary" , "py_library" )
41- load ("@pip_process//:requirements.bzl" , "all_requirements" , "requirement" )
44+ load ("@aspect_rules_py//py:defs.bzl" , "py_binary" )
45+ load ("@pip_process//:requirements.bzl" , "all_requirements" )
4246load ("@rules_pkg//pkg:mappings.bzl" , "pkg_files" , "strip_prefix" )
43- load ("@rules_pkg//pkg:tar.bzl" , "pkg_tar" )
4447load ("@rules_python//sphinxdocs:sphinx.bzl" , "sphinx_build_binary" , "sphinx_docs" )
45- load ("@rules_python//sphinxdocs:sphinx_docs_library.bzl" , "sphinx_docs_library" )
4648load ("@score_tooling//:defs.bzl" , "score_virtualenv" )
4749
4850def _rewrite_needs_json_to_docs_sources (labels ):
@@ -56,10 +58,47 @@ def _rewrite_needs_json_to_docs_sources(labels):
5658 out .append (s )
5759 return out
5860
59- def docs (source_dir = "docs" , data = [], deps = []):
61+ def _rewrite_needs_json_to_sourcelinks (labels ):
62+ """Replace '@repo//:needs_json' -> '@repo//:sourcelinks_json' for every item."""
63+ out = []
64+ for x in labels :
65+ s = str (x )
66+ if s .endswith ("//:needs_json" ):
67+ out .append (s .replace ("//:needs_json" , "//:sourcelinks_json" ))
68+ else :
69+ out .append (s )
70+ return out
71+
72+ def _merge_sourcelinks (name , sourcelinks ):
73+ """Merge multiple sourcelinks JSON files into a single file.
74+
75+ Args:
76+ name: Name for the merged sourcelinks target
77+ sourcelinks: List of sourcelinks JSON file targets
6078 """
61- Creates all targets related to documentation.
79+
80+ native .genrule (
81+ name = name ,
82+ srcs = sourcelinks ,
83+ outs = [name + ".json" ],
84+ cmd = """
85+ $(location @score_docs_as_code//scripts_bazel:merge_sourcelinks) \
86+ --output $@ \
87+ $(SRCS)
88+ """ ,
89+ tools = ["@score_docs_as_code//scripts_bazel:merge_sourcelinks" ],
90+ )
91+
92+ def docs (source_dir = "docs" , data = [], deps = [], scan_code = []):
93+ """Creates all targets related to documentation.
94+
6295 By using this function, you'll get any and all updates for documentation targets in one place.
96+
97+ Args:
98+ source_dir: The source directory containing documentation files. Defaults to "docs".
99+ data: Additional data files to include in the documentation build.
100+ deps: Additional dependencies for the documentation build.
101+ scan_code: List of code targets to scan for source code links.
63102 """
64103
65104 call_path = native .package_name ()
@@ -100,70 +139,79 @@ def docs(source_dir = "docs", data = [], deps = []):
100139 visibility = ["//visibility:public" ],
101140 )
102141
142+ _sourcelinks_json (name = "sourcelinks_json" , srcs = scan_code )
143+
103144 data_with_docs_sources = _rewrite_needs_json_to_docs_sources (data )
145+ additional_combo_sourcelinks = _rewrite_needs_json_to_sourcelinks (data )
146+ _merge_sourcelinks (name = "merged_sourcelinks" , sourcelinks = [":sourcelinks_json" ] + additional_combo_sourcelinks )
104147
105148 py_binary (
106149 name = "docs" ,
107150 tags = ["cli_help=Build documentation:\n bazel run //:docs" ],
108151 srcs = ["@score_docs_as_code//src:incremental.py" ],
109- data = data ,
152+ data = data + [ ":sourcelinks_json" ] ,
110153 deps = deps ,
111154 env = {
112155 "SOURCE_DIRECTORY" : source_dir ,
113156 "DATA" : str (data ),
114157 "ACTION" : "incremental" ,
158+ "SCORE_SOURCELINKS" : "$(location :sourcelinks_json)" ,
115159 },
116160 )
117161
118162 py_binary (
119163 name = "docs_combo_experimental" ,
120164 tags = ["cli_help=Build full documentation with all dependencies:\n bazel run //:docs_combo_experimental" ],
121165 srcs = ["@score_docs_as_code//src:incremental.py" ],
122- data = data_with_docs_sources ,
166+ data = data_with_docs_sources + [ ":merged_sourcelinks" ] ,
123167 deps = deps ,
124168 env = {
125169 "SOURCE_DIRECTORY" : source_dir ,
126170 "DATA" : str (data_with_docs_sources ),
127171 "ACTION" : "incremental" ,
172+ "SCORE_SOURCELINKS" : "$(location :merged_sourcelinks)" ,
128173 },
129174 )
130175
131176 py_binary (
132177 name = "docs_check" ,
133178 tags = ["cli_help=Verify documentation:\n bazel run //:docs_check" ],
134179 srcs = ["@score_docs_as_code//src:incremental.py" ],
135- data = data ,
180+ data = data + [ ":sourcelinks_json" ] ,
136181 deps = deps ,
137182 env = {
138183 "SOURCE_DIRECTORY" : source_dir ,
139184 "DATA" : str (data ),
140185 "ACTION" : "check" ,
186+ "SCORE_SOURCELINKS" : "$(location :sourcelinks_json)" ,
141187 },
142188 )
143189
144190 py_binary (
145191 name = "live_preview" ,
146192 tags = ["cli_help=Live preview documentation in the browser:\n bazel run //:live_preview" ],
147193 srcs = ["@score_docs_as_code//src:incremental.py" ],
148- data = data ,
194+ data = data + [ ":sourcelinks_json" ] ,
149195 deps = deps ,
150196 env = {
151197 "SOURCE_DIRECTORY" : source_dir ,
152198 "DATA" : str (data ),
153199 "ACTION" : "live_preview" ,
200+ "SCORE_SOURCELINKS" : "$(location :sourcelinks_json)" ,
154201 },
155202 )
156203
157204 py_binary (
158205 name = "live_preview_combo_experimental" ,
159206 tags = ["cli_help=Live preview full documentation with all dependencies in the browser:\n bazel run //:live_preview_combo_experimental" ],
160207 srcs = ["@score_docs_as_code//src:incremental.py" ],
161- data = data_with_docs_sources ,
208+ data = data_with_docs_sources + [ ":merged_sourcelinks" ] ,
162209 deps = deps ,
163210 env = {
164211 "SOURCE_DIRECTORY" : source_dir ,
165212 "DATA" : str (data_with_docs_sources ),
166213 "ACTION" : "live_preview" ,
214+ "SCORE_SOURCELINKS" : "$(location :merged_sourcelinks)" ,
167215 },
168216 )
169217
@@ -193,3 +241,28 @@ def docs(source_dir = "docs", data = [], deps = []):
193241 tools = data ,
194242 visibility = ["//visibility:public" ],
195243 )
244+
245+ def _sourcelinks_json (name , srcs ):
246+ """
247+ Creates a target that generates a JSON file with source code links.
248+
249+ See https://eclipse-score.github.io/docs-as-code/main/how-to/source_to_doc_links.html
250+
251+ Args:
252+ name: Name of the target
253+ srcs: Source files to scan for traceability tags
254+ """
255+ output_file = name + ".json"
256+
257+ native .genrule (
258+ name = name ,
259+ srcs = srcs ,
260+ outs = [output_file ],
261+ cmd = """
262+ $(location @score_docs_as_code//scripts_bazel:generate_sourcelinks) \
263+ --output $@ \
264+ $(SRCS)
265+ """ ,
266+ tools = ["@score_docs_as_code//scripts_bazel:generate_sourcelinks" ],
267+ visibility = ["//visibility:public" ],
268+ )
0 commit comments