Swift: make codegen resilient to formatting errors

More in general, the managed renderer flow does things more sensibly
in case an exception is thrown:
* it will not remove any file
* it will drop already written files from the registry, so that codegen
  won't be skipped for those files during the next run
This commit is contained in:
Paolo Tranquilli
2022-11-30 13:39:56 +01:00
parent d53d275bba
commit 76db5f22b3
2 changed files with 51 additions and 6 deletions

View File

@@ -103,12 +103,18 @@ class RenderManager(Renderer):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
for f in self._existing - self._skipped - self.written:
self._hashes.pop(self._get_path(f), None)
f.unlink(missing_ok=True)
log.info(f"removed {f.name}")
for f in self.written:
self._hashes[self._get_path(f)].post = self._hash_file(f)
if exc_val is None:
for f in self._existing - self._skipped - self.written:
self._hashes.pop(self._get_path(f), None)
f.unlink(missing_ok=True)
log.info(f"removed {f.name}")
for f in self.written:
self._hashes[self._get_path(f)].post = self._hash_file(f)
else:
# if an error was encountered, drop already written files from the registry
# so that they get the chance to be regenerated again during the next run
for f in self.written:
self._hashes.pop(self._get_path(f), None)
self._dump_registry()
def _do_write(self, mnemonic: str, contents: str, output: pathlib.Path):