def copy_directory(source_path, *args, **options, &block)
      source_path = source_path.to_s
      check_path(source_path)
      source = escape_glob_path(source_path)
      dest_path = (args.first || source).to_s
      opts = {recursive: true}.merge(options)
      pattern = opts[:recursive] ? ::File.join(source, '**') : source
      glob_pattern = ::File.join(pattern, '*')

      Dir.glob(glob_pattern, ::File::FNM_DOTMATCH).sort.each do |file_source|
        next if ::File.directory?(file_source)
        next if opts[:exclude] && file_source.match(opts[:exclude])

        dest = ::File.join(dest_path, file_source.gsub(source_path, '.'))
        file_dest = ::Pathname.new(dest).cleanpath.to_s

        copy_file(file_source, file_dest, **options, &block)
      end
    end
def copy_directory(source_path, *args, **options, &block)
      source_path = source_path.to_s
      check_path(source_path)
      source = escape_glob_path(source_path)
      dest_path = (args.first || source).to_s
      opts = {recursive: true}.merge(options)
      pattern = opts[:recursive] ? ::File.join(source, '**') : source
      glob_pattern = ::File.join(pattern, '*')

      Dir.glob(glob_pattern, ::File::FNM_DOTMATCH).sort.each do |file_source|
        next if ::File.directory?(file_source)
        next if opts[:exclude] && file_source.match(opts[:exclude])

        dest = ::File.join(dest_path, file_source.gsub(source_path, '.'))
        file_dest = ::Pathname.new(dest).cleanpath.to_s

        copy_file(file_source, file_dest, **options, &block)
      end
    end
Copy directory recursively from source to destination path

 Any files names wrapped within % sign will be expanded by
 executing corresponding method and inserting its value.
 Assuming the following directory structure:

  app/
    %name%.rb
    command.rb.erb
    README.md

  Invoking:
    copy_directory("app", "new_app")
  The following directory structure should be created where
  name resolves to 'cli' value:

  new_app/
    cli.rb
    command.rb
    README

 @param [String, Pathname] source_path
 @param [Hash[Symbol]] options
 @option options [Symbol] :preserve
   If true, the owner, group, permissions and modified time
   are preserved on the copied file, defaults to false.
 @option options [Symbol] :recursive
   If false, copies only top level files, defaults to true.
 @option options [Symbol] :exclude
   A regex that specifies files to ignore when copying.

 @example
   copy_directory("app", "new_app", recursive: false)
   copy_directory("app", "new_app", exclude: /docs/)

 @api public