Guest User

Untitled

a guest
May 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'optparse'
  3.  
  4. options = {}
  5. optionparser.new do |opts|
  6. opts.banner = 'usage: find-types.rb [options]'
  7.  
  8. opts.on('-f', '--file file', 'swift source file') { |v| options[:file] = v }
  9. opts.on('-d',
  10. '--directory directory',
  11. 'directory containing swift files') do |v|
  12. options[:directory] = v
  13. end
  14. end.parse!
  15.  
  16. def parse_file_contents(contents)
  17. structs = []
  18. protocols = []
  19. begin
  20. contents.split("\n").each do |x|
  21. if x =~ /^public\s*struct\s*([a-za-z0-9]{3,})/
  22. structs << regexp.last_match[1]
  23. elsif x =~ /^public\s*protocol\s*([a-za-z0-9]{3,})/
  24. protocols << regexp.last_match[1]
  25. end
  26. end
  27. rescue argumenterror => err
  28. puts "failed to parse file: #{err}"
  29. end
  30.  
  31. { structs: structs, protocols: protocols }
  32. end
  33.  
  34. def procdir(dir)
  35. dir[file.join(dir, '**', '*')].reject { |p| file.directory? p }
  36. end
  37.  
  38. file_contents =
  39. if options.key?(:file)
  40. parse_file_contents(file.read(options[:file]))
  41. elsif options.key?(:directory)
  42. contents = { structs: [], protocols: [] }
  43. files = procdir(options[:directory]).select do |x|
  44. x.end_with?('.swift')
  45. end
  46. files.each do |fname|
  47. file_contents = parse_file_contents(file.read(fname))
  48. contents[:structs] += file_contents[:structs]
  49. contents[:protocols] += file_contents[:protocols]
  50. end
  51. contents
  52. end
  53.  
  54. formatted_structs = file_contents[:structs].map { |x| "\"#{x}\"" }.join(' ')
  55. formatted_protocols = file_contents[:protocols].map { |x| "\"#{x}\"" }.join(' ')
  56. puts "'(#{formatted_structs})"
  57. puts "'(#{formatted_protocols})"
Add Comment
Please, Sign In to add comment