DRYer
This commit is contained in:
parent
b1f9139ca5
commit
841b368021
39
gen-rc-go.py
39
gen-rc-go.py
@ -2,9 +2,11 @@
|
|||||||
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Dict, List, Tuple, Union
|
from contextlib import contextmanager
|
||||||
|
from typing import Dict, Iterator, List, Tuple, Union
|
||||||
|
|
||||||
import kitty.constants as kc
|
import kitty.constants as kc
|
||||||
from kittens.tui.operations import Mode
|
from kittens.tui.operations import Mode
|
||||||
@ -16,6 +18,9 @@ from kitty.key_names import (
|
|||||||
from kitty.rc.base import RemoteCommand, all_command_names, command_for_name
|
from kitty.rc.base import RemoteCommand, all_command_names, command_for_name
|
||||||
|
|
||||||
|
|
||||||
|
changed: List[str] = []
|
||||||
|
|
||||||
|
|
||||||
def serialize_as_go_string(x: str) -> str:
|
def serialize_as_go_string(x: str) -> str:
|
||||||
return x.replace('\\', '\\\\').replace('\n', '\\n').replace('"', '\\"')
|
return x.replace('\\', '\\\\').replace('\n', '\\n').replace('"', '\\"')
|
||||||
|
|
||||||
@ -210,11 +215,10 @@ def load_ref_map() -> Dict[str, Dict[str, str]]:
|
|||||||
return data # type: ignore
|
return data # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def generate_constants() -> str:
|
||||||
ref_map = load_ref_map()
|
ref_map = load_ref_map()
|
||||||
with open('constants_generated.go', 'w') as f:
|
dp = ", ".join(map(lambda x: f'"{serialize_as_go_string(x)}"', kc.default_pager_for_help))
|
||||||
dp = ", ".join(map(lambda x: f'"{serialize_as_go_string(x)}"', kc.default_pager_for_help))
|
return f'''\
|
||||||
f.write(f'''\
|
|
||||||
// auto-generated by {__file__} do no edit
|
// auto-generated by {__file__} do no edit
|
||||||
|
|
||||||
package kitty
|
package kitty
|
||||||
@ -235,7 +239,23 @@ var CharacterKeyNameAliases = map[string]string{serialize_go_dict(character_key_
|
|||||||
var ConfigModMap = map[string]uint16{serialize_go_dict(config_mod_map)}
|
var ConfigModMap = map[string]uint16{serialize_go_dict(config_mod_map)}
|
||||||
var RefMap = map[string]string{serialize_go_dict(ref_map['ref'])}
|
var RefMap = map[string]string{serialize_go_dict(ref_map['ref'])}
|
||||||
var DocTitleMap = map[string]string{serialize_go_dict(ref_map['doc'])}
|
var DocTitleMap = map[string]string{serialize_go_dict(ref_map['doc'])}
|
||||||
''')
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def replace_if_needed(path: str) -> Iterator[io.StringIO]:
|
||||||
|
buf = io.StringIO()
|
||||||
|
yield buf
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
orig = f.read()
|
||||||
|
new = buf.getvalue()
|
||||||
|
if orig != new:
|
||||||
|
changed.append(path)
|
||||||
|
with open(path, 'w') as f:
|
||||||
|
f.write(new)
|
||||||
|
|
||||||
|
|
||||||
|
def update_at_commands() -> None:
|
||||||
with open('tools/cmd/at/template.go') as f:
|
with open('tools/cmd/at/template.go') as f:
|
||||||
template = f.read()
|
template = f.read()
|
||||||
for name in all_command_names():
|
for name in all_command_names():
|
||||||
@ -252,5 +272,12 @@ var DocTitleMap = map[string]string{serialize_go_dict(ref_map['doc'])}
|
|||||||
raise SystemExit(cp.returncode)
|
raise SystemExit(cp.returncode)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
with replace_if_needed('constants_generated.go') as f:
|
||||||
|
f.write(generate_constants())
|
||||||
|
update_at_commands()
|
||||||
|
print(json.dumps(changed, indent=2))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
11
setup.py
11
setup.py
@ -880,18 +880,9 @@ def safe_makedirs(path: str) -> None:
|
|||||||
|
|
||||||
def update_go_generated_files(args: Options, kitty_exe: str) -> None:
|
def update_go_generated_files(args: Options, kitty_exe: str) -> None:
|
||||||
# update all the various auto-generated go files, if needed
|
# update all the various auto-generated go files, if needed
|
||||||
rc_sources = [x for x in glob.glob('kitty/rc/*.py') if os.path.basename(x) not in ('base.py', '__init__.py')]
|
|
||||||
rc_objects = glob.glob('tools/cmd/at/*_generated.go')
|
|
||||||
generated = rc_objects + glob.glob('constants_generated.go')
|
|
||||||
sources = ['gen-rc-go.py', 'kitty/constants.py', 'setup.py', 'kitty/docs_ref_map_generated.h', 'tools/cmd/at/template.go'] + rc_sources
|
|
||||||
if generated:
|
|
||||||
oldest_generated = min(map(os.path.getmtime, generated))
|
|
||||||
newest_source = max(map(os.path.getmtime, sources))
|
|
||||||
if oldest_generated > newest_source and len(rc_sources) == len(rc_objects) and 'constants_generated.go' in generated:
|
|
||||||
return
|
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
print('Updating Go generated files...')
|
print('Updating Go generated files...')
|
||||||
cp = subprocess.run([kitty_exe, '+launch', os.path.join(base, 'gen-rc-go.py')])
|
cp = subprocess.run([kitty_exe, '+launch', os.path.join(base, 'gen-rc-go.py')], stdout=subprocess.PIPE)
|
||||||
if cp.returncode != 0:
|
if cp.returncode != 0:
|
||||||
raise SystemExit(cp.returncode)
|
raise SystemExit(cp.returncode)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user