PageRenderTime 52ms CodeModel.GetById 24ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/scalate-jruby/src/main/resources/haml-3.0.25/Rakefile

http://github.com/scalate/scalate
Rakefile | 436 lines | 392 code | 21 blank | 23 comment | 11 complexity | be88349f6b50117341790fae79d517c7 MD5 | raw file
  1# ----- Utility Functions -----
  2
  3def scope(path)
  4  File.join(File.dirname(__FILE__), path)
  5end
  6
  7# ----- Benchmarking -----
  8
  9desc <<END
 10Benchmark haml against ERb.
 11  TIMES=n sets the number of runs. Defaults to 1000.
 12END
 13task :benchmark do
 14  sh "ruby test/benchmark.rb #{ENV['TIMES']}"
 15end
 16
 17# ----- Default: Testing ------
 18
 19if ENV["RUN_CODE_RUN"] == "true"
 20  task :default => :"test:rails_compatibility"
 21else
 22  task :default => :test
 23end
 24
 25require 'rake/testtask'
 26
 27Rake::TestTask.new do |t|
 28  t.libs << 'lib'
 29  test_files = FileList[scope('test/**/*_test.rb')]
 30  test_files.exclude(scope('test/rails/*'))
 31  test_files.exclude(scope('test/plugins/*'))
 32  test_files.exclude(scope('test/haml/spec/*'))
 33  t.test_files = test_files
 34  t.verbose = true
 35end
 36Rake::Task[:test].send(:add_comment, <<END)
 37To run with an alternate version of Rails, make test/rails a symlink to that version.
 38END
 39
 40# ----- Packaging -----
 41
 42# Don't use Rake::GemPackageTast because we want prerequisites to run
 43# before we load the gemspec.
 44desc "Build all the packages."
 45task :package => [:revision_file, :submodules, :permissions] do
 46  load scope('haml.gemspec')
 47  Gem::Builder.new(HAML_GEMSPEC).build
 48  pkg = "#{HAML_GEMSPEC.name}-#{HAML_GEMSPEC.version}"
 49  mkdir_p "pkg"
 50  verbose(true) {mv "#{pkg}.gem", "pkg/#{pkg}.gem"}
 51
 52  sh %{rm -f pkg/#{pkg}.tar.gz}
 53  verbose(false) {HAML_GEMSPEC.files.each {|f| sh %{tar rf pkg/#{pkg}.tar #{f}}}}
 54  sh %{gzip pkg/#{pkg}.tar}
 55end
 56
 57task :permissions do
 58  sh %{chmod -R a+rx bin}
 59  sh %{chmod -R a+r .}
 60  require 'shellwords'
 61  Dir.glob('test/**/*_test.rb') do |file|
 62    next if file =~ %r{^test/haml/spec/}
 63    sh %{chmod a+rx #{file}}
 64  end
 65end
 66
 67task :revision_file do
 68  require 'lib/haml'
 69
 70  release = Rake.application.top_level_tasks.include?('release') || File.exist?(scope('EDGE_GEM_VERSION'))
 71  if Haml.version[:rev] && !release
 72    File.open(scope('REVISION'), 'w') { |f| f.puts Haml.version[:rev] }
 73  elsif release
 74    File.open(scope('REVISION'), 'w') { |f| f.puts "(release)" }
 75  else
 76    File.open(scope('REVISION'), 'w') { |f| f.puts "(unknown)" }
 77  end
 78end
 79
 80# We also need to get rid of this file after packaging.
 81at_exit { File.delete(scope('REVISION')) rescue nil }
 82
 83desc "Install Haml as a gem. Use SUDO=1 to install with sudo."
 84task :install => [:package] do
 85  gem  = RUBY_PLATFORM =~ /java/  ? 'jgem' : 'gem' 
 86  sh %{#{'sudo ' if ENV["SUDO"]}#{gem} install --no-ri pkg/haml-#{File.read(scope('VERSION')).strip}}
 87end
 88
 89desc "Release a new Haml package to Rubyforge."
 90task :release => [:check_release, :package] do
 91  name = File.read(scope("VERSION_NAME")).strip
 92  version = File.read(scope("VERSION")).strip
 93  sh %{rubyforge add_release haml haml "#{name} (v#{version})" pkg/haml-#{version}.gem}
 94  sh %{rubyforge add_file    haml haml "#{name} (v#{version})" pkg/haml-#{version}.tar.gz}
 95  sh %{gem push pkg/haml-#{version}.gem}
 96end
 97
 98# Ensures that the version have been updated for a new release.
 99task :check_release do
100  version = File.read(scope("VERSION")).strip
101  raise "There have been changes since current version (#{version})" if changed_since?(version)
102  raise "VERSION_NAME must not be 'Bleeding Edge'" if File.read(scope("VERSION_NAME")) == "Bleeding Edge"
103end
104
105# Reads a password from the command line.
106#
107# @param name [String] The prompt to use to read the password
108def read_password(prompt)
109  require 'readline'
110  system "stty -echo"
111  Readline.readline("#{prompt}: ").strip
112ensure
113  system "stty echo"
114  puts
115end
116
117# Returns whether or not the repository, or specific files,
118# has/have changed since a given revision.
119#
120# @param rev [String] The revision to check against
121# @param files [Array<String>] The files to check.
122#   If this is empty, checks the entire repository
123def changed_since?(rev, *files)
124  IO.popen("git diff --exit-code #{rev} #{files.join(' ')}") {}
125  return !$?.success?
126end
127
128# Returns whether or not the given Emacs mode file (haml or sass)
129# has changed since the given version.
130#
131# @param mode [String, Symbol] The name of the mode
132# @param version [String] The version number
133# @return [String, nil] The version number if the version has changed
134def mode_unchanged?(mode, version)
135  mode_version = File.read(scope("extra/#{mode}-mode.el")).scan(/^;; Version: (.*)$/).first.first
136  return false if mode_version == version
137  return mode_version unless changed_since?(mode_version, "extra/#{mode}-mode.el")
138  raise "#{mode}-mode.el version is #{version.inspect}, but it has changed as of #{version.inspect}"
139  return false
140end
141
142task :submodules do
143  if File.exist?(File.dirname(__FILE__) + "/.git")
144    sh %{git submodule sync}
145    sh %{git submodule update --init}
146  elsif !File.exist?(File.dirname(__FILE__) + "/vendor/fssm/lib")
147    warn <<WARN
148WARNING: vendor/fssm doesn't exist, and this isn't a git repository so
149I can't get it automatically!
150WARN
151  end
152end
153
154task :release_edge do
155  ensure_git_cleanup do
156    puts "#{'=' * 50} Running rake release_edge"
157
158    sh %{git checkout edge-gem}
159    sh %{git reset --hard origin/edge-gem}
160    sh %{git merge origin/master}
161
162    # Get the current master branch version
163    version = File.read(scope('VERSION')).strip.split('.')
164    pr = version[3]
165    version = version.map {|n| n.to_i}
166    unless pr || (version[1] % 2 == 1 && version[2] == 0)
167      raise "#{version.join('.')} is not a development version" 
168    end
169
170    # Bump the edge gem version
171    edge_version = File.read(scope('EDGE_GEM_VERSION')).strip.split('.').map {|n| n.to_i}
172    if !pr && (edge_version[0..1] != version[0..1])
173      # A new master branch version was released, reset the edge gem version
174      edge_version[0..1] = version[0..1]
175      edge_version[2] = 0
176    else
177      # Just bump the teeny version
178      edge_version[2] += 1
179    end
180    edge_version = edge_version.join('.')
181    File.open(scope('EDGE_GEM_VERSION'), 'w') {|f| f.puts(edge_version)}
182    sh %{git commit -m "Bump edge gem version to #{edge_version}." EDGE_GEM_VERSION}
183    sh %{git push origin edge-gem}
184
185    # Package the edge gem with the proper version
186    File.open(scope('VERSION'), 'w') {|f| f.puts(edge_version)}
187    sh %{rake package}
188    sh %{git checkout VERSION}
189
190    sh %{rubyforge add_release haml haml-edge "Bleeding Edge (v#{edge_version})" pkg/haml-edge-#{edge_version}.gem}
191    sh %{gem push pkg/haml-edge-#{edge_version}.gem}
192  end
193end
194
195task :watch_for_update do
196  sh %{ruby extra/update_watch.rb}
197end
198
199# ----- Documentation -----
200
201task :rdoc do
202  puts '=' * 100, <<END, '=' * 100
203Haml uses the YARD documentation system (http://github.com/lsegal/yard).
204Install the yard gem and then run "rake doc".
205END
206end
207
208begin
209  require 'yard'
210
211  namespace :doc do
212    task :sass do
213      require scope('lib/sass')
214      Dir[scope("yard/default/**/*.sass")].each do |sass|
215        File.open(sass.gsub(/sass$/, 'css'), 'w') do |f|
216          f.write(Sass::Engine.new(File.read(sass)).render)
217        end
218      end
219    end
220
221    desc "List all undocumented methods and classes."
222    task :undocumented do
223      opts = ENV["YARD_OPTS"] || ""
224      ENV["YARD_OPTS"] = opts.dup + <<OPTS
225 --list --query "
226  object.docstring.blank? &&
227  !(object.type == :method && object.is_alias?)"
228OPTS
229      Rake::Task['yard'].execute
230    end
231  end
232
233  YARD::Rake::YardocTask.new do |t|
234    t.files = FileList.new(scope('lib/**/*.rb')) do |list|
235      list.exclude('lib/haml/template/patch.rb')
236      list.exclude('lib/haml/template/plugin.rb')
237      list.exclude('lib/haml/railtie.rb')
238      list.exclude('lib/haml/helpers/action_view_mods.rb')
239      list.exclude('lib/haml/helpers/xss_mods.rb')
240      list.exclude('lib/sass/plugin/merb.rb')
241      list.exclude('lib/sass/plugin/rails.rb')
242      list.exclude('lib/sass/less.rb')
243    end.to_a
244    t.options << '--incremental' if Rake.application.top_level_tasks.include?('redoc')
245    t.options += FileList.new(scope('yard/*.rb')).to_a.map {|f| ['-e', f]}.flatten
246    files = FileList.new(scope('doc-src/*')).to_a.sort_by {|s| s.size} + %w[MIT-LICENSE VERSION]
247    t.options << '--files' << files.join(',')
248    t.options << '--template-path' << scope('yard')
249    t.options << '--title' << ENV["YARD_TITLE"] if ENV["YARD_TITLE"]
250
251    t.before = lambda do
252      if ENV["YARD_OPTS"]
253        require 'shellwords'
254        t.options.concat(Shellwords.shellwords(ENV["YARD_OPTS"]))
255      end
256    end
257  end
258  Rake::Task['yard'].prerequisites.insert(0, 'doc:sass')
259  Rake::Task['yard'].instance_variable_set('@comment', nil)
260
261  desc "Generate Documentation"
262  task :doc => :yard
263  task :redoc => :yard
264rescue LoadError
265  desc "Generate Documentation"
266  task :doc => :rdoc
267  task :yard => :rdoc
268end
269
270task :pages do
271  ensure_git_cleanup do
272    puts "#{'=' * 50} Running rake pages PROJ=#{ENV["PROJ"].inspect}"
273    raise 'No ENV["PROJ"]!' unless proj = ENV["PROJ"]
274    sh %{git checkout #{proj}-pages}
275    sh %{git reset --hard origin/#{proj}-pages}
276
277    Dir.chdir("/var/www/#{proj}-pages") do
278      sh %{git fetch origin}
279
280      sh %{git checkout stable}
281      sh %{git reset --hard origin/stable}
282
283      sh %{git checkout #{proj}-pages}
284      sh %{git reset --hard origin/#{proj}-pages}
285      sh %{rake build --trace}
286      sh %{mkdir -p tmp}
287      sh %{touch tmp/restart.txt}
288    end
289  end
290end
291
292# ----- Coverage -----
293
294begin
295  require 'rcov/rcovtask'
296
297  Rcov::RcovTask.new do |t|
298    t.test_files = FileList[scope('test/**/*_test.rb')]
299    t.rcov_opts << '-x' << '"^\/"'
300    if ENV['NON_NATIVE']
301      t.rcov_opts << "--no-rcovrt"
302    end
303    t.verbose = true
304  end
305rescue LoadError; end
306
307# ----- Profiling -----
308
309begin
310  require 'ruby-prof'
311
312  desc <<END
313Run a profile of haml.
314  ENGINE=str sets the engine to be profiled. Defaults to Haml.
315  TIMES=n sets the number of runs. Defaults to 1000.
316  FILE=str sets the file to profile.
317    Defaults to 'standard' for Haml and 'complex' for Sass.
318  OUTPUT=str sets the ruby-prof output format.
319    Can be Flat, CallInfo, or Graph. Defaults to Flat. Defaults to Flat.
320END
321  task :profile do
322    engine = (ENV['ENGINE'] || 'haml').downcase
323    times  = (ENV['TIMES'] || '1000').to_i
324    file   = ENV['FILE']
325
326    if engine == 'sass'
327      require 'lib/sass'
328
329      file = File.read(scope("test/sass/templates/#{file || 'complex'}.sass"))
330      result = RubyProf.profile { times.times { Sass::Engine.new(file).render } }
331    else
332      require 'lib/haml'
333
334      file = File.read(scope("test/haml/templates/#{file || 'standard'}.haml"))
335      obj = Object.new
336      Haml::Engine.new(file).def_method(obj, :render)
337      result = RubyProf.profile { times.times { obj.render } }
338    end
339
340    RubyProf.const_get("#{(ENV['OUTPUT'] || 'Flat').capitalize}Printer").new(result).print 
341  end
342rescue LoadError; end
343
344# ----- Testing Multiple Rails Versions -----
345
346rails_versions = [
347  "v2.3.5",
348  "v2.2.3",
349  "v2.1.2",
350]
351rails_versions << "v2.0.5" if RUBY_VERSION =~ /^1\.8/
352
353def test_rails_version(version)
354  Dir.chdir "test/rails" do
355    sh %{git checkout #{version}}
356  end
357  puts "Testing Rails #{version}"
358  Rake::Task['test'].reenable
359  Rake::Task['test'].execute
360end
361
362namespace :test do
363  desc "Test all supported versions of rails. This takes a while."
364  task :rails_compatibility do
365    sh %{rm -rf test/rails}
366    puts "Checking out rails. Please wait."
367    sh %{git clone git://github.com/rails/rails.git test/rails}
368    begin
369      rails_versions.each {|version| test_rails_version version}
370
371      puts "Checking out rails_xss. Please wait."
372      sh %{git clone git://github.com/NZKoz/rails_xss.git test/plugins/rails_xss}
373      test_rails_version(rails_versions.find {|s| s =~ /^v2\.3/})
374    ensure
375      `rm -rf test/rails`
376      `rm -rf test/plugins`
377    end
378  end
379end
380
381# ----- Handling Updates -----
382
383def email_on_error
384  yield
385rescue Exception => e
386  IO.popen("sendmail nex342@gmail.com", "w") do |sm|
387    sm << "From: nex3@nex-3.com\n" <<
388      "To: nex342@gmail.com\n" <<
389      "Subject: Exception when running rake #{Rake.application.top_level_tasks.join(', ')}\n" <<
390      e.message << "\n\n" <<
391      e.backtrace.join("\n")
392  end
393ensure
394  raise e if e
395end
396
397def ensure_git_cleanup
398  email_on_error {yield}
399ensure
400  sh %{git reset --hard HEAD}
401  sh %{git clean -xdf}
402  sh %{git checkout master}
403end
404
405task :handle_update do
406  email_on_error do
407    unless ENV["REF"] =~ %r{^refs/heads/(master|stable|(?:haml|sass)-pages)$}
408      puts "#{'=' * 20} Ignoring rake handle_update REF=#{ENV["REF"].inspect}"
409      next
410    end
411    branch = $1
412
413    puts
414    puts
415    puts '=' * 150
416    puts "Running rake handle_update REF=#{ENV["REF"].inspect}"
417
418    sh %{git fetch origin}
419    sh %{git checkout stable}
420    sh %{git reset --hard origin/stable}
421    sh %{git checkout master}
422    sh %{git reset --hard origin/master}
423
424    if branch == "master"
425      sh %{rake release_edge --trace}
426    elsif branch == "stable"
427      sh %{rake pages --trace PROJ=haml}
428      sh %{rake pages --trace PROJ=sass}
429    elsif branch =~ /^(haml|sass)-pages$/
430      sh %{rake pages --trace PROJ=#{$1}}
431    end
432
433    puts 'Done running handle_update'
434    puts '=' * 150
435  end
436end