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