Class: Homesick::CLI

Inherits:
Thor
  • Object
show all
Includes:
Actions::FileActions, Actions::GitActions, Utils, Version, Thor::Actions
Defined in:
lib/homesick/cli.rb

Overview

Homesick’s command line interface

Constant Summary

Constants included from Utils

Utils::PRETENDABLE, Utils::QUIETABLE

Constants included from Version

Version::MAJOR, Version::MINOR, Version::PATCH, Version::STRING

Constants included from Actions::GitActions

Actions::GitActions::MIN_VERSION, Actions::GitActions::STRING

Instance Method Summary collapse

Methods included from Actions::GitActions

#git_add, #git_clone, #git_commit_all, #git_diff, #git_init, #git_pull, #git_push, #git_remote_add, #git_status, #git_submodule_init, #git_submodule_update, #git_version_correct?

Methods included from Actions::FileActions

#handle_symlink_action, #ln_s, #mv, #rm, #rm_link, #rm_r, #rm_rf

Constructor Details

#initialize(args = [], options = {}, config = {}) ⇒ CLI

Returns a new instance of CLI.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/homesick/cli.rb', line 21

def initialize(args = [], options = {}, config = {})
  super
  # Check if git is installed
  unless git_version_correct?
    say_status :error, "Git version >= #{Homesick::Actions::GitActions::STRING} must be installed to use Homesick", :red
    exit(1)
  end
  # Hack in support for diffing symlinks
  # Also adds support for checking if destination or content is a directory
  shell_metaclass = class << shell; self; end
  shell_metaclass.send(:define_method, :show_diff) do |destination, source|
    destination = Pathname.new(destination)
    source = Pathname.new(source)
    return 'Unable to create diff: destination or content is a directory' if destination.directory? || source.directory?
    return super(destination, File.binread(source)) unless destination.symlink?
    say "- #{destination.readlink}", :red, true
    say "+ #{source.expand_path}", :green, true
  end
end

Instance Method Details

#cd(castle = DEFAULT_CASTLE_NAME) ⇒ Object



258
259
260
261
262
263
264
265
266
267
# File 'lib/homesick/cli.rb', line 258

def cd(castle = DEFAULT_CASTLE_NAME)
  check_castle_existance castle, 'cd'
  castle_dir = repos_dir.join(castle)
  say_status "cd #{castle_dir.realpath}",
             "Opening a new shell in castle '#{castle}'. To return to the original one exit from the new shell.",
             :green
  inside castle_dir do
    system(ENV['SHELL'])
  end
end

#clone(uri, destination = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/homesick/cli.rb', line 42

def clone(uri, destination=nil)
  destination = Pathname.new(destination) unless destination.nil?

  inside repos_dir do
    if File.exist?(uri)
      uri = Pathname.new(uri).expand_path
      fail "Castle already cloned to #{uri}" if uri.to_s.start_with?(repos_dir.to_s)

      destination = uri.basename if destination.nil?

      ln_s uri, destination
    elsif uri =~ GITHUB_NAME_REPO_PATTERN
      destination = Pathname.new(uri).basename if destination.nil?
      git_clone "https://github.com/#{Regexp.last_match[1]}.git",
                destination: destination
    elsif uri =~ /%r([^%r]*?)(\.git)?\Z/ || uri =~ /[^:]+:([^:]+)(\.git)?\Z/
      destination = Pathname.new(Regexp.last_match[1].gsub(/\.git$/, '')).basename if destination.nil?
      git_clone uri, destination: destination
    else
      fail "Unknown URI format: #{uri}"
    end

    setup_castle(destination)
  end
end

#commit(name = DEFAULT_CASTLE_NAME, message = nil) ⇒ Object



105
106
107
# File 'lib/homesick/cli.rb', line 105

def commit(name = DEFAULT_CASTLE_NAME, message = nil)
  commit_castle name, message
end

#destroy(name) ⇒ Object



249
250
251
252
253
254
255
# File 'lib/homesick/cli.rb', line 249

def destroy(name)
  check_castle_existance name, 'destroy'
  return unless shell.yes?('This will destroy your castle irreversible! Are you sure?')

  unlink(name)
  rm_rf repos_dir.join(name)
end

#diff(castle = DEFAULT_CASTLE_NAME) ⇒ Object



215
216
217
218
219
220
# File 'lib/homesick/cli.rb', line 215

def diff(castle = DEFAULT_CASTLE_NAME)
  check_castle_existance(castle, 'diff')
  inside repos_dir.join(castle) do
    git_diff
  end
end

#exec(castle, *args) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/homesick/cli.rb', line 291

def exec(castle, *args)
  check_castle_existance castle, 'exec'
  unless args.count > 0
    say_status :error,
               'You must pass a shell command to execute',
               :red
    exit(1)
  end
  full_command = args.join(' ')
  say_status "exec '#{full_command}'",
             "#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'",
             :green
  inside repos_dir.join(castle) do
    system(full_command)
  end
end

#exec_all(*args) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/homesick/cli.rb', line 310

def exec_all(*args)
  unless args.count > 0
    say_status :error,
               'You must pass a shell command to execute',
               :red
    exit(1)
  end
  full_command = args.join(' ')
  inside_each_castle do |castle|
    say_status "exec '#{full_command}'",
               "#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'",
               :green
    system(full_command)
  end
end

#generate(castle) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/homesick/cli.rb', line 229

def generate(castle)
  castle = Pathname.new(castle).expand_path

  github_user = `git config github.user`.chomp
  github_user = nil if github_user == ''
  github_repo = castle.basename

  empty_directory castle
  inside castle do
    git_init
    if github_user
      url = "[email protected]:#{github_user}/#{github_repo}.git"
      git_remote_add 'origin', url
    end

    empty_directory 'home'
  end
end


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/homesick/cli.rb', line 136

def link(name = DEFAULT_CASTLE_NAME)
  check_castle_existance(name, 'symlink')

  inside castle_dir(name) do
    subdirs = subdirs(name)

    # link files
    symlink_each(name, castle_dir(name), subdirs)

    # link files in subdirs
    subdirs.each do |subdir|
      symlink_each(name, subdir, subdirs)
    end
  end
end

#listObject



198
199
200
201
202
203
204
# File 'lib/homesick/cli.rb', line 198

def list
  inside_each_castle do |castle|
    say_status castle.relative_path_from(repos_dir).to_s,
               `git config remote.origin.url`.chomp,
               :cyan
  end
end

#open(castle = DEFAULT_CASTLE_NAME) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/homesick/cli.rb', line 271

def open(castle = DEFAULT_CASTLE_NAME)
  unless ENV['EDITOR']
    say_status :error,
               'The $EDITOR environment variable must be set to use this command',
               :red

    exit(1)
  end
  check_castle_existance castle, 'open'
  castle_dir = repos_dir.join(castle)
  say_status "#{castle_dir.realpath}: #{ENV['EDITOR']} .",
             "Opening the root directory of castle '#{castle}' in editor '#{ENV['EDITOR']}'.",
             :green
  inside castle_dir do
    system("#{ENV['EDITOR']} .")
  end
end

#pull(name = DEFAULT_CASTLE_NAME) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/homesick/cli.rb', line 93

def pull(name = DEFAULT_CASTLE_NAME)
  if options[:all]
    inside_each_castle do |castle|
      say castle.to_s.gsub(repos_dir.to_s + '/', '') + ':'
      update_castle castle
    end
  else
    update_castle name
  end
end

#push(name = DEFAULT_CASTLE_NAME) ⇒ Object



110
111
112
# File 'lib/homesick/cli.rb', line 110

def push(name = DEFAULT_CASTLE_NAME)
  push_castle name
end

#rc(name = DEFAULT_CASTLE_NAME) ⇒ Object



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

def rc(name = DEFAULT_CASTLE_NAME)
  inside repos_dir do
    destination = Pathname.new(name)
    homesickrc = destination.join('.homesickrc').expand_path
    return unless homesickrc.exist?
    proceed = options[:force] || shell.yes?("#{name} has a .homesickrc. Proceed with evaling it? (This could be destructive)")
    return say_status 'eval skip', "not evaling #{homesickrc}, #{destination} may need manual configuration", :blue unless proceed
    say_status 'eval', homesickrc
    inside destination do
      eval homesickrc.read, binding, homesickrc.expand_path.to_s
    end
  end
end

#show_path(castle = DEFAULT_CASTLE_NAME) ⇒ Object



223
224
225
226
# File 'lib/homesick/cli.rb', line 223

def show_path(castle = DEFAULT_CASTLE_NAME)
  check_castle_existance(castle, 'show_path')
  say repos_dir.join(castle)
end

#status(castle = DEFAULT_CASTLE_NAME) ⇒ Object



207
208
209
210
211
212
# File 'lib/homesick/cli.rb', line 207

def status(castle = DEFAULT_CASTLE_NAME)
  check_castle_existance(castle, 'status')
  inside repos_dir.join(castle) do
    git_status
  end
end

#track(file, castle = DEFAULT_CASTLE_NAME) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/homesick/cli.rb', line 153

def track(file, castle = DEFAULT_CASTLE_NAME)
  castle = Pathname.new(castle)
  file = Pathname.new(file.chomp('/'))
  check_castle_existance(castle, 'track')

  absolute_path = file.expand_path
  relative_dir = absolute_path.relative_path_from(home_dir).dirname
  castle_path = Pathname.new(castle_dir(castle)).join(relative_dir)
  FileUtils.mkdir_p castle_path

  # Are we already tracking this or anything inside it?
  target = Pathname.new(castle_path.join(file.basename))
  if target.exist?
    if absolute_path.directory?
      move_dir_contents(target, absolute_path)
      absolute_path.rmtree
      subdir_remove(castle, relative_dir + file.basename)

    elsif more_recent? absolute_path, target
      target.delete
      mv absolute_path, castle_path
    else
      say_status(:track,
                 "#{target} already exists, and is more recent than #{file}. Run 'homesick SYMLINK CASTLE' to create symlinks.",
                 :blue)
    end
  else
    mv absolute_path, castle_path
  end

  inside home_dir do
    absolute_path = castle_path + file.basename
    home_path = home_dir + relative_dir + file.basename
    ln_s absolute_path, home_path
  end

  inside castle_path do
    git_add absolute_path
  end

  # are we tracking something nested? Add the parent dir to the manifest
  subdir_add(castle, relative_dir) unless relative_dir.eql?(Pathname.new('.'))
end


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/homesick/cli.rb', line 115

def unlink(name = DEFAULT_CASTLE_NAME)
  check_castle_existance(name, 'symlink')

  inside castle_dir(name) do
    subdirs = subdirs(name)

    # unlink files
    unsymlink_each(name, castle_dir(name), subdirs)

    # unlink files in subdirs
    subdirs.each do |subdir|
      unsymlink_each(name, subdir, subdirs)
    end
  end
end

#versionObject



327
328
329
# File 'lib/homesick/cli.rb', line 327

def version
  say Homesick::Version::STRING
end