Module: Reivt::Util

Defined in:
lib/reivt/util.rb

Overview

A collection of utility functions for use with reivt

Class Method Summary collapse

Class Method Details

.doc_from_path(path, has_diff = false) ⇒ Revit::Document

Wrapper for creating a Document object from a path

Parameters:

  • path (String)

    Location on the filesystem to access for files

  • has_diff (Boolean) (defaults to: false)

    A flag to tell if the document has a dif

  • spinner (TTY:Spinner)

    A spinner for feedback

Returns:

  • (Revit::Document)

    A newly created Document object



25
26
27
28
29
30
# File 'lib/reivt/util.rb', line 25

def self.doc_from_path(path, has_diff = false)
  blob = File.read(path)
  doc_name = File.basename(path)
  content_type = Util.ext_to_lang(File.extname(path))
  Document.new(blob, content_type, doc_name, has_diff)
end

.docs_from_dir(path) ⇒ Array<Reivt::Document>

Creates a list of documents from files in the passed directory

Parameters:

  • path (String)

    Location on the filesystem to access for files

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reivt/util.rb', line 38

def self.docs_from_dir(path)
  docs = []

  # Recursively get all file paths in a directory
  entries = Dir.glob("#{path}/**/*").reject { |entry| Dir.exist?(entry) }

  entries.each do |entry|
    docs.push(Util.doc_from_path(entry)) if entry != '.' && entry != '..'
  end

  docs
end

.docs_from_repo(path) ⇒ Array<Reivt::Document>

Creates a list of documents from repo changelist

Parameters:

  • path (String)

    Location on the filesystem to access for files

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/reivt/util.rb', line 57

def self.docs_from_repo(path)
  docs = []
  repo = Rugged::Repository.discover(path)

  if repo.bare? || repo.empty?
    raise Reivt::BaemptyException, "Bad repo: #{path}"
  end

  commit = repo.head.target
  diff = commit.parents.first.diff(commit)

  diff.find_similar!
  diff.each_delta do |d|
    file_path = d.new_file[:path]
    docs.push(Util.doc_from_path("#{path}/#{file_path}", true))

    ofile = repo.lookup(d.old_file[:oid])
    nfile = repo.lookup(d.new_file[:oid])
    diff_file = Tempfile.new([File.basename(file_path).to_s, '.diff'])

    diff_file.write(ofile.diff(nfile).to_s)
    docs.push(Util.doc_from_path(diff_file.path))
    diff_file.close
    diff_file.unlink
  end

  docs
end

.ext_to_lang(extension) ⇒ String

Translates a file extenstion to its corresponding programming language.

Parameters:

  • extension (String)

    The file extension to be analyzed

Returns:

  • (String)

    The name of the language corresponding to the extension



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/reivt/util.rb', line 92

def self.ext_to_lang(extension)
  case extension.downcase
  when '.rb', '.gemspec' then 'ruby'
  when '.py', '.pyw', '.pyd', '.py3' then 'python'
  when '.c', '.cpp', '.h', '.hpp' then 'c/c++'
  when '.js', '.jsx' then 'javascript'
  when '.java', '.class' then 'java'
  when '.json' then 'json'
  when '.yml', '.yaml' then 'yaml'
  when '.xml' then 'xml'
  when '.txt', ' ' then 'plain text'
  when '.sh', '.zsh', '.bash' then 'shell script'
  when '.md', '.markdown' then 'markdown'
  when '.fountain' then 'fountain'
  when '.diff' then 'diff'
  else
    'unknown'
  end
end