require 'rubygems'
require "bundler/gem_tasks"
require 'bundler/setup'

require 'rake'
require 'terminal-table'

require 'ruby_app'

namespace :ruby_app do

  desc 'Create console'
  task :console do |task|
    system('cd ./lib/ruby_app && clear && bundle exec ../../bin/ruby_app console')
  end

  desc 'Run'
  task :run => ['ruby_app:cache:destroy'] do |task|
    system('cd ./lib/ruby_app && clear && bundle exec ../../bin/ruby_app run')
  end

  desc 'Get version'
  task :version do |task|
    puts RubyApp::VERSION
  end

  desc 'Push to master, release, and increment version'
  task :release do |task|
      system('git push origin master && rake release')
      version_file = File.join(RubyApp::ROOT, %w[version.rb])
      RubyApp::VERSION =~ /(\d+)\.(\d+)\.(\d+)/
      system("sed 's|[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*|#{$1}.#{$2}.#{$3.to_i + 1}|g' < '#{version_file}' > '#{version_file}.out'; rm '#{version_file}'; mv '#{version_file}.out' '#{version_file}'")
      system('git commit --all --message=\'Incrementing version\'')
  end

  desc 'List all scripts and their url\'s'
  task :scripts do |task|
    RubyApp::Application.create_context! do
      table = Terminal::Table.new(:title => 'Steps',
                                  :headings => ['Script',
                                                'Url']) do |table|
        RubyApp::Session.get_scripts.each do |script|
          table.add_row([script.name,
                         script.url])
        end
        puts table
      end
    end
  end

  namespace :cache do

    desc 'List all cached files'
    task :list do
      system('find . | grep \'\\.cache\'')
    end

    desc 'Remove all cached files'
    task :destroy do
      puts 'Removing cached files ...'
      system('find . -name \'.cache\' | xargs rm -rv')
    end

  end

end
