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
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 8 additions and 2 deletions

View File

@ -4,6 +4,7 @@
import io import io
import json import json
import os import os
import subprocess
import sys import sys
from contextlib import contextmanager, suppress from contextlib import contextmanager, suppress
from functools import lru_cache from functools import lru_cache
@ -295,7 +296,7 @@ var DocTitleMap = map[string]string{serialize_go_dict(ref_map['doc'])}
# Boilerplate {{{ # Boilerplate {{{
@contextmanager @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() buf = io.StringIO()
yield buf yield buf
orig = '' 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 new = f'// Code generated by {os.path.basename(__file__)}; DO NOT EDIT.\n\n' + new
if orig != new: if orig != new:
changed.append(path) 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: with open(path, 'w') as f:
f.write(new) f.write(new)

View File

@ -148,7 +148,7 @@ class GoOption:
def as_option(self, cmd_name: str = 'cmd', depth: int = 0, group: str = '') -> str: def as_option(self, cmd_name: str = 'cmd', depth: int = 0, group: str = '') -> str:
add = f'AddToGroup("{serialize_as_go_string(group)}", ' if group else 'Add(' add = f'AddToGroup("{serialize_as_go_string(group)}", ' if group else 'Add('
aliases = ' '.join(self.obj_dict['aliases']) aliases = ' '.join(sorted(self.obj_dict['aliases']))
ans = f'''{cmd_name}.{add}cli.OptionSpec{{ ans = f'''{cmd_name}.{add}cli.OptionSpec{{
Name: "{serialize_as_go_string(aliases)}", Name: "{serialize_as_go_string(aliases)}",
Type: "{self.type}", Type: "{self.type}",