Class: TodosExport::Main

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Main

Returns a new instance of Main.



14
15
16
17
# File 'lib/todos_export.rb', line 14

def initialize(target)
  self.target = target
  self.exportables = []
end

Instance Attribute Details

#exportablesObject

Returns the value of attribute exportables.



12
13
14
# File 'lib/todos_export.rb', line 12

def exportables
  @exportables
end

#filesObject

Returns the value of attribute files.



12
13
14
# File 'lib/todos_export.rb', line 12

def files
  @files
end

#targetObject

Returns the value of attribute target.



12
13
14
# File 'lib/todos_export.rb', line 12

def target
  @target
end

Instance Method Details

#delete_exportable(file, line) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/todos_export.rb', line 83

def delete_exportable(file, line)
  lines = File.readlines(file)
  lines.delete_at(line - 1)

  File.open(file, "w") do |f|
    lines.each { |l| f.puts(l) }
  end

  self.find_exportables
end

#delete_exportablesObject



94
95
96
97
98
99
# File 'lib/todos_export.rb', line 94

def delete_exportables
  while !self.exportables.empty? do
    self.delete_exportable(self.exportables[0][:file], self.exportables[0][:line])
    self.find_exportables
  end
end

#executeObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/todos_export.rb', line 101

def execute
  self.find_exportables

  say("Found #{self.exportable_todos.size} TODO's, " \
    "#{self.exportable_fixmes.size} FIXME's and " \
    "#{self.exportable_bugs.size} BUG's" \
    " in #{self.exportables.map { |x| x[:file] }.uniq.size } of #{exportables.map { |x| x[:file] }.size } files searched.")
  say("\n")

  choose do |menu|
    menu.prompt = "Export to: "
    menu.choice('Github Issues') { TodosExport::GithubIssues.new(self).run }
  end
end

#exportable_bugsObject



79
80
81
# File 'lib/todos_export.rb', line 79

def exportable_bugs
  self.exportables.select { |x| x[:type] == 'BUG' }
end

#exportable_fixmesObject



75
76
77
# File 'lib/todos_export.rb', line 75

def exportable_fixmes
  self.exportables.select { |x| x[:type] == 'FIXME' }
end

#exportable_todosObject



71
72
73
# File 'lib/todos_export.rb', line 71

def exportable_todos
  self.exportables.select { |x| x[:type] == 'TODO' }
end

#find_exportablesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/todos_export.rb', line 31

def find_exportables
  self.find_files
  @exportables = []

  self.files.each do |file|
    File.open(file) do |f|
      f.each_with_index do |line, number|
        search = line.scan(/((?:#)(?:| )(TODO|FIXME|BUG):?(.*)$)/i)
        if !search.empty?
          self.exportables << {
            :type => search[0][1].upcase,
            :content => search[0][2].strip,
            :original_content => search[0][0],
            :file => file.gsub(/^(.\/)/, ''),
            :original_file => file,
            :line => number + 1
          }
        end
      end
    end
  end
end

#find_filesObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/todos_export.rb', line 19

def find_files
  if File.file?(self.target)
    self.files = [self.target]
  elsif File.directory?(self.target)
    self.files = Dir.glob(File.join(self.target, "**", "*.rb"))
  else
    abort "#{target} does not exist."
  end

  return self.files
end

#git_directory?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/todos_export.rb', line 54

def git_directory?
  begin
    Rugged::Repository.new(self.target)
    return true
  rescue
    return false
  end
end

#git_head_shaObject



63
64
65
66
67
68
69
# File 'lib/todos_export.rb', line 63

def git_head_sha
  if git_directory?
    Rugged::Repository.new(self.target).head.target
  else
    nil
  end
end