Skip to content

Commit 0a20885

Browse files
authored
Merge pull request #13056 from github/redsun82/swift-mark-generated-files
Codegen: mark generated checked in files as such
2 parents 09ba9a7 + 287b23c commit 0a20885

File tree

7 files changed

+1950
-996
lines changed

7 files changed

+1950
-996
lines changed

misc/codegen/codegen.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def _parse_args() -> argparse.Namespace:
5858
help="output directory for generated C++ files, required if trap or cpp is provided to "
5959
"--generate"),
6060
p.add_argument("--generated-registry",
61-
help="registry file containing information about checked-in generated code"),
61+
help="registry file containing information about checked-in generated code. A .gitattributes"
62+
"file is generated besides it to mark those files with linguist-generated=true. Must"
63+
"be in a directory containing all generated code."),
6264
]
6365
p.add_argument("--script-name",
6466
help="script name to put in header comments of generated files. By default, the path of this "
@@ -108,7 +110,7 @@ def run():
108110
log_level = logging.INFO
109111
logging.basicConfig(format="{levelname} {message}", style='{', level=log_level)
110112
for target in opts.generate:
111-
generate(target, opts, render.Renderer(opts.script_name, opts.root_dir))
113+
generate(target, opts, render.Renderer(opts.script_name))
112114

113115

114116
if __name__ == "__main__":

misc/codegen/lib/render.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ class Error(Exception):
2525
class Renderer:
2626
""" Template renderer using mustache templates in the `templates` directory """
2727

28-
def __init__(self, generator: pathlib.Path, root_dir: pathlib.Path):
28+
def __init__(self, generator: pathlib.Path):
2929
self._r = pystache.Renderer(search_dirs=str(paths.templates_dir), escape=lambda u: u)
30-
self._root_dir = root_dir
3130
self._generator = generator
3231

33-
def _get_path(self, file: pathlib.Path):
34-
return file.relative_to(self._root_dir)
35-
3632
def render(self, data: object, output: pathlib.Path):
3733
""" Render `data` to `output`.
3834
@@ -60,7 +56,7 @@ def _do_write(self, mnemonic: str, contents: str, output: pathlib.Path):
6056

6157
def manage(self, generated: typing.Iterable[pathlib.Path], stubs: typing.Iterable[pathlib.Path],
6258
registry: pathlib.Path, force: bool = False) -> "RenderManager":
63-
return RenderManager(self._generator, self._root_dir, generated, stubs, registry, force)
59+
return RenderManager(self._generator, generated, stubs, registry, force)
6460

6561

6662
class RenderManager(Renderer):
@@ -85,10 +81,10 @@ class Hashes:
8581
pre: str
8682
post: typing.Optional[str] = None
8783

88-
def __init__(self, generator: pathlib.Path, root_dir: pathlib.Path, generated: typing.Iterable[pathlib.Path],
84+
def __init__(self, generator: pathlib.Path, generated: typing.Iterable[pathlib.Path],
8985
stubs: typing.Iterable[pathlib.Path],
9086
registry: pathlib.Path, force: bool = False):
91-
super().__init__(generator, root_dir)
87+
super().__init__(generator)
9288
self._registry_path = registry
9389
self._force = force
9490
self._hashes = {}
@@ -117,10 +113,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):
117113
self._hashes.pop(self._get_path(f), None)
118114
# clean up the registry from files that do not exist any more
119115
for f in list(self._hashes):
120-
if not (self._root_dir / f).exists():
116+
if not (self._registry_path.parent / f).exists():
121117
self._hashes.pop(f)
122118
self._dump_registry()
123119

120+
def _get_path(self, file: pathlib.Path):
121+
return file.relative_to(self._registry_path.parent)
122+
124123
def _do_write(self, mnemonic: str, contents: str, output: pathlib.Path):
125124
hash = self._hash_string(contents)
126125
rel_output = self._get_path(output)
@@ -186,13 +185,17 @@ def _load_registry(self):
186185
try:
187186
with open(self._registry_path) as reg:
188187
for line in reg:
189-
filename, prehash, posthash = line.split()
190-
self._hashes[pathlib.Path(filename)] = self.Hashes(prehash, posthash)
188+
if line.strip():
189+
filename, prehash, posthash = line.split()
190+
self._hashes[pathlib.Path(filename)] = self.Hashes(prehash, posthash)
191191
except FileNotFoundError:
192192
pass
193193

194194
def _dump_registry(self):
195195
self._registry_path.parent.mkdir(parents=True, exist_ok=True)
196-
with open(self._registry_path, 'w') as out:
196+
with open(self._registry_path, 'w') as out, open(self._registry_path.parent / ".gitattributes", "w") as attrs:
197+
print(f"/{self._registry_path.name}", "linguist-generated", file=attrs)
198+
print("/.gitattributes", "linguist-generated", file=attrs)
197199
for f, hashes in sorted(self._hashes.items()):
198200
print(f, hashes.pre, hashes.post, file=out)
201+
print(f"/{f}", "linguist-generated", file=attrs)

misc/codegen/test/test_qlgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def ql_output_path(): return paths.root_dir / "ql/lib/other/path"
2626
def ql_test_output_path(): return paths.root_dir / "ql/test/path"
2727

2828

29-
def generated_registry_path(): return paths.root_dir / "registry.list"
29+
def generated_registry_path(): return paths.root_dir / "ql/registry.list"
3030

3131

3232
def import_file(): return stub_path().with_suffix(".qll")

0 commit comments

Comments
 (0)