dotfu 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ed0e8ea77f9d7aee9b7cee356605c2cccade658
4
- data.tar.gz: 78416629973e69872bfe0f60a055b9619ef17a90
3
+ metadata.gz: 3be9876e7c3827258b2d2fbf1efd68a6963df450
4
+ data.tar.gz: 948c87f6da4ea64b88ee514543cecf3fa80cbe12
5
5
  SHA512:
6
- metadata.gz: cabfbe695f9754bb16a36c93e1a7cc5a082455c03490ae3d2878712bf90a9f7e72fe25b3fde609620cea3a5172fdf3d5252533da68eb5940e13dd8d198b1e8ef
7
- data.tar.gz: 9078d91a95e8dc1b011a7541225292d49e429fe961a9edbec80bd66e4d661df0d42726a5586f39dadc35bd416d4427698062ef1fd3644d329612ee8a648035c4
6
+ metadata.gz: 416127a74da102f1c562a98ed28315d1b23ffce0eede1f8dcfb621e85e1d6c9de62dcc389f17877c5e84a7c053c4a24227cbe0b86434ef03931468f098268589
7
+ data.tar.gz: 0c963d190761c0f9e64ad9789b89960b4bcce6047e1c7c15c7b36e34bf4fe692d9f99f8253ea3a2822f492778a6e9aad5cce026ffe97a2a5e0cb85e20a9962cc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.2.0
2
+ * Backups! sooner then expected.
3
+ * Will now only make dot files in the home dir.
4
+ * updating actually works now, though needs to be refactored.
5
+
1
6
  # 0.1.1
2
7
  * Switched from calling git myself to using the one on rubygems. Has issues, but less.
3
8
  * Fixed branch logic.
data/lib/dotfu/repos.rb CHANGED
@@ -6,6 +6,7 @@ module Dotfu
6
6
  attr_accessor :config_file
7
7
  attr_accessor :working_dir
8
8
  attr_accessor :target_dir
9
+ attr_accessor :backup_dir
9
10
 
10
11
  # r can be either a repo, or a user:repo pair.
11
12
  def initialize(arg = nil)
@@ -14,13 +15,12 @@ module Dotfu
14
15
  end
15
16
 
16
17
  ### Specialized attribute methods
17
- def config_file
18
- return @config_file ? @config_file : "dotfu.json"
18
+ def backup_dir
19
+ return @backup_dir ? @backup_dir : "#{Bini.data_dir}/backups/#{repo}"
19
20
  end
20
21
 
21
- # prepend repo with dotfiles- if it doesn't exist as it is set.
22
- def repo=(word)
23
- return @repo = word.start_with?('dotfiles-') ? word : "dotfiles-#{word}"
22
+ def config_file
23
+ return @config_file ? @config_file : "dotfu.json"
24
24
  end
25
25
 
26
26
  # target_dir should always return something.
@@ -28,9 +28,9 @@ module Dotfu
28
28
  return @target_dir ? @target_dir : Dir.home
29
29
  end
30
30
 
31
- # return user or the user in the config file.
32
- def user
33
- return @user ? @user : Dotfu.config_user
31
+ # prepend repo with dotfiles- if it doesn't exist as it is set.
32
+ def repo=(word)
33
+ return @repo = word.start_with?('dotfiles-') ? word : "dotfiles-#{word}"
34
34
  end
35
35
 
36
36
  # return the explicit directory this repo is cloned into.
@@ -39,7 +39,35 @@ module Dotfu
39
39
  return "#{Bini.data_dir}/repos/#{user}/#{repo}"
40
40
  end
41
41
 
42
+ # return user or the user in the config file.
43
+ def user
44
+ return @user ? @user : Dotfu.config_user
45
+ end
46
+
42
47
  ### Work methods
48
+ def backup
49
+ return nil if installed?
50
+ files = existing_files false
51
+
52
+ if files.any?
53
+ FileUtils.mkdir_p backup_dir
54
+
55
+ files.each do |target_file|
56
+ working_file = target_file.split("#{target_dir}/").last
57
+ next if is_my_file? target_file
58
+
59
+ begin
60
+ FileUtils.mv target_file, "#{backup_dir}/#{working_file}"
61
+ rescue Exception => e
62
+ puts e
63
+ exit
64
+ #puts raise RuntimeError.new "File move failed for: #{working_file} to #{backup_dir}/#{working_file} failed: #{e}"
65
+ end
66
+ end
67
+ end
68
+
69
+ return true
70
+ end
43
71
 
44
72
  # initial clone
45
73
  def clone
@@ -64,13 +92,12 @@ module Dotfu
64
92
  r = Git.init working_dir
65
93
 
66
94
  result = r.checkout(@branch ? branch : "master")
67
-
68
95
  raise RuntimeError.new result unless result
69
96
 
70
- # lets check if we have anything in the way, and abort instead of partially
71
- # installing
72
- existing_files = target_files.select {|f| File.exist? f}
73
- raise NotImplementedError.new "File(s) exist: #{existing_files}" if existing_files.any?
97
+ # Backup!
98
+ if existing_files(false)
99
+ puts "wtf, backup failed" unless backup
100
+ end
74
101
 
75
102
  # And now that we are ok with everything, lets make some fucking files.
76
103
  # TODO: add deep linking (mkdir + ln_s for each target) or shallow (just the first directory)
@@ -83,17 +110,31 @@ module Dotfu
83
110
  def pull
84
111
  return nil if !repo || !user
85
112
  r = Git.init working_dir
86
- #TODO: I'm confident that the implicit decleration of first here is going
87
- # to muck something up for someone. Find a way to do this explicitly.
113
+ r.fetch
114
+ #TODO: I'm confident that the implicit decleration of first here is going to muck something up for someone. Find a way to do this explicitly.
88
115
  return r.remote.merge
89
116
  end
90
117
 
118
+ # Restore files (as neccessary)
119
+ def restore
120
+ files = Dir.glob("#{backup_dir}/**/*")
121
+
122
+ raise "Files in the way" if existing_files
123
+
124
+ return true if files.empty?
125
+
126
+ files.each do |f|
127
+ FileUtils.mv f, target_dir
128
+ end
129
+
130
+ return true
131
+ end
132
+
91
133
  def uninstall
92
134
  raise RuntimeError.new "Not installed." unless installed?
93
135
 
94
- # ok we are not installed, lets clear the links. Later, this will restore
95
- # the backups (or something similiar).
96
136
  target_files.each {|f| FileUtils.rm f}
137
+ restore
97
138
  return true
98
139
  end
99
140
 
@@ -124,11 +165,12 @@ module Dotfu
124
165
  return true
125
166
  end
126
167
 
127
- # Return true if this file was installed from our repo.
168
+ # Return true if this file is linked to this repo.
128
169
  def is_my_file?(file)
129
170
  return true if File.exist?(file) && File.symlink?(file) && File.readlink(file).start_with?(working_dir)
130
171
  return false
131
172
  end
173
+
132
174
  # return an [Array] of base filenames.
133
175
  def files
134
176
  if !@files
@@ -139,10 +181,26 @@ module Dotfu
139
181
  return @files
140
182
  end
141
183
 
184
+ # return an array of existing files in the way.
185
+ # Accepts a [Boolean] as the only argument, true to return files we linked (default),
186
+ # false if we want just the files that are in the way.
187
+ def existing_files(my_files = true)
188
+ # I can do this in a convoluted set of if checks, of a couple readable selects.
189
+ output = target_files.select { |f| File.exist? f }
190
+ output.delete_if { |f| my_files && is_my_file?(f)}
191
+
192
+ return output
193
+ end
194
+
142
195
  # Return the target file.
143
196
  # Takes a [String] (explicit conversion) or [Array] for index lookup.
144
- def target_file(file)
145
- "#{target_dir}/.#{file_string(file)}"
197
+ # dot_home is a [Boolean] that will dot the target dir if it is the home
198
+ # dir only.
199
+ def target_file(file, dot_home = true)
200
+ output = "#{target_dir}/"
201
+ output += "." if dot_home && target_dir == Dir.home
202
+ output += file_string(file)
203
+ return output
146
204
  end
147
205
 
148
206
  # Return an [Array] of target files.
@@ -161,7 +219,6 @@ module Dotfu
161
219
  files.map {|f| working_file f}
162
220
  end
163
221
 
164
-
165
222
  private
166
223
  # So our input is now username@repo:branch
167
224
  # Repo is the only one required.
@@ -181,7 +238,7 @@ module Dotfu
181
238
  return nil unless config_file?
182
239
 
183
240
  content = Yajl.load open("#{working_dir}/#{config_file}")
184
- @target_dir = content["target_directory"] if content["target_directory"]
241
+ @target_dir = content["target_directory"].chomp("/") if content["target_directory"]
185
242
  end
186
243
 
187
244
  # Accepts a string or fixnum. Returns either the string or the files[fixnum]
@@ -191,3 +248,4 @@ module Dotfu
191
248
  end
192
249
  end
193
250
  end
251
+
data/lib/dotfu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dotfu
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotfu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Brodeur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-03 00:00:00.000000000 Z
11
+ date: 2013-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bini
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  requirements: []
134
134
  rubyforge_project:
135
- rubygems_version: 2.0.0
135
+ rubygems_version: 2.0.2
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: More to come . . .