yaworsw/euler-manager

View on GitHub
bin/euler

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'rubygems'

require 'commander/import'
require 'colorize'
require 'peach'

require_relative '../lib/euler'

program :version, Euler::VERSION
program :description, 'Project Euler solution manager.'

command :run do |c|
  c.syntax  = 'euler run [problem_id] [language]'
  c.summary = 'Runs a solution.'
  c.description = c.summary
  c.example 'Runs the ruby solution for problem #1', 'euler run 1 ruby'
  c.action do |args, options|
    problem_id, language = Euler.params_from_dir_or_args args
    solution = Euler::Solution.new(problem_id, language)

    puts
    puts solution.run
    puts
  end
end

command :test do |c|
  c.syntax  = 'euler test [problem_id] [language]'
  c.summary = 'Tests a solution.'
  c.description = c.summary
  c.example 'Tests the ruby solution for problem #1', 'euler test 1 ruby'
  c.action do |args, options|
    problem_id, language = Euler.params_from_dir_or_args args

    solution = Euler::Solution.new(problem_id, language)
    problem  = solution.problem

    correct  = solution.correct?

    puts
    puts "Testing #{solution.language} solution to"
    puts "##{problem.id} - #{problem.name}".bold
    puts
    puts "Result:   " + solution.result.cyan
    puts

    if solution.correct?
      puts "                         PASS                         ".bold.on_green
    else
      puts "                         FAIL                         ".bold.on_red
    end

    puts
  end
end

command :test_all do |c|
  c.syntax  = 'euler test_all'
  c.summary = 'Tests all solutions.'
  c.description = c.summary
  c.action do |args, options|
    solutions = Euler::Solution.all

    failures = []

    puts
    puts "Testing all solutions."
    puts
    Euler::Solution.all.peach do |solution|
      if solution.correct?
        print ".".green
      else
        failures << solution
        print "X".bold.red
      end
    end
    puts
    puts
    if failures.empty?
      print "                   ".bold.on_green
      print "All TESTS PASSED".bold.on_green
      puts  "                   ".bold.on_green
    else
      if failures.length == 1
        print "                  ".bold.on_red
        print "THERE WAS A FAILURE".bold.on_red
        print "                  ".bold.on_red
      else
        print "                 ".bold.on_red
        print "THERE WERE #{failures.length} FAILURES".bold.on_red
        print "                 ".bold.on_red
      end
      failures.each do |failure|
        puts
        puts "#{failure.problem.to_s} (#{failure.language})".bold
        puts "Result:   " + failure.result.cyan
      end
    end
    puts

    # fail if not failures.empty?
  end
end

command :new do |c|
  c.syntax  = 'euler new [problem_id] [language]'
  c.summary = 'Initialize a solution.'
  c.description = c.summary
  c.example 'Initialize a ruby solution for problem #1', 'euler new 1 ruby'
  c.action do |args, options|
    problem_id = args.shift
    language   = args.shift

    begin
      solution = Euler::Solution.new(problem_id, language)
      problem  = solution.problem
    rescue

    end

    solution.init

    puts
    puts "Done initializing empty #{solution.language} solution for"
    puts "##{problem.id} - #{problem.name}".bold
    puts
  end
end

command :desc do |c|
  c.syntax  = 'euler desc [problem_id]'
  c.summary = 'View the description for a problem.'
  c.description = c.summary
  c.example 'View the for description for problem #1', 'euler desc 1'
  c.action do |args, options|
    begin
      problem_id, language = Euler.params_from_dir_or_args args
    rescue
      problem_id = args[0]
    end

    problem = Euler::Problem.find(problem_id)

    puts
    puts "##{problem.id} - #{problem.name}".bold
    puts
    puts problem.content
    puts
  end
end

command :init do |c|
  c.syntax  = 'euler init'
  c.summary = 'Initialize an Eulerfile.rb in the current directory'
  c.description = c.summary
  c.action do
    tmpl = "#{File.dirname(__FILE__)}/../templates/Eulerfile.rb"
    file = "#{Dir.pwd}/Eulerfile.rb"
    FileUtils.cp(tmpl, file) unless File.exists?(file)
  end
end

command :include_images do |c|
  c.syntax  = 'euler include_images'
  c.summary = 'Copy Project Euler\'s image files.'
  c.description = c.summary
  c.action do
    images = "#{File.dirname(__FILE__)}/../data/images"
    dest   = "#{Euler.root}/.data/images"
    FileUtils.mkdir_p(dest)
    FileUtils.cp_r(images, dest)
  end
end