require 'rake'
require 'rake/rdoctask'
require 'rake/gempackagetask'
require 'rubygems'
Gem.manage_gems

#
# ===========================  U N I T   T E S T S  ===========================
#

desc 'Run unit tests'
task :test do
  # Might wait until I've got some...
end

#
# ===========================  P A C K A G I N G  =============================
#

PKG_VERSION = File.read('VERSION').strip

spec = Gem::Specification.new do |s|
  s.name = 'dev-utils'
  s.version = PKG_VERSION
  s.summary = 'Debugging utilities: breakpoints, debugging, and tracing.'
  s.description = s.summary
  s.add_dependency('extensions', '>= 0.5')
  s.required_ruby_version = '>= 1.8.1'
  s.files = FileList['[A-Z]*', 'setup.rb', '{etc,lib,test}/**/*', 'examples/*.rb'].to_a
  s.require_path = 'lib'
  s.autorequire = nil
  s.has_rdoc = true
  s.extra_rdoc_files = FileList['[A-Z]*'].to_a
  s.rdoc_options << '--main' << 'README.txt' << '--title' << 'dev-utils API Documentation'
  s.test_files = []
  s.author = 'Gavin Sinclair'
  s.email = 'gsinclair@soyabean.com.au'
  s.homepage = 'http://dev-utils.rubyforge.org'
  s.rubyforge_project = 'dev-utils'
end

Rake::GemPackageTask.new(spec) do |pkg|
  pkg.package_dir = 'build/pkg'
  pkg.need_zip = false
  pkg.need_tar = true
end

desc "Install the gem"
task :install => :repackage do
  sh "gem install --local --no-rdoc build/pkg/dev-utils-#{PKG_VERSION}"
end

#
# ==============================  W E B S I T E ===============================
#

directory 'build/www'
directory 'build/www/api'

desc "Build the RDoc documentation"
Rake::RDocTask.new(:rdoc) do |rt|
  rt.rdoc_dir = 'build/www/api'
  rt.rdoc_files.include('[A-Z]*', 'lib/**/*.rb')
  rt.main = 'README.txt'
  rt.title = 'dev-utils API Documentation'
  rt.options << '--inline-source'
end

#
# Building the web pages (source: etc/doc/*.textile; target: build/www/*.html)
# 
# I should be able to name the targets, generated from the sources, and have a rule that
# generates HTML from Textile.  This rule should take account of timestamps, and keep in mind
# that a change in the CSS or generate.rb or links.dat requires regeneration throughout.
#

desc "Build the Textile documentation"
task :textile => ['build/www']

dependencies = FileList['etc/doc/{*.css,*.rb,*.dat}'].to_a
Dir['etc/doc/*.textile'].each do |source|
  target = source.sub('etc/doc', 'build/www').sub('.textile', '.html')
    # Set the file task, so Rake knows about it.
  file target => [source, *dependencies] do
    require 'etc/doc/generate'
    generate_document source, 'build/www'
  end
    # Collect the targets, so the 'textile' task can aggregate them all.
  task :textile => target
end

desc "Build the web page"
task :www => [:rerdoc, :textile] do
end

desc 'Build a text version of the home page'
task :textpage => :textile do
  text = `lynx -dump build/www/index.html`
    # There is some filtering we need to do.
    #   ***** This is very delicate and needs to be kept up to date! *****
  if true
      # Remove top of page until the text "About dev-utils"
    text.sub!(/\A.*?^\s*About dev-utils/m, "\nAbout dev-utils")
      # Remove unwanted references.
    unwanted_refs = '5678'
    text.gsub!(/\[[#{unwanted_refs}]\]/, '')
    unwanted_refs.split(//).each do |label|
      text.sub!(/^\s*#{label}\. \S+\s*$/, '')
    end
      # Turn file:// URLs into http:// URLs.
    text.gsub!(%r{file://\S+/www/}, 'http://dev-utils.rubyforge.org/')
  end
  puts text
end

#
# ===========================  P U B L I S H I N G  ===========================
#

desc 'Publish the web stuff to RubyForge'
task 'publish-textile' => :textile do
  Dir.chdir 'build/www' do
    system "scp #{Dir['*.html'].join(' ')} gsinclair@rubyforge.org:/var/www/gforge-projects/dev-utils"
  end
end

desc 'Publish the API documentation to RubyForge'
task 'publish-api' => :rerdoc do
  Dir.chdir 'build/www' do
    system "scp -r api gsinclair@rubyforge.org:/var/www/gforge-projects/dev-utils"
  end
end

# vim: ft=ruby

