Skip to content

Commit

Permalink
Yield the filename on directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 7, 2009
1 parent 80d902e commit 3c46894
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

* Improve spec coverage for Thor::Runner

== 0.11.x, released 2009-07-01
== 0.12, released 2009-11-06

* [#7] Do not force white color on status
* [#8] Yield a block with the filename on directory

== 0.11, released 2009-07-01

* Added a rake compatibility layer. It allows you to use spec and rdoc tasks on
Thor classes.
Expand Down
1 change: 1 addition & 0 deletions lib/thor/actions/create_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def invoke!
FileUtils.mkdir_p(File.dirname(destination))
File.open(destination, 'w'){ |f| f.write render }
end
given_destination
end

protected
Expand Down
14 changes: 9 additions & 5 deletions lib/thor/actions/directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ module Actions
# directory "doc"
# directory "doc", "docs", :recursive => false
#
def directory(source, destination=nil, config={})
action Directory.new(self, source, destination || source, config)
def directory(source, destination=nil, config={}, &block)
action Directory.new(self, source, destination || source, config, &block)
end

class Directory < EmptyDirectory #:nodoc:
attr_reader :source

def initialize(base, source, destination=nil, config={})
def initialize(base, source, destination=nil, config={}, &block)
@source = File.expand_path(base.find_in_source_paths(source.to_s))
@block = block
super(base, destination, { :recursive => true }.merge(config))
end

Expand All @@ -70,16 +71,19 @@ def execute!
Dir[lookup].each do |file_source|
next if File.directory?(file_source)
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
file_destination.gsub!('/./', '/')

case file_source
when /\.empty_directory$/
dirname = File.dirname(file_destination).gsub(/\/\.$/, '')
next if dirname == given_destination
base.empty_directory(dirname, config)
when /\.tt$/
base.template(file_source, file_destination[0..-4], config)
destination = base.template(file_source, file_destination[0..-4], config)
@block.call(destination) if @block
else
base.copy_file(file_source, file_destination, config)
destination = base.copy_file(file_source, file_destination, config)
@block.call(destination) if @block
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/thor/actions/empty_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def invoke!
def revoke!
say_status :remove, :red
::FileUtils.rm_rf(destination) if !pretend? && exists?
given_destination
end

protected
Expand Down
4 changes: 2 additions & 2 deletions spec/actions/create_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def silence!
invoke!.must be_empty
end

it "returns the destination" do
it "returns the given destination" do
capture(:stdout) do
create_file("doc/config.rb").invoke!.must == File.join(destination_root, "doc/config.rb")
create_file("doc/config.rb").invoke!.must == "doc/config.rb"
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/actions/directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def exists_and_identical?(source_path, destination_path)
content.must =~ /create doc\/rdoc\.rb/
content.must =~ /create doc\/components/
end

it "yields a block" do
invoke!("doc") do |f|
%(doc/README doc/config.rb doc/rdoc.rb).must include(f)
%(doc/components/).must_not include(f)
end
end
end

describe "#revoke!" do
Expand Down

0 comments on commit 3c46894

Please sign in to comment.