Class: Linguist::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/linguist/repository.rb

Overview

A Repository is an abstraction of a Grit::Repo or a basic file system tree. It holds a list of paths pointing to Blobish objects.

Its primary purpose is for gathering language statistics across the entire project.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum) ⇒ Repository

Public: Initialize a new Repository

enum - Enumerator that responds to ‘each` and

yields Blob objects

Returns a Repository



27
28
29
30
31
32
33
# File 'lib/linguist/repository.rb', line 27

def initialize(enum)
  @enum = enum
  @computed_stats = false
  @language = @size = nil
  @sizes = Hash.new { 0 }
  @file_breakdown = Hash.new { |h,k| h[k] = Array.new }
end

Class Method Details

.from_directory(base_path) ⇒ Object

Public: Initialize a new Repository from a File directory

base_path - A path String

Returns a Repository



15
16
17
18
19
# File 'lib/linguist/repository.rb', line 15

def self.from_directory(base_path)
  new Dir["#{base_path}/**/*"].
    select { |f| File.file?(f) }.
    map { |path| FileBlob.new(path, base_path) }
end

Instance Method Details

#breakdown_by_fileObject

Public: Return the language breakdown of this repository by file



65
66
67
68
# File 'lib/linguist/repository.rb', line 65

def breakdown_by_file
  compute_stats
  @file_breakdown
end

#compute_statsObject

Internal: Compute language breakdown for each blob in the Repository.

Returns nothing



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/linguist/repository.rb', line 73

def compute_stats
  return if @computed_stats

  @enum.each do |blob|
    # Skip files that are likely binary
    next if blob.likely_binary?

    # Skip vendored or generated blobs
    next if blob.vendored? || blob.generated? || blob.language.nil?

    # Only include programming languages and acceptable markup languages
    if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name)

      # Build up the per-file breakdown stats
      @file_breakdown[blob.language.group.name] << blob.name

      @sizes[blob.language.group] += blob.size
    end
  end

  # Compute total size
  @size = @sizes.inject(0) { |s,(_,v)| s + v }

  # Get primary language
  if primary = @sizes.max_by { |(_, size)| size }
    @language = primary[0]
  end

  @computed_stats = true

  nil
end

#languageObject

Public: Get primary Language of repository.

Returns a Language



51
52
53
54
# File 'lib/linguist/repository.rb', line 51

def language
  compute_stats
  @language
end

#languagesObject

Public: Returns a breakdown of language stats.

Examples

# => { Language['Ruby'] => 46319,
       Language['JavaScript'] => 258 }

Returns a Hash of Language keys and Integer size values.



43
44
45
46
# File 'lib/linguist/repository.rb', line 43

def languages
  compute_stats
  @sizes
end

#sizeObject

Public: Get the total size of the repository.

Returns a byte size Integer



59
60
61
62
# File 'lib/linguist/repository.rb', line 59

def size
  compute_stats
  @size
end