Created
November 21, 2015 15:38
-
-
Save YusukeHosonuma/a2bc13b4ab14ef702828 to your computer and use it in GitHub Desktop.
Count swift functions
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
def swift_files(path, files) | |
Dir::glob(path + '/*').each {|fname| | |
if fname.include?('Test') # no search test dir & files | |
return | |
end | |
if FileTest.directory?(fname) | |
swift_files(fname, files) | |
else | |
if fname.include?('.swift') | |
files.push(fname) | |
end | |
end | |
} | |
end | |
def is_test_function?(line) | |
['Test', 'test', 'setUp', 'tearDown'].each {|word| | |
if line.include?(word) | |
return true | |
end | |
} | |
return false | |
end | |
def is_function?(line) | |
return !is_comment?(line) && line.include?('func') && !is_test_function?(line) | |
end | |
def is_subscript?(line) | |
return !is_comment?(line) && line.include?('subscript') && !is_test_function?(line) | |
end | |
def is_computed_property?(line) | |
return !is_comment?(line) && /var.*{/ =~ line | |
end | |
def is_comment?(line) | |
return line.include?('//') | |
end | |
files = [] | |
swift_files('.', files) | |
func_count = 0 | |
subscript_count = 0 | |
computed_property_count = 0 | |
files.each {|file| | |
puts '' | |
puts "#{File.basename(file)}:" | |
File.open(file) {|f| | |
f.each_line do |line| | |
line_s = line.gsub(/^\s*/, '') | |
if is_function?(line_s) | |
func_count = func_count + 1 | |
puts "[F] #{line_s}" | |
elsif is_subscript?(line_s) | |
subscript_count = subscript_count + 1 | |
puts "[S] #{line_s}" | |
elsif is_computed_property?(line_s) | |
computed_property_count = computed_property_count + 1 | |
puts "[S] #{line_s}" | |
end | |
end | |
} | |
} | |
total = func_count + subscript_count + computed_property_count | |
puts <<EOS | |
============ report ============ | |
function: #{func_count} | |
subscript: #{subscript_count} | |
computed property: #{computed_property_count} | |
---------------------------- | |
total: #{total} | |
EOS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment