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.
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 |
# File 'lib/dotsmack.rb', line 60 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.
52 53 54 |
# File 'lib/dotsmack.rb', line 52 def additional_ignores @additional_ignores end |
#dotconfig ⇒ Object
Returns the value of attribute dotconfig.
52 53 54 |
# File 'lib/dotsmack.rb', line 52 def dotconfig @dotconfig end |
#dotignore ⇒ Object
Returns the value of attribute dotignore.
52 53 54 |
# File 'lib/dotsmack.rb', line 52 def dotignore @dotignore end |
#path2config ⇒ Object
Returns the value of attribute path2config.
52 53 54 |
# File 'lib/dotsmack.rb', line 52 def path2config @path2config end |
#path2ignore ⇒ Object
Returns the value of attribute path2ignore.
52 53 54 |
# File 'lib/dotsmack.rb', line 52 def path2ignore @path2ignore end |
Instance Method Details
#config(path) ⇒ Object
Returns dotconfig for path.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/dotsmack.rb', line 187 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.
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 |
# File 'lib/dotsmack.rb', line 215 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.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/dotsmack.rb', line 161 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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/dotsmack.rb', line 136 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.
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 |
# File 'lib/dotsmack.rb', line 102 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 |