Class: FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/pandoras_box/file_manager.rb

Constant Summary collapse

PANDORA_PATH =
"#{ENV['HOME']}/.pandorasbox/"
CURRENT_BOX_PATH =
"#{PANDORA_PATH}.current_box"

Class Method Summary collapse

Class Method Details

.add_box_bundle(box) ⇒ Object



22
23
24
25
26
27
28
29
# 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) ⇒ Object



109
110
111
112
113
114
115
# 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) ⇒ Object



31
32
33
# File 'lib/pandoras_box/file_manager.rb', line 31

def camelize(term)
  term.split('_').each { |s| s.capitalize! }.join('')
end

.current_box=(box) ⇒ Object



105
106
107
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



117
118
119
# File 'lib/pandoras_box/file_manager.rb', line 117

def generate_backup_folder(subdir_name)
  generate_from_array(["backups/#{subdir_name}/"])
end

.generate_basic_foldersObject



17
18
19
20
# 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) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# 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_folderObject



121
122
123
# File 'lib/pandoras_box/file_manager.rb', line 121

def generate_staging_folder
  generate_from_array(['staging/'])
end

.get_current_boxObject



95
96
97
98
99
100
101
102
103
# 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_boxesObject

Loads the .rb files for the boxes



9
10
11
12
13
14
15
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



70
71
72
# File 'lib/pandoras_box/file_manager.rb', line 70

def repo_dir_name(repo)
  repo.match(/\/(.*)/).to_s.delete('/')
end


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pandoras_box/file_manager.rb', line 35

def symlink_all(subdir_name, repo)
  ask            = CLI.prompt_boolean('Ask for every symlink?')
  path_to_subdir = "boxes/#{subdir_name}"
  unless repo.empty?
    path_to_subdir = "staging/#{repo_dir_name(repo)}/#{subdir_name}"
  end
  linkables = Dir.glob("#{PANDORA_PATH}#{path_to_subdir}**/*.symlink")

  linkables.each do |linkable|
    file = linkable.split('/').last.split('.symlink').last
    proceed = true
    if ask
      proceed = CLI.prompt_boolean("Link #{file}?")
    end
    if proceed
      target = "#{ENV['HOME']}/.#{file}"
      modulify(file, linkable)
      File.symlink(linkable, target)
    end
  end
end