Method: TTY::File.create_directory

Defined in:
lib/tty/file.rb

.create_directory(destination, *args, context: nil, verbose: true, color: :green, noop: false, force: false, skip: false, quiet: true) ⇒ void

This method returns an undefined value.

Create directory structure

Examples:

create_directory("/path/to/dir")
tree =
  "app" => [
    "README.md",
    ["Gemfile", "gem "tty-file""],
    "lib" => [
      "cli.rb",
      ["file_utils.rb", "require "tty-file""]
    ]
    "spec" => []
  ]

create_directory(tree)

Parameters:

  • the path or data structure describing directory tree

  • (defaults to: nil)

    the context for template evaluation

  • (defaults to: true)

    when true leaves prompt output, otherwise clears

  • (defaults to: false)

    when true overwrites existing files, false by default

  • (defaults to: false)

    when true skips this action

  • (defaults to: true)

    when true displays logging information

  • (defaults to: :green)

    the name for the color to format display message, :green by default

API:

  • public



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/tty/file.rb', line 187

def create_directory(destination, *args, context: nil, verbose: true,
                     color: :green, noop: false, force: false, skip: false,
                     quiet: true)
  parent = args.size.nonzero? ? args.pop : nil
  if destination.is_a?(String) || destination.is_a?(Pathname)
    destination = { destination.to_s => [] }
  end

  destination.each do |dir, files|
    path = parent.nil? ? dir : ::File.join(parent, dir)
    unless ::File.exist?(path)
      ::FileUtils.mkdir_p(path)
      log_status(:create, path, verbose: verbose, color: color)
    end

    files.each do |filename, contents|
      if filename.respond_to?(:each_pair)
        create_directory(filename, path, context: context,
                         verbose: verbose, color: color, noop: noop,
                         force: force, skip: skip, quiet: quiet)
      else
        create_file(::File.join(path, filename), contents, context: context,
                    verbose: verbose, color: color, noop: noop, force: force,
                    skip: skip, quiet: quiet)
      end
    end
  end
end