Method: Thor::Actions#inside
- Defined in:
- lib/thor/actions.rb
#inside(dir = "", config = {}, &block) ⇒ Object
Do something in the root or on a provided subfolder. If a relative path is given it’s referenced from the current root. The full path is yielded to the block you provide. The path is set back to the previous path when the method exits.
Returns the value yielded by the block.
Parameters
- dir<String>
-
the directory to move to.
- config<Hash>
-
give :verbose => true to log and use padding.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/thor/actions.rb', line 170 def inside(dir = "", config = {}, &block) verbose = config.fetch(:verbose, false) pretend = [:pretend] say_status :inside, dir, verbose shell.padding += 1 if verbose @destination_stack.push File.(dir, destination_root) # If the directory doesn't exist and we're not pretending if !File.exist?(destination_root) && !pretend require "fileutils" FileUtils.mkdir_p(destination_root) end result = nil if pretend # In pretend mode, just yield down to the block result = block.arity == 1 ? yield(destination_root) : yield else require "fileutils" FileUtils.cd(destination_root) { result = block.arity == 1 ? yield(destination_root) : yield } end @destination_stack.pop shell.padding -= 1 if verbose result end |