module C

Core Module

Public Class Methods

add_dir(dir) click to toggle source

Add a directory to the build.

This will run the “dir.rub” file in that directory synchronously. Any values that that directory defines will be available when this call returns.

This function only runs scripts once, if the script has already run this function will return success without running the script, and as the script has already been run the exported values should be available.

# File lib/rub/r/i/runner.rb, line 63
def self.add_dir(dir)
        dir = C.path(dir)
        
        if not dir.directory?
                raise "\"#{dir}\" is not a directory!"
        end
        
        dir += 'dir.rub'
        if not dir.exist?
                raise "\"#{dir}\" does not exist!"
        end
        dir = dir.realpath
        
        R::I::Runner.do_file(dir)
end
chash(o) click to toggle source

Get a consistant hash of an object.

# File lib/rub/c.rb, line 62
def self.chash(o)
        if o.is_a? Array
                return o.map{|i| chash i}.join
        end
        
        # Super hacky, strip out object-ids, because they change every
        # invocation, but use inspect.  It works alright.
        r = o.inspect.gsub(/(?<!:):0x[0-9a-f]*/, '')
        
        # Modules don't print themselfs meaningfully.
        if o.is_a? Module
                r << o.pretty_print_instance_variables.map{|k| [k, o.instance_variable_get(k)] }.inspect
        end

        r     
end
find_command(cmd) click to toggle source

Find an executable on the system.

This searches the system for execrable in the appropriate locations (example $PATH on UNIX).

This function caches its result both in memory and between Rub runs. Feel free to call it often.

@param cmd [String] The name of the command (basename only). @return [Pathname,nil] Pathname, or nil if not found.

@example

C::find_command 'true'    #=> #<Pathname:/usr/bin/true>
C::find_command 'cc'      #=> #<Pathname:/home/kevincox/.local/bin/cc>
C::find_command 'sl'      #=> #<Pathname:/usr/bin/sl>
C::find_command 'python'  #=> #<Pathname:/usr/bin/python>
C::find_command 'explode' #=> nil
# File lib/rub/c.rb, line 243
def self.find_command(cmd)
        pn = Pathname.new(cmd)
        if pn.absolute?
                #return pn.executable? ? pn : nil
                return pn
        end
        
        exe = R.spersistant["C.find_command.#{cmd}"]
        
        exe and exe.executable? and return exe
        
        exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
        names = exts.map{|e| cmd+e}
        ENV['PATH'].split(File::PATH_SEPARATOR)
                   .map{|d|Pathname.new(d)}
                   .each do |d|
                names.each do |n|
                        e = d + n
                        #p e
                        
                        if e.executable?
                                exe = e
                                break
                        end
                end
                
                exe and break
        end
        
        R.spersistant["C.find_command.#{cmd}"] = exe
end
generator(src, cmd, out, desc: nil) click to toggle source

Add a generator to the build

This function provides a simple api for creating {R::TargetGenerator} targets. It creates a target that simply runs one or more commands to transform it’s inputs into outputs. This interface handles all build caching and parallelization.

@param src [Array<Pathname,String>,Pathname,String]

The source file or list of source files.

@param cmd [Array<Array<Pathname,String>>,Array<Pathname,String>]

The command or list of commands to run.  Commands will run in
order.

@param out [Array<Pathname,String>,Pathname,String]

The output files of the command.

@param desc The verb for this step in the process. (See

{R::TargetGenerator#action})

@return [Array<Pathname>] The output files. This will represent the same

values passed in to the +out+ parameter but it
will be a new Array and all the values will be
Pathnames.
# File lib/rub/c.rb, line 208
def self.generator(src, cmd, out, desc: nil)
        t = R::TargetGenerator.new
        
        desc and t.action = desc
        
        src = R::Tool.make_set_paths(src)
        out = R::Tool.make_set_paths(out)
        cmd[0].is_a?(Array) or cmd = [cmd]
        
        t.input .merge(src)
        t.output.merge(out)
        t.add_cmds cmd
        
        t.register
        
        out
end
glob(glob) click to toggle source

Glob pathnames.

@see Dir.glob

@param glob [String] @return [Set<Pathname>]

# File lib/rub/c.rb, line 57
def self.glob(glob)
        Set.new Dir.glob(glob).map{|e| C.path(e) }
end
path(p) click to toggle source

Expand a path.

@return [Pathname]

# File lib/rub/c.rb, line 32
def self.path(p)
        p.is_a? Symbol   and return p
        p.is_a? Pathname and return p.expand_path
        p = p.to_s
        
        #p = case p[0]
        #     when '!'
        #             Pathname.new(p[1..-1])
        #     when '>'
        #             R::Env.out_dir + p[1..-1]
        #     when '<'
        #             R::Env.src_dir + p[1..-1]
        #     else
        #             Pathname.new(p)
        #end
        
        Pathname.new(p).expand_path
end
tag(t) click to toggle source

Get a tag.

If the tag already exists it returns the existing {Tag} object otherwise it creates and returns a new {Tag} instance.

@param t [Symbol] The tag name. @return [Tag] The tag object.

# File lib/rub/c.rb, line 179
def self.tag(t)
        R.find_target(t) || Tag.new(t)
end
unique_path(base, seed) click to toggle source

Return a probably unique file name.

This file can be used as a build target.

@param base [String] The basename of the file. @param seed [Object] A value to use for the folder name, keeping this the

same across invocations allows predictable names,
preventing unnecessary rebuilds.
# File lib/rub/c.rb, line 100
def self.unique_path(base, seed)
        R::Env.out_dir + 'c/unique/' + unique_segment(seed) + base
end
unique_segment(*seed) click to toggle source

Create a probably unique path segment.

Creates a string in the form ‘$stuff/’ that will probably be unique.

@param seed [Object] A value to use for the folder name, keeping this the

same across invocations allows predictable names,
preventing unnecessary rebuilds.
# File lib/rub/c.rb, line 86
def self.unique_segment(*seed)
        seed ||= caller_locations(1,1)
        
        return Digest::SHA1.hexdigest(chash(seed))
end