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/

Instance Method Summary collapse

Methods included from Actions

#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.



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

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

Instance Method Details

#clone(uri) ⇒ Object



20
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
# File 'lib/homesick.rb', line 20

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 "[email protected]:#{$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



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

def commit(name)
  commit_castle name

end

#generate(castle) ⇒ Object



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

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



130
131
132
133
134
# File 'lib/homesick.rb', line 130

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



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

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



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

def push(name)
  push_castle name

end


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

def symlink(name)
  check_castle_existance(name, "symlink")

  inside castle_dir(name) do
    files = Pathname.glob('{.*,*}').reject{|a| [".",".."].include?(a.to_s)}
    files.each do |path|
      absolute_path = path.expand_path

      inside home_dir do
        adjusted_path = (home_dir + path).basename

        ln_s absolute_path, adjusted_path
      end
    end
  end
end

#track(file, castle) ⇒ Object



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

def track(file, castle)
  castle = Pathname.new(castle)
  file = Pathname.new(file)
  check_castle_existance(castle, 'track')
  
  absolute_path = file.expand_path
  castle_path = castle_dir(castle)
  mv absolute_path, castle_path

  inside home_dir do
    absolute_path = castle_dir(castle) + file.basename
    home_path = home_dir + file
    ln_s absolute_path, home_path
  end
end