Class: Homesick

Inherits:
Thor
  • Object
show all
Includes:
Actions, Thor::Actions
Defined in:
lib/homesick.rb,
lib/homesick/shell.rb,
lib/homesick/actions.rb

Defined Under Namespace

Modules: Actions Classes: Shell

Constant Summary collapse

GITHUB_NAME_REPO_PATTERN =
/\A([A-Za-z_-]+\/[A-Za-z_-]+)\Z/
SUBDIR_FILENAME =
".homesick_subdir"

Instance Method Summary collapse

Methods included from Actions

#git_add, #git_clone, #git_commit_all, #git_init, #git_pull, #git_push, #git_remote_add, #git_submodule_init, #git_submodule_update, #ln_s, #mv

Constructor Details

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

Returns a new instance of Homesick.



15
16
17
18
# File 'lib/homesick.rb', line 15

def initialize(args=[], options={}, config={})
  super
  self.shell = Homesick::Shell.new
end

Instance Method Details

#clone(uri) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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.rb', line 21

def clone(uri)
  inside repos_dir do
    destination = nil
    if File.exist?(uri)
      uri = Pathname.new(uri).expand_path
      if uri.to_s.start_with?(repos_dir.to_s)
        raise "Castle already cloned to #{uri}"
      end

      destination = uri.basename

      ln_s uri, destination
    elsif uri =~ GITHUB_NAME_REPO_PATTERN
      destination = Pathname.new($1)
      git_clone "git://github.com/#{$1}.git", :destination => destination
    elsif uri =~ /\/([^\/]*?)(\.git)?\Z/
      destination = Pathname.new($1)
      git_clone uri
    elsif uri =~ /[^:]+:([^:]+)(\.git)?\Z/
      destination = Pathname.new($1)
      git_clone uri
    else
      raise "Unknown URI format: #{uri}"
    end

    if destination.join('.gitmodules').exist?
      inside destination do
        git_submodule_init
        git_submodule_update
      end
    end

    homesickrc = destination.join('.homesickrc').expand_path
    if homesickrc.exist?
      proceed = shell.yes?("#{uri} has a .homesickrc. Proceed with evaling it? (This could be destructive)")
      if proceed
        shell.say_status "eval", homesickrc
        inside destination do
          eval homesickrc.read, binding, homesickrc.expand_path
        end
      else
        shell.say_status "eval skip", "not evaling #{homesickrc}, #{destination} may need manual configuration", :blue
      end
    end
  end
end

#commit(name) ⇒ Object



83
84
85
86
# File 'lib/homesick.rb', line 83

def commit(name)
  commit_castle name

end

#generate(castle) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/homesick.rb', line 165

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

#listObject



158
159
160
161
162
# File 'lib/homesick.rb', line 158

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

#pull(name = "") ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/homesick.rb', line 70

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

end

#push(name) ⇒ Object



89
90
91
92
# File 'lib/homesick.rb', line 89

def push(name)
  push_castle name

end


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/homesick.rb', line 96

def symlink(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

#track(file, castle) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
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
# File 'lib/homesick.rb', line 113

def track(file, castle)
  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
      shell.say_status(:track, "#{target} already exists, and is more recent than #{file}. Run 'homesick SYMLINK CASTLE' to create symlinks.", :blue) unless options[:quiet]
    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
  unless relative_dir.eql?(Pathname.new('.'))
    subdir_add(castle, relative_dir)
  end
end