require "todos_export/version" require 'todos_export/github_issues' require "highline/import" require 'octokit' require 'rugged' require 'awesome_print' HighLine.track_eof = false module TodosExport class Main attr_accessor :target, :files, :exportables def initialize(target) self.target = target self.exportables = [] end 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 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 def git_directory? begin Rugged::Repository.new(self.target) return true rescue return false end end def git_head_sha if git_directory? Rugged::Repository.new(self.target).head.target else nil end end def exportable_todos self.exportables.select { |x| x[:type] == 'TODO' } end def exportable_fixmes self.exportables.select { |x| x[:type] == 'FIXME' } end def exportable_bugs self.exportables.select { |x| x[:type] == 'BUG' } end 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 def delete_exportables while !self.exportables.empty? do self.delete_exportable(self.exportables[0][:file], self.exportables[0][:line]) self.find_exportables end end 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 end end