forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__main__.py
More file actions
265 lines (225 loc) · 7.91 KB
/
__main__.py
File metadata and controls
265 lines (225 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Entry point for running the parser package as a script.
Usage:
# With codegen modules path:
python ... --codegen-path /path/to/codegen
# With output directory:
python ... --output-dir /path/to/output
"""
import argparse
import os
import subprocess
import sys
import tempfile
from .config import parse_config_file
from .main import build_snapshot
from .path_utils import get_react_native_dir
from .snapshot_diff import check_snapshots
DOXYGEN_CONFIG_FILE = ".doxygen.config.generated"
def build_doxygen_config(
directory: str,
include_directories: list[str] = None,
exclude_patterns: list[str] = None,
definitions: dict[str, str | int] = None,
input_filter: str = None,
) -> None:
if include_directories is None:
include_directories = []
if exclude_patterns is None:
exclude_patterns = []
if definitions is None:
definitions = {}
include_directories_str = " ".join(include_directories)
exclude_patterns_str = "\\\n".join(exclude_patterns)
if len(exclude_patterns) > 0:
exclude_patterns_str = f"\\\n{exclude_patterns_str}"
definitions_str = " ".join(
[
f'{key}="{value}"' if isinstance(value, str) else f"{key}={value}"
for key, value in definitions.items()
]
)
input_filter_str = input_filter if input_filter else ""
# read the template file
with open(os.path.join(directory, ".doxygen.config.template")) as f:
template = f.read()
# replace the placeholder with the actual path
config = (
template.replace("${INPUTS}", include_directories_str)
.replace("${EXCLUDE_PATTERNS}", exclude_patterns_str)
.replace("${PREDEFINED}", definitions_str)
.replace("${DOXYGEN_INPUT_FILTER}", input_filter_str)
)
# write the config file
with open(os.path.join(directory, DOXYGEN_CONFIG_FILE), "w") as f:
f.write(config)
def build_snapshot_for_view(
api_view: str,
react_native_dir: str,
include_directories: list[str],
exclude_patterns: list[str],
definitions: dict[str, str | int],
output_dir: str,
verbose: bool = True,
input_filter: str = None,
) -> None:
if verbose:
print(f"Generating API view: {api_view}")
print("Generating Doxygen config file")
build_doxygen_config(
react_native_dir,
include_directories=include_directories,
exclude_patterns=exclude_patterns,
definitions=definitions,
input_filter=input_filter,
)
# If there is already a doxygen output directory, delete it
if os.path.exists(os.path.join(react_native_dir, "api")):
if verbose:
print("Deleting existing output directory")
subprocess.run(["rm", "-rf", os.path.join(react_native_dir, "api")])
if verbose:
print("Running Doxygen")
# Run doxygen with the config file
doxygen_bin = os.environ.get("DOXYGEN_BIN", "doxygen")
result = subprocess.run(
[doxygen_bin, DOXYGEN_CONFIG_FILE],
cwd=react_native_dir,
capture_output=True,
text=True,
)
# Check the result
if result.returncode != 0:
if verbose:
print(f"Doxygen finished with error: {result.stderr}")
else:
if verbose:
print("Doxygen finished successfully")
# Delete the Doxygen config file
if verbose:
print("Deleting Doxygen config file")
subprocess.run(["rm", DOXYGEN_CONFIG_FILE], cwd=react_native_dir)
# build snapshot, convert to string, and save to file
snapshot = build_snapshot(os.path.join(react_native_dir, "api", "xml"))
snapshot_string = snapshot.to_string()
output_file = os.path.join(output_dir, f"{api_view}Cxx.api")
os.makedirs(output_dir, exist_ok=True)
with open(output_file, "w") as f:
f.write("// @" + "generated by scripts/cxx-api\n\n")
f.write(snapshot_string)
return snapshot_string
def main():
parser = argparse.ArgumentParser(
description="Generate API snapshots from C++ headers"
)
parser.add_argument(
"--output-dir",
type=str,
help="Output directory for the snapshot",
)
parser.add_argument(
"--codegen-path",
type=str,
help="Path to codegen generated code",
)
parser.add_argument(
"--check",
action="store_true",
help="Generate snapshots to a temp directory and compare against committed ones",
)
parser.add_argument(
"--snapshot-dir",
type=str,
help="Directory containing committed snapshots for comparison (used with --check)",
)
parser.add_argument(
"--test",
action="store_true",
help="Run on the local test directory instead of the react-native directory",
)
args = parser.parse_args()
verbose = not args.check
doxygen_bin = os.environ.get("DOXYGEN_BIN", "doxygen")
version_result = subprocess.run(
[doxygen_bin, "--version"],
capture_output=True,
text=True,
)
if verbose:
print(f"Using Doxygen {version_result.stdout.strip()} ({doxygen_bin})")
# Define the path to the react-native directory
react_native_package_dir = (
os.path.join(get_react_native_dir(), "packages", "react-native")
if not args.test
else os.path.join(get_react_native_dir(), "scripts", "cxx-api", "manual_test")
)
if verbose:
print(f"Running in directory: {react_native_package_dir}")
if verbose and args.codegen_path:
print(f"Codegen output path: {os.path.abspath(args.codegen_path)}")
input_filter_path = os.path.join(
get_react_native_dir(),
"scripts",
"cxx-api",
"input_filters",
"doxygen_strip_comments.py",
)
input_filter = None
if os.path.exists(input_filter_path):
input_filter = f"python3 {input_filter_path}"
# Parse config file
config_path = os.path.join(
get_react_native_dir(), "scripts", "cxx-api", "config.yml"
)
snapshot_configs = parse_config_file(
config_path,
get_react_native_dir(),
codegen_path=args.codegen_path,
)
def build_snapshots(output_dir: str, verbose: bool) -> None:
if not args.test:
for config in snapshot_configs:
build_snapshot_for_view(
api_view=config.snapshot_name,
react_native_dir=react_native_package_dir,
include_directories=config.inputs,
exclude_patterns=config.exclude_patterns,
definitions=config.definitions,
output_dir=output_dir,
verbose=verbose,
)
else:
snapshot = build_snapshot_for_view(
api_view="Test",
react_native_dir=react_native_package_dir,
include_directories=[],
exclude_patterns=[],
definitions={},
output_dir=output_dir,
verbose=verbose,
input_filter=input_filter,
)
if verbose:
print(snapshot)
if args.check:
with tempfile.TemporaryDirectory() as tmpdir:
build_snapshots(tmpdir, verbose=False)
snapshot_dir = args.snapshot_dir or os.path.join(
get_react_native_dir(), "scripts", "cxx-api", "api-snapshots"
)
if not check_snapshots(tmpdir, snapshot_dir):
sys.exit(1)
print("All snapshot checks passed")
else:
output_dir = (
args.output_dir
if args.output_dir
else os.path.join(
get_react_native_dir(), "scripts", "cxx-api", "api-snapshots"
)
)
build_snapshots(output_dir, verbose=True)