dotify 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ require 'dotify'
2
+ require 'dotify/config'
3
+
4
+ module Dotify
5
+ class FileList
6
+ class << self
7
+
8
+ def home
9
+ result = paths(File.join(Config.home, '.*'))
10
+ filter_ignore_files!(result, :dotfiles)
11
+ end
12
+
13
+ def dotify
14
+ result = paths(File.join(Config.path, '.*'))
15
+ filter_ignore_files!(result, :dotify)
16
+ end
17
+
18
+ def list(glob)
19
+ filenames(paths(glob))
20
+ end
21
+
22
+ def paths(glob)
23
+ filter_dot_directories!(Dir[glob])
24
+ end
25
+
26
+ def filter_dot_directories!(files)
27
+ files.select { |f| !['.', '..'].include?(Files.filename(f)) }
28
+ end
29
+
30
+ def filter_ignore_files!(files, ignore)
31
+ ignoring = Config.ignore(ignore)
32
+ files.select { |f| !ignoring.include?(Files.filename(f)) }
33
+ end
34
+
35
+ def filenames(files)
36
+ files.map { |f| Files.filename(f) }
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -11,62 +11,61 @@ module Dotify
11
11
  include Thor::Actions
12
12
 
13
13
  def dots
14
- @dots ||= file_list(File.join(Config.path, "/.*"))
14
+ @dots ||= FileList.dotify
15
15
  return @dots unless block_given?
16
- @dots.each {|d| yield(d, file_name(d)) }
16
+ @dots.each {|d| yield(d, filename(d)) }
17
+ end
18
+
19
+ def unlinked
20
+ dots = self.dots.map { |d| filename(d) }
21
+ installed = self.installed.map {|i| filename(i)}
22
+ unlinked = (dots - installed).map{|f| dotfile(f) }
23
+ return unlinked unless block_given?
24
+ unlinked.each {|u| yield(u, filename(u)) }
17
25
  end
18
26
 
19
27
  def installed
20
- dots = self.dots.map { |f| file_name(f) }
21
- installed = file_list(File.join(Config.home, ".*")).select do |i|
22
- dots.include?(file_name(i))
28
+ dots = self.dots.map { |f| filename(f) }
29
+ installed = FileList.home.select do |d|
30
+ dots.include?(filename(d))
23
31
  end
24
32
  return installed unless block_given?
25
- installed.each {|i| yield(i, file_name(i)) }
33
+ installed.each {|i| yield(i, filename(i)) }
26
34
  end
27
35
 
28
- def unlinked
29
- dots = self.dots.map { |f| file_name(f) }
30
- installed = self.installed.map {|f| file_name(f)}
31
- unlinked = (dots - installed).map{|f| dotfile(f) }
32
- return unlinked unless block_given?
33
- unlinked.each {|u| yield(u, file_name(u)) }
36
+ def uninstalled
37
+ dots = self.dots.map { |f| filename(f) }
38
+ installed = FileList.home.select do |d|
39
+ !dots.include?(filename(d))
40
+ end
41
+ return installed unless block_given?
42
+ installed.each {|i| yield(i, filename(i)) }
34
43
  end
35
44
 
36
- def file_name(file)
37
- file.split("/").last
45
+ def filename(file)
46
+ %r{([^<>:"\/\\\|\?\*]*)\Z}.match(file).to_s
38
47
  end
39
48
 
40
49
  def template?(file)
41
- file_name(file).match(/(tt|erb)$/) ? true : false
50
+ filename(file).match(/(tt|erb)\Z/) ? true : false
42
51
  end
43
52
 
44
53
  def dotfile(file)
45
- File.join(Config.home, file_name(file))
54
+ File.join(Config.home, filename(file))
46
55
  end
47
56
 
48
57
  def dotify(file)
49
- File.join(Config.path, file_name(file))
58
+ File.join(Config.path, filename(file))
50
59
  end
51
60
 
52
61
  def link_dotfile(file)
53
- FileUtils.ln_s(file_name(file), Config.home) == 0 ? true : false
62
+ FileUtils.ln_s(filename(file), Config.home) == 0 ? true : false
54
63
  end
55
64
 
56
65
  def unlink_dotfile(file)
57
- FileUtils.rm_rf File.join(Config.home, file_name(file))
66
+ FileUtils.rm_rf File.join(Config.home, filename(file))
58
67
  end
59
68
 
60
- private
61
-
62
- def file_list(dir_glob)
63
- filter_dot_directories!(Dir[dir_glob])
64
- end
65
-
66
- def filter_dot_directories!(files)
67
- files.select { |f| !['.', '..'].include?(file_name(f)) }
68
- end
69
-
70
69
  end
71
70
  end
72
71
  end
@@ -1,3 +1,3 @@
1
1
  module Dotify
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module Dotify
5
+ class VersionChecker
6
+ class << self
7
+ def run_check!
8
+ if @version.nil?
9
+ resp = Net::HTTP.get('rubygems.org', '/api/v1/versions/dotify.json')
10
+ json = JSON.parse(resp)
11
+ @version = json.map { |v| v['number'] }.max
12
+ end
13
+ @version
14
+ end
15
+
16
+ def current?
17
+ Dotify::VERSION == self.version
18
+ end
19
+
20
+ def out_of_date?
21
+ !current?
22
+ end
23
+
24
+ def version
25
+ @version || self.run_check!
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'dotify/config'
3
2
  require 'dotify/cli'
4
3
  require 'thor'
5
4
 
@@ -3,55 +3,35 @@ require 'dotify/errors'
3
3
  require 'dotify/config'
4
4
 
5
5
  describe Dotify::Config do
6
- let(:fixtures) { File.join(%x{pwd}.chomp, 'spec/fixtures') }
7
- before do
8
- Fake.tearup
9
- Dotify::Config.stub(:user_home) { Fake.root_path }
10
- #Dotify::Config.stub(:config_file) { File.join(fixtures, '.dotrc-default') }
11
- #Dotify::Config.load_config!
12
- end
13
- after do
14
- Fake.teardown
6
+ describe Dotify::Config, "#installed?" do
7
+ it "should return false if Dotify has not been setup" do
8
+ File.should_receive(:directory?).with(File.join(Dotify::Config.home, Dotify::Config.dirname)).and_return(false)
9
+ Dotify::Config.installed?.should == false
10
+ end
11
+ it "should return true if Dotify has been setup" do
12
+ File.should_receive(:directory?).with(File.join(Dotify::Config.home, Dotify::Config.dirname)).and_return(true)
13
+ Dotify::Config.installed?.should == true
14
+ end
15
15
  end
16
- describe "setters" do
17
- #it "should be able to set the current shell (not actually yet used)" do
18
- # Dotify::Config.shell = 'zsh'
19
- # Dotify::Config.shell.should == 'zsh'
20
- # Dotify::Config.shell = 'bash'
21
- # Dotify::Config.shell.should == 'bash'
22
- #end
23
- #it "should raise an error if the shell specified does not exist" do
24
- # expect { Dotify::Config.shell = 'fake' }.to raise_error Dotify::NonValidShell
25
- #end
26
- #it "should be able to set the current profile name (not actually yet used)" do
27
- # Dotify::Config.profile = 'james'
28
- # Dotify::Config.profile.should == 'james'
29
- #end
30
- #it "should be able to show the dotify path" do
31
- # Dotify::Config.path.should == File.join(Dotify::Config.home, '.dotify')
32
- #end
16
+ describe "options" do
17
+ it "should be able to show the home path" do
18
+ Dotify::Config.home.should == Thor::Util.user_home
19
+ end
33
20
  it "should be able to show the dotify path" do
34
21
  Dotify::Config.path.should == File.join(Dotify::Config.home, '.dotify')
35
22
  end
36
- it "should be able to show the dotify backup path" do
37
- Dotify::Config.backup.should == File.join(Dotify::Config.path, '.backup')
38
- end
39
23
  end
40
- describe "config files" do
41
- #before do
42
- # Dotify::Config.stub(:home) { Fake.root_path }
43
- # Dotify::Config.stub(:config_file) { File.join(fixtures, '.dotrc-mattbridges') }
44
- #end
45
- #it "should not try to assign improper config values" do
46
- # Dotify::Config.should_receive(:profile=)
47
- # Dotify::Config.should_receive(:shell=)
48
- # Dotify::Config.should_not_receive(:something_fake=)
49
- # Dotify::Config.load_config!
50
- #end
51
- #it "should load the config and set the variables" do
52
- # Dotify::Config.load_config!
53
- # Dotify::Config.profile.should == 'mattdbridges'
54
- # Dotify::Config.shell.should == 'zsh'
55
- #end
24
+ describe "ignore files" do
25
+ before do
26
+ Dotify::Config.stub(:config) do
27
+ { :ignore => { :dotfiles => %w[.gemrc], :dotify => %w[.gitmodule] } }
28
+ end
29
+ end
30
+ it "should retrieve the list of dotfiles to ignore in the home directory" do
31
+ Dotify::Config.ignore(:dotfiles).should include '.gemrc'
32
+ end
33
+ it "should retrieve the list of dotify files to ignore" do
34
+ Dotify::Config.ignore(:dotify).should include '.gitmodule'
35
+ end
56
36
  end
57
37
  end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+ require 'dotify/config'
3
+ require 'dotify/file_list'
4
+
5
+ describe Dotify::FileList do
6
+
7
+ describe Dotify::FileList, "#home" do
8
+ before do
9
+ Dotify::Config.stub(:home).and_return("/home/test")
10
+ end
11
+ it "should call FileList#list with the correct path" do
12
+ Dotify::FileList.should_receive(:paths) \
13
+ .with("/home/test/.*").once \
14
+ .and_return(['/root/test/.vimrc', '/root/test/.bashrc', '/root/test/.zshrc'])
15
+ Dotify::FileList.home
16
+ end
17
+ it "should drop files that have been specified to be ignored" do
18
+ Dotify::FileList.stub(:paths) do
19
+ ['/root/test/.gitconfig', '/root/test/.vimrc', '/root/test/.bashrc', '/root/test/.zshrc']
20
+ end
21
+ Dotify::Config.stub(:ignore).with(:dotfiles).and_return %w[.zshrc .vimrc]
22
+ result = Dotify::FileList.home
23
+ result.should include '/root/test/.bashrc'
24
+ result.should include '/root/test/.gitconfig'
25
+ result.should_not include '/root/test/.zshrc'
26
+ result.should_not include '/root/test/.vimrc'
27
+ end
28
+ end
29
+
30
+ describe Dotify::FileList, "#dotify" do
31
+ it "should call FileList#list with the correct path" do
32
+ Dotify::Config.stub(:path).and_return("/home/test/.dotify")
33
+ Dotify::FileList.should_receive(:paths) \
34
+ .with("/home/test/.dotify/.*").once \
35
+ .and_return(['/spec/test/.vimrc', '/spec/test/.bashrc', '/spec/test/.zshrc'])
36
+ Dotify::FileList.dotify
37
+ end
38
+ it "should drop files that have been specified to be ignored" do
39
+ Dotify::FileList.stub(:paths) do
40
+ ['/dotify/test/.gitconfig', '/dotify/test/.vimrc', '/dotify/test/.bashrc', '/dotify/test/.zshrc']
41
+ end
42
+ Dotify::Config.stub(:ignore).with(:dotify).and_return %w[.gitconfig .bashrc]
43
+ result = Dotify::FileList.dotify
44
+ result.should include '/dotify/test/.vimrc'
45
+ result.should include '/dotify/test/.zshrc'
46
+ result.should_not include '/dotify/test/.bashrc'
47
+ result.should_not include '/dotify/test/.gitconfig'
48
+ end
49
+ end
50
+
51
+ describe Dotify::FileList, "#list" do
52
+ let(:glob) { '/spec/test/.*' }
53
+ it "should pull the glob of dotfiles from a directory" do
54
+ Dir.should_receive(:[]).with(glob).and_return(%w[. .. /spec/test/.vimrc /spec/test/.bashrc /spec/test/.zshrc])
55
+ Dotify::FileList.list(glob)
56
+ end
57
+ describe "return values" do
58
+ before do
59
+ Dir.stub(:[]).with(glob).and_return(%w[. .. /spec/test/.vimrc /spec/test/.bashrc /spec/test/.zshrc])
60
+ end
61
+ let(:files) { Dotify::FileList.list(glob) }
62
+ it "should return the right filenames" do
63
+ files.should include '.vimrc'
64
+ files.should include '.bashrc'
65
+ files.should include '.zshrc'
66
+ end
67
+ it "should filter out . and .. directories" do
68
+ files.should_not include '.'
69
+ files.should_not include '..'
70
+ end
71
+ end
72
+ end
73
+
74
+ describe Dotify::FileList, "#paths" do
75
+ let(:glob) { '/spec/test/.*' }
76
+ it "should pull the glob of dotfiles from a directory" do
77
+ Dir.should_receive(:[]).with(glob).and_return(%w[. .. /spec/test/.vimrc /spec/test/.bashrc /spec/test/.zshrc])
78
+ Dotify::FileList.paths(glob)
79
+ end
80
+ describe "return values" do
81
+ before do
82
+ Dir.stub(:[]).with(glob).and_return(%w[. .. /spec/test/.vimrc /spec/test/.bashrc /spec/test/.zshrc])
83
+ end
84
+ let(:files) { Dotify::FileList.paths(glob) }
85
+ it "should return the right directories" do
86
+ files.should include '/spec/test/.vimrc'
87
+ files.should include '/spec/test/.bashrc'
88
+ files.should include '/spec/test/.zshrc'
89
+ end
90
+ it "should filter out . and .. directories" do
91
+ files = Dotify::FileList.paths(glob)
92
+ files.should_not include '.'
93
+ files.should_not include '..'
94
+ end
95
+ end
96
+ end
97
+
98
+ describe Dotify::FileList, "#filenames" do
99
+ let(:files) { %w[/spec/test/.vimrc /spec/test/.bashrc /spec/test/.zshrc] }
100
+ it "should use Files#filename to change the files" do
101
+ Dotify::Files.should_receive(:filename).exactly(files.count).times
102
+ Dotify::FileList.filenames(files)
103
+ end
104
+ it "return only" do
105
+ result = Dotify::FileList.filenames(files)
106
+ result.should include '.vimrc'
107
+ result.should include '.bashrc'
108
+ result.should include '.zshrc'
109
+ end
110
+ end
111
+
112
+
113
+ end
@@ -4,25 +4,22 @@ require 'dotify/files'
4
4
  require 'fileutils'
5
5
 
6
6
  describe Dotify::Files do
7
- let(:fixtures) { File.join(%x{pwd}.chomp, 'spec/fixtures') }
8
- before do
9
- Fake.tearup
10
- #Dotify::Config.stub(:config_file) { File.join(fixtures, '.dotrc-default') }
11
- Dotify::Config.stub(:home) { Fake.root_path }
12
- Dotify::Config.load_config!
13
- end
14
- after do
15
- Fake.teardown
16
- end
17
7
 
18
- it "should respond to the right methods" do
19
- Dotify::Files.should respond_to :dots
20
- Dotify::Files.should respond_to :installed
8
+ describe "methods" do
9
+ it "should respond to dots" do
10
+ Dotify::Files.should respond_to :dots
11
+ end
12
+ it "should respond to installed" do
13
+ Dotify::Files.should respond_to :installed
14
+ end
15
+ it "should respond to unlinked" do
16
+ Dotify::Files.should respond_to :unlinked
17
+ end
21
18
  end
22
19
 
23
- it "should split a file_name correct" do
24
- Dotify::Files.file_name("some/random/path/to/file.txt").should == 'file.txt'
25
- Dotify::Files.file_name("another/path/no_extension").should == 'no_extension'
20
+ it "should split a filename correct" do
21
+ Dotify::Files.filename("some/random/path/to/file.txt").should == 'file.txt'
22
+ Dotify::Files.filename("another/path/no_extension").should == 'no_extension'
26
23
  end
27
24
 
28
25
  describe Dotify::Files, "#dotfile" do
@@ -41,21 +38,21 @@ describe Dotify::Files do
41
38
 
42
39
  describe Dotify::Files, "#dots" do
43
40
  before do
44
- Dotify::Files.stub(:file_list) do
45
- ['/spec/test/.vimrc', '/spec/test/.bashrc', '/spec/test/.zshrc']
41
+ Dotify::FileList.stub(:dotify) do
42
+ ['/spec/test/.bash_profile', '/spec/test/.bashrc', '/spec/test/.zshrc']
46
43
  end
47
44
  end
45
+ let!(:files) { Dotify::Files.dots }
48
46
  it "should return the list of dotfiles in the dotify path" do
49
- files = Dotify::Files.dots.map { |f| Dotify::Files.file_name(f) }
50
- files.should include '.vimrc'
47
+ files
48
+ files.map! { |f| Dotify::Files.filename(f) }
49
+ files.should include '.bash_profile'
51
50
  files.should include '.bashrc'
52
51
  files.should include '.zshrc'
53
- files.should_not include '.' # current and upper directories
54
- files.should_not include '..'
55
52
  end
56
53
  it "shoud yield the files if a block is given" do
57
- files = Dotify::Files.dots.map { |d| [d, Dotify::Files.file_name(d)] }
58
- expect { |b| Dotify::Files.dots(&b) }.to yield_successive_args(*files)
54
+ yields = files.map { |f| [f, Dotify::Files.filename(f)] }
55
+ expect { |b| Dotify::Files.dots(&b) }.to yield_successive_args(*yields)
59
56
  end
60
57
  end
61
58
 
@@ -69,35 +66,60 @@ describe Dotify::Files do
69
66
  end
70
67
  end
71
68
  it "should return the list of unlinked dotfiles in the root path" do
72
- unlinked = Dotify::Files.unlinked.map { |u| Dotify::Files.file_name(u) }
69
+ unlinked = Dotify::Files.unlinked.map { |u| Dotify::Files.filename(u) }
73
70
  unlinked.should include '.vimrc'
74
71
  unlinked.should_not include '.bashrc'
75
72
  end
76
73
  it "shoud yield the unlinked files if a block is given" do
77
- unlinked = Dotify::Files.unlinked.map { |u| [u, Dotify::Files.file_name(u)] }
74
+ unlinked = Dotify::Files.unlinked.map { |u| [u, Dotify::Files.filename(u)] }
78
75
  expect { |b| Dotify::Files.unlinked(&b) }.to yield_successive_args(*unlinked)
79
76
  end
80
77
  end
81
78
 
82
79
  describe Dotify::Files, "#installed" do
83
80
  before do
84
- fake_root, dotify = Fake.paths
85
- FileUtils.touch File.join(fake_root, '.vimrc')
86
- FileUtils.touch File.join(fake_root, '.bashrc')
81
+ Dotify::Files.stub(:dots) do
82
+ %w[/spec/test/.zshrc /spec/test/.bashrc /spec/test/.vimrc /spec/test/.dotify]
83
+ end
84
+ Dotify::FileList.stub(:home) do
85
+ %w[/root/test/.bashrc /root/test/.vimrc]
86
+ end
87
87
  end
88
88
  it "should return the list of installed dotfiles in the root path" do
89
- installed = Dotify::Files.installed.map { |i| Dotify::Files.file_name(i) }
89
+ installed = Dotify::Files.installed.map { |i| Dotify::Files.filename(i) }
90
90
  installed.should include '.vimrc'
91
91
  installed.should include '.bashrc'
92
92
  installed.should_not include '.zshrc'
93
93
  installed.should_not include '.dotify'
94
94
  end
95
95
  it "shoud yield the installed files if a block is given" do
96
- installed = Dotify::Files.installed.map { |i| [i, Dotify::Files.file_name(i)] }
96
+ installed = Dotify::Files.installed.map { |i| [i, Dotify::Files.filename(i)] }
97
97
  expect { |b| Dotify::Files.installed(&b) }.to yield_successive_args(*installed)
98
98
  end
99
99
  end
100
100
 
101
+ describe Dotify::Files, "#uninstalled" do
102
+ before do
103
+ Dotify::Files.stub(:dots) do
104
+ %w[/spec/test/.zshrc /spec/test/.bashrc /spec/test/.vimrc /spec/test/.dotify]
105
+ end
106
+ Dotify::FileList.stub(:home) do
107
+ %w[/root/test/.zshrc /root/test/.bashrc /root/test/.pryrc /root/test/.dropbox]
108
+ end
109
+ end
110
+ it "should return the list of uninstalled dotfiles in the root path" do
111
+ uninstalled = Dotify::Files.uninstalled.map { |i| Dotify::Files.filename(i) }
112
+ uninstalled.should_not include '.zshrc'
113
+ uninstalled.should_not include '.bashrc'
114
+ uninstalled.should include '.pryrc'
115
+ uninstalled.should include '.dropbox'
116
+ end
117
+ it "shoud yield the installed files if a block is given" do
118
+ uninstalled = Dotify::Files.uninstalled.map { |i| [i, Dotify::Files.filename(i)] }
119
+ expect { |b| Dotify::Files.uninstalled(&b) }.to yield_successive_args(*uninstalled)
120
+ end
121
+ end
122
+
101
123
  describe Dotify::Files, "#template?" do
102
124
  it "should return true if the string given is a .tt or .erb template" do
103
125
  Dotify::Files.template?("testing.erb").should == true
@@ -117,7 +139,7 @@ describe Dotify::Files do
117
139
  describe Dotify::Files, "#link_dotfile" do
118
140
  it "should receive a file and link it into the root path" do
119
141
  first = File.join(Dotify::Config.path, ".vimrc")
120
- FileUtils.should_receive(:ln_s).with(Dotify::Files.file_name(first), Dotify::Config.home).once
142
+ FileUtils.should_receive(:ln_s).with(Dotify::Files.filename(first), Dotify::Config.home).once
121
143
  Dotify::Files.link_dotfile first
122
144
  end
123
145
  end
@@ -125,7 +147,7 @@ describe Dotify::Files do
125
147
  describe Dotify::Files, "#unlink_dotfile" do
126
148
  it "should receive a file and remove it from the root" do
127
149
  first = "/spec/test/.file"
128
- FileUtils.stub(:rm_rf).with(File.join(Dotify::Config.home, Dotify::Files.file_name(first))).once
150
+ FileUtils.stub(:rm_rf).with(File.join(Dotify::Config.home, Dotify::Files.filename(first))).once
129
151
  Dotify::Files.unlink_dotfile first
130
152
  FileUtils.unstub(:rm_rf)
131
153
  end