Module: Notes

Extended by:
Notes
Included in:
Notes
Defined in:
lib/notes-cli/web.rb,
lib/notes-cli.rb,
lib/notes-cli/cli.rb,
lib/notes-cli/stats.rb,
lib/notes-cli/tasks.rb,
lib/notes-cli/server.rb,
lib/notes-cli/options.rb,
lib/notes-cli/version.rb

Overview

Internal options parser

Defined Under Namespace

Modules: Options, Stats, Tasks Classes: CLI, Server, Task, Web

Constant Summary collapse

COLORS =
{
  'yellow' => 33
}
VERSION =
'2.0.0'

Instance Method Summary collapse

Instance Method Details

#blame(filename, line_num) ⇒ Object

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

Returns Hash



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



68
69
70
# File 'lib/notes-cli.rb', line 68

def colorize(color, str)
  "\e[#{COLORS[color]};1m#{str}\033[0m"
end

#git?Boolean

Are we in a git repo?

Returns:

  • (Boolean)


20
21
22
23
24
25
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


79
80
81
82
# 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?Boolean

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

Returns:

  • (Boolean)


13
14
15
16
17
# 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

#rootObject

The root directory in which we’re searching



5
6
7
# File 'lib/notes-cli.rb', line 5

def root
  defined?(Rails) ? Rails.root : Dir.pwd
end

#shortname(filename) ⇒ Object

Convert a filename to a relative path

filename - String

Ex:

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

Returns String



36
37
38
# File 'lib/notes-cli.rb', line 36

def shortname(filename)
  filename.gsub(Dir.pwd, '').gsub(/^\//, '')
end

#valid_files(options) ⇒ Object

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>



91
92
93
94
95
96
97
98
99
100
101
102
103
# 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