-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathreplace-on-build.py
63 lines (52 loc) · 1.69 KB
/
replace-on-build.py
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
#!/usr/bin/env python3
#
# Inject Semgrep version info and other variable data into the documentation.
#
# 'foo.md.template' becomes 'foo.md' which is ready to be processed by the
# documentation server.
#
import subprocess
import json
from dataclasses import dataclass
@dataclass
class Replace:
"""A search-and-replace query.
The output file is 'dst_file'.
The input file has an extra ".template" extension.
"""
dst_file: str
find: str
replace: str
def replace_in_file(rep: Replace):
src_file = rep.dst_file + ".template"
with open(src_file) as f:
in_data = f.read()
out_data = in_data.replace(rep.find, rep.replace)
with open(rep.dst_file,"w") as f:
f.write(out_data)
DEFAULT_SEMGREPIGNORE_URL = "https://raw.githubusercontent.com/semgrep/semgrep/develop/src/targeting/default.semgrepignore"
DEFAULT_SEMGREPIGNORE = subprocess.run(
["curl", DEFAULT_SEMGREPIGNORE_URL],
capture_output=True
).stdout.decode("utf-8")
RELEASE_NAME = json.loads(subprocess.run(["curl","https://api.github.com/repos/semgrep/semgrep/releases/latest"], capture_output=True).stdout)["tag_name"]
# List of text replacements to occur when building the docs
replacements = [
Replace(
dst_file="docs/cli-reference.md",
find="DEFAULT_SEMGREPIGNORE_TEXT",
replace=DEFAULT_SEMGREPIGNORE,
),
Replace(
dst_file="docs/ignoring-files-folders-code.md",
find="DEFAULT_SEMGREPIGNORE_TEXT",
replace=DEFAULT_SEMGREPIGNORE,
),
Replace(
dst_file="docs/extensions/overview.md",
find="SEMGREP_VERSION_LATEST",
replace=RELEASE_NAME,
),
]
for replacement in replacements:
replace_in_file(replacement)