Annotate script syntax errors

develop
lethosor 2020-06-30 00:24:03 -04:00
parent e95edc095c
commit fedf68cc7c
2 changed files with 29 additions and 3 deletions

@ -106,10 +106,10 @@ jobs:
python travis/script-docs.py python travis/script-docs.py
- name: Check Lua syntax - name: Check Lua syntax
run: | run: |
python travis/script-syntax.py --ext=lua --cmd="luac5.3 -p" python travis/script-syntax.py --ext=lua --cmd="luac5.3 -p" --github-actions
- name: Check Ruby syntax - name: Check Ruby syntax
run: | run: |
python travis/script-syntax.py --ext=rb --cmd="ruby -c" python travis/script-syntax.py --ext=rb --cmd="ruby -c" --github-actions
check-pr: check-pr:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.event_name == 'pull_request' if: github.event_name == 'pull_request'

@ -3,6 +3,24 @@ import os
import subprocess import subprocess
import sys import sys
def print_stderr(stderr, args):
if not args.github_actions:
sys.stderr.write(stderr + '\n')
return
for line in stderr.split('\n'):
parts = list(map(str.strip, line.split(':')))
# e.g. luac prints "luac:" in front of messages, so find the first part
# containing the actual filename
for i in range(len(parts) - 1):
if parts[i].endswith('.' + args.ext) and parts[i + 1].isdigit():
print('::error file=%s,line=%s::%s' % (parts[i], parts[i + 1], ':'.join(parts[i + 2:])))
break
else:
print(line)
def main(args): def main(args):
root_path = os.path.abspath(args.path) root_path = os.path.abspath(args.path)
cmd = args.cmd.split(' ') cmd = args.cmd.split(' ')
@ -19,7 +37,13 @@ def main(args):
continue continue
full_path = os.path.join(cur, filename) full_path = os.path.join(cur, filename)
try: try:
subprocess.check_output(cmd + [full_path]) p = subprocess.Popen(cmd + [full_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_, stderr = p.communicate()
stderr = stderr.decode('utf-8', errors='ignore')
if stderr:
print_stderr(stderr, args)
if p.returncode != 0:
err = True
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
err = True err = True
except IOError: except IOError:
@ -34,5 +58,7 @@ if __name__ == '__main__':
parser.add_argument('--path', default='.', help='Root directory') parser.add_argument('--path', default='.', help='Root directory')
parser.add_argument('--ext', help='Script extension', required=True) parser.add_argument('--ext', help='Script extension', required=True)
parser.add_argument('--cmd', help='Command', required=True) parser.add_argument('--cmd', help='Command', required=True)
parser.add_argument('--github-actions', action='store_true',
help='Enable GitHub Actions workflow command output')
args = parser.parse_args() args = parser.parse_args()
main(args) main(args)