Sha256: 781d3a2e8dc1ad8df30fa265205fd582cb8569d8efd2eb094a185c3a43b52b99

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true
require 'rugged'
require 'linguist'

module Quality
  # Uses the Linguist gem to find and classify source files.
  #
  # Note: Requires files to be commited within a git repo.
  class LinguistSourceFileGlobber
    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

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

    def ruby_files
      @breakdown_by_file['Ruby'] || []
    end

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

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

    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

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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quality-23.0.0 lib/quality/linguist_source_file_globber.rb