Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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})"
Add Comment
Please, Sign In to add comment