Module: Homesick::Actions

Included in:
Homesick
Defined in:
lib/homesick/actions.rb

Instance Method Summary collapse

Instance Method Details

#git_add(file, config = {}) ⇒ Object



72
73
74
75
# File 'lib/homesick/actions.rb', line 72

def git_add(file, config = {})
  say_status 'git add file', '', :green unless options[:quiet]
  system "git add #{file}" unless options[:pretend]
end

#git_clone(repo, config = {}) ⇒ Object

TODO move this to be more like thor’s template, empty_directory, etc



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/homesick/actions.rb', line 4

def git_clone(repo, config = {})
  config ||= {}
  destination = config[:destination] || begin
    repo =~ /([^\/]+)(?:\.git)?$/
   $1
  end

  destination = Pathname.new(destination) unless destination.kind_of?(Pathname)
  FileUtils.mkdir_p destination.dirname

  if ! destination.directory?
    say_status 'git clone', "#{repo} to #{destination.expand_path}", :green unless options[:quiet]
    system "git clone -q #{repo} #{destination}" unless options[:pretend]
  else
    say_status :exist, destination.expand_path, :blue unless options[:quiet]
  end
end

#git_commit_all(config = {}) ⇒ Object



67
68
69
70
# File 'lib/homesick/actions.rb', line 67

def git_commit_all(config = {})
  say_status 'git commit all', '', :green unless options[:quiet]
  system "git commit -v -a" unless options[:pretend]
end

#git_init(path = ".") ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/homesick/actions.rb', line 22

def git_init(path = ".")
  path = Pathname.new(path)

  inside path do
    unless path.join('.git').exist?
      say_status 'git init', '' unless options[:quiet]
      system "git init >/dev/null" unless options[:pretend]
    else
      say_status 'git init', 'already initialized', :blue unless options[:quiet]
    end
  end
end

#git_pull(config = {}) ⇒ Object



57
58
59
60
# File 'lib/homesick/actions.rb', line 57

def git_pull(config = {})
  say_status 'git pull', '', :green unless options[:quiet]
  system "git pull --quiet" unless options[:pretend]
end

#git_push(config = {}) ⇒ Object



62
63
64
65
# File 'lib/homesick/actions.rb', line 62

def git_push(config = {})
  say_status 'git push', '', :green unless options[:quiet]
  system "git push" unless options[:pretend]
end

#git_remote_add(name, url) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/homesick/actions.rb', line 35

def git_remote_add(name, url)
  existing_remote = `git config remote.#{name}.url`.chomp
  existing_remote = nil if existing_remote == ''

  unless existing_remote
    say_status 'git remote', "add #{name} #{url}" unless options[:quiet]
    system "git remote add #{name} #{url}" unless options[:pretend]
  else
    say_status 'git remote', "#{name} already exists", :blue unless options[:quiet]
  end
end

#git_submodule_init(config = {}) ⇒ Object



47
48
49
50
# File 'lib/homesick/actions.rb', line 47

def git_submodule_init(config = {})
  say_status 'git submodule', 'init', :green unless options[:quiet]
  system "git submodule --quiet init" unless options[:pretend]
end

#git_submodule_update(config = {}) ⇒ Object



52
53
54
55
# File 'lib/homesick/actions.rb', line 52

def git_submodule_update(config = {})
  say_status 'git submodule', 'update', :green unless options[:quiet]
  system "git submodule --quiet update --init --recursive >/dev/null 2>&1" unless options[:pretend]
end

#ln_s(source, destination, config = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/homesick/actions.rb', line 93

def ln_s(source, destination, config = {})
  source = Pathname.new(source)
  destination = Pathname.new(destination)
  FileUtils.mkdir_p destination.dirname

  if destination.symlink?
    if destination.readlink == source
      say_status :identical, destination.expand_path, :blue unless options[:quiet]
    else
      say_status :conflict, "#{destination} exists and points to #{destination.readlink}", :red unless options[:quiet]

      if options[:force] || shell.file_collision(destination) { source }
        system "ln -nsf #{source} #{destination}" unless options[:pretend]
      end
    end
  elsif destination.exist?
    say_status :conflict, "#{destination} exists", :red unless options[:quiet]

    if options[:force] || shell.file_collision(destination) { source }
      system "ln -sf #{source} #{destination}" unless options[:pretend]
    end
  else
    say_status :symlink, "#{source.expand_path} to #{destination.expand_path}", :green unless options[:quiet]
    system "ln -s #{source} #{destination}" unless options[:pretend]
  end
end

#mv(source, destination, config = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/homesick/actions.rb', line 77

def mv(source, destination, config = {})
  source = Pathname.new(source)
  destination = Pathname.new(destination + source.basename)

  if destination.exist?
    say_status :conflict, "#{destination} exists", :red unless options[:quiet]

    if options[:force] || shell.file_collision(destination) { source }
      system "mv #{source} #{destination}" unless options[:pretend]
    end
  else
    # this needs some sort of message here.
    system "mv #{source} #{destination}" unless options[:pretend]
  end
end