aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Jorge Canizales <jcanizales@google.com>2015-10-26 10:44:38 -0700
committerGravatar Jorge Canizales <jcanizales@google.com>2015-10-26 10:44:38 -0700
commitbbb7774b83a19edff725a7e269d6c75193193c13 (patch)
tree80fa33399a1355219594940e12ad854460f36bd4
parent88c32843498aaa60cee60d7676c0d1c352c82c9a (diff)
Make the script support multiple input files
-rwxr-xr-xsrc/objective-c/change-comments.py122
-rw-r--r--src/objective-c/format-all-comments.sh3
2 files changed, 65 insertions, 60 deletions
diff --git a/src/objective-c/change-comments.py b/src/objective-c/change-comments.py
index 16dc863a02..4ce25705b6 100755
--- a/src/objective-c/change-comments.py
+++ b/src/objective-c/change-comments.py
@@ -4,97 +4,99 @@ import re
import sys
-if len(sys.argv) != 2:
- print("Please provide a source file name as only argument.")
+if len(sys.argv) < 2:
+ print("Please provide at least one source file name as argument.")
quit()
-print("Modifying format of {file} comments in place...".format(
- file = sys.argv[1],
-))
+for file_name in sys.argv[1:]:
+ print("Modifying format of {file} comments in place...".format(
+ file = file_name,
+ ))
-# Input
-lines = []
+ # Input
-with open(sys.argv[1], "r") as input_file:
- lines = input_file.readlines()
+ lines = []
-def peek():
- return lines[0]
+ with open(file_name, "r") as input_file:
+ lines = input_file.readlines()
-def read_line():
- return lines.pop(0)
+ def peek():
+ return lines[0]
-def more_input_available():
- return lines
+ def read_line():
+ return lines.pop(0)
+ def more_input_available():
+ return lines
-# Output
-output_lines = []
+ # Output
-def write(line):
- output_lines.append(line)
+ output_lines = []
-def flush_output():
- with open(sys.argv[1], "w") as otuput_file:
- for line in output_lines:
- otuput_file.write(line)
+ def write(line):
+ output_lines.append(line)
+ def flush_output():
+ with open(file_name, "w") as otuput_file:
+ for line in output_lines:
+ otuput_file.write(line)
-# Pattern matching
-comment_regex = r'^(\s*)//\s(.*)$'
+ # Pattern matching
-def is_comment(line):
- return re.search(comment_regex, line)
+ comment_regex = r'^(\s*)//\s(.*)$'
-def isnt_comment(line):
- return not is_comment(line)
+ def is_comment(line):
+ return re.search(comment_regex, line)
-def next_line(predicate):
- if not more_input_available():
- return False
- return predicate(peek())
+ def isnt_comment(line):
+ return not is_comment(line)
+ def next_line(predicate):
+ if not more_input_available():
+ return False
+ return predicate(peek())
-# Transformation
-def indentation_of(line):
- match = re.search(comment_regex, line)
- return match.group(1)
+ # Transformation
-def content(line):
- match = re.search(comment_regex, line)
- return match.group(2)
+ def indentation_of(line):
+ match = re.search(comment_regex, line)
+ return match.group(1)
-def format_as_block(comment_block):
- if len(comment_block) == 0:
- return []
+ def content(line):
+ match = re.search(comment_regex, line)
+ return match.group(2)
- indent = indentation_of(comment_block[0])
+ def format_as_block(comment_block):
+ if len(comment_block) == 0:
+ return []
- if len(comment_block) == 1:
- return [indent + "/** " + content(comment_block[0]) + " */\n"]
+ indent = indentation_of(comment_block[0])
- block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"]
- return [indent + line + "\n" for line in block]
+ if len(comment_block) == 1:
+ return [indent + "/** " + content(comment_block[0]) + " */\n"]
+ block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"]
+ return [indent + line.rstrip() + "\n" for line in block]
-# Main algorithm
-while more_input_available():
- while next_line(isnt_comment):
- write(read_line())
+ # Main algorithm
- comment_block = []
- # Get all lines in the same comment block. We could restrict the indentation
- # to be the same as the first line of the block, but it's probably ok.
- while (next_line(is_comment)):
- comment_block.append(read_line())
+ while more_input_available():
+ while next_line(isnt_comment):
+ write(read_line())
- for line in format_as_block(comment_block):
- write(line)
+ comment_block = []
+ # Get all lines in the same comment block. We could restrict the indentation
+ # to be the same as the first line of the block, but it's probably ok.
+ while (next_line(is_comment)):
+ comment_block.append(read_line())
-flush_output()
+ for line in format_as_block(comment_block):
+ write(line)
+
+ flush_output()
diff --git a/src/objective-c/format-all-comments.sh b/src/objective-c/format-all-comments.sh
new file mode 100644
index 0000000000..c4ea87d336
--- /dev/null
+++ b/src/objective-c/format-all-comments.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/bash
+
+find . -type f -name "*.h" ! -path "*/Pods/*" ! -path "./generated_libraries/*" ! -path "./examples/*" ! -path "./tests/*" | xargs ./change-comments.py