Rakefile 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env ruby
  2. # -*- ruby -*-
  3. require 'rake/clean'
  4. SRC_DIR = 'src'
  5. PROB_DIR = 'koans'
  6. DOWNLOAD_DIR = 'download'
  7. SRC_FILES = FileList["#{SRC_DIR}/*"]
  8. KOAN_FILES = SRC_FILES.pathmap("#{PROB_DIR}/%f")
  9. ZIP_FILE = "#{DOWNLOAD_DIR}/rubykoans.zip"
  10. CLEAN.include("**/*.rbc")
  11. module Koans
  12. extend Rake::DSL if defined?(Rake::DSL)
  13. # Remove solution info from source
  14. # __(a,b) => __
  15. # _n_(number) => __
  16. # # __ =>
  17. def Koans.remove_solution(line)
  18. line = line.gsub(/\b____\([^\)]+\)/, "____")
  19. line = line.gsub(/\b___\([^\)]+\)/, "___")
  20. line = line.gsub(/\b__\([^\)]+\)/, "__")
  21. line = line.gsub(/\b_n_\([^\)]+\)/, "_n_")
  22. line = line.gsub(%r(/\#\{__\}/), "/__/")
  23. line = line.gsub(/\s*#\s*__\s*$/, '')
  24. line
  25. end
  26. def Koans.make_koan_file(infile, outfile)
  27. if infile =~ /neo/
  28. cp infile, outfile
  29. else
  30. open(infile) do |ins|
  31. open(outfile, "w") do |outs|
  32. state = :copy
  33. ins.each do |line|
  34. state = :skip if line =~ /^ *#--/
  35. case state
  36. when :copy
  37. outs.puts remove_solution(line)
  38. else
  39. # do nothing
  40. end
  41. state = :copy if line =~ /^ *#\+\+/
  42. end
  43. end
  44. end
  45. end
  46. end
  47. end
  48. module RubyImpls
  49. # Calculate the list of relevant Ruby implementations.
  50. def self.find_ruby_impls
  51. rubys = `rvm list`.gsub(/=>/,'').split(/\n/).map { |x| x.strip }.reject { |x| x.empty? || x =~ /^rvm/ }.sort
  52. expected.map { |impl|
  53. last = rubys.grep(Regexp.new(Regexp.quote(impl))).last
  54. last ? last.split.first : nil
  55. }.compact
  56. end
  57. # Return a (cached) list of relevant Ruby implementations.
  58. def self.list
  59. @list ||= find_ruby_impls
  60. end
  61. # List of expected ruby implementations.
  62. def self.expected
  63. %w(ruby-1.8.7 ruby-1.9.2 jruby ree)
  64. end
  65. end
  66. task :default => :walk_the_path
  67. task :walk_the_path do
  68. cd PROB_DIR
  69. ruby 'path_to_enlightenment.rb'
  70. end
  71. directory DOWNLOAD_DIR
  72. directory PROB_DIR
  73. desc "(re)Build zip file"
  74. task :zip => [:clobber_zip, :package]
  75. task :clobber_zip do
  76. rm ZIP_FILE
  77. end
  78. file ZIP_FILE => KOAN_FILES + [DOWNLOAD_DIR] do
  79. sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
  80. end
  81. desc "Create packaged files for distribution"
  82. task :package => [ZIP_FILE]
  83. desc "Upload the package files to the web server"
  84. task :upload => [ZIP_FILE] do
  85. sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
  86. end
  87. desc "Generate the Koans from the source files from scratch."
  88. task :regen => [:clobber_koans, :gen]
  89. desc "Generate the Koans from the changed source files."
  90. task :gen => KOAN_FILES + [PROB_DIR + "/README.rdoc"]
  91. task :clobber_koans do
  92. rm_r PROB_DIR
  93. end
  94. file PROB_DIR + "/README.rdoc" => "README.rdoc" do |t|
  95. cp "README.rdoc", t.name
  96. end
  97. SRC_FILES.each do |koan_src|
  98. file koan_src.pathmap("#{PROB_DIR}/%f") => [PROB_DIR, koan_src] do |t|
  99. Koans.make_koan_file koan_src, t.name
  100. end
  101. end
  102. task :run do
  103. puts 'koans'
  104. Dir.chdir("#{SRC_DIR}") do
  105. puts "in #{Dir.pwd}"
  106. sh "ruby path_to_enlightenment.rb"
  107. end
  108. end
  109. desc "Pre-checkin tests (=> run_all)"
  110. task :cruise => :run_all
  111. desc "Run the completed koans againts a list of relevant Ruby Implementations"
  112. task :run_all do
  113. results = []
  114. RubyImpls.list.each do |impl|
  115. puts "=" * 40
  116. puts "On Ruby #{impl}"
  117. sh ". rvm #{impl}; rake run"
  118. results << [impl, "RAN"]
  119. puts
  120. end
  121. puts "=" * 40
  122. puts "Summary:"
  123. puts
  124. results.each do |impl, res|
  125. puts "#{impl} => RAN"
  126. end
  127. puts
  128. RubyImpls.expected.each do |requested_impl|
  129. impl_pattern = Regexp.new(Regexp.quote(requested_impl))
  130. puts "No Results for #{requested_impl}" unless results.detect { |x| x.first =~ impl_pattern }
  131. end
  132. end