module Reivt::Util
A collection of utility functions for use with reivt
@author [brwnrclse]
Public Class Methods
Wrapper for creating a Document
object from a path, removes all carriage
returns from file contents
@param path [String] Location on the filesystem to access for files
@param has_diff [Boolean] A flag to tell if the document has a dif
@param spinner [TTY:Spinner] A spinner for feedback
@return [Revit::Document] A newly created Document
object
# File lib/reivt/util.rb, line 26 def self.doc_from_path(path, has_diff = false) blob = File.read(path).gsub("\r\n", "\n") doc_name = File.basename(path) content_type = Util.ext_to_lang(File.extname(path)) Document.new(blob, content_type, doc_name, has_diff) end
Creates a list of documents from files in the passed directory
@param path [String] Location on the filesystem to access for files
@return [Array<Reivt::Document>] List of documents from directory
# File lib/reivt/util.rb, line 39 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
Creates a list of documents from repo changelist
@param path [String] Location on the filesystem to access for files
@return [Array<Reivt::Document>] List of documents from repo commit
# File lib/reivt/util.rb, line 58 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
Translates a file extenstion to its corresponding programming language.
@param extension [String] The file extension to be analyzed
@return [String] The name of the language corresponding to the extension
# File lib/reivt/util.rb, line 93 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