Class: Quality::LinguistSourceFileGlobber

Inherits:
Object
  • Object
show all
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 Method Summary collapse

Constructor Details

#initialize(repo: Rugged::Repository.new('.'), commit: repo.head, project: Linguist::Repository.new(repo, commit.target_id), file_blob: Linguist::FileBlob, pwd: Dir.pwd) ⇒ LinguistSourceFileGlobber

Returns a new instance of LinguistSourceFileGlobber.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/quality/linguist_source_file_globber.rb', line 10

def initialize(repo: Rugged::Repository.new('.'),
               commit: repo.head,
               project: Linguist::Repository.new(repo, commit.target_id),
               file_blob: Linguist::FileBlob,
               pwd: Dir.pwd)
  @repo = repo
  @commit = commit
  @project = project
  @breakdown_by_file = @project.breakdown_by_file
  @file_blob = file_blob
  @pwd = pwd
end

Instance Method Details

#all_filesObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/quality/linguist_source_file_globber.rb', line 32

def all_files
  @source_files ||= begin
    files = []
    tree = @commit.target.tree
    tree.walk(:preorder) do |root, file|
      unless file[:type] != :blob || submodule_or_symlink?(file)
        files << "#{root}#{file[:name]}"
      end
    end
    files
  end
end

#js_filesObject



56
57
58
# File 'lib/quality/linguist_source_file_globber.rb', line 56

def js_files
  @breakdown_by_file['JavaScript'] || []
end

#python_filesObject



52
53
54
# File 'lib/quality/linguist_source_file_globber.rb', line 52

def python_files
  @breakdown_by_file['Python'] || []
end

#real_files_matchingObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/quality/linguist_source_file_globber.rb', line 60

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

#ruby_filesObject



45
46
47
48
49
50
# File 'lib/quality/linguist_source_file_globber.rb', line 45

def ruby_files
  # Linguist treats Gemfile.lock as Ruby code.
  #
  # https://github.com/github/linguist/issues/1740
  @breakdown_by_file['Ruby'] - ['Gemfile.lock'] || []
end

#source_and_doc_filesObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/quality/linguist_source_file_globber.rb', line 79

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_filesObject



71
72
73
74
75
76
77
# File 'lib/quality/linguist_source_file_globber.rb', line 71

def source_files
  @source_files ||= begin
    real_files_matching do |blob|
      !blob.language.nil?
    end
  end
end

#submodule_or_symlink?(file) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/quality/linguist_source_file_globber.rb', line 23

def submodule_or_symlink?(file)
  # Skip submodules and symlinks
  mode = file[:mode]
  mode_format = (mode & 0170000)
  mode_format == 0120000 ||
    mode_format == 040000 ||
    mode_format == 0160000
end