-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRubySlippers.rb
executable file
·257 lines (220 loc) · 6.46 KB
/
RubySlippers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env ruby
# RubySlippers - There is no place like /home
require "fileutils"
require "rubygems"
require "thor"
require "grit"
include Grit
class RubySlippers < Thor
include Thor::Actions
@@source_root = File.expand_path("../", __FILE__)
@@repository = @@source_root + "/home"
@@home = ENV["HOME"]
desc "commit", "Commit any modified files in your repository"
method_option :debug, :type => :boolean
def commit()
self.commit(options)
end
desc "push", "Forced push"
method_option :debug, :type => :boolean
def push()
self.push(options)
end
desc "pull", "Forced pull"
method_option :debug, :type => :boolean
def pull()
self.pull(options)
end
desc "init GIT_URI", "Initialize and setup your home repository"
method_option :debug, :type => :boolean
def init(uri)
if File.exists?(@@repository + "/.git")
say "Repository already exists!", :red
exit 1
else
FileUtils.mkdir @@repository if ! File.exists?(@@repository)
Grit.debug = true if options[:debug]
grit = Grit::Git.new(@@repository)
grit.clone({}, uri, @@repository)
end
end
desc "carry FILE [TARGET]", "Carry a file or directory in your repository"
method_option :debug, :type => :boolean
method_option :force, :type => :boolean, :alias => "-f"
def carry(file, target=nil)
if ! File.exists?(file)
say "File not found!", :red
exit 1
elsif ! file.match(/^\//)
file = Dir.pwd + "/" + file
end
if ! target.nil?
target = @@repository + "/" + target
if File.directory?(target)
target.slice(-1,1) if target.match(/\/$/)
target << "/" + File.basename(file)
end
else
target = @@repository + "/" + File.basename(file)
end
if File.exists?(target)
if ! options[:force]
say "Target file already exists!", :red
exit 1
end
elsif ! File.exists?(File.dirname(target))
FileUtils.mkdir_p(File.dirname(target))
end
if File.directory?(file)
FileUtils.cp_r(file, target)
else
FileUtils.cp(file, target)
end
Dir.chdir(@@repository) do
target.gsub!(@@repository + "/", "")
Grit.debug = true if options[:debug]
repo = Grit::Repo.new(@@repository)
repo.add(target)
repo.commit_index("CARRY #{target}")
self.push
say "You are carrying #{target}"
end
end
desc "drop FILE", "Drop (delete) a file or directory from your repository"
method_option :debug, :type => :boolean
def drop(file)
if ! file.match(/^\//)
file = @@repository + "/" + file
elsif ! file.match(/^#{@@repository}/)
say "Cannot drop file outsie of repository!", :red
exit 1
end
if ! File.exists?(file)
say "File does not exist!", :red
exit 1
elsif File.directory?(file)
FileUtils.remove(file, :recursive)
else
FileUtils.remove(file)
end
Dir.chdir(@@repository) do
file.gsub!(@@repository + "/", "")
Grit.debug = true if options[:debug]
repo = Grit::Repo.new(@@repository)
repo.remove(file)
repo.commit_index("DROP #{file}")
self.push
say "You have dropped #{file}"
end
end
desc "equip FILE TARGET", "Symlink file from repository into your home directory"
method_option :debug, :type => :boolean
method_option :force, :type => :boolean
method_option :persist, :type => :boolean, :alias => "-p"
def equip(file, target)
if file.match(/^\//) && ! file.match(/^#{@@repository}/)
say "Equiped file path should be relative to repository root", :red
exit 1
else
file = @@repository + "/" + file
end
if ! File.exists?(file)
say "File does not exist!", :red
exit 1
end
if target.nil?
target = @@home + "/" + File.basename(file)
elsif target.match(/^\//) && ! file.match(/^#{@@home}/)
say "Target file must be in your home dir", :red
exit 1
else
target = @@home + "/" + target
end
if File.exists?(target)
if ! File.symlink?(target)
if ! options[:force]
say "Target file exists!", :red
exit 1
else
FileUtils.mv(target, target + ".rsbkup")
end
elsif File.readlink(target) != file
FileUtils.mv(target, target + ".rsbkup")
else
say "File is already equipped!"
exit 0
end
end
FileUtils.ln_s(file, target)
if options[:persist]
# persistance
end
end
desc "unequip FILE", "Unequip a file from your repository"
method_option :debug, :type => :boolean
method_option :force, :type => :boolean
method_option :persist, :type => :boolean
method_option :restore, :type => :boolean
def unequip(file)
if file.match(/^\//) && ! file.match(/^#{@@home}/)
say "Equipped file path should be relative to your home dir", :red
exit 1
else
file = @@home + "/" + file
end
if ! File.exists?(file)
say "File does not exist!", :red
exit 1
else
if ! File.symlnk?(file)
say "File is not a symlink!", :red
exit 1
elsif ! File.readlink(file).match(/^#{@@repository}/)
say "File does not appear to be a previously equipped file!", :reda
exit 1
end
end
if File.exists?(file + ".rsbkup") && options[:restore]
FileUtils.mv(file + ".rsbkup", file)
else
FileUtils.rm(file)
end
if options[:persist]
# persistance
end
end
no_tasks do
def pull(options={})
Grit.debug = true if options[:debug]
grit = Grit::Git.new(@@repository + "/.git")
grit.pull({}, "origin", "master")
end
def push(options={})
Grit.debug = true if options[:debug]
grit = Grit::Git.new(@@repository + "/.git")
grit.push({}, "origin", "master")
end
def commit(options={})
Grit.debug = true if options[:debug]
repo = Grit::Repo.new(@@repository)
if repo.status.changed.any?
say "You have uncommited changes in your repository!", :red
repo.status.changed.each do |k,f|
say "M\t#{f.path}"
end
confirm = ask "Do you want to commit these changes now? [y,n]: "
if confirm == "y"
Dir.chdir(@@repository) do
repo.status.changed.each do |k,f|
file = f.path.gsub(/^#{@@repository}/, "")
repo.add(file)
end
end
end
repo.commit_index("COMMIT MODIFIED FILES")
self.push
end
end
end
end
RubySlippers.start