Created
October 30, 2016 15:52
-
-
Save mitsuse/e68b94c26eb7487cc638f023dfbac758 to your computer and use it in GitHub Desktop.
Recursively format source files written in Swiftm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
def main(args): | |
for p in walk(args.path, pattern='.*\.swift$'): | |
format(p, args.indent_width) | |
def format(path, indent_width): | |
import subprocess | |
command = [ | |
'sourcekitten', | |
'format', | |
'--file', path, | |
'--indent-width', str(indent_width) | |
] | |
subprocess.Popen( | |
command, | |
stdin=None, | |
stdout=None, | |
stderr=None | |
).wait() | |
def walk(base_path, pattern=None): | |
import os | |
import re | |
from os import path | |
def match(name): | |
if pattern is None: | |
return True | |
return re.match(pattern, name) is not None | |
for (directory, _, files) in os.walk(base_path): | |
for f in filter(match, files): | |
yield path.join(directory, f) | |
def parse(raw_args): | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument( | |
'--path', | |
'-p', | |
metavar='PATH', | |
required=True, | |
type=str, | |
help='the path of directory which contains source files to be formatted' | |
) | |
parser.add_argument( | |
'--indent-width', | |
'-i', | |
metavar='WIDTH', | |
required=True, | |
type=int, | |
help='the width of indent' | |
) | |
return parser.parse_args(raw_args) | |
if __name__ == '__main__': | |
import sys | |
main(parse(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment