Class: Rake::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/rake.rb

Overview

Rake main application object. When invoking rake from the command line, a Rake::Application object is created and run.

Constant Summary collapse

RAKEFILES =
['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb']
OPTIONS =
[
  ['--dry-run',  '-n', GetoptLong::NO_ARGUMENT,
	"Do a dry run without executing actions."],
  ['--help',     '-H', GetoptLong::NO_ARGUMENT,
	"Display this help message."],
  ['--libdir',   '-I', GetoptLong::REQUIRED_ARGUMENT,
	"Include LIBDIR in the search path for required modules."],
  ['--nosearch', '-N', GetoptLong::NO_ARGUMENT,
	"Do not search parent directories for the Rakefile."],
  ['--prereqs',  '-P', GetoptLong::NO_ARGUMENT,
	"Display the tasks and dependencies, then exit."],
  ['--quiet',    '-q', GetoptLong::NO_ARGUMENT,
	"Do not log messages to standard output."],
  ['--rakefile', '-f', GetoptLong::REQUIRED_ARGUMENT,
	"Use FILE as the rakefile."],
  ['--require',  '-r', GetoptLong::REQUIRED_ARGUMENT,
	"Require MODULE before executing rakefile."],
  ['--silent',   '-s', GetoptLong::NO_ARGUMENT,
	"Like --quiet, but also suppresses the 'in directory' announcement."],
  ['--tasks',    '-T', GetoptLong::NO_ARGUMENT,
	"Display the tasks and dependencies, then exit."],
  ['--trace',    '-t', GetoptLong::NO_ARGUMENT,
	"Turn on invoke/execute tracing, enable full backtrace."],
  ['--usage',    '-h', GetoptLong::NO_ARGUMENT,
	"Display usage."],
  ['--verbose',  '-v', GetoptLong::NO_ARGUMENT,
	"Log message to standard output (default)."],
  ['--version',  '-V', GetoptLong::NO_ARGUMENT,
	"Display the program version."],
  ['--classic-namespace', '-C', GetoptLong::NO_ARGUMENT,
	"Put Task and FileTask in the top level namespace"],
]

Instance Method Summary collapse

Constructor Details

#initializeApplication

Create a Rake::Application object.



1243
1244
1245
1246
1247
1248
1249
1250
1251
# File 'lib/rake.rb', line 1243

def initialize
  @rakefile = nil
  @pending_imports = []
  @imported = []
  @nosearch = false
  @loaders = {}
  @default_loader = Rake::DefaultLoader.new
  Rake.application = self
end

Instance Method Details

#add_import(fn) ⇒ Object

Add a file to the list of files to be imported.



1400
1401
1402
# File 'lib/rake.rb', line 1400

def add_import(fn)
  @pending_imports << fn
end

#add_loader(ext, loader) ⇒ Object

Add a loader to handle imported files ending in the extension ext.



1418
1419
1420
1421
# File 'lib/rake.rb', line 1418

def add_loader(ext, loader)
  ext = ".#{ext}" unless ext =~ /^\./
  @loaders[ext] = loader
end

#collect_tasksObject

Collect the list of tasks on the command line. If no tasks are give, return a list containing only the default task. Environmental assignments are processed at this time as well.



1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
# File 'lib/rake.rb', line 1386

def collect_tasks
  tasks = []
  ARGV.each do |arg|
	if arg =~ /^(\w+)=(.*)$/
	  ENV[$1] = $2
	else
	  tasks << arg
	end
  end
  tasks.push("default") if tasks.size == 0
  tasks
end

#command_line_optionsObject

Return a list of the command line options supported by the program.



1311
1312
1313
# File 'lib/rake.rb', line 1311

def command_line_options
  OPTIONS.collect { |lst| lst[0..-2] }
end

#const_warning(const_name) ⇒ Object

Warn about deprecated use of top level constant names.



1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
# File 'lib/rake.rb', line 1424

def const_warning(const_name)
  @const_warning ||= false
  if ! @const_warning
	puts %{WARNING: Deprecated reference to top-level constant '#{const_name}'} +
	  %{found at: #{rakefile_location}}
	puts %{    Use --classic-namespace on rake command}
	puts %{    or 'require "rake/classic_namespace"' in Rakefile}
  end
  @const_warning = true
end

#display_prerequisitesObject

Display the tasks and prerequisites



1302
1303
1304
1305
1306
1307
# File 'lib/rake.rb', line 1302

def display_prerequisites
  Rake::Task.tasks.each do |t|
	puts "rake #{t.name}"
	t.prerequisites.each { |pre| puts "    #{pre}" }
  end
end

#display_tasks_and_commentsObject

Display the tasks and dependencies.



1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
# File 'lib/rake.rb', line 1288

def display_tasks_and_comments
  width = Rake::Task.tasks.select { |t|
	t.comment
  }.collect { |t|
	t.name.length
  }.max
  Rake::Task.tasks.each do |t|
	if t.comment
	  printf "rake %-#{width}s  # %s\n", t.name, t.comment
	end
  end
end

#do_option(opt, value) ⇒ Object

Do the option defined by opt and value.



1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
# File 'lib/rake.rb', line 1316

def do_option(opt, value)
  case opt
  when '--dry-run'
	verbose(true)
	nowrite(true)
	$dryrun = true
	$trace = true
  when '--help'
	help
	exit
  when '--libdir'
	$:.push(value)
  when '--nosearch'
	@nosearch = true
  when '--prereqs'
	$show_prereqs = true
  when '--quiet'
	verbose(false)
  when '--rakefile'
	RAKEFILES.clear
	RAKEFILES << value
  when '--require'
	require value
  when '--silent'
	verbose(false)
	$silent = true
  when '--tasks'
	$show_tasks = true
  when '--trace'
	$trace = true
	verbose(true)
  when '--usage'
	usage
	exit
  when '--verbose'
	verbose(true)
  when '--version'
	puts "rake, version #{RAKEVERSION}"
	exit
  when '--classic-namespace'
	require 'rake/classic_namespace'
  else
	fail "Unknown option: #{opt}"
  end
end

#handle_optionsObject

Read and handle the command line options.



1363
1364
1365
1366
# File 'lib/rake.rb', line 1363

def handle_options
  opts = GetoptLong.new(*command_line_options)
  opts.each { |opt, value| do_option(opt, value) }
end

#have_rakefileObject

True if one of the files in RAKEFILES is in the current directory. If a match is found, it is copied into @rakefile.



1255
1256
1257
1258
1259
1260
1261
1262
1263
# File 'lib/rake.rb', line 1255

def have_rakefile
  RAKEFILES.each do |fn|
	if File.exist?(fn)
	  @rakefile = fn
	  return true
	end
  end
  return false
end

#helpObject

Display the rake command line help.



1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
# File 'lib/rake.rb', line 1271

def help
  usage
  puts
  puts "Options are ..."
  puts
  OPTIONS.sort.each do |long, short, mode, desc|
	if mode == GetoptLong::REQUIRED_ARGUMENT
	  if desc =~ /\b([A-Z]{2,})\b/
 long = long + "=#{$1}"
	  end
	end
	printf "  %-20s (%s)\n", long, short
	printf "      %s\n", desc
  end
end

#load_importsObject

Load the pending list of imported files.



1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
# File 'lib/rake.rb', line 1405

def load_imports
  while fn = @pending_imports.shift
	next if @imported.member?(fn)
	Rake::Task[fn].invoke if Rake::Task.task_defined?(fn)
	ext = File.extname(fn)
	loader = @loaders[ext] || @default_loader
	loader.load(fn)
	@imported << fn
  end
end

#load_rakefileObject



1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
# File 'lib/rake.rb', line 1368

def load_rakefile
  here = Dir.pwd
  while ! have_rakefile
	Dir.chdir("..")
	if Dir.pwd == here || @nosearch
	  fail "No Rakefile found (looking for: #{RAKEFILES.join(', ')})"
	end
	here = Dir.pwd
  end
  puts "(in #{Dir.pwd})" unless $silent
  $rakefile = @rakefile
  load @rakefile
  load_imports
end

#rakefile_locationObject



1435
1436
1437
1438
1439
1440
1441
# File 'lib/rake.rb', line 1435

def rakefile_location
  begin
	fail
  rescue RuntimeError => ex
	ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
  end
end

#runObject

Run the rake application.



1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
# File 'lib/rake.rb', line 1444

def run
  handle_options
  begin
	tasks = collect_tasks
	load_rakefile
	if $show_tasks
	  display_tasks_and_comments
	elsif $show_prereqs
	  display_prerequisites
	else
	  tasks.each { |task_name| Rake::Task[task_name].invoke }
	end
  rescue Exception => ex
	puts "rake aborted!"
	puts ex.message
	if $trace
	  puts ex.backtrace.join("\n")
	else
	  puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
	end
	exit(1)
  end    
end

#usageObject

Display the program usage line.



1266
1267
1268
# File 'lib/rake.rb', line 1266

def usage
  puts "rake [-f rakefile] {options} targets..."
end