module Utilrb

Utilrb is yet another Ruby toolkit, in the spirit of facets. It includes all the standard class extensions used by www.rock-robotics.org projects.

Constants

DOC_MODE
LIB_DIR
VERSION

Public Class Methods

doc(target = 'docs', options = Hash.new) click to toggle source
# File lib/utilrb/doc/rake.rb, line 25
def self.doc(target = 'docs', options = Hash.new)
    if target.kind_of?(Hash)
        target, options = 'docs', target
    end

    options = Kernel.validate_options options,
        :include => [File.join('lib', '**', '*.rb'), File.join('ext', '**', '*.cc')],
        :exclude => [],
        :target_dir => 'doc',
        :title => '',
        :plugins => [],
        :files => []

    case DOC_MODE
    when 'yard'
        task = YARD::Rake::YardocTask.new(target)
        task.files.concat(options[:include])
        task.options << '--title' << options[:title] << '--output-dir' << options[:target_dir]
        if !options[:files].empty?
            task.options << "--files" << options[:files].join(",")
        end
        options[:plugins].each do |plugin_name|
            require "#{plugin_name}/yard"
        end

        task_clobber = ::Rake::Task.define_task "clobber_#{target}" do 
            FileUtils.rm_rf options[:target_dir]
        end
        task_clobber.add_description "Remove #{target} products"

        name = ::Rake.application.current_scope.dup
        if name.respond_to?(:path)
            name = name.map(&:to_s).reverse
        end
        name << task.name
        task_re = ::Rake::Task.define_task "re#{target}" do
            FileUtils.rm_rf options[:target_dir]
            ::Rake::Task[name.join(":")].invoke
        end
        task_re.add_description "Force a rebuild of #{target}"

    when "rdoc"
        task = RDoc::Task.new(target)
        task.rdoc_files.include(*options[:include])
        task.rdoc_files.exclude(*options[:exclude])
        task.title = options[:title]
        task.rdoc_dir = File.expand_path(options[:target_dir])

    else
        [target, "re#{target}"].each do |task_name|
            t = ::Rake::Task.define_task(task_name) do
                STDERR.puts "Documentation generation is disabled: install either the yard or rdoc gems"
            end
            t.add_description "Disabled on this installation as neither yard nor rdoc are available"
        end
    end
end
doc?() click to toggle source
# File lib/utilrb/doc/rake.rb, line 17
def self.doc?
    if DOC_MODE
        true
    else
        false
    end
end
spawn(*cmdline) click to toggle source
# File lib/utilrb/spawn.rb, line 5
def self.spawn(*cmdline)
    options =
        if cmdline.last.kind_of?(Hash)
            cmdline.pop
        else Hash.new
        end

    options = Kernel.validate_options options, :redirect => nil,
        :working_directory => nil,
        :nice => nil

    output  = options[:redirect]
    workdir = options[:working_directory]

    read, write = IO.pipe
    pid = fork do 
        if output
            if !output.kind_of?(IO)
                output_file_name = output.
                    gsub('%p', ::Process.pid.to_s)
                if workdir
                    output_file_name = File.expand_path(output_file_name, workdir)
                end
                output = File.open(output, 'a')
            end
        end
        
        if output
            STDERR.reopen(output)
            STDOUT.reopen(output)
        end

        read.close
        write.sync = true
        write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
        ::Process.setpgrp
        if options[:nice]
            Process.setpriority(Process::PRIO_PROCESS, 0, options[:nice])
        end

        begin
            if workdir
                Dir.chdir(workdir)
            end
            exec(*cmdline)
        rescue Exception
            write.write("FAILED")
        end
    end

    write.close
    if read.read == "FAILED"
        raise SpawnFailed, "cannot start #{cmdline.inspect}"
    end
    pid
end