module Notes

Internal options parser

A web dashboard for annotations This is intended to be mounted within an application, e.g.

mount Notes::Web => '/notes'

Constants

COLORS
VERSION

Public Instance Methods

blame(filename, line_num) click to toggle source

Parse raw output from git-blame(1) (results not interpreted except for SHA)

Returns Hash

# File lib/notes-cli.rb, line 44
def blame(filename, line_num)
  fields = {}

  begin
    Dir.chdir(root) do
      blame = `git blame -L#{line_num},#{line_num} --line-porcelain -- #{filename} 2>/dev/null`.split("\n")
      sha = blame.shift.split(' ').first
      fields['sha'] = sha if sha != '0'*40 # Only use actual commit SHAs

      blame.each do |line|
        fieldname, *values = line.split(' ')
        fields[fieldname]  = values.join(' ')
      end
    end
  rescue
  end

  fields
end
colorize(color, str) click to toggle source
# File lib/notes-cli.rb, line 68
def colorize(color, str)
  "\e[#{COLORS[color]};1m#{str}\033[0m"
end
git?() click to toggle source

Are we in a git repo?

# File lib/notes-cli.rb, line 20
def git?
  Dir.chdir(root) do
    `git status 2>/dev/null`
    return $?.success?
  end
end
is_directory_or_excluded?(excluded, f) click to toggle source

Determine if a file handle should be rejected based on type and directories specified in options

excluded - Array of directories to exclude from search f - A String filename

Return Boolean

# File lib/notes-cli.rb, line 79
def is_directory_or_excluded?(excluded, f)
  is_in_excluded_dir = excluded.any? { |dir| File.dirname(f).include?(dir) }
  File.directory?(f) || is_in_excluded_dir
end
rails?() click to toggle source

Are we being included into a Rails project?

TODO: this is pretty hacky but we can’t rely on the definition of the Rails constant from the CLI and it works on both 3 & 4

# File lib/notes-cli.rb, line 13
def rails?
  path = root.to_s + '/config/application.rb'
  return false unless File.exists?(path)
  File.read(path).include?('Rails::Application')
end
root() click to toggle source

The root directory in which we’re searching

# File lib/notes-cli.rb, line 5
def root
  defined?(Rails) ? Rails.root : Dir.pwd
end
shortname(filename) click to toggle source

Convert a filename to a relative path

filename - String

Ex:

shortname("/Users/andrew/code/notes-cli/bin/notes")
=> "bin/notes"

Returns String

# File lib/notes-cli.rb, line 36
def shortname(filename)
  filename.gsub(Dir.pwd, '').gsub(/^\//, '')
end
valid_files(options) click to toggle source

Return an array of valid filenames for parsing

options

:locations - Array of files and directories to search
:exclude   - Array of directories to exclude from search

Return Array<String>

# File lib/notes-cli.rb, line 91
def valid_files(options)
  locations = options[:locations]
  excluded  = options[:exclude]

  locations.flat_map do |loc|
    if File.directory?(loc)
      Dir[ File.join(loc, "**/*") ]
        .reject do |f| is_directory_or_excluded?(excluded, f) end
    else
      loc
    end
  end
end