Module: Utilrb

Defined in:
lib/utilrb/common.rb,
lib/utilrb.rb,
lib/utilrb/test.rb,
lib/utilrb/yard.rb,
lib/utilrb/spawn.rb,
lib/utilrb/version.rb,
lib/utilrb/weakref.rb,
lib/utilrb/doc/rake.rb,
lib/utilrb/pkgconfig.rb,
lib/utilrb/event_loop.rb,
lib/utilrb/timepoints.rb,
lib/utilrb/rake_common.rb,
lib/utilrb/thread_pool.rb,
lib/utilrb/configsearch/configuration_finder.rb

Overview

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.

Defined Under Namespace

Modules: Rake, SelfTest, Timepoints, YARD Classes: ConfigurationFinder, EventLoop, PkgConfig, SpawnFailed, ThreadPool, WeakRef

Constant Summary collapse

LIB_DIR =
File.expand_path(File.dirname(__FILE__))
VERSION =
"3.1.0"
DOC_MODE =
begin
    require 'yard'
    require 'yard/rake/yardoc_task'
    'yard'
rescue LoadError
    begin
        require 'rdoc/task'
        'rdoc'
    rescue LoadError
    end
end

Class Method Summary collapse

Class Method Details

.doc(target = 'docs', options = Hash.new) ⇒ Object



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
# 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?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/utilrb/doc/rake.rb', line 17

def self.doc?
    if DOC_MODE
        true
    else
        false
    end
end

.spawn(*cmdline) ⇒ Object



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
# 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