Permalink
Browse files

sent environment variables to templates: src and dest files information.

  • Loading branch information...
1 parent e6fdf3f commit cf3b26c221664a3e353fb7ae8f48663477d37e40 @tdtds committed Nov 10, 2010
Showing with 35 additions and 21 deletions.
  1. +32 −15 lib/rego/app.rb
  2. +3 −6 lib/rego/block/template.rb
View
@@ -13,61 +13,78 @@ class MakeLink < REGOError; end
class App
def initialize( src, dest, opt = {} )
@src, @dest, @opt = src, dest, opt
+ @src.chop! if %r|/$| =~ @src
+ @dest.chop! if %r|/$| =~ @dest
+ @env = {
+ :src => @src,
+ :dest => @dest
+ }
end
def run
Pathname::glob( "#{@src}/**/{.*,*}" ) do |src|
relative = src.to_s[@src.size, src.to_s.size]
+ @env[:src_file] = relative
+ @env[:dest_file] = relative.dup
if src.directory?
Pathname::new( @dest + relative ).mkpath
else
case src.to_s
when /\.rego$/
- r = relative.sub( /\.rego$/, '' )
+ @env[:dest_file] = relative.sub( /\.rego$/, '' )
begin
- next if !@opt[:force] && (src.mtime < Pathname::new( @dest + r ).mtime)
+ next if !@opt[:force] && (src.mtime < Pathname::new( dest_file( @env ) ).mtime)
rescue Errno::ENOENT
end
$stderr.puts relative if @opt[:verbose]
- processing( src, @dest, r, :template )
+ processing( @env, :template )
when /\.ignore$/
# ignore this file
else
- processing( src, @dest, relative, :symlink )
+ processing( @env, :symlink )
end
end
end
end
- def processing( src_file, dest_path, relative_file, filter )
+ private
+ def src_file( env )
+ env[:src] + env[:src_file]
+ end
+
+ def dest_file( env )
+ env[:dest] + env[:dest_file]
+ end
+
+ def processing( env, filter )
result = ''
begin
- File::open( src_file, 'r:utf-8' ) do |f|
- result = __send__( filter, f.read, relative_file )
+ File::open( src_file( env ), 'r:utf-8' ) do |f|
+ result = __send__( filter, f.read, env )
end
- File::open( dest_path + relative_file, 'w' ) do |f|
+ File::open( dest_file( env ), 'w' ) do |f|
f.write( result )
end
rescue MakeLink
- Pathname::new(dest_path + relative_file).make_symlink( src_file ) rescue Errno::EEXIST
+ Pathname::new( dest_file( env ) ).make_symlink( src_file( env ) ) rescue Errno::EEXIST
end
end
- def template( tmpl, relative )
- tmpl.insert( 0, "do_template {\nrelative '#{relative}'\n" ) << "\n}"
+ def template( tmpl, env )
+ tmpl.insert( 0, "do_template( env ) {\n" ) << "\n}"
instance_eval( tmpl )
end
- def do_template( &block )
- REGO::Block::Template::new( &block ).result
+ def do_template( env, &block )
+ REGO::Block::Template::new( env, &block ).result
end
- def dead_copy( tmpl, relative )
+ def dead_copy( tmpl, env )
tmpl
end
- def symlink( tmpl, relative )
+ def symlink( tmpl, env )
raise MakeLink::new
end
end
@@ -7,16 +7,13 @@
module REGO::Block
class Template
- def initialize( &block )
+ def initialize( env, &block )
+ @env = env
@child = []
instance_eval( &block ) if block_given?
self
end
- def relative( file_name )
- @@relative = file_name.sub( %r|/index.html$|, '/' )
- end
-
def result
@child.join( "\n" )
end
@@ -25,7 +22,7 @@ def method_missing( name, *args, &block )
if block
begin
klass = REGO::Block::const_get( name.to_s.capitalize )
- @child << klass::new( &block ).result
+ @child << klass::new( @env, &block ).result
rescue NameError
begin
require name.to_s

0 comments on commit cf3b26c

Please sign in to comment.