| #!/usr/bin/env {{ python }} | |
| import os | |
| import subprocess | |
| import sys | |
| TOPLEVEL = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'], encoding='utf-8').strip() | |
| LOCATION = os.path.join(TOPLEVEL, r'{{ location }}') | |
| SCRIPTS = os.path.dirname(os.path.dirname(LOCATION)) | |
| def files(staged=True): | |
| try: | |
| lines = [] | |
| for line in subprocess.check_output( | |
| ['git', 'diff', '--name-only'] + (['--staged'] if staged else []), | |
| encoding='utf-8', | |
| ).splitlines(): | |
| if line: | |
| lines.append(line) | |
| return lines | |
| except subprocess.CalledProcessError: | |
| return [] | |
| staged = files(staged=True) | |
| unstaged = files(staged=False) | |
| project_files = [] | |
| for line in staged or unstaged: | |
| if line.endswith('pbxproj'): | |
| project_files.append(line) | |
| if project_files: | |
| code = subprocess.run(['perl', os.path.join(SCRIPTS, 'sort-Xcode-project-file')] + project_files).returncode | |
| if staged: | |
| code += subprocess.run(['git', 'add'] + project_files).returncode | |
| sys.exit(code) | |
| sys.exit(0) |