Module: Lookfile

Defined in:
lib/lookfile.rb,
lib/lookfile/version.rb

Overview

main module of lookfile gem

Constant Summary collapse

BASE_DIR =
'~/'.freeze
LOOKFILE_DIR =
'.lookfile'.freeze
VERSION =
'0.1.5'.freeze

Class Method Summary collapse

Class Method Details

.add_files(files_path, base_dir = BASE_DIR) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lookfile.rb', line 20

def add_files(files_path, base_dir = BASE_DIR)
  files_path = [files_path] unless files_path.is_a?(Array)
  files_path = files_path.map { |file_path| File.expand_path(file_path) }
  added_files = []
  error_files = []
  files_path.each do |file_path|
    list = add_one_file(file_path, base_dir) ? added_files : error_files
    list << file_path
  end
  [added_files, error_files]
end

.add_one_file(file_path, base_dir = BASE_DIR) ⇒ Object



68
69
70
71
72
# File 'lib/lookfile.rb', line 68

def add_one_file(file_path, base_dir = BASE_DIR)
  lookfile_dir = load_lookfile_dir(base_dir)
  folder_path = lookfile_dir + file_path.scan(%r{(.+)\/}).flatten.first
  cp_file(file_path, folder_path)
end

.cp_file(file_path, folder_path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lookfile.rb', line 74

def cp_file(file_path, folder_path)
  FileUtils.mkpath(folder_path)
  begin
    FileUtils.cp(file_path, folder_path)
    true
  rescue => error
    puts folder_path
    puts error
    false
  end
end

.initialize(base_dir = BASE_DIR) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/lookfile.rb', line 12

def initialize(base_dir = BASE_DIR)
  lookfile_dir = load_lookfile_dir(base_dir)
  return nil if File.directory?(lookfile_dir)
  Dir.mkdir(lookfile_dir)
  Git.init(base_dir)
  lookfile_dir
end

.list_files(base_dir = BASE_DIR) ⇒ Object



86
87
88
89
90
91
# File 'lib/lookfile.rb', line 86

def list_files(base_dir = BASE_DIR)
  lookfile_dir = load_lookfile_dir(base_dir)
  files_regex = %r{^#{lookfile_dir}(?!\/.git)(.+)$}
  files_path = `find #{lookfile_dir} -type f`.scan(files_regex).flatten
  files_path
end

.load_lookfile_dir(base_dir = BASE_DIR) ⇒ Object



102
103
104
105
106
# File 'lib/lookfile.rb', line 102

def load_lookfile_dir(base_dir = BASE_DIR)
  base_dir = File.expand_path(base_dir)
  base_dir += '/' if base_dir[-1] != '/'
  base_dir + LOOKFILE_DIR
end

.push(base_dir = BASE_DIR) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/lookfile.rb', line 42

def push(base_dir = BASE_DIR)
  update_files(base_dir)
  message = Git.commit(base_dir)
  return 'Nothing to update' if message.nil?
  Git.push(base_dir) if Git.remote?(base_dir)
  message
end

.restore(files_path = [], base_dir = BASE_DIR) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lookfile.rb', line 50

def restore(files_path = [], base_dir = BASE_DIR)
  user_path = File.expand_path('~')
  lookfile_dir = load_lookfile_dir(base_dir)
  files_path = [files_path] unless files_path.is_a?(Array)
  message = 'Restore Files:'
  files_path.each do |file|
    path = file.gsub(%r{\/home\/[^\/]+}, user_path)
    folder = path.scan(%r{(.+)+\/}).flatten.first
    message += "\n #{path}" if cp_file(lookfile_dir + file, folder)
  end
  message
end

.set_repository(repository_ssh_name, base_dir = BASE_DIR) ⇒ Object



63
64
65
66
# File 'lib/lookfile.rb', line 63

def set_repository(repository_ssh_name, base_dir = BASE_DIR)
  Git.set_remote(repository_ssh_name, base_dir)
  Git.rebase(base_dir)
end

.show(base_dir = BASE_DIR) ⇒ Object



32
33
34
35
# File 'lib/lookfile.rb', line 32

def show(base_dir = BASE_DIR)
  files_path = list_files(base_dir)
  show_files('Files on lookfile:', files_path)
end

.show_files(header_message, files_path) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/lookfile.rb', line 93

def show_files(header_message, files_path)
  message = header_message.to_s unless files_path.empty?
  files_path.each do |file_path|
    message += "\n  #{file_path}"
  end
  message ||= ''
  message
end

.status(base_dir = BASE_DIR) ⇒ Object



37
38
39
40
# File 'lib/lookfile.rb', line 37

def status(base_dir = BASE_DIR)
  update_files(base_dir)
  Git.status(base_dir)
end

.update_files(base_dir = BASE_DIR) ⇒ Object



108
109
110
111
112
113
# File 'lib/lookfile.rb', line 108

def update_files(base_dir = BASE_DIR)
  lookfile_dir = load_lookfile_dir(base_dir)
  files_regex = %r{^#{lookfile_dir}(?!\/.git)(.+)$}
  files_path = `find #{lookfile_dir} -type f`.scan(files_regex).flatten
  add_files(files_path, base_dir)
end