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