Disable colorization when the NO_COLOR env variable is defined.
[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     else
33       open(infile) do |ins|
34         open(outfile, "w") do |outs|
35           state = :copy
36           ins.each do |line|
37             state = :skip if line =~ /^ *#--/
38             case state
39             when :copy
40               outs.puts remove_solution(line)
41             else
42               # do nothing
43             end
44             state = :copy if line =~ /^ *#\+\+/
45           end
46         end
47       end
48     end
49   end
50 end
51
52 task :default => :walk_the_path
53
54 task :walk_the_path do
55   cd 'koans'
56   ruby 'path_to_enlightenment.rb'
57 end
58
59 Rake::RDocTask.new do |rd|
60   rd.main = "README.rdoc"
61   rd.rdoc_files.include("README.rdoc", "koans/*.rb")
62 end
63
64 directory DIST_DIR
65 directory PROB_DIR
66
67 file ZIP_FILE => KOAN_FILES + [DIST_DIR] do
68   sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
69 end
70
71 file TAR_FILE => KOAN_FILES + [DIST_DIR] do
72   sh "tar zcvf #{TAR_FILE} #{PROB_DIR}"
73 end
74
75 desc "Create packaged files for distribution"
76 task :package => [TAR_FILE, ZIP_FILE]
77
78 desc "Upload the package files to the web server"
79 task :upload => [TAR_FILE, ZIP_FILE] do
80   sh "scp #{TAR_FILE} linode:sites/onestepback.org/download"
81   sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
82 end
83
84 desc "Check that the require files match the about_* files"
85 task :check do
86   about_files = Dir['src/about_*.rb'].size
87   about_requires = `grep require src/path_to_enlightenment.rb | wc -l`.to_i
88   puts "# of about files:    #{about_files}"
89   puts "# of about requires: #{about_requires}"
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