diff options
| -rw-r--r-- | .github/workflows/format.yaml | 21 | ||||
| -rw-r--r-- | lib/python/qmk/cli/format/text.py | 64 |
2 files changed, 61 insertions, 24 deletions
diff --git a/.github/workflows/format.yaml b/.github/workflows/format.yaml index 0bb870106..9e165e0e8 100644 --- a/.github/workflows/format.yaml +++ b/.github/workflows/format.yaml | |||
| @@ -19,7 +19,9 @@ jobs: | |||
| 19 | container: qmkfm/qmk_cli | 19 | container: qmkfm/qmk_cli |
| 20 | 20 | ||
| 21 | steps: | 21 | steps: |
| 22 | - uses: rlespinasse/github-slug-action@v3.x | 22 | - name: Install dependencies |
| 23 | run: | | ||
| 24 | apt-get update && apt-get install -y dos2unix | ||
| 23 | 25 | ||
| 24 | - uses: actions/checkout@v2 | 26 | - uses: actions/checkout@v2 |
| 25 | with: | 27 | with: |
| @@ -31,12 +33,17 @@ jobs: | |||
| 31 | output: ' ' | 33 | output: ' ' |
| 32 | fileOutput: ' ' | 34 | fileOutput: ' ' |
| 33 | 35 | ||
| 34 | - name: Run qmk format-c and qmk format-python | 36 | - name: Run qmk formatters |
| 35 | shell: 'bash {0}' | 37 | shell: 'bash {0}' |
| 36 | run: | | 38 | run: | |
| 37 | qmk format-c --core-only -n $(< ~/files.txt) | 39 | qmk format-c --core-only $(< ~/files.txt) |
| 38 | format_c_exit=$? | 40 | qmk format-python |
| 39 | qmk format-python -n | 41 | qmk format-text $(< ~/files.txt) |
| 40 | format_python_exit=$? | 42 | git diff |
| 41 | 43 | ||
| 42 | exit $((format_c_exit + format_python_exit)) | 44 | - name: Fail when formatting required |
| 45 | run: | | ||
| 46 | for file in $(git diff --name-only); do | ||
| 47 | echo "::error file=${file}::::File Requires Formatting" | ||
| 48 | done | ||
| 49 | test -z "$(git diff --name-only)" | ||
diff --git a/lib/python/qmk/cli/format/text.py b/lib/python/qmk/cli/format/text.py index e7e07b729..6dd451189 100644 --- a/lib/python/qmk/cli/format/text.py +++ b/lib/python/qmk/cli/format/text.py | |||
| @@ -1,27 +1,57 @@ | |||
| 1 | """Ensure text files have the proper line endings. | 1 | """Ensure text files have the proper line endings. |
| 2 | """ | 2 | """ |
| 3 | from subprocess import CalledProcessError | 3 | from itertools import islice |
| 4 | from subprocess import DEVNULL | ||
| 4 | 5 | ||
| 5 | from milc import cli | 6 | from milc import cli |
| 6 | 7 | ||
| 8 | from qmk.path import normpath | ||
| 7 | 9 | ||
| 10 | |||
| 11 | def _get_chunks(it, size): | ||
| 12 | """Break down a collection into smaller parts | ||
| 13 | """ | ||
| 14 | it = iter(it) | ||
| 15 | return iter(lambda: tuple(islice(it, size)), ()) | ||
| 16 | |||
| 17 | |||
| 18 | def dos2unix_run(files): | ||
| 19 | """Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything | ||
| 20 | """ | ||
| 21 | for chunk in _get_chunks(files, 10): | ||
| 22 | dos2unix = cli.run(['dos2unix', *chunk]) | ||
| 23 | |||
| 24 | if dos2unix.returncode: | ||
| 25 | return False | ||
| 26 | |||
| 27 | |||
| 28 | @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') | ||
| 29 | @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.') | ||
| 30 | @cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.') | ||
| 8 | @cli.subcommand("Ensure text files have the proper line endings.", hidden=True) | 31 | @cli.subcommand("Ensure text files have the proper line endings.", hidden=True) |
| 9 | def format_text(cli): | 32 | def format_text(cli): |
| 10 | """Ensure text files have the proper line endings. | 33 | """Ensure text files have the proper line endings. |
| 11 | """ | 34 | """ |
| 12 | try: | 35 | # Find the list of files to format |
| 13 | file_list_cmd = cli.run(['git', 'ls-files', '-z'], check=True) | 36 | if cli.args.files: |
| 14 | except CalledProcessError as e: | 37 | files = list(cli.args.files) |
| 15 | cli.log.error('Could not get file list: %s', e) | 38 | |
| 16 | exit(1) | 39 | if cli.args.all_files: |
| 17 | except Exception as e: | 40 | cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files))) |
| 18 | cli.log.error('Unhandled exception: %s: %s', e.__class__.__name__, e) | 41 | |
| 19 | cli.log.exception(e) | 42 | elif cli.args.all_files: |
| 20 | exit(1) | 43 | git_ls_cmd = ['git', 'ls-files'] |
| 21 | 44 | git_ls = cli.run(git_ls_cmd, stdin=DEVNULL) | |
| 22 | dos2unix = cli.run(['xargs', '-0', 'dos2unix'], stdin=None, input=file_list_cmd.stdout) | 45 | files = list(filter(None, git_ls.stdout.split('\n'))) |
| 23 | 46 | ||
| 24 | if dos2unix.returncode != 0: | 47 | else: |
| 25 | print(dos2unix.stderr) | 48 | git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch] |
| 26 | 49 | git_diff = cli.run(git_diff_cmd, stdin=DEVNULL) | |
| 27 | return dos2unix.returncode | 50 | files = list(filter(None, git_diff.stdout.split('\n'))) |
| 51 | |||
| 52 | # Sanity check | ||
| 53 | if not files: | ||
| 54 | cli.log.error('No changed files detected. Use "qmk format-text -a" to format all files') | ||
| 55 | return False | ||
| 56 | |||
| 57 | return dos2unix_run(files) | ||
