1
0
Fork 0

[CLI] Add common util for dumping generated content (#16674)

This commit is contained in:
Joel Challis 2022-03-18 01:09:29 +00:00 committed by GitHub
parent 417f089115
commit e5823b5650
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 132 additions and 116 deletions

View file

@ -5,7 +5,6 @@ import sys
import shutil
from pathlib import Path
from subprocess import DEVNULL
from time import strftime
from milc import cli
import jsonschema
@ -14,8 +13,6 @@ import qmk.keymap
from qmk.constants import QMK_FIRMWARE, KEYBOARD_OUTPUT_PREFIX
from qmk.json_schema import json_load, validate
time_fmt = '%Y-%m-%d-%H:%M:%S'
def _find_make():
"""Returns the correct make command for this environment.
@ -136,37 +133,6 @@ def get_make_parallel_args(parallel=1):
return parallel_args
def create_version_h(skip_git=False, skip_all=False):
"""Generate version.h contents
"""
if skip_all:
current_time = "1970-01-01-00:00:00"
else:
current_time = strftime(time_fmt)
if skip_git:
git_version = "NA"
chibios_version = "NA"
chibios_contrib_version = "NA"
else:
git_version = get_git_version(current_time)
chibios_version = get_git_version(current_time, "chibios", "os")
chibios_contrib_version = get_git_version(current_time, "chibios-contrib", "os")
version_h_lines = f"""/* This file was automatically generated. Do not edit or copy.
*/
#pragma once
#define QMK_VERSION "{git_version}"
#define QMK_BUILDDATE "{current_time}"
#define CHIBIOS_VERSION "{chibios_version}"
#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"
"""
return version_h_lines
def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_vars):
"""Convert a configurator export JSON file into a C file and then compile it.
@ -201,9 +167,6 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_va
keymap_dir.mkdir(exist_ok=True, parents=True)
keymap_c.write_text(c_text)
version_h = Path('quantum/version.h')
version_h.write_text(create_version_h())
# Return a command that can be run to make the keymap and flash if given
verbose = 'true' if cli.config.general.verbose else 'false'
color = 'true' if cli.config.general.color else 'false'
@ -357,3 +320,20 @@ def in_virtualenv():
"""
active_prefix = getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix
return active_prefix != sys.prefix
def dump_lines(output_file, lines, quiet=True):
"""Handle dumping to stdout or file
Creates parent folders if required
"""
generated = '\n'.join(lines) + '\n'
if output_file and output_file.name != '-':
output_file.parent.mkdir(parents=True, exist_ok=True)
if output_file.exists():
output_file.replace(output_file.parent / (output_file.name + '.bak'))
output_file.write_text(generated)
if not quiet:
cli.log.info(f'Wrote {output_file.name} to {output_file}.')
else:
print(generated)