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.



17
18
19
20
# File 'lib/todos_export.rb', line 17

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

Instance Attribute Details

#exportablesObject

Returns the value of attribute exportables.



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

def exportables
  @exportables
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#delete_exportable(file, line) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/todos_export.rb', line 89

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



100
101
102
103
104
105
# File 'lib/todos_export.rb', line 100

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/todos_export.rb', line 107

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('STDOUT') { TodosExport::StdOut.new(self).run }
    menu.choice('Github Issues') { TodosExport::GithubIssues.new(self).run }
  end
end

#exportable_bugsObject



85
86
87
# File 'lib/todos_export.rb', line 85

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

#exportable_fixmesObject



81
82
83
# File 'lib/todos_export.rb', line 81

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

#exportable_todosObject



77
78
79
# File 'lib/todos_export.rb', line 77

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

#find_exportablesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/todos_export.rb', line 34

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?
          line = number + 1

          self.exportables << {
            :type => search[0][1].upcase,
            :content => search[0][2].strip,
            :original_content => search[0][0],
            :file => file.gsub(/^(.\/)/, ''),
            :original_file => file,
            :line => line,
            :line_peek => File.readlines(file)[line].strip
          }
        end
      end
    end
  end
end

#find_filesObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/todos_export.rb', line 22

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)


60
61
62
63
64
65
66
67
# File 'lib/todos_export.rb', line 60

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

#git_head_shaObject



69
70
71
72
73
74
75
# File 'lib/todos_export.rb', line 69

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