1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
require 'rubygems'
require 'rake'
require 'rake/clean'
require 'fileutils'
require 'ostruct'
require 'find'
class OpenStruct; undef :gem; end
# TODO: make my own openstruct type object that includes descriptions
# TODO: use the descriptions to output help on the available bones options
PROJ = OpenStruct.new(
# Project Defaults
:name => nil,
:summary => nil,
:description => nil,
:changes => nil,
:authors => nil,
:email => nil,
:url => "\000",
:version => ENV['VERSION'] || '0.0.0',
:exclude => %w(tmp$ bak$ ~$ CVS \.svn/ \.git/ ^pkg/),
:release_name => ENV['RELEASE'],
# System Defaults
:ruby_opts => %w(-w),
:libs => [],
:history_file => 'History.txt',
:readme_file => 'README.txt',
:ignore_file => '.bnsignore',
# Announce
:ann => OpenStruct.new(
:file => 'announcement.txt',
:text => nil,
:paragraphs => [],
:email => {
:from => nil,
:to => %w(ruby-talk@ruby-lang.org),
:server => 'localhost',
:port => 587,
:domain => ENV['HOSTNAME'],
:acct => nil,
:passwd => nil,
:authtype => :plain
}
),
# Gem Packaging
:gem => OpenStruct.new(
:dependencies => [],
:development_dependencies => [],
:executables => nil,
:extensions => FileList['ext/**/extconf.rb'],
:files => nil,
:need_tar => true,
:need_zip => false,
:extras => {}
),
# File Annotations
:notes => OpenStruct.new(
:exclude => %w(^tasks/setup\.rb$),
:extensions => %w(.txt .rb .erb .rdoc) << '',
:tags => %w(FIXME OPTIMIZE TODO)
),
# Rcov
:rcov => OpenStruct.new(
:dir => 'coverage',
:opts => %w[--sort coverage -T],
:threshold => 90.0,
:threshold_exact => false
),
# Rdoc
:rdoc => OpenStruct.new(
:opts => [],
:include => %w(^lib/ ^bin/ ^ext/ \.txt$ \.rdoc$),
:exclude => %w(extconf\.rb$),
:main => nil,
:dir => 'doc',
:remote_dir => nil
),
# Rubyforge
:rubyforge => OpenStruct.new(
:name => "\000"
),
# Rspec
:spec => OpenStruct.new(
:files => FileList['spec/**/*_spec.rb'],
:opts => []
),
# Subversion Repository
:svn => OpenStruct.new(
:root => nil,
:path => '',
:trunk => 'trunk',
:tags => 'tags',
:branches => 'branches'
),
# Test::Unit
:test => OpenStruct.new(
:files => FileList['test/**/test_*.rb'],
:file => 'test/all.rb',
:opts => []
)
)
# Load the other rake files in the tasks folder
tasks_dir = File.expand_path(File.dirname(__FILE__))
post_load_fn = File.join(tasks_dir, 'post_load.rake')
rakefiles = Dir.glob(File.join(tasks_dir, '*.rake')).sort
rakefiles.unshift(rakefiles.delete(post_load_fn)).compact!
import(*rakefiles)
# Setup the project libraries
%w(lib ext).each {|dir| PROJ.libs << dir if test ?d, dir}
# Setup some constants
WIN32 = %r/djgpp|(cyg|ms|bcc)win|mingw/ =~ RUBY_PLATFORM unless defined? WIN32
DEV_NULL = WIN32 ? 'NUL:' : '/dev/null'
def quiet( &block )
io = [STDOUT.dup, STDERR.dup]
STDOUT.reopen DEV_NULL
STDERR.reopen DEV_NULL
block.call
ensure
STDOUT.reopen io.first
STDERR.reopen io.last
$stdout, $stderr = STDOUT, STDERR
end
DIFF = if WIN32 then 'diff.exe'
else
if quiet {system "gdiff", __FILE__, __FILE__} then 'gdiff'
else 'diff' end
end unless defined? DIFF
SUDO = if WIN32 then ''
else
if quiet {system 'which sudo'} then 'sudo'
else '' end
end
RCOV = WIN32 ? 'rcov.bat' : 'rcov'
RDOC = WIN32 ? 'rdoc.bat' : 'rdoc'
GEM = WIN32 ? 'gem.bat' : 'gem'
%w(rcov spec/rake/spectask rubyforge bones facets/ansicode).each do |lib|
begin
require lib
Object.instance_eval {const_set "HAVE_#{lib.tr('/','_').upcase}", true}
rescue LoadError
Object.instance_eval {const_set "HAVE_#{lib.tr('/','_').upcase}", false}
end
end
HAVE_SVN = (Dir.entries(Dir.pwd).include?('.svn') and
system("svn --version 2>&1 > #{DEV_NULL}"))
HAVE_GIT = (Dir.entries(Dir.pwd).include?('.git') and
system("git --version 2>&1 > #{DEV_NULL}"))
# Add bones as a development dependency
#
if HAVE_BONES
bones_version = defined?(Bones::VERSION) ? Bones::VERSION : Bones.version
PROJ.gem.development_dependencies << ['bones', ">= #{bones_version}"]
end
# Reads a file at +path+ and spits out an array of the +paragraphs+
# specified.
#
# changes = paragraphs_of('History.txt', 0..1).join("\n\n")
# summary, *description = paragraphs_of('README.txt', 3, 3..8)
#
def paragraphs_of( path, *paragraphs )
title = String === paragraphs.first ? paragraphs.shift : nil
ary = File.read(path).delete("\r").split(/\n\n+/)
result = if title
tmp, matching = [], false
rgxp = %r/^=+\s*#{Regexp.escape(title)}/i
paragraphs << (0..-1) if paragraphs.empty?
ary.each do |val|
if val =~ rgxp
break if matching
matching = true
rgxp = %r/^=+/i
elsif matching
tmp << val
end
end
tmp
else ary end
result.values_at(*paragraphs)
end
# Adds the given gem _name_ to the current project's dependency list. An
# optional gem _version_ can be given. If omitted, the newest gem version
# will be used.
#
def depend_on( name, version = nil )
spec = Gem.source_index.find_name(name).last
version = spec.version.to_s if version.nil? and !spec.nil?
PROJ.gem.dependencies << case version
when nil; [name]
when %r/^\d/; [name, ">= #{version}"]
else [name, version] end
end
# Adds the given arguments to the include path if they are not already there
#
def ensure_in_path( *args )
args.each do |path|
path = File.expand_path(path)
$:.unshift(path) if test(?d, path) and not $:.include?(path)
end
end
# Find a rake task using the task name and remove any description text. This
# will prevent the task from being displayed in the list of available tasks.
#
def remove_desc_for_task( names )
Array(names).each do |task_name|
task = Rake.application.tasks.find {|t| t.name == task_name}
next if task.nil?
task.instance_variable_set :@comment, nil
end
end
# Change working directories to _dir_, call the _block_ of code, and then
# change back to the original working directory (the current directory when
# this method was called).
#
def in_directory( dir, &block )
curdir = pwd
begin
cd dir
return block.call
ensure
cd curdir
end
end
# Scans the current working directory and creates a list of files that are
# candidates to be in the manifest.
#
def manifest
files = []
exclude = PROJ.exclude.dup
comment = %r/^\s*#/
# process the ignore file and add the items there to the exclude list
if test(?f, PROJ.ignore_file)
ary = []
File.readlines(PROJ.ignore_file).each do |line|
next if line =~ comment
line.chomp!
line.strip!
next if line.nil? or line.empty?
glob = line =~ %r/\*\./ ? File.join('**', line) : line
Dir.glob(glob).each {|fn| ary << "^#{Regexp.escape(fn)}"}
end
exclude.concat ary
end
# generate a regular expression from the exclude list
exclude = Regexp.new(exclude.join('|'))
Find.find '.' do |path|
path.sub! %r/^(\.\/|\/)/o, ''
next unless test ?f, path
next if path =~ exclude
files << path
end
files.sort!
end
# We need a "valid" method thtat determines if a string is suitable for use
# in the gem specification.
#
class Object
def valid?
return !(self.empty? or self == "\000") if self.respond_to?(:to_str)
return false
end
end
# EOF
|