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