diff options
author | jim <jim@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-21 03:22:43 +0000 |
---|---|---|
committer | jim <jim@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-21 03:22:43 +0000 |
commit | 79f9b6c2cbfbd1231815e7323cedfd769f99c64b (patch) | |
tree | 21888e10d241e2d60f3904fe18ed030489663b2f /lib/rake | |
parent | 9fbc6596ccf089a26ec7c38e2837b667790edbf6 (diff) | |
download | ruby-79f9b6c2cbfbd1231815e7323cedfd769f99c64b.tar.gz |
Added Rake 0.8.0
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14385 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rake')
-rw-r--r-- | lib/rake/classic_namespace.rb | 8 | ||||
-rw-r--r-- | lib/rake/clean.rb | 33 | ||||
-rw-r--r-- | lib/rake/gempackagetask.rb | 103 | ||||
-rw-r--r-- | lib/rake/loaders/makefile.rb | 40 | ||||
-rw-r--r-- | lib/rake/packagetask.rb | 184 | ||||
-rw-r--r-- | lib/rake/rake_test_loader.rb | 5 | ||||
-rw-r--r-- | lib/rake/rdoctask.rb | 147 | ||||
-rwxr-xr-x | lib/rake/ruby182_test_unit_fix.rb | 23 | ||||
-rw-r--r-- | lib/rake/runtest.rb | 23 | ||||
-rw-r--r-- | lib/rake/tasklib.rb | 18 | ||||
-rw-r--r-- | lib/rake/testtask.rb | 161 |
11 files changed, 745 insertions, 0 deletions
diff --git a/lib/rake/classic_namespace.rb b/lib/rake/classic_namespace.rb new file mode 100644 index 0000000000..feb7569966 --- /dev/null +++ b/lib/rake/classic_namespace.rb @@ -0,0 +1,8 @@ +# The following classes used to be in the top level namespace. +# Loading this file enables compatibility with older Rakefile that +# referenced Task from the top level. + +Task = Rake::Task +FileTask = Rake::FileTask +FileCreationTask = Rake::FileCreationTask +RakeApp = Rake::Application diff --git a/lib/rake/clean.rb b/lib/rake/clean.rb new file mode 100644 index 0000000000..4ee2c5ac95 --- /dev/null +++ b/lib/rake/clean.rb @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby + +# The 'rake/clean' file defines two file lists (CLEAN and CLOBBER) and +# two rake tasks (:clean and :clobber). +# +# [:clean] Clean up the project by deleting scratch files and backup +# files. Add files to the CLEAN file list to have the :clean +# target handle them. +# +# [:clobber] Clobber all generated and non-source files in a project. +# The task depends on :clean, so all the clean files will +# be deleted as well as files in the CLOBBER file list. +# The intent of this task is to return a project to its +# pristine, just unpacked state. + +require 'rake' + +CLEAN = Rake::FileList["**/*~", "**/*.bak", "**/core"] +CLEAN.clear_exclude.exclude { |fn| + fn.pathmap("%f") == 'core' && File.directory?(fn) +} + +desc "Remove any temporary products." +task :clean do + CLEAN.each { |fn| rm_r fn rescue nil } +end + +CLOBBER = Rake::FileList.new + +desc "Remove any generated file." +task :clobber => [:clean] do + CLOBBER.each { |fn| rm_r fn rescue nil } +end diff --git a/lib/rake/gempackagetask.rb b/lib/rake/gempackagetask.rb new file mode 100644 index 0000000000..a4e5cd1907 --- /dev/null +++ b/lib/rake/gempackagetask.rb @@ -0,0 +1,103 @@ +#!/usr/bin/env ruby + +# Define a package task library to aid in the definition of GEM +# packages. + +require 'rubygems' +require 'rake' +require 'rake/packagetask' +require 'rubygems/user_interaction' +require 'rubygems/builder' + +begin + Gem.manage_gems +rescue NoMethodError => ex + # Using rubygems prior to 0.6.1 +end + +module Rake + + # Create a package based upon a Gem spec. Gem packages, as well as + # zip files and tar/gzipped packages can be produced by this task. + # + # In addition to the Rake targets generated by PackageTask, a + # GemPackageTask will also generate the following tasks: + # + # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.gem"</b>] + # Create a Ruby GEM package with the given name and version. + # + # Example using a Ruby GEM spec: + # + # require 'rubygems' + # + # 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 + # 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 + # + class GemPackageTask < PackageTask + # Ruby GEM spec containing the metadata for this package. The + # name, version and package_files are automatically determined + # from the GEM spec and don't need to be explicitly provided. + attr_accessor :gem_spec + + # Create a GEM Package task library. Automatically define the gem + # if a block is given. If no block is supplied, then +define+ + # needs to be called to define the task. + def initialize(gem_spec) + init(gem_spec) + yield self if block_given? + define if block_given? + end + + # Initialization tasks without the "yield self" or define + # operations. + def init(gem) + super(gem.name, gem.version) + @gem_spec = gem + @package_files += gem_spec.files if gem_spec.files + end + + # Create the Rake tasks and actions specified by this + # GemPackageTask. (+define+ is automatically called if a block is + # given to +new+). + def define + super + task :package => [:gem] + desc "Build the gem file #{gem_file}" + task :gem => ["#{package_dir}/#{gem_file}"] + file "#{package_dir}/#{gem_file}" => [package_dir] + @gem_spec.files do + when_writing("Creating GEM") { + Gem::Builder.new(gem_spec).build + verbose(true) { + mv gem_file, "#{package_dir}/#{gem_file}" + } + } + end + end + + def gem_file + if @gem_spec.platform == Gem::Platform::RUBY + "#{package_name}.gem" + else + "#{package_name}-#{@gem_spec.platform}.gem" + end + end + + end +end diff --git a/lib/rake/loaders/makefile.rb b/lib/rake/loaders/makefile.rb new file mode 100644 index 0000000000..f66eb3b35f --- /dev/null +++ b/lib/rake/loaders/makefile.rb @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby + +module Rake + + # Makefile loader to be used with the import file loader. + class MakefileLoader + + # Load the makefile dependencies in +fn+. + def load(fn) + buffer = '' + open(fn) do |mf| + mf.each do |line| + next if line =~ /^\s*#/ + buffer << line + if buffer =~ /\\$/ + buffer.sub!(/\\\n/, ' ') + state = :append + else + process_line(buffer) + buffer = '' + end + end + end + process_line(buffer) if buffer != '' + end + + private + + # Process one logical line of makefile data. + def process_line(line) + file_task, args = line.split(':') + return if args.nil? + dependents = args.split + file file_task => dependents + end + end + + # Install the handler + Rake.application.add_loader('mf', MakefileLoader.new) +end diff --git a/lib/rake/packagetask.rb b/lib/rake/packagetask.rb new file mode 100644 index 0000000000..71b66a6481 --- /dev/null +++ b/lib/rake/packagetask.rb @@ -0,0 +1,184 @@ +#!/usr/bin/env ruby + +# Define a package task libarary to aid in the definition of +# redistributable package files. + +require 'rake' +require 'rake/tasklib' + +module Rake + + # Create a packaging task that will package the project into + # distributable files (e.g zip archive or tar files). + # + # The PackageTask will create the following targets: + # + # [<b>:package</b>] + # Create all the requested package files. + # + # [<b>:clobber_package</b>] + # Delete all the package files. This target is automatically + # added to the main clobber target. + # + # [<b>:repackage</b>] + # Rebuild the package files from scratch, even if they are not out + # of date. + # + # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tgz"</b>] + # Create a gzipped tar package (if <em>need_tar</em> is true). + # + # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.gz"</b>] + # Create a gzipped tar package (if <em>need_tar_gz</em> is true). + # + # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.bz2"</b>] + # Create a bzip2'd tar package (if <em>need_tar_bz2</em> is true). + # + # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.zip"</b>] + # Create a zip package archive (if <em>need_zip</em> is true). + # + # Example: + # + # Rake::PackageTask.new("rake", "1.2.3") do |p| + # p.need_tar = true + # p.package_files.include("lib/**/*.rb") + # end + # + class PackageTask < TaskLib + # Name of the package (from the GEM Spec). + attr_accessor :name + + # Version of the package (e.g. '1.3.2'). + attr_accessor :version + + # Directory used to store the package files (default is 'pkg'). + attr_accessor :package_dir + + # True if a gzipped tar file (tgz) should be produced (default is false). + attr_accessor :need_tar + + # True if a gzipped tar file (tar.gz) should be produced (default is false). + attr_accessor :need_tar_gz + + # True if a bzip2'd tar file (tar.bz2) should be produced (default is false). + attr_accessor :need_tar_bz2 + + # True if a zip file should be produced (default is false) + attr_accessor :need_zip + + # List of files to be included in the package. + attr_accessor :package_files + + # Tar command for gzipped or bzip2ed archives. The default is 'tar'. + attr_accessor :tar_command + + # Zip command for zipped archives. The default is 'zip'. + attr_accessor :zip_command + + # Create a Package Task with the given name and version. + def initialize(name=nil, version=nil) + init(name, version) + yield self if block_given? + define unless name.nil? + end + + # Initialization that bypasses the "yield self" and "define" step. + def init(name, version) + @name = name + @version = version + @package_files = Rake::FileList.new + @package_dir = 'pkg' + @need_tar = false + @need_tar_gz = false + @need_tar_bz2 = false + @need_zip = false + @tar_command = 'tar' + @zip_command = 'zip' + end + + # Create the tasks defined by this task library. + def define + fail "Version required (or :noversion)" if @version.nil? + @version = nil if :noversion == @version + + desc "Build all the packages" + task :package + + desc "Force a rebuild of the package files" + task :repackage => [:clobber_package, :package] + + desc "Remove package products" + task :clobber_package do + rm_r package_dir rescue nil + end + + task :clobber => [:clobber_package] + + [ + [need_tar, tgz_file, "z"], + [need_tar_gz, tar_gz_file, "z"], + [need_tar_bz2, tar_bz2_file, "j"] + ].each do |(need, file, flag)| + if need + task :package => ["#{package_dir}/#{file}"] + file "#{package_dir}/#{file}" => [package_dir_path] + package_files do + chdir(package_dir) do + sh %{#{@tar_command} #{flag}cvf #{file} #{package_name}} + end + end + end + end + + if need_zip + task :package => ["#{package_dir}/#{zip_file}"] + file "#{package_dir}/#{zip_file}" => [package_dir_path] + package_files do + chdir(package_dir) do + sh %{#{@zip_command} -r #{zip_file} #{package_name}} + end + end + end + + directory package_dir + + file package_dir_path => @package_files do + mkdir_p package_dir rescue nil + @package_files.each do |fn| + f = File.join(package_dir_path, fn) + fdir = File.dirname(f) + mkdir_p(fdir) if !File.exist?(fdir) + if File.directory?(fn) + mkdir_p(f) + else + rm_f f + safe_ln(fn, f) + end + end + end + self + end + + def package_name + @version ? "#{@name}-#{@version}" : @name + end + + def package_dir_path + "#{package_dir}/#{package_name}" + end + + def tgz_file + "#{package_name}.tgz" + end + + def tar_gz_file + "#{package_name}.tar.gz" + end + + def tar_bz2_file + "#{package_name}.tar.bz2" + end + + def zip_file + "#{package_name}.zip" + end + end + +end diff --git a/lib/rake/rake_test_loader.rb b/lib/rake/rake_test_loader.rb new file mode 100644 index 0000000000..8d7dad3c94 --- /dev/null +++ b/lib/rake/rake_test_loader.rb @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +# Load the test files from the command line. + +ARGV.each { |f| load f unless f =~ /^-/ } diff --git a/lib/rake/rdoctask.rb b/lib/rake/rdoctask.rb new file mode 100644 index 0000000000..54adc6feb5 --- /dev/null +++ b/lib/rake/rdoctask.rb @@ -0,0 +1,147 @@ +#!/usr/bin/env ruby + +require 'rake' +require 'rake/tasklib' + +module Rake + + # Create a documentation task that will generate the RDoc files for + # a project. + # + # The RDocTask will create the following targets: + # + # [<b><em>rdoc</em></b>] + # Main task for this RDOC task. + # + # [<b>:clobber_<em>rdoc</em></b>] + # Delete all the rdoc files. This target is automatically + # added to the main clobber target. + # + # [<b>:re<em>rdoc</em></b>] + # Rebuild the rdoc files from scratch, even if they are not out + # of date. + # + # Simple Example: + # + # Rake::RDocTask.new do |rd| + # rd.main = "README.rdoc" + # rd.rdoc_files.include("README.rdoc", "lib/**/*.rb") + # end + # + # You may wish to give the task a different name, such as if you are + # generating two sets of documentation. For instance, if you want to have a + # development set of documentation including private methods: + # + # Rake::RDocTask.new(:rdoc_dev) do |rd| + # rd.main = "README.doc" + # rd.rdoc_files.include("README.rdoc", "lib/**/*.rb") + # rd.options << "--all" + # end + # + # The tasks would then be named :<em>rdoc_dev</em>, :clobber_<em>rdoc_dev</em>, and + # :re<em>rdoc_dev</em>. + # + class RDocTask < TaskLib + # Name of the main, top level task. (default is :rdoc) + attr_accessor :name + + # Name of directory to receive the html output files. (default is "html") + attr_accessor :rdoc_dir + + # Title of RDoc documentation. (default is none) + attr_accessor :title + + # Name of file to be used as the main, top level file of the + # RDoc. (default is none) + attr_accessor :main + + # Name of template to be used by rdoc. (default is 'html') + attr_accessor :template + + # List of files to be included in the rdoc generation. (default is []) + attr_accessor :rdoc_files + + # List of options to be passed rdoc. (default is []) + attr_accessor :options + + # Run the rdoc process as an external shell (default is false) + attr_accessor :external + + # Create an RDoc task named <em>rdoc</em>. Default task name is +rdoc+. + def initialize(name=:rdoc) # :yield: self + @name = name + @rdoc_files = Rake::FileList.new + @rdoc_dir = 'html' + @main = nil + @title = nil + @template = 'html' + @external = false + @options = [] + yield self if block_given? + define + end + + # Create the tasks defined by this task lib. + def define + if name.to_s != "rdoc" + desc "Build the RDOC HTML Files" + end + + desc "Build the #{name} HTML Files" + task name + + desc "Force a rebuild of the RDOC files" + task paste("re", name) => [paste("clobber_", name), name] + + desc "Remove rdoc products" + task paste("clobber_", name) do + rm_r rdoc_dir rescue nil + end + + task :clobber => [paste("clobber_", name)] + + directory @rdoc_dir + task name => [rdoc_target] + file rdoc_target => @rdoc_files + [$rakefile] do + rm_r @rdoc_dir rescue nil + args = option_list + @rdoc_files + if @external + argstring = args.join(' ') + sh %{ruby -Ivendor vender/rd #{argstring}} + else + require 'rdoc/rdoc' + RDoc::RDoc.new.document(args) + end + end + self + end + + def option_list + result = @options.dup + result << "-o" << @rdoc_dir + result << "--main" << quote(main) if main + result << "--title" << quote(title) if title + result << "-T" << quote(template) if template + result + end + + def quote(str) + if @external + "'#{str}'" + else + str + end + end + + def option_string + option_list.join(' ') + end + + private + + def rdoc_target + "#{rdoc_dir}/index.html" + end + + end +end diff --git a/lib/rake/ruby182_test_unit_fix.rb b/lib/rake/ruby182_test_unit_fix.rb new file mode 100755 index 0000000000..f02c7879eb --- /dev/null +++ b/lib/rake/ruby182_test_unit_fix.rb @@ -0,0 +1,23 @@ +module Test + module Unit + module Collector + class Dir + undef collect_file + def collect_file(name, suites, already_gathered) + # loadpath = $:.dup + dir = File.dirname(File.expand_path(name)) + $:.unshift(dir) unless $:.first == dir + if(@req) + @req.require(name) + else + require(name) + end + find_test_cases(already_gathered).each{|t| add_suite(suites, t.suite)} + ensure + # $:.replace(loadpath) + $:.delete_at $:.rindex(dir) + end + end + end + end +end diff --git a/lib/rake/runtest.rb b/lib/rake/runtest.rb new file mode 100644 index 0000000000..3f1d205201 --- /dev/null +++ b/lib/rake/runtest.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +require 'test/unit' +require 'test/unit/assertions' + +module Rake + include Test::Unit::Assertions + + def run_tests(pattern='test/test*.rb', log_enabled=false) + Dir["#{pattern}"].each { |fn| + puts fn if log_enabled + begin + load fn + rescue Exception => ex + puts "Error in #{fn}: #{ex.message}" + puts ex.backtrace + assert false + end + } + end + + extend self +end diff --git a/lib/rake/tasklib.rb b/lib/rake/tasklib.rb new file mode 100644 index 0000000000..465a58a0c7 --- /dev/null +++ b/lib/rake/tasklib.rb @@ -0,0 +1,18 @@ +#!/usr/bin/env ruby + +require 'rake' + +module Rake + + # Base class for Task Libraries. + class TaskLib + + include Cloneable + + # Make a symbol by pasting two strings together. + def paste(a,b) + (a.to_s + b.to_s).intern + end + end + +end diff --git a/lib/rake/testtask.rb b/lib/rake/testtask.rb new file mode 100644 index 0000000000..79154e422b --- /dev/null +++ b/lib/rake/testtask.rb @@ -0,0 +1,161 @@ +#!/usr/bin/env ruby + +# Define a task library for running unit tests. + +require 'rake' +require 'rake/tasklib' + +module Rake + + # Create a task that runs a set of tests. + # + # Example: + # + # Rake::TestTask.new do |t| + # t.libs << "test" + # t.test_files = FileList['test/test*.rb'] + # t.verbose = true + # end + # + # If rake is invoked with a "TEST=filename" command line option, + # then the list of test files will be overridden to include only the + # filename specified on the command line. This provides an easy way + # to run just one test. + # + # If rake is invoked with a "TESTOPTS=options" command line option, + # then the given options are passed to the test process after a + # '--'. This allows Test::Unit options to be passed to the test + # suite. + # + # Examples: + # + # rake test # run tests normally + # rake test TEST=just_one_file.rb # run just one test file. + # rake test TESTOPTS="-v" # run in verbose mode + # rake test TESTOPTS="--runner=fox" # use the fox test runner + # + class TestTask < TaskLib + + # Name of test task. (default is :test) + attr_accessor :name + + # List of directories to added to $LOAD_PATH before running the + # tests. (default is 'lib') + attr_accessor :libs + + # True if verbose test output desired. (default is false) + attr_accessor :verbose + + # Test options passed to the test suite. An explicit + # TESTOPTS=opts on the command line will override this. (default + # is NONE) + attr_accessor :options + + # Request that the tests be run with the warning flag set. + # E.g. warning=true implies "ruby -w" used to run the tests. + attr_accessor :warning + + # Glob pattern to match test files. (default is 'test/test*.rb') + attr_accessor :pattern + + # Style of test loader to use. Options are: + # + # * :rake -- Rake provided test loading script (default). + # * :testrb -- Ruby provided test loading script. + # * :direct -- Load tests using command line loader. + # + attr_accessor :loader + + # Array of commandline options to pass to ruby when running test loader. + attr_accessor :ruby_opts + + # Explicitly define the list of test files to be included in a + # test. +list+ is expected to be an array of file names (a + # FileList is acceptable). If both +pattern+ and +test_files+ are + # used, then the list of test files is the union of the two. + def test_files=(list) + @test_files = list + end + + # Create a testing task. + def initialize(name=:test) + @name = name + @libs = ["lib"] + @pattern = nil + @options = nil + @test_files = nil + @verbose = false + @warning = false + @loader = :rake + @ruby_opts = [] + yield self if block_given? + @pattern = 'test/test*.rb' if @pattern.nil? && @test_files.nil? + define + end + + # Create the tasks defined by this task lib. + def define + lib_path = @libs.join(File::PATH_SEPARATOR) + desc "Run tests" + (@name==:test ? "" : " for #{@name}") + task @name do + run_code = '' + RakeFileUtils.verbose(@verbose) do + run_code = + case @loader + when :direct + "-e 'ARGV.each{|f| load f}'" + when :testrb + "-S testrb #{fix}" + when :rake + rake_loader + end + @ruby_opts.unshift( "-I#{lib_path}" ) + @ruby_opts.unshift( "-w" ) if @warning + ruby @ruby_opts.join(" ") + + " \"#{run_code}\" " + + file_list.collect { |fn| "\"#{fn}\"" }.join(' ') + + " #{option_list}" + end + end + self + end + + def option_list # :nodoc: + ENV['TESTOPTS'] || @options || "" + end + + def file_list # :nodoc: + if ENV['TEST'] + FileList[ ENV['TEST'] ] + else + result = [] + result += @test_files.to_a if @test_files + result += FileList[ @pattern ].to_a if @pattern + FileList[result] + end + end + + def fix # :nodoc: + case RUBY_VERSION + when '1.8.2' + find_file 'rake/ruby182_test_unit_fix' + else + nil + end || '' + end + + def rake_loader # :nodoc: + find_file('rake/rake_test_loader') or + fail "unable to find rake test loader" + end + + def find_file(fn) # :nodoc: + $LOAD_PATH.each do |path| + file_path = File.join(path, "#{fn}.rb") + return file_path if File.exist? file_path + end + nil + end + + end +end |