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