Class: Dotsmack::Smacker
- Inherits:
-
Object
- Object
- Dotsmack::Smacker
- Defined in:
- lib/dotsmack.rb
Overview
Smacker dotfile scanner/enumerator.
Instance Attribute Summary collapse
-
#additional_ignores ⇒ Object
Returns the value of attribute additional_ignores.
-
#dotconfig ⇒ Object
Returns the value of attribute dotconfig.
-
#dotignore ⇒ Object
Returns the value of attribute dotignore.
-
#path2config ⇒ Object
Returns the value of attribute path2config.
-
#path2ignore ⇒ Object
Returns the value of attribute path2ignore.
Instance Method Summary collapse
-
#config(path) ⇒ Object
Returns dotconfig for path.
-
#enumerate(paths) ⇒ Object
Recursively enumerate files named or nested in paths array.
-
#ignore?(path) ⇒ Boolean
Determines whether path is dotignored.
-
#initialize(dotignore = nil, additional_ignores = [], dotconfig = nil) ⇒ Smacker
constructor
Create a Smacker dotfile scanner/enumerator.
-
#scan_for_dotconfig!(dir) ⇒ Object
Scan for dotconfig.
-
#scan_for_dotignore!(dir) ⇒ Object
Scan for dotignore.
Constructor Details
#initialize(dotignore = nil, additional_ignores = [], dotconfig = nil) ⇒ Smacker
Create a Smacker dotfile scanner/enumerator.
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 |
# File 'lib/dotsmack.rb', line 67 def initialize(dotignore = nil, additional_ignores = [], dotconfig = nil) @dotignore = dotignore @dotconfig = dotconfig @additional_ignores = additional_ignores @path2dotignore = {} @path2dotconfig = {} if !dotignore.nil? home_dotignore = "#{HOME}#{File::SEPARATOR}#{dotignore}" @path2dotignore[PARENT_OF_HOME] = [] @path2dotignore[HOME] = if File.exist?(home_dotignore) open(home_dotignore).read.split("\n") else [] end end if !dotconfig.nil? home_dotconfig = "#{HOME}#{File::SEPARATOR}#{dotconfig}" @path2dotconfig[PARENT_OF_HOME] = nil @path2dotconfig[HOME] = if File.exist?(home_dotconfig) open(home_dotconfig).read else nil end end end |
Instance Attribute Details
#additional_ignores ⇒ Object
Returns the value of attribute additional_ignores.
59 60 61 |
# File 'lib/dotsmack.rb', line 59 def additional_ignores @additional_ignores end |
#dotconfig ⇒ Object
Returns the value of attribute dotconfig.
59 60 61 |
# File 'lib/dotsmack.rb', line 59 def dotconfig @dotconfig end |
#dotignore ⇒ Object
Returns the value of attribute dotignore.
59 60 61 |
# File 'lib/dotsmack.rb', line 59 def dotignore @dotignore end |
#path2config ⇒ Object
Returns the value of attribute path2config.
59 60 61 |
# File 'lib/dotsmack.rb', line 59 def path2config @path2config end |
#path2ignore ⇒ Object
Returns the value of attribute path2ignore.
59 60 61 |
# File 'lib/dotsmack.rb', line 59 def path2ignore @path2ignore end |
Instance Method Details
#config(path) ⇒ Object
Returns dotconfig for path.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/dotsmack.rb', line 194 def config(path) path = File.absolute_path(path) dir = if File.directory?(path) path else File.('..', path) end relative_to_home = dir.start_with?(HOME) if !relative_to_home dir = HOME end scan_for_dotconfig!(dir) @path2dotconfig[dir] end |
#enumerate(paths) ⇒ Object
Recursively enumerate files named or nested in paths array.
Skips dotignored files.
Returns [file, dotconfig] pairs.
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 258 |
# File 'lib/dotsmack.rb', line 222 def enumerate(paths) files = [] paths.each do |path| # STDIN idiom if path == '-' files << [ path, if @dotconfig.nil? nil else config(Dir.pwd) end ] # Skip symlinks elsif File.symlink?(path) nil elsif File.directory?(path) && (@dotignore.nil? || !ignore?(path)) files.concat( enumerate( Find.find(path).reject do |p| File.directory?(p) end ) ) elsif !ignore?(path) files << [ path, if @dotconfig.nil? nil else config(path) end ] end end files end |
#ignore?(path) ⇒ Boolean
Determines whether path is dotignored.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/dotsmack.rb', line 168 def ignore?(path) path = File.absolute_path(path) dir = if File.directory?(path) path else File.('..', path) end relative_to_home = dir.start_with?(HOME) if !relative_to_home dir = HOME end scan_for_dotignore!(dir) (@path2dotignore[dir] + @additional_ignores).any? do |pattern| Dotsmack::fnmatch?(pattern, path) end end |
#scan_for_dotconfig!(dir) ⇒ Object
Scan for dotconfig.
Assumes dir is an absolute path. Assumes dir is not a symlink. Assumes dir is $HOME-relative.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/dotsmack.rb', line 143 def scan_for_dotconfig!(dir) if @path2dotconfig.has_key?(dir) @path2dotconfig[dir] else candidate = "#{dir}#{File::SEPARATOR}#{@dotconfig}" parent = File.('..', dir) # Use first dotconfig found. dotconfig = if File.exist?(candidate) open(candidate).read else scan_for_dotconfig!(parent) end @path2dotconfig[dir] = dotconfig dotconfig end end |
#scan_for_dotignore!(dir) ⇒ Object
Scan for dotignore.
Assumes dir is an absolute path. Assumes dir is not a symlink. Assumes dir is $HOME-relative.
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 |
# File 'lib/dotsmack.rb', line 109 def scan_for_dotignore!(dir) if @path2dotignore.has_key?(dir) @path2dotignore[dir] else candidate = "#{dir}#{File::SEPARATOR}#{@dotignore}" dotignore = if File.exist?(candidate) open(candidate).read.split("\n") else [] end parent = File.('..', dir) # Determine parent dotignore parents_dotignore = scan_for_dotignore!(parent) # Merge candidate and parent information dotignore.concat(parents_dotignore) @path2dotignore[dir] = dotignore dotignore end end |