Ruby Rake 'advanced' rule arguments

P

Peter Pk

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 = prefix + files;
}
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
 
P

Peter Pk

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top