Skip to content

Instantly share code, notes, and snippets.

@RobertAudi
Created November 17, 2011 04:38
Show Gist options
  • Save RobertAudi/1372373 to your computer and use it in GitHub Desktop.
Save RobertAudi/1372373 to your computer and use it in GitHub Desktop.
Something I hacked in an hour tonight. Disgusting code but useful output.
#!/usr/bin/env ruby
# encoding: utf-8
COLORS_ENABLED = TRUE
VALID_TASKS = {
"TODO" => /TODO[\s,:]+(\S.*)$/,
"FIXME" => /FIX ?ME[\s,:]+(\S.*)$/,
"CHANGED" => /CHANGED[\s,:]+(\S.*)$/,
"NOTE" => /NOTE[\s,:]+(\S.*)$/
}
EXCLUDED_DIRS = [
/\/coverage\//
]
ALL_TASKS = {}
def init_tasks
VALID_TASKS.each do |task_type, regex|
ALL_TASKS[task_type] = []
end
end
def parsef(file)
EXCLUDED_DIRS.each do |regex|
if file =~ regex
return
end
end
File.open(file, 'r') do |f|
line_number = 1
while line = f.gets
VALID_TASKS.each do |task_type, regex|
result = line.match(regex)
unless result.nil?
task = {
file: file,
line_number: line_number,
task: result.to_a.last
}
ALL_TASKS[task_type] << task
end
end
line_number += 1
end
end
end
def parsea(args)
if args.length == 0
raise ArgumentError, "You need to pass a directory or file as an argument."
end
args.each do |arg|
begin
parse arg if File.file?(arg)
rescue ArgumentError
end
end
end
def parse(file_or_args)
if file_or_args.is_a? Array
parsea file_or_args
else
parsef file_or_args
end
end
def print_task(task_type, tasks)
unless tasks.empty?
puts "\n#{task_type}:\n-----\n"
tasks.each do |a_task|
puts a_task[:task]
if COLORS_ENABLED
puts " \e[30m\e[1mline #{a_task[:line_number]} in #{a_task[:file]}\e[0m"
else
puts " line #{a_task[:line_number]} in #{a_task[:file]}"
end
end
end
end
def print_tasks
ALL_TASKS.each do |task_type, tasks|
print_task(task_type, tasks)
end
end
def files_in_current_dir
files = Dir["./*"] | Dir["./.*"]
files.delete_if { |file| File.directory? file }
files
end
def collect_args
args = []
ARGV.each do |arg|
if File.directory?(arg)
if arg == "."
files = files_in_current_dir | Dir["#{arg}/**/*.*"]
else
files = Dir["#{arg}/**/*.*"]
end
files.each do |file|
args << file
end
else
args << arg
end
end
args
end
# --------------------------------------------------------
init_tasks
parse collect_args
print_tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment