Class: Quality::LinguistSourceFileGlobber
- Inherits:
-
Object
- Object
- Quality::LinguistSourceFileGlobber
- Defined in:
- lib/quality/linguist_source_file_globber.rb
Overview
Uses the Linguist gem to find and classify source files.
Note: Requires files to be commited within a git repo.
Instance Attribute Summary collapse
Instance Method Summary collapse
- #all_files ⇒ Object
-
#initialize(repo: Rugged::Repository.new('.'), commit: repo.head, project: Linguist::Repository.new(repo, commit.target_id), file_blob: Linguist::FileBlob, file_class: File, pwd: Dir.pwd) ⇒ LinguistSourceFileGlobber
constructor
A new instance of LinguistSourceFileGlobber.
- #js_files ⇒ Object
- #language_files(language) ⇒ Object
- #markdown_files ⇒ Object
- #ok_to_process?(filename, file) ⇒ Boolean
- #python_files ⇒ Object
- #real_files_matching ⇒ Object
- #real_files_of_type(file_type) ⇒ Object
- #ruby_files ⇒ Object
- #scala_files ⇒ Object
- #shell_files ⇒ Object
- #source_and_doc_files ⇒ Object
- #source_files ⇒ Object
- #submodule_or_symlink?(file) ⇒ Boolean
Constructor Details
#initialize(repo: Rugged::Repository.new('.'), commit: repo.head, project: Linguist::Repository.new(repo, commit.target_id), file_blob: Linguist::FileBlob, file_class: File, pwd: Dir.pwd) ⇒ LinguistSourceFileGlobber
Returns a new instance of LinguistSourceFileGlobber.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/quality/linguist_source_file_globber.rb', line 11 def initialize(repo: Rugged::Repository.new('.'), commit: repo.head, project: Linguist::Repository.new(repo, commit.target_id), file_blob: Linguist::FileBlob, file_class: File, pwd: Dir.pwd) @repo = repo @commit = commit @project = project @breakdown_by_file = @project.breakdown_by_file @file_blob = file_blob @file_class = file_class @exclude_files = nil @pwd = pwd end |
Instance Attribute Details
#exclude_files ⇒ Object
83 84 85 |
# File 'lib/quality/linguist_source_file_globber.rb', line 83 def exclude_files @exclude_files || [] end |
Instance Method Details
#all_files ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/quality/linguist_source_file_globber.rb', line 44 def all_files @all_files ||= begin files = [] tree = @commit.target.tree tree.walk(:preorder) do |root, file| filename = "#{root}#{file[:name]}" files << filename if ok_to_process?(filename, file) end files end end |
#js_files ⇒ Object
79 80 81 |
# File 'lib/quality/linguist_source_file_globber.rb', line 79 def js_files language_files('JavaScript') end |
#language_files(language) ⇒ Object
56 57 58 |
# File 'lib/quality/linguist_source_file_globber.rb', line 56 def language_files(language) (@breakdown_by_file[language] || []) - exclude_files end |
#markdown_files ⇒ Object
123 124 125 |
# File 'lib/quality/linguist_source_file_globber.rb', line 123 def markdown_files @markdown_files ||= real_files_of_type('Markdown') end |
#ok_to_process?(filename, file) ⇒ Boolean
36 37 38 39 40 41 42 |
# File 'lib/quality/linguist_source_file_globber.rb', line 36 def ok_to_process?(filename, file) file[:type] == :blob && !submodule_or_symlink?(file) && @file_class.exist?(filename) && !@file_class.symlink?(filename) && @file_class.readable?(filename) end |
#python_files ⇒ Object
67 68 69 |
# File 'lib/quality/linguist_source_file_globber.rb', line 67 def python_files language_files('Python') end |
#real_files_matching ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/quality/linguist_source_file_globber.rb', line 87 def real_files_matching all_files.select do |filename| blob = @file_blob.new(filename, @pwd) if blob.generated? || blob.vendored? false else yield blob, filename end end end |
#real_files_of_type(file_type) ⇒ Object
119 120 121 |
# File 'lib/quality/linguist_source_file_globber.rb', line 119 def real_files_of_type(file_type) real_files_matching { |blob, _filename| blob.language.to_s == file_type } end |
#ruby_files ⇒ Object
60 61 62 63 64 65 |
# File 'lib/quality/linguist_source_file_globber.rb', line 60 def ruby_files # Linguist treats Gemfile.lock as Ruby code. # # https://github.com/github/linguist/issues/1740 language_files('Ruby') - ['Gemfile.lock'] end |
#scala_files ⇒ Object
71 72 73 |
# File 'lib/quality/linguist_source_file_globber.rb', line 71 def scala_files language_files('Scala') end |
#shell_files ⇒ Object
75 76 77 |
# File 'lib/quality/linguist_source_file_globber.rb', line 75 def shell_files language_files('Shell') end |
#source_and_doc_files ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/quality/linguist_source_file_globber.rb', line 106 def source_and_doc_files @source_and_doc_files ||= begin real_files_matching do |blob, _filename| if blob.documentation? || !blob.language.nil? true else # puts "Excluding #{filename} from source_and_doc_files" false end end end end |
#source_files ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/quality/linguist_source_file_globber.rb', line 98 def source_files @source_files ||= begin real_files_matching do |blob| !blob.language.nil? && !blob.documentation? end end end |
#submodule_or_symlink?(file) ⇒ Boolean
29 30 31 32 33 34 |
# File 'lib/quality/linguist_source_file_globber.rb', line 29 def submodule_or_symlink?(file) # Skip submodules and symlinks mode = file[:filemode] mode_format = (mode & 0o0170000) [0o0120000, 0o040000, 0o0160000].include? mode_format end |