# Rakefile for rake        -*- ruby -*-

# Copyright 2003 by Jim Weirich (jweirich@one.net)
# All rights reserved.

# This file is may be distributed under an MIT style license.  See
# MIT-LICENSE for details.

begin
  require 'rubygems'
rescue Exception
  nil
end
require 'rake/clean'
require 'rake/testtask'
require 'rake/gempackagetask'
require 'rake/rdoctask'

# Determine the current version of the software

if `ruby -Ilib ./bin/rake --version` =~ /\S+$/
  PKG_VERSION = $&
else
  PKG_VERSION = "0.0.0"
end

# The default task is run if rake is given no explicit arguments.

desc "Default Task"
task :default => :test

# Define a test task.

Rake::TestTask.new { |t|
  t.libs << "test"
  t.verbose = true
}

directory 'testdata'
task :test => ['testdata']

Rake::TestTask.new(:contrib_test) { |ctest|
  ctest.pattern = 'test/contrib/test*.rb'
  ctest.verbose = true
}

# Define a test that will run all the test targets.

desc "Run all test targets"
task :testall => [:test, :contrib_test]

# Install rake using the standard install.rb script.

desc "Install the application"
task :install do
  ruby "install.rb"
end

# Create a task to build the RDOC documentation tree.

rd = Rake::RDocTask.new("rdoc") { |rdoc|
  rdoc.rdoc_dir = 'html'
#  rdoc.template = 'kilmer'
  rdoc.template = 'css2'
  rdoc.title    = "Rake -- Ruby Make"
  rdoc.options << '--line-numbers'
  rdoc.rdoc_files.include('README', 'MIT-LICENSE', 'TODO', 'CHANGES')
  rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
  rdoc.rdoc_files.exclude(/\bcontrib\b/)
}

# ====================================================================
# Create a task that will package the Rake software into distributable
# tar, zip and gem files.

PKG_FILES = FileList[
  'install.rb',
  'gem_install_bin.rb',
  '[A-Z]*',
  'bin/**/*', 
  'lib/**/*.rb', 
  'test/**/*.rb',
  'doc/**/*'
]

if ! defined?(Gem)
  puts "Package Target requires RubyGEMs"
else
  spec = Gem::Specification.new do |s|
    s.platform = Gem::Platform::RUBY
    s.summary = "Ruby based make-like utility."
    s.name = 'rake'
    s.version = PKG_VERSION
    s.requirements << 'none'
    s.require_path = 'lib'
    s.autorequire = 'rake'
    s.files = PKG_FILES.to_a
    s.author = 'Jim Weirich'
    s.email = 'jweirich@one.net'
    s.homepage = 'http://onestepback.org'
    s.has_rdoc = true
    s.rubyforge_project = 'rake'
    s.description = <<EOF
Rake is a Make-like program implemented in Ruby. Tasks
and dependencies are specified in standard Ruby syntax. 
EOF
  end
  
  Rake::GemPackageTask.new(spec) do |pkg|
    pkg.need_zip = true
    pkg.need_tar = true
  end
end

# Misc tasks =========================================================

desc "Count lines in the main rake file"
task :lines do
  lines = 0
  codelines = 0
  open("lib/rake.rb") { |f|
    while line = f.gets
      lines += 1
      next if line =~ /^\s*$/
      next if line =~ /^\s*#/
      codelines += 1
    end
  }
  puts "Lines #{lines}, LOC #{codelines}"
end

ARCHIVEDIR = '/mnt/usb'

task :archive => [:package] do
  cp FileList["pkg/*.tgz", "pkg/*.zip", "pkg/*.gem"], ARCHIVEDIR
end

# Define an optional publish target in an external file.  If the
# publish.rf file is not found, the publish targets won't be defined.

load "publish.rf" if File.exist? "publish.rf"
