class FileManager

Constants

CURRENT_BOX_PATH
PANDORA_PATH

Public Class Methods

add_box_bundle(box) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 22
def add_box_bundle(box)
  generate_basic_folders
  download_box(box)
  generate_from_array(['custom_boxes/'])
  Dir.glob("#{PANDORA_PATH}staging/#{repo_dir_name(box)}/boxes/*").each do |file|
    FileUtils.move(file, "#{PANDORA_PATH}custom_boxes/#{File.basename(file)}")
  end
end
backup(target, backup_dest) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 109
def backup(target, backup_dest)
  if File.exists?(backup_dest)
    FileUtils.rm_rf(backup_dest)
  end

  FileUtils.copy(target, backup_dest)
end
camelize(term) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 31
def camelize(term)
  term.split('_').each { |s| s.capitalize! }.join('')
end
current_box=(box) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 105
def current_box=(box)
  File.open(CURRENT_BOX_PATH, 'w') { |file| file.write(box) }
end
download_box(repo) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 57
def download_box(repo)
  Dir.chdir("#{PANDORA_PATH}staging/") do
    repo_dir_name = repo_dir_name(repo)
    if `ls`.include?(repo_dir_name)
      Dir.chdir(repo_dir_name) do
        puts `git pull`
      end
    else
      puts `git clone https://github.com/#{repo}`
    end
  end
end
generate_backup_folder(subdir_name) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 117
def generate_backup_folder(subdir_name)
  generate_from_array(["backups/#{subdir_name}/"])
end
generate_basic_folders() click to toggle source
# File lib/pandoras_box/file_manager.rb, line 17
def generate_basic_folders
  FileManager.generate_backup_folder(@name)
  FileManager.generate_staging_folder
end
generate_from_array(structure, subdir_name=nil) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 125
def generate_from_array(structure, subdir_name=nil)
  structure.each do |path|
    if subdir_name
      path = "boxes/#{subdir_name}/#{path}"
    end
    if path.reverse[0] == '/'
      FileUtils.mkpath("#{PANDORA_PATH}#{path}")
    else
      FileUtils.touch("#{PANDORA_PATH}#{path}")
    end
  end
end
generate_staging_folder() click to toggle source
# File lib/pandoras_box/file_manager.rb, line 121
def generate_staging_folder
  generate_from_array(['staging/'])
end
get_current_box() click to toggle source
# File lib/pandoras_box/file_manager.rb, line 95
def get_current_box
  if File.exist?(CURRENT_BOX_PATH)
    File.open(CURRENT_BOX_PATH, 'r').read
  else
    FileUtils.touch(CURRENT_BOX_PATH)
    self.current_box ='Default'
    'Default'
  end
end
load_boxes() click to toggle source

Loads the .rb files for the boxes

# File lib/pandoras_box/file_manager.rb, line 9
def load_boxes
  generate_from_array(['custom_boxes/'])
  Dir["#{PANDORA_PATH}custom_boxes/*.rb"].each do |file|
    require file
    Object.const_get(FileManager.camelize(File.basename(file, '.rb'))).new
  end
end
modulify(file, linkable) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 74
def modulify(file, linkable)
  target      = "#{ENV['HOME']}/.#{file}"
  backup_dest = "#{PANDORA_PATH}backups/#{file}"
  if File.exists?(target)
    backup(target, backup_dest)

    if File.file?(linkable)
      dir_move = File.dirname(linkable)
    else
      dir_move = "#{PANDORA_PATH}boxes/#{get_current_box}/#{linkable}"
    end

    if !File.directory?(dir_move) || !File.directory?(dir_move + '/modules/')
      FileUtils.mkpath(dir_move)
      FileUtils.mkpath(dir_move + '/modules/')
    end

    FileUtils.move(target, dir_move + '/modules/' + file)
  end
end
repo_dir_name(repo) click to toggle source
# File lib/pandoras_box/file_manager.rb, line 70
def repo_dir_name(repo)
  repo.match(/\/(.*)/).to_s.delete('/')
end