Fix spurious rebuilds of generated go code

This commit is contained in:
Kovid Goyal
2022-10-04 08:50:19 +05:30
parent e536ef7844
commit f57832f501
2 changed files with 8 additions and 2 deletions

View File

@@ -4,6 +4,7 @@
import io
import json
import os
import subprocess
import sys
from contextlib import contextmanager, suppress
from functools import lru_cache
@@ -295,7 +296,7 @@ var DocTitleMap = map[string]string{serialize_go_dict(ref_map['doc'])}
# Boilerplate {{{
@contextmanager
def replace_if_needed(path: str) -> Iterator[io.StringIO]:
def replace_if_needed(path: str, show_diff: bool = False) -> Iterator[io.StringIO]:
buf = io.StringIO()
yield buf
orig = ''
@@ -305,6 +306,11 @@ def replace_if_needed(path: str) -> Iterator[io.StringIO]:
new = f'// Code generated by {os.path.basename(__file__)}; DO NOT EDIT.\n\n' + new
if orig != new:
changed.append(path)
if show_diff:
with open(path + '.new', 'w') as f:
f.write(new)
subprocess.run(['diff', '-Naurp', path, f.name], stdout=open('/dev/tty', 'w'))
os.remove(f.name)
with open(path, 'w') as f:
f.write(new)