Added bonus question about the file read.
[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 TAR_FILE = "#{DIST_DIR}/rubykoans.tgz"
15 ZIP_FILE = "#{DIST_DIR}/rubykoans.zip"
16
17 CLOBBER.include(DIST_DIR)
18
19 module Koans
20   def Koans.remove_solution(line)
21     line = line.gsub(/\b____\([^\)]+\)/, "____")
22     line = line.gsub(/\b___\([^\)]+\)/, "___")
23     line = line.gsub(/\b__\([^\)]+\)/, "__")
24     line = line.gsub(/\b_n_\([^\)]+\)/, "_n_")
25     line = line.gsub(%r(/\#\{__\}/), "/__/")
26     line
27   end
28
29   def Koans.make_koan_file(infile, outfile)
30     if infile =~ /edgecase/
31       cp infile, outfile
32     elsif infile =~ /autotest/
33       cp_r infile, outfile
34     else
35       open(infile) do |ins|
36         open(outfile, "w") do |outs|
37           state = :copy
38           ins.each do |line|
39             state = :skip if line =~ /^ *#--/
40             case state
41             when :copy
42               outs.puts remove_solution(line)
43             else
44               # do nothing
45             end
46             state = :copy if line =~ /^ *#\+\+/
47           end
48         end
49       end
50     end
51   end
52 end
53
54 task :default => :walk_the_path
55
56 task :walk_the_path do
57   cd 'koans'
58   ruby 'path_to_enlightenment.rb'
59 end
60
61 Rake::RDocTask.new do |rd|
62   rd.main = "README.rdoc"
63   rd.rdoc_files.include("README.rdoc", "koans/*.rb")
64 end
65
66 directory DIST_DIR
67 directory PROB_DIR
68
69 file ZIP_FILE => KOAN_FILES + [DIST_DIR] do
70   sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
71 end
72
73 file TAR_FILE => KOAN_FILES + [DIST_DIR] do
74   sh "tar zcvf #{TAR_FILE} #{PROB_DIR}"
75 end
76
77 desc "Create packaged files for distribution"
78 task :package => [TAR_FILE, ZIP_FILE]
79
80 desc "Upload the package files to the web server"
81 task :upload => [TAR_FILE, ZIP_FILE] do
82   sh "scp #{TAR_FILE} linode:sites/onestepback.org/download"
83   sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
84 end
85
86 desc "Check that the require files match the about_* files"
87 task :check do
88   about_files = Dir['src/about_*.rb'].size
89   about_requires = `grep require src/path_to_enlightenment.rb | wc -l`.to_i
90   puts "# of about files:    #{about_files}"
91   puts "# of about requires: #{about_requires}"
92 end
93
94 desc "Generate the Koans from the source files from scratch."
95 task :regen => [:clobber_koans, :gen]
96
97 desc "Generate the Koans from the changed source files."
98 task :gen => KOAN_FILES + [PROB_DIR + "/README.rdoc"]
99 task :clobber_koans do
100   rm_r PROB_DIR
101 end
102
103 file PROB_DIR + "/README.rdoc" => "README.rdoc" do |t|
104   cp "README.rdoc", t.name
105 end
106
107 SRC_FILES.each do |koan_src|
108   file koan_src.pathmap("#{PROB_DIR}/%f") => [PROB_DIR, koan_src] do |t|
109     Koans.make_koan_file koan_src, t.name
110   end
111 end