Class: CTodo::LocalFS

Inherits:
Object
  • Object
show all
Defined in:
lib/ctodo/localfs.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ LocalFS

Returns a new instance of LocalFS.



11
12
13
14
15
16
17
# File 'lib/ctodo/localfs.rb', line 11

def initialize(conf)
	@enabled = true
	if @enabled
		@parent_dir = [:git_repo_dir, :hg_repo_dir, :cur_dir].map {|k| conf[k]}.select {|v| not v.nil?}.first
		@todo_labels = (conf[:all] ? ALL_LABELS : IMP_LABELS).join('|')
	end
end

Instance Method Details

#get_issues(issues) ⇒ Object



19
20
21
22
# File 'lib/ctodo/localfs.rb', line 19

def get_issues(issues)
	return if not @enabled
	traverse(@parent_dir, issues)  
end

#grep_for_todo(path, issues) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ctodo/localfs.rb', line 43

def grep_for_todo(path, issues)
	GREP_EXT_EXCLUDE.each do |e|
		return if path.end_with?(e)
	end
	cf = CommentFilter.new(path)
	spath = remove_common_path(path)
	File.open(path, 'r') do |f|
		linenr = 1
		f.readlines.map {|line| cf.filter(line)}.each do |line|
			m = line.match("[\\W](#{@todo_labels})\\(([\\w]+)\\):[\\W]+(.*)$")
			if not m.nil?
				loc = "#{spath}:#{linenr}"
				tags = [Tag.new(m[1], tag_color(m[1])), Tag.new(m[2], ColorUtils.rgb4string(m[2]))]
				issues << Issue.new(m[3], loc, nil, tags)
				next
			end
			m = line.match("[\\W](#{@todo_labels}):[\\W]+(.*)$")
			if not m.nil?
				loc = "#{spath}:#{linenr}"
				issues << Issue.new(m[2], loc, nil, Tag.new(m[1], tag_color(m[1])))
				next
			end
			m = line.match("[\\W](#{@todo_labels})[\\W]+(.*)$")
			if not m.nil?
				loc = "#{spath}:#{linenr}"
				issues << Issue.new(m[2], loc, nil, Tag.new(m[1], tag_color(m[1])))
			end
			linenr += 1
		end
	end
end

#remove_common_path(file) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ctodo/localfs.rb', line 75

def remove_common_path(file)
	begin
		return Pathname.new(file).relative_path_from(Pathname.new(Dir.getwd))
	rescue ArgumentError => e
		return file
	end
end

#tag_color(title) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/ctodo/localfs.rb', line 83

def tag_color(title)
	case title
	when 'BUG'
		color = "ff0000"
	else
		color = "ffffff"
	end
end

#traverse(dir, issues) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ctodo/localfs.rb', line 24

def traverse(dir, issues)
	Dir.entries(dir).each do |e|
		next if TRAVERSE_EXCLUDE.include?(e)
		path = File.join(dir, e)
		# grep symlinked files but don't follow symlinked
		# directories
		if File.directory?(path) and not File.symlink?(path)
			traverse(path, issues)
		elsif File.file?(path)
			begin
				grep_for_todo(path, issues)
			rescue => e
				# ignore errors silently
				#puts "#{path}: #{e}"
			end
		end
	end
end