Class: Cejo::Ops::Dots

Inherits:
Object
  • Object
show all
Defined in:
lib/cejo/ops/dots.rb

Overview

Mirror Lar files in $HOME.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(utils, root) ⇒ Dots

Returns a new instance of Dots.



12
13
14
15
16
17
# File 'lib/cejo/ops/dots.rb', line 12

def initialize(utils, root)
  @utils = utils
  @root = Pathname.new root if root
  @home = Pathname.new Dir.home
  @target_link = {}
end

Instance Attribute Details

#homeObject (readonly)

Returns the value of attribute home.



10
11
12
# File 'lib/cejo/ops/dots.rb', line 10

def home
  @home
end

#ignore_theseObject (readonly)

Returns the value of attribute ignore_these.



10
11
12
# File 'lib/cejo/ops/dots.rb', line 10

def ignore_these
  @ignore_these
end

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/cejo/ops/dots.rb', line 10

def root
  @root
end

Returns the value of attribute target_link.



10
11
12
# File 'lib/cejo/ops/dots.rb', line 10

def target_link
  @target_link
end

#utilsObject (readonly)

Returns the value of attribute utils.



10
11
12
# File 'lib/cejo/ops/dots.rb', line 10

def utils
  @utils
end

Instance Method Details

#backup_filesObject



71
72
73
74
75
76
# File 'lib/cejo/ops/dots.rb', line 71

def backup_files
  target_link.each do |target, link_name|
    puts "#{target}#{link_name}"
    backup_this link_name
  end
end

#backup_this(this) ⇒ Object

Move file from home to a /home/backup/file or delete it if the file it is pointing does not exist



66
67
68
69
# File 'lib/cejo/ops/dots.rb', line 66

def backup_this(this)
  warn "WARNING: #{this} found! Deleting/Moving it."
  this.delete if this.exist? # TODO: if file exist back/delete up it
end


55
56
57
58
59
60
61
62
# File 'lib/cejo/ops/dots.rb', line 55

def feed_target_link
  root_files_folders[:files].each do |target|
    next if ignored_ones.include? target.basename.to_s # TODO: .reject ignored_ones

    symlink_name = to_home target
    target_link.store(target, symlink_name)
  end
end

#ignored_onesObject

ignore these ones



86
87
88
# File 'lib/cejo/ops/dots.rb', line 86

def ignored_ones
  ['LICENSE', root.join('.git').to_path.to_s].freeze
end

#make_foldersObject

Create only the folders, if those do not exist



45
46
47
48
49
50
51
52
53
# File 'lib/cejo/ops/dots.rb', line 45

def make_folders
  root_files_folders[:folders].each do |fld|
    folder = to_home fld
    next if folder.exist?

    puts folder
    folder.mkdir
  end
end

#root_files_foldersObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cejo/ops/dots.rb', line 19

def root_files_folders
  files = []
  folders = []

  Find.find(root) do |file|
    file = Pathname.new file
    next if file.to_path.include? '.git'
    next if file == root

    files << file if file.file?
    folders << file if file.directory?
  end

  { folders: folders, files: files }
end

#runObject



90
91
92
93
94
95
# File 'lib/cejo/ops/dots.rb', line 90

def run
  feed_target_link
  make_folders
  backup_files
  symlink_files
end


78
79
80
81
82
83
# File 'lib/cejo/ops/dots.rb', line 78

def symlink_files
  target_link.each do |target, link_name|
    puts "#{target}#{link_name}"
    link_name.make_symlink target # As enumerator yielding folder to symlink
  end
end

#to_home(this) ⇒ Object

transform origin file absolute path with home as its root instead /a/b/c.tar –> /home/b/c.tar



37
38
39
40
41
42
# File 'lib/cejo/ops/dots.rb', line 37

def to_home(this)
  origin = this.to_path
  homey = home.to_path.concat('/')
  result = origin.gsub(root.to_path, homey)
  Pathname.new result
end