Sha256: 306edca1797e5eb4a61d0d5c288ba3e3176e45d3f07605cb5a5c08d1acb18479

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'fileutils'

class FolderCopier
    def initialize(source_path, destination_path)
        @source_path = source_path
        @destination_path = destination_path
    end

    def copy(delete_if_exists: true)
        if File.exist?(@destination_path)
            if delete_if_exists
                FileUtils.rm_rf(@destination_path)
            end
        end
        FileUtils.mkdir_p(@destination_path)
        copy_files_and_folders(@source_path, @destination_path)
    end

    private

    def copy_files_and_folders(source, destination)
        if File.file?(source)
            FileUtils.cp_r(source, destination)
        elsif File.directory?(source)
            Dir.foreach(source) do |item|
                next if item == '.' || item == '..'
                source_item_path = File.join(source, item)
                destination_item_path = File.join(destination, item)

                if File.directory?(source_item_path)
                    FileUtils.mkdir_p(destination_item_path) unless File.directory?(destination_item_path)
                    copy_files_and_folders(source_item_path, destination_item_path)
                else
                    FileUtils.cp_r(source_item_path, destination_item_path)
                end
            end
        else
            raise "Source is not a file or directory: #{source}"
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solara-0.2.0 solara/lib/core/scripts/folder_copier.rb
solara-0.1.0 solara/lib/core/scripts/folder_copier.rb