Skip to content

Instantly share code, notes, and snippets.

@msanders
Created May 19, 2018 13:50
Show Gist options
  • Save msanders/c408ec761bfbe2ecb663289004ed688e to your computer and use it in GitHub Desktop.
Save msanders/c408ec761bfbe2ecb663289004ed688e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
options = {}
optionparser.new do |opts|
opts.banner = 'usage: find-types.rb [options]'
opts.on('-f', '--file file', 'swift source file') { |v| options[:file] = v }
opts.on('-d',
'--directory directory',
'directory containing swift files') do |v|
options[:directory] = v
end
end.parse!
def parse_file_contents(contents)
structs = []
protocols = []
begin
contents.split("\n").each do |x|
if x =~ /^public\s*struct\s*([a-za-z0-9]{3,})/
structs << regexp.last_match[1]
elsif x =~ /^public\s*protocol\s*([a-za-z0-9]{3,})/
protocols << regexp.last_match[1]
end
end
rescue argumenterror => err
puts "failed to parse file: #{err}"
end
{ structs: structs, protocols: protocols }
end
def procdir(dir)
dir[file.join(dir, '**', '*')].reject { |p| file.directory? p }
end
file_contents =
if options.key?(:file)
parse_file_contents(file.read(options[:file]))
elsif options.key?(:directory)
contents = { structs: [], protocols: [] }
files = procdir(options[:directory]).select do |x|
x.end_with?('.swift')
end
files.each do |fname|
file_contents = parse_file_contents(file.read(fname))
contents[:structs] += file_contents[:structs]
contents[:protocols] += file_contents[:protocols]
end
contents
end
formatted_structs = file_contents[:structs].map { |x| "\"#{x}\"" }.join(' ')
formatted_protocols = file_contents[:protocols].map { |x| "\"#{x}\"" }.join(' ')
puts "'(#{formatted_structs})"
puts "'(#{formatted_protocols})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment