require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/packagetask'
require 'rake/gempackagetask'
require 'rake/contrib/rubyforgepublisher'

require File.join(File.dirname(__FILE__), 'lib/feed_tools', 'version')

PKG_NAME      = 'feedtools'
PKG_VERSION   = FeedTools::FEED_TOOLS_VERSION::STRING
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"

RELEASE_NAME  = "REL #{PKG_VERSION}"

RUBY_FORGE_PROJECT = PKG_NAME
RUBY_FORGE_USER    = "sporkmonger"
RUBY_FORGE_PATH    = "/var/www/gforge-projects/#{RUBY_FORGE_PROJECT}"

PKG_FILES = FileList[
    "lib/**/*", "test/**/*", "examples/**/*", "doc/**/*", "db/**/*",
    "[A-Z]*", "install.rb", "rakefile"
].exclude(/\bCVS\b|~$/).exclude(/database\.yml/).exclude(/\._.*/)

desc "Default Task"
task :default => [ :test_all ]

# Run the unit tests

Rake::TestTask.new("test_all") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/*_test.rb']
  t.verbose = true
}
Rake::TestTask.new("amp_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/amp_test.rb']
  t.verbose = true
}
Rake::TestTask.new("atom_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/atom_test.rb']
  t.verbose = true
}
Rake::TestTask.new("cache_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/cache_test.rb']
  t.verbose = true
}
Rake::TestTask.new("cdf_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/cdf_test.rb']
  t.verbose = true
}
Rake::TestTask.new("encoding_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/encoding_test.rb']
  t.verbose = true
}
Rake::TestTask.new("generation_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/generation_test.rb']
  t.verbose = true
}
Rake::TestTask.new("helper_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/helper_test.rb']
  t.verbose = true
}
Rake::TestTask.new("interface_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/interface_test.rb']
  t.verbose = true
}
Rake::TestTask.new("nonstandard_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/nonstandard_test.rb']
  t.verbose = true
}
Rake::TestTask.new("rdf_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/rdf_test.rb']
  t.verbose = true
}
Rake::TestTask.new("rss_test") { |t|
  t.libs << "test"
  t.test_files = FileList['test/unit/rss_test.rb']
  t.verbose = true
}

# Generate the RDoc documentation

Rake::RDocTask.new { |rdoc|
  rdoc.rdoc_dir = 'doc'
  rdoc.title    = "Feed Tools -- caching system for xml news feeds"
  rdoc.options << '--line-numbers' << '--inline-source' <<
    '--accessor' << 'cattr_accessor=object'
  rdoc.template = "#{ENV['template']}.rb" if ENV['template']
  rdoc.rdoc_files.include('README', 'CHANGELOG')
  rdoc.rdoc_files.include('lib/**/*.rb')
  rdoc.rdoc_files.include('db/**/*.sql')
}

# Create compressed packages

dist_dirs = [ "lib", "test", "db" ]

spec = Gem::Specification.new do |s|
  s.name = PKG_NAME
  s.version = PKG_VERSION
  s.summary = "Parsing, generation, and caching system for xml news feeds."
  s.description = "Implements a simple system for handling xml news feeds with caching."

  s.files = [ "rakefile", "install.rb", "README", "CHANGELOG" ]
  dist_dirs.each do |dir|
    s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if do |item|
      item.include?( "\.svn" ) || item.include?( "database\.yml" )
    end
  end
  
  s.add_dependency('activerecord', '>= 1.10.1')
  s.add_dependency('uuidtools', '>= 1.0.0')
  s.add_dependency('builder', '>= 1.2.4')

  s.require_path = 'lib'
  
  begin
  s.post_install_message = <<-TEXT

    FeedTool's caching schema has changed to allow Feed objects to be
    serialized to the cache.  This should offer some limited speed up
    in some cases.

  TEXT
  rescue Exception
  end

  s.has_rdoc = true
  s.extra_rdoc_files = %w( README )
  s.rdoc_options.concat ['--main',  'README']
  
  s.author = "Bob Aman"
  s.email = "bob@sporkmonger.com"
  s.homepage = "http://sporkmonger.com/projects/feedtools"
  s.rubyforge_project = "feedtools"
end
  
Rake::GemPackageTask.new(spec) do |p|
  p.gem_spec = spec
  p.need_tar = true
  p.need_zip = true
end

task :profile do
  $:.unshift(File.dirname(__FILE__) + '/lib')
  require 'feed_tools'
  puts "Profiling FeedTools #{FeedTools::FEED_TOOLS_VERSION::STRING}..."
  command = 'ruby -rprofile -e \'' +
    '$:.unshift(File.dirname(__FILE__) + "/lib");' +
    'require "feed_tools";' +
    'FeedTools::Feed.open(' +
      '"http://hsivonen.iki.fi/test/unknown-namespace.atom")' +
        '.build_xml("atom", 1.0)\''
  `#{command}`
end

task :lines do
  lines, codelines, total_lines, total_codelines = 0, 0, 0, 0

  for file_name in FileList["lib/**/*.rb"]
    f = File.open(file_name)

    while line = f.gets
      lines += 1
      next if line =~ /^\s*$/
      next if line =~ /^\s*#/
      codelines += 1
    end
    puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d", codelines)} | #{file_name}"
    
    total_lines     += lines
    total_codelines += codelines
    
    lines, codelines = 0, 0
  end

  puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
end


# Publishing ------------------------------------------------------

namespace :publish do
  desc "Publish the API documentation"
  task :api => [ "rdoc" ] do 
    Rake::SshDirPublisher.new(
      "#{RUBY_FORGE_USER}@rubyforge.org",
      "#{RUBY_FORGE_PATH}/",
      "doc"
    ).upload
  end

  desc "Runs all of the publishing tasks"
  task :all => ["publish:api"] do
  end
end
