Created
July 12, 2018 12:03
-
-
Save aminbenarieb/baf1d35c3a98e1800fd912a2333c5301 to your computer and use it in GitHub Desktop.
Xcode: Import fixes
This file contains hidden or 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 python3 | |
import os | |
match_keyword ="<some stuff here to find>" | |
import_line = "<some stuff needed to be imported>" | |
def find_first_line_match_idx(lines, match_str): | |
lastmatchIdx = None | |
idx = 0 | |
for line in lines: | |
if match_str in line: | |
lastmatchIdx = idx | |
idx += 1 | |
return lastmatchIdx | |
# traverse root directory, and list directories as dirs and files as files | |
for root, dirs, files in os.walk("."): | |
path = root.split(os.sep) | |
print((len(path) - 1) * '---', os.path.basename(root)) | |
for file in files: | |
path_to_file = root+"/"+file | |
with open(path_to_file, encoding = "ISO-8859-1") as f: | |
lines = f.readlines() | |
for line in lines: | |
if match_keyword in line: | |
print(len(path) * '---', file) | |
last_import_line_idx = find_first_line_match_idx(lines, "#import") | |
lines.insert(last_import_line_idx+1, import_line) | |
break | |
f = open(path_to_file, "w", encoding = "ISO-8859-1") | |
contents = "".join(lines) | |
f.write(contents) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment