chunker/Rakefile
author mahlon
Thu, 04 Jun 2009 17:28:02 +0000
branchruby-modules
changeset 3 233041485364
parent 2 e5c705047540
child 4 01a3332bfe0a
permissions -rw-r--r--
Require spec implicitly.

#!/usr/bin/env rake
#
# Chunker Rakefile
#

require 'rubygems'
require 'pathname'

require 'rake'
require 'spec'
require 'rake/testtask'
require 'rake/packagetask'
require 'rake/gempackagetask'
require 'spec/rake/spectask'
require 'rubygems/installer'
require 'rubygems/uninstaller'


######################################################################
### P A T H S  A N D  F I L E S
######################################################################

BASEDIR = Pathname.new( __FILE__ ).expand_path.dirname.relative_path_from( Pathname.getwd )

TEXT_FILES = %w{ Rakefile README LICENSE }.collect {|f| BASEDIR + f }

SPECDIR    = BASEDIR + 'spec'
SPEC_FILES = Pathname.glob( SPECDIR + '**/*_spec.rb' ).reject {|f| f =~ /^\.svn/ }

LIBDIR    = BASEDIR + 'lib'
LIB_FILES = Pathname.glob( LIBDIR + '**/*.rb').reject {|i| i =~ /\.svn/ }

RELEASE_FILES = TEXT_FILES + LIB_FILES + SPEC_FILES


######################################################################
### H E L P E R S
######################################################################

### Given a +file+ path, find the first captured match of +pattern+,
### or the string 'UNKNOWN' if not found. (easy to notice something is wrong.)
###
def find_pattern( file, pattern )
	ver = nil
	File.open( file ) do |f|
		ver = f.each do |line|
			break $1 if line =~ pattern
		end
	end
	return ver.is_a?( String ) ? ver : 'UNKNOWN'
end


######################################################################
### P A C K A G E   C O N S T A N T S
######################################################################

PKG_NAME      = 'chunker'
PKG_VERSION   = find_pattern( LIBDIR + 'chunker.rb', /VERSION = ['"](\d\.\d(?:\/\d)?)['"]/ )
PKG_REVISION  = find_pattern( LIBDIR + 'chunker.rb', /SVNRev = .+Rev: (\d+)/ )
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}.#{PKG_REVISION}"


######################################################################
### T A S K S
######################################################################

task :default => [ :test, :package ]


### Task: run rspec tests
###
desc "Run tests"
Spec::Rake::SpecTask.new('test') do |task|
	task.spec_files = SPEC_FILES
	task.spec_opts  = %w{ -c -fs }
end


### Task: generate ctags
### This assumes exuberant ctags, since ctags 'native' doesn't support ruby anyway.
###
desc "Generate a ctags 'tags' file from Chunker source"
task :ctags do
	sh "ctags -R #{LIBDIR}"
end


### Task: Create gem from source
###
gem = Gem::Specification.new do |gem|
	pkg_build = PKG_REVISION || 0

	gem.summary           = "A convenience library for parsing __END__ tokens consistently."
	gem.name              = PKG_NAME
	gem.version           = "%s.%s" % [ PKG_VERSION, pkg_build ]
	gem.author            = 'Mahlon E. Smith'
	gem.email             = 'mahlon@martini.nu'
	gem.homepage          = 'http://projects.martini.nu/ruby-modules/wiki/Chunker'
	gem.rubyforge_project = 'mahlon'
	gem.has_rdoc          = true

	gem.files = RELEASE_FILES.
		collect {|f| f.relative_path_from(BASEDIR).to_s }
	gem.test_files	= SPEC_FILES.
		collect {|f| f.relative_path_from(BASEDIR).to_s }

	gem.description = <<-EOF
	Ruby provides an automatic constant called DATA, which is an IO object
	that references all text in the current file under an __END__ token.

	I find it convenient to use the __END__ area to store all sorts of
	stuff, rather than have to worry about distributing separate files.

	The DATA constant is determined from whatever ruby believes $0 to be.
	It doesn't work inside of other required libraries, so you'll see stuff
	like this all the time:

	END = File.open( __FILE__ ).read.split( /^__END__/, 2 ).last

	It works, but it's more work than I want to do.

	Chunker solves this by parsing __END__ tokens for you, and making it
	available in the form of a 'DATA_END' constant.  It installs this
	constant into the class that includes Chunker, so you can use it again
	and again, assuming you use a different file for each class.

	It also automatically parses out other things that look like tokens, so
	you can easily have multiple, distinct documents all embedded into the
	__END__ block.
	EOF
end

Rake::GemPackageTask.new( gem ) do |pkg|
	pkg.need_zip     = true
	pkg.need_tar     = true
	pkg.need_tar_bz2 = true
end


### Task: install
###
task :install_gem => [ :package ] do
	$stderr.puts
	installer = Gem::Installer.new( "pkg/#{PKG_FILE_NAME}.gem" )
	installer.install
end
task :install => [ :install_gem ]


### Task: uninstall
###
task :uninstall_gem do
	uninstaller = Gem::Uninstaller.new( PKG_NAME )
	uninstaller.uninstall
end
task :uninstall => [ :uninstall_gem ]