Method: Manager#manage

Defined in:
lib/manager.rb

#manage(f, data = nil, l = nil) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/manager.rb', line 338

def manage f, data = nil, l = nil
	#! The ordering between the following two lines is important. If done otherwise,
	#   the same file called through different paths would be loaded multiple times.
	return Dir.entries(f).each{|f| manage(f)} if File.directory?(f) unless f.nil?
	return if data.nil? and @files.any?{|k, _| k == f}
	begin
		l ||= File.read(f) &.count($/) unless f.nil?
	rescue Errno::ENOENT => e
		raise "#{e.message}\n"\
		"Called from #{caller_locations[2].absolute_path}:#{caller_locations[2].lineno}"
	end
	@files.push([f, {mtime: f && File.new(f) &.mtime, lines: (data ? data.count($/) + 1 : l)}])
	#!`nil` denotes that "source unknown" methods will be immune from "misplaced" error.
	return if f.nil?
	@annotation_extractor = AnnotationExtractor.new(f)
	tracing_classes = {}
	constants_stack = []
	Object.module_start(constants_stack, tracing_classes)
	tp_module_start = TracePoint.new(:class) do
		|tp|
		modul, _f, l = tp.binding.receiver, tp.path, tp.lineno
		next unless _f == f
		@annotation_extractor.read_upto(modul, :module, nil, [_f, l])
		modul.module_start(constants_stack, tracing_classes)
	end
	tp_module_end = TracePoint.new(:end) do
		|tp|
		modul, _f, l = tp.binding.receiver, tp.path, tp.lineno
		next unless _f == f
		@annotation_extractor.read_upto(modul, :module_end, nil, [_f, l])
		modul.module_end(constants_stack)
	end
	tp_module_start.enable
	tp_module_end.enable
	begin
		#! `l` for the spec code lines, `1` for the `__END__` line, and `1` for starting at 1.
		data ? TOPLEVEL_BINDING.eval(data, f, l + 1 + 1) : load(f)
	rescue Exception => e
		Console.abort(e)
	end
	tp_module_start.disable
	tp_module_end.disable
	Object.module_end(constants_stack)
	tracing_classes.each_key do
		|modul|
		modul.singleton_class.__send__(:remove_method, :singleton_method_added)
		modul.singleton_class.__send__(:remove_method, :method_added)
	end
	@annotation_extractor.close
	nil
end