Class: KFileset::FileSet
- Inherits:
-
Object
- Object
- KFileset::FileSet
- Defined in:
- lib/k_fileset/file_set.rb
Overview
FileSet will build up a list of files and/or folders that match a whitelist.
Path <PathEntry> will point to a real file or folder
That list of files can be filtered using exclusions that follow either Glob or Regex patterns.
Resources:
- Rake-FileList: https://github.dev/ruby/rake/blob/5c60da8644a9e4f655e819252e3b6ca77f42b7af/lib/rake/file_list.rb
- Glob vs Regex: https://www.linuxjournal.com/content/globbing-and-regex-so-similar-so-different
- Glob patterns: http://web.mit.edu/racket_v612/amd64_ubuntu1404/racket/doc/file/glob.html
require 'rake/file_list'
Rake::FileList['**/*'].exclude(*File.read('.gitignore').split)
Constant Summary collapse
- DEF_FLAGS =
File::FNM_PATHNAME | File::FNM_EXTGLOB
- DEF_FLAGS_DOTMATCH =
File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
- DEFAULT_IGNORE_PATTERNS =
[ %r{(^|[/\\])CVS([/\\]|$)}, %r{(^|[/\\])\.svn([/\\]|$)}, /\.bak$/, /~$/ ].freeze
- IGNORE_LAMBDAS =
{ # Match Examples: 'target/deep/a', 'target/deep/a/.', 'target/deep/a/b', 'target/deep/a/b/.' folder: ->(path) { File.directory?(path) }, # Match Examples: 'target/deep/a', 'target/deep/a/b' folder_current: ->(path) { File.directory?(path) && !path.end_with?('.') }, # Match Examples: 'target/deep/a/.', 'target/deep/a/b/.' folder_current_dot: ->(path) { File.directory?(path) && path.end_with?('.') } }.freeze
- DEFAULT_IGNORE_LAMBDAS =
proc { |fn| fn =~ /(^|[/\])core$/ && !File.directory?(fn) }
[ IGNORE_LAMBDAS[:folder] ].freeze
- GLOB_PATTERN =
Expression to detect standard file GLOB pattern
/[*?\[{]/.freeze
Instance Attribute Summary collapse
-
#file_set ⇒ Object
writeonly
paths / resources / path_entries / valid_resources.
-
#whitelist ⇒ Object
readonly
whitelist (rules).
Instance Method Summary collapse
- #absolute_paths ⇒ Object
-
#add(path) ⇒ Object
Add absolute file.
- #clear ⇒ Object
- #debug ⇒ Object
-
#glob(glob, exclude: nil, flags: DEF_FLAGS, use_defaults: true) ⇒ Object
Add a Glob with option exclusions to the whitelist.
-
#initialize ⇒ FileSet
constructor
attr_reader :whitelist_rules # whitelist (rules) attr_reader :whitelist_files # whitelist (rules).
- #length ⇒ Object
-
#path_entries ⇒ Object
false end.
- #pwd ⇒ Object
- #relative_paths ⇒ Object
- #remove(path) ⇒ Object
Constructor Details
Instance Attribute Details
#file_set=(value) ⇒ Object
paths / resources / path_entries / valid_resources
47 48 49 |
# File 'lib/k_fileset/file_set.rb', line 47 def file_set=(value) @file_set = value end |
#whitelist ⇒ Object (readonly)
whitelist (rules)
48 49 50 |
# File 'lib/k_fileset/file_set.rb', line 48 def whitelist @whitelist end |
Instance Method Details
#absolute_paths ⇒ Object
120 121 122 |
# File 'lib/k_fileset/file_set.rb', line 120 def absolute_paths path_entries.map { |entry| entry.realpath.to_s } end |
#add(path) ⇒ Object
Add absolute file
83 84 85 86 87 88 89 |
# File 'lib/k_fileset/file_set.rb', line 83 def add(path) path = Pathname.new(path).realpath return if file_set.key?(path) return unless whitelist.match?(file) file_set.add(file) end |
#clear ⇒ Object
91 92 93 94 95 |
# File 'lib/k_fileset/file_set.rb', line 91 def clear @dirty = true @file_set.clear @whitelist.clear end |
#debug ⇒ Object
133 134 135 |
# File 'lib/k_fileset/file_set.rb', line 133 def debug path_entries.each(&:debug) end |
#glob(glob, exclude: nil, flags: DEF_FLAGS, use_defaults: true) ⇒ Object
Add a Glob with option exclusions to the whitelist
74 75 76 77 78 79 80 |
# File 'lib/k_fileset/file_set.rb', line 74 def glob(glob, exclude: nil, flags: DEF_FLAGS, use_defaults: true) exclude = add_default_exclusions(exclude, use_defaults) @whitelist.add(glob, exclude, flags) @dirty = true self end |
#length ⇒ Object
97 98 99 |
# File 'lib/k_fileset/file_set.rb', line 97 def length file_set.length end |
#path_entries ⇒ Object
false end
112 113 114 |
# File 'lib/k_fileset/file_set.rb', line 112 def path_entries @path_entries ||= file_set.values.sort_by(&:key) end |
#pwd ⇒ Object
124 125 126 |
# File 'lib/k_fileset/file_set.rb', line 124 def pwd Dir.pwd end |
#relative_paths ⇒ Object
116 117 118 |
# File 'lib/k_fileset/file_set.rb', line 116 def relative_paths path_entries.map(&:to_path) # relative_path } end |
#remove(path) ⇒ Object
128 129 130 131 |
# File 'lib/k_fileset/file_set.rb', line 128 def remove(path) path = abs_path(path) file_set.delete(path) end |