Guest User

Untitled

a guest
Mar 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. ##
  4. # Helpers
  5. ##
  6.  
  7. module Helper
  8. module_function
  9.  
  10. def display_start(count:)
  11. puts "Inspecting #{count} file(s)\n\n"
  12. end
  13.  
  14. def display_end(count:, offenses:)
  15. puts "#{count} file inspected, #{offenses.nonzero? || "no"} offense(s) detected"
  16. end
  17.  
  18. def regex
  19. @regex ||= {
  20. ruby_file: /[\/\-_\w]+.rb$/,
  21. not_commited_line: /0{8}\s+\(Not Committed Yet \d{4}\-\d{2}\-\d{2}\s{1}\d{2}:\d{2}:\d{2}\s{1}\-\d{4}\s+(\d+)\)/,
  22. modified_or_created: /\s?(M|\?{2}\s)/,
  23. offense: /^.+.rb:\d+:\d+:\sC:/
  24. }
  25. end
  26.  
  27. def normalize_file_name(files)
  28. files.map do |file|
  29. file.split(regex[:modified_or_created]).last
  30. end
  31. end
  32.  
  33. def offenses_for(file)
  34. linter_offenses(`rubocop #{file} | grep -E '\.rb:.+:.+' -A2`)
  35. end
  36.  
  37. def offenses_on_diff_for(file)
  38. changed_lines = `git blame #{file}`
  39. .scan(regex[:not_commited_line])
  40. .flatten
  41.  
  42. linter_offenses(`rubocop #{file} | grep -E '\.rb:(#{changed_lines.join('|')})+:' -A2`)
  43. end
  44.  
  45. def linter_offenses(result)
  46. return 0 if result.empty?
  47. puts result
  48. result.scan(regex[:offense]).size
  49. end
  50. end
  51.  
  52. ##
  53. # Linter
  54. ##
  55.  
  56. file = ARGV[0].to_s
  57. file_count = 1
  58. offenses = 0
  59.  
  60. if !file.empty? && File.exist?(file)
  61. Helper.display_start(count: file_count)
  62. offenses += Helper.offenses_on_diff_for(file)
  63. else
  64. modified, created = `git status --porcelain`
  65. .split(/\n/)
  66. .select { |status| status =~ Helper.regex[:ruby_file]}
  67. .partition { |status| status =~ /^\sM\s/ }
  68. .map { |files| Helper.normalize_file_name(files) }
  69.  
  70. file_count = modified.size + created.size
  71. Helper.display_start(count: file_count)
  72.  
  73. offenses += modified.map(&Helper.method(:offenses_on_diff_for)).reduce(:+)
  74. offenses += created.map(&Helper.method(:offenses_for)).reduce(:+)
  75. end
  76.  
  77. Helper.display_end(count: file_count, offenses: offenses)
Add Comment
Please, Sign In to add comment