Forum: Ruby Ruby Rake 'advanced' rule arguments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Peter P. (Guest)
on 2009-01-10 01:52
Just starting out with Rake and Ruby, and made a little rakefile
(below).

It does a simple dependency based file copy of source files to a "dist"
directory. I works fine, but I was wondering if there is a way to
retrieve the "regex" argument to Rake::rule in the proc that generates
or modifies the "task_name". If so I wouldn't have to add a variable
(testregex in rakefile) in the scope that calls "rule".  Or maybe a way
to access $' $` and $& from the regex test that matches the rule's
target (task_name)

If this is better posted to another list let me know ;^>

PK

# **** rakefile ****
task :default => [:test]

LIBSRCDIR = '../../common'

# I didn't see this in the "FileList"
def addprefix(prefix,files)
  a = FileList.new() # can one allocate to files.size?
  files.each_index { |i|
    a[i] = prefix + files[i];
  }
  return(a)
end

# gotta do what ya gotta do
def regexifyPath(str)
  return(str.gsub(/\//,'\/').
            gsub(/\./,'\.'))
end

# copy in source for AiroNetSampleLib.dll

NETLIBSRC = [
  'HTTPClient.cpp',
  'HTTPInputStream.cpp',
  'HTTPChunkedOutputStream.cpp',
  'HTTPClientImpl.h',
]

NETLIBDIR = './NetSampleLib'
directory NETLIBDIR

NETLIBSRC_FULL = addprefix(NETLIBDIR + '/', NETLIBSRC)

testregex = Regexp.new('^' + regexifyPath(NETLIBDIR + '/'))

rule( testregex => [
    proc { |task_name| task_name.sub(testregex, LIBSRCDIR + '/aironet/')
}
  ])  do |t|
    puts "cp #{t.source} #{t.name}"
    cp t.source, t.name
end

task :test => NETLIBSRC_FULL do |t|
    puts "building #{t.name}"
    puts "from #{t.prerequisites}"
end
Peter P. (Guest)
on 2009-01-10 23:08
Anyway - yes I'm learning ... answered my own question
The miracle of subroutines and scope solves the problem and factors out
nicely.

The below make a rule to copy tarket "[destdir]/file"s from srcdir to
destdir

*******

def copyRule(destdir, srcdir)

  if(srcdir =~ /\/\z/)
  srcdir = $`
  end

  if(destdir =~ /\/\z/)
        # rake doesn't check both, string based
  directory(destdir)
  srcdir = $`
  end

  directory(destdir)

  regex =
  Regexp.new('^' + regexifyPath(destdir) + '\/[^\/]+\z')

  Rake::Task::create_rule( regex => [
    proc { |task_name|
      task_name =~ /\/[^\/]+\z/
      task_name = srcdir + $&
    }
    ])  do |t|
    cp t.source, t.name
  end
end

Peter I myself wrote:
> Just starting out with Rake and Ruby, and made a little rakefile
> (below).
This topic is locked and can not be replied to.