dotfile_linker 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -18,8 +18,14 @@ This gem is hosted on [rubygems.org](https://rubygems.org/gems/dotfile_linker),
18
18
  ## Usage
19
19
 
20
20
  Run `dotfile_linker` from your dotfiles directory. The script will then run through each file that isn't already
21
- symlinked in your home directory and ask if you want to symlink it. The `-d` option cycles through existing symlinks in
22
- your home directory and asks if you'd like to remove them.
21
+ symlinked in your home directory and ask if you want to symlink it. The `-u` option unlinks and restores files to your
22
+ home directory.
23
+
24
+ ![Link example](http://i.imgur.com/k4O1z.jpg)
25
+
26
+
27
+
28
+ ![Unlink example](http://i.imgur.com/7JUY9.png)
23
29
 
24
30
  ## Contributing
25
31
 
@@ -1,3 +1,3 @@
1
1
  module DotfileLinker
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -19,6 +19,20 @@ module DotfileLinker
19
19
  @options = {}
20
20
  end
21
21
 
22
+ def parse_options
23
+ optparse = OptionParser.new do |opts|
24
+ opts.on('-p', '--path PATH', 'Use [PATH] as dotfiles directory (instead of current directory)') { |path| @options[:path] = File.expand_path(path) }
25
+ opts.on_tail('-u', '--unlink', 'Unlink mode') { @options[:unlink_mode] = true }
26
+ opts.on_tail('-v', '--version', 'Show version') { puts VERSION; exit }
27
+ opts.on_tail('-h', '--help', 'Show this message') { puts opts; exit }
28
+ end
29
+ optparse.parse!
30
+ end
31
+
32
+ def each_dotfile(dirname)
33
+ Dir.foreach(dirname) { |filename| yield filename if filename =~ /^\./ }
34
+ end
35
+
22
36
  def dotfiles_dir
23
37
  @options[:path] || Dir.pwd
24
38
  end
@@ -33,12 +47,12 @@ module DotfileLinker
33
47
 
34
48
  def raise_if_home_and_dotfiles_dir_match
35
49
  if File.expand_path(home_dir) == File.expand_path(dotfiles_dir)
36
- raise InvalidDotfilesDir, "Please specify your dotfiles directory by running `link_dotfiles` from that path, or providing a --path flag".red
50
+ raise InvalidDotfilesDir, "#{ dotfiles_dir } is not a valid dotfiles directory. Please specify your dotfiles directory by running `link_dotfiles` from that path, or providing a --path flag".red
37
51
  end
38
52
  end
39
53
 
40
- def user_response(message)
41
- puts message
54
+ def user_response(message, choices)
55
+ puts "#{message} #{choices}"
42
56
  case gets.strip
43
57
  when /^y/i
44
58
  :yes
@@ -49,12 +63,12 @@ module DotfileLinker
49
63
  when /^q/i
50
64
  :quit
51
65
  else
52
- user_response("Please enter a valid response")
66
+ user_response("Please enter a valid response", choices)
53
67
  end
54
68
  end
55
69
 
56
- def user_response_or_exit(message)
57
- response = user_response(message)
70
+ def user_response_or_exit(message, choices)
71
+ response = user_response(message, choices)
58
72
  if response == :quit
59
73
  puts "Exiting"
60
74
  exit
@@ -62,16 +76,6 @@ module DotfileLinker
62
76
  response
63
77
  end
64
78
 
65
- def parse_options
66
- optparse = OptionParser.new do |opts|
67
- opts.on('-p', '--path PATH', String, 'Use [PATH] as dotfiles directory (instead of current directory)') { |path| @options[:path] = File.expand_path(path) }
68
- opts.on_tail('-u', '--unlink', 'Unlink mode') { @options[:unlink_mode] = true }
69
- opts.on_tail('-v', '--version', 'Show version') { puts VERSION; exit }
70
- opts.on_tail('-h', '--help', 'Show this message') { puts opts; exit }
71
- end
72
- optparse.parse!
73
- end
74
-
75
79
  def ignore_list
76
80
  @ignore_list ||=
77
81
  begin
@@ -95,26 +99,26 @@ module DotfileLinker
95
99
  home_dir_file_path = File.expand_path("~/#{ filename }")
96
100
  dotfiles_dir_file_path = File.expand_path("#{ dotfiles_dir }/#{ filename }")
97
101
  unless File.symlink?(home_dir_file_path) || exclude_file?(filename)
98
- case user_response_or_exit("move and link #{ home_dir_file_path.human_filename.magenta } -> #{ dotfiles_dir_file_path.human_filename.cyan }? (y/n/i[gnore])")
102
+ case user_response_or_exit("move and link #{ home_dir_file_path.human_filename.magenta } -> #{ dotfiles_dir_file_path.human_filename.cyan }?", " (y/n/i[gnore]/q)")
99
103
  when :yes
100
104
  FileUtils.mv(home_dir_file_path, dotfiles_dir_file_path, :verbose => true)
101
105
  FileUtils.ln_s(dotfiles_dir_file_path, home_dir_file_path, :verbose => true)
102
106
  when :ignore
103
107
  ignore_file(filename)
104
- puts "ignored #{filename.cyan}"
108
+ puts "added #{filename.cyan} to ignore file"
105
109
  end
106
110
  end
107
111
  end
108
112
 
109
113
  def link_files
110
- Dir.foreach(home_dir) { |filename| link_file(filename) }
114
+ each_dotfile(home_dir) { |filename| link_file(filename) }
111
115
  end
112
116
 
113
117
  def unlink_file(filename)
114
118
  home_dir_symlink_path = File.expand_path("~/#{ filename }")
115
119
  dotfiles_dir_file_path = File.expand_path("#{ dotfiles_dir }/#{ filename }")
116
120
  if File.symlink?(home_dir_symlink_path)
117
- case user_response_or_exit("unlink #{ home_dir_symlink_path.human_filename.magenta } and restore #{ dotfiles_dir_file_path.human_filename.cyan }? (y/n)")
121
+ case user_response_or_exit("unlink #{ home_dir_symlink_path.human_filename.magenta } and restore #{ dotfiles_dir_file_path.human_filename.cyan }?", " (y/n/q)")
118
122
  when :yes
119
123
  FileUtils.rm(home_dir_symlink_path, :verbose => true)
120
124
  FileUtils.mv(dotfiles_dir_file_path, home_dir_symlink_path, :verbose => true)
@@ -123,12 +127,12 @@ module DotfileLinker
123
127
  end
124
128
 
125
129
  def unlink_files
126
- Dir.foreach(dotfiles_dir) { |filename| unlink_file(filename) }
130
+ each_dotfile(dotfiles_dir) { |filename| unlink_file(filename) }
127
131
  end
128
132
 
129
133
  def start
130
- raise_if_home_and_dotfiles_dir_match
131
134
  parse_options
135
+ raise_if_home_and_dotfiles_dir_match
132
136
  if @options[:unlink_mode]
133
137
  unlink_files
134
138
  else
@@ -21,12 +21,25 @@ describe DotfileLinker::Linker do
21
21
  values.each do |k, v|
22
22
  v.each do |response|
23
23
  @linker.stub(:gets).and_return(response)
24
- @linker.user_response('fake message').should == k
24
+ @linker.user_response('fake message', 'fake options').should == k
25
25
  end
26
26
  end
27
27
  end
28
28
  end
29
29
 
30
+ describe "#each_dotfile" do
31
+ it "should return only files starting with ." do
32
+ dotfiles = %w{ .bash_profile .emacs .gitconfig .tmux.conf }
33
+ normal_files = %w{ Applications Users SomeFolder test}
34
+ all_files = dotfiles + normal_files
35
+ expectation = Dir.should_receive(:foreach).with('/some/path/')
36
+ all_files.each { |file| expectation.and_yield(file) }
37
+ actual = []
38
+ @linker.each_dotfile('/some/path/') { |filename| actual << filename }
39
+ actual.should == dotfiles
40
+ end
41
+ end
42
+
30
43
  describe "#exclude_file?" do
31
44
  it "excludes files in blacklist" do
32
45
  %w{ . .. .git }.each { |filename| @linker.exclude_file?(filename).should be }
@@ -88,8 +101,8 @@ describe DotfileLinker::Linker do
88
101
  end
89
102
 
90
103
  it "should call #ignore_file" do
91
- @linker.should_receive(:ignore_file).with("file I want to ignore")
92
- @linker.link_file("file I want to ignore")
104
+ @linker.should_receive(:ignore_file).with(".dotfile_i_want_to_ignore")
105
+ @linker.link_file(".dotfile_i_want_to_ignore")
93
106
  end
94
107
  end
95
108
 
@@ -147,7 +160,7 @@ describe DotfileLinker::Linker do
147
160
  [true, false].each do |home_dir_file_is_symlink|
148
161
  describe "when symlink #{ "isn't" unless home_dir_file_is_symlink } in ~" do
149
162
  it "should #{ "not" unless home_dir_file_is_symlink } remove symlink and move file" do
150
- filename = "file_to_unlink"
163
+ filename = ".file_to_unlink"
151
164
  home_dir_file_path = "#{ ENV['HOME'] }/#{ filename }"
152
165
  dotfiles_dir_file_path = "#{ @linker.dotfiles_dir }/#{ filename }"
153
166
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotfile_linker
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dillon Kearns