Class: Crawling::Instance

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

Instance Method Summary collapse

Constructor Details

#initialize(config_dir: nil, home_dir: nil, merge_app: nil) ⇒ Instance

Returns a new instance of Instance.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crawling.rb', line 54

def initialize(config_dir: nil, home_dir: nil, merge_app: nil)
  @home_dir = home_dir || ENV['HOME']
  @config_dir = config_dir || "#{@home_dir}/.config/crawling"
  @config_pathname = Pathname.new(@config_dir).expand_path
  @merge_app = merge_app || 'nvim -d %s %h'

  stores = {
    'home' => @home_dir,
    # 'system' => '/',
  }
  @stores = stores.map do |store_dir, sys_dir|
    store_dir = File.join(@config_dir, store_dir)
    Store.new store_dir, sys_dir
  end
end

Instance Method Details

#add(paths) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/crawling.rb', line 78

def add paths
  raise "add command requires paths" if paths.empty?

  paths.each do |path|
    raise "path #{path} does not exist" unless File.exist? path

    each_with_storage_path(files_from path) do |file, storage_file|
      Crawling.copy_file file, storage_file
    end
  end
end

#cdObject



70
71
72
73
74
75
76
# File 'lib/crawling.rb', line 70

def cd
  FileUtils.mkdir_p @config_dir unless Dir.exist? @config_dir
  Dir.chdir @config_dir
  puts "creating shell in #{@config_dir}, type exit or ctrl-D to exit"
  system ENV['SHELL']
  puts "crawling shell exited"
end

#diff(paths = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/crawling.rb', line 108

def diff paths = nil
  each_with_storage_path(files_from_paths_or_all paths) do |file, storage_file|
    missing_from = file_or_storage_file_doesnt_exist file, storage_file
    if missing_from
      puts "#{file}: doesn't exist in #{missing_from}"
      next
    end

    diff = get_diff storage_file, file
    unless diff == ''
      puts "#{file}:"
      puts diff
      puts
    end
  end
end

#get(paths) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/crawling.rb', line 90

def get paths
  raise "get command requires paths" if paths.empty?

  each_with_storage_path(paths) do |path, storage_path|
    raise "path #{path} does not exist in storage" unless File.exist? storage_path

    files_from(storage_path).each do |storage_file|
      if storage_file == storage_path
        Crawling.copy_file storage_file, path
      else
        # path was a directory so recalculate new system path
        path_offset = storage_file[storage_path.length..-1]
        Crawling.copy_file storage_file, path + path_offset
      end
    end
  end
end

#merge(paths = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/crawling.rb', line 125

def merge paths = nil
  each_with_storage_path(files_from_paths_or_all paths) do |file, storage_file|
    missing_from = file_or_storage_file_doesnt_exist file, storage_file
    if missing_from
      case missing_from
      when 'system'
        puts "#{file}: creating in system from store"
        Crawling.copy_file storage_file, file
      when 'store'
        puts "#{file}: creating in store from system"
        Crawling.copy_file file, storage_file
      else
        puts "#{file}: does not exist in system or store"
      end

      next
    end

    while (diff_string = get_diff storage_file, file) != ''
      print "#{file}: [a]dd to store, [g]et from store, [d]iff, [m]erge, [s]kip? "
      answer = STDIN.gets.chomp
      case answer
      when 'a'
        Crawling.copy_file file, storage_file
        break
      when 'g'
        Crawling.copy_file storage_file, file
        break
      when 'd'
        puts diff_string
        puts
        redo
      when 'm'
        system *@merge_app.sub('%s', storage_file).sub('%h', file).split(' ')
      when 's'
        break
      else
        puts 'please answer with a, d, g, m, or s'
        redo
      end
    end
  end
end

#pullObject



169
170
171
172
# File 'lib/crawling.rb', line 169

def pull
  Dir.chdir @config_dir
  system 'git pull'
end