utils 0.87.0 → 0.89.0
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.
- checksums.yaml +4 -4
- data/bin/discover +16 -13
- data/lib/utils/finder/files.rb +218 -0
- data/lib/utils/finder.rb +48 -164
- data/lib/utils/version.rb +1 -1
- data/utils.gemspec +4 -4
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b21da845f5e0937016b3f9cc29f8b103060e983463d1968d4a7431bd25530967
|
|
4
|
+
data.tar.gz: 1890ec20374a0dfaa622db99388b1c32104140cc9022d922793e13f961d96d89
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bde7061f6fe57eba9053975c92a5c0d100221263f6a5e8aaa643555ae86f128730278ce2861e8d0d34947dbe32091d6d3cfa8f9239fadcb72695b0cabaeabff9
|
|
7
|
+
data.tar.gz: 003b6cdc53672c33e9ad68dbfd00e93bbd2ae8b3518095d023cc55426816dbb69a0c35972b06742b6d7fa4e1790d89729fa30280cd6ad9e5d55a27286ff2b531
|
data/bin/discover
CHANGED
|
@@ -93,7 +93,7 @@ Version is #{File.basename($0)} #{Utils::VERSION}.
|
|
|
93
93
|
exit 1
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
-
args = go 'n:I:i:a:p:
|
|
96
|
+
args = go 'n:I:i:a:p:lLcreEvdsDgh', defaults: { ?a => '\\w' }
|
|
97
97
|
args[?h] and usage
|
|
98
98
|
|
|
99
99
|
Term::ANSIColor.coloring = (STDIN.tty? && ENV['TERM'] !~ /dumb/) && !args[?c]
|
|
@@ -107,20 +107,23 @@ if args[?s]
|
|
|
107
107
|
else
|
|
108
108
|
pattern = ARGV.shift || ''
|
|
109
109
|
end
|
|
110
|
-
roots =
|
|
110
|
+
roots = nil
|
|
111
|
+
files = nil
|
|
112
|
+
if args[?g]
|
|
113
|
+
git_ls_files = `git ls-files`
|
|
114
|
+
$?.success? or abort('need git in path')
|
|
115
|
+
files = git_ls_files.each_line.map { |f| File.expand_path(f.chomp) }
|
|
116
|
+
else
|
|
117
|
+
roots = (ARGV.empty? ? [ Dir.pwd ] : ARGV).map { |f| File.expand_path(f) }
|
|
118
|
+
end
|
|
111
119
|
|
|
112
120
|
finder = -> * {
|
|
113
|
-
Finder.new(
|
|
114
|
-
:pattern => pattern,
|
|
115
|
-
:args => args,
|
|
116
|
-
:roots => roots,
|
|
117
|
-
:config => config
|
|
118
|
-
)
|
|
121
|
+
Finder.new(pattern:, args:, roots:, files:, config:)
|
|
119
122
|
}
|
|
120
123
|
|
|
121
124
|
search = -> * {
|
|
122
|
-
f
|
|
123
|
-
pattern
|
|
125
|
+
f = nil
|
|
126
|
+
pattern = pattern.dup
|
|
124
127
|
args[?n] = Term::ANSIColor.terminal_lines - 1
|
|
125
128
|
found = Search.new(
|
|
126
129
|
match: -> answer {
|
|
@@ -151,11 +154,11 @@ search = -> * {
|
|
|
151
154
|
|
|
152
155
|
case
|
|
153
156
|
when args[?l]
|
|
154
|
-
puts finder.().
|
|
157
|
+
puts finder.().search.paths.map {
|
|
155
158
|
Pathname.new(_1).expand_path.relative_path_from(Dir.pwd)
|
|
156
159
|
}
|
|
157
160
|
when args[?L]
|
|
158
|
-
puts finder.().
|
|
161
|
+
puts finder.().search.paths
|
|
159
162
|
when args[?s]
|
|
160
163
|
search.()
|
|
161
164
|
when args[?E]
|
|
@@ -164,7 +167,7 @@ when args[?e]
|
|
|
164
167
|
edit_files(*finder.().search.paths)
|
|
165
168
|
when args[?D]
|
|
166
169
|
args[?d] = true
|
|
167
|
-
paths = finder.().
|
|
170
|
+
paths = finder.().current_paths.select { |p| File.directory?(p) }.reverse
|
|
168
171
|
leaf_dirs = []
|
|
169
172
|
until paths.empty?
|
|
170
173
|
path = paths.shift
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# A module that provides file system traversal and path management
|
|
2
|
+
# functionality for the Utils library.
|
|
3
|
+
#
|
|
4
|
+
# This module includes methods for discovering root directories, generating
|
|
5
|
+
# unique index paths, creating and loading file path caches, and resetting
|
|
6
|
+
# indexes based on configuration settings.
|
|
7
|
+
module Utils::Finder::Files
|
|
8
|
+
include Utils::XDG
|
|
9
|
+
include Tins::Find
|
|
10
|
+
|
|
11
|
+
# The current_paths method retrieves the cached file paths from the index.
|
|
12
|
+
#
|
|
13
|
+
# This method loads and returns the file paths that have been previously
|
|
14
|
+
# indexed and stored in the cache, providing quick access to the collection
|
|
15
|
+
# of paths without reprocessing the file system.
|
|
16
|
+
#
|
|
17
|
+
# @return [ Array<String> ] an array of file path strings that were
|
|
18
|
+
# previously indexed and cached
|
|
19
|
+
def current_paths
|
|
20
|
+
load_paths
|
|
21
|
+
end
|
|
22
|
+
memoize method: :current_paths
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
# The discover_roots method processes an array of root patterns and expands
|
|
27
|
+
# them into actual directory paths.
|
|
28
|
+
#
|
|
29
|
+
# This method takes an array of root patterns, which may include glob patterns,
|
|
30
|
+
# and uses Dir[r] to expand each pattern into matching directory paths.
|
|
31
|
+
# It handles the case where the input roots array is nil by defaulting to an
|
|
32
|
+
# empty array.
|
|
33
|
+
#
|
|
34
|
+
# @param roots [ Array<String>, nil ] an array of root patterns or nil
|
|
35
|
+
#
|
|
36
|
+
# @return [ Array<String> ] an array of expanded directory paths that match
|
|
37
|
+
# the input patterns
|
|
38
|
+
def discover_roots(roots)
|
|
39
|
+
roots = Array(roots)
|
|
40
|
+
roots.inject([]) { |rs, r| rs.concat Dir[r] }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The index_path method generates a unique file path for storing finder
|
|
44
|
+
# results.
|
|
45
|
+
#
|
|
46
|
+
# This method creates a standardized location in the temporary directory for
|
|
47
|
+
# caching finder path data based on the root directories being processed.
|
|
48
|
+
# It ensures uniqueness by hashing the sorted root paths and uses the current
|
|
49
|
+
# script name as part of the directory structure.
|
|
50
|
+
#
|
|
51
|
+
# @return [ String ] the full file path where finder results should be stored
|
|
52
|
+
def index_path
|
|
53
|
+
roots = @roots.map { |r| File.expand_path(r) }.uniq.sort
|
|
54
|
+
filename = "finder-paths-" + Digest::MD5.new.update(roots.inspect).hexdigest
|
|
55
|
+
XDG_CACHE_HOME.sub_dir_path('utils') + filename
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The create_paths method generates and stores path information by building a
|
|
59
|
+
# list of paths, writing them to a secure file, and then returning the list
|
|
60
|
+
# of paths.
|
|
61
|
+
#
|
|
62
|
+
# @return [ Array ] an array containing the paths that were built and written
|
|
63
|
+
# to the index file
|
|
64
|
+
def create_paths
|
|
65
|
+
paths = build_paths
|
|
66
|
+
File.secure_write(index_path) do |output|
|
|
67
|
+
output.puts paths
|
|
68
|
+
end
|
|
69
|
+
paths
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The load_paths method reads and processes indexed file paths from disk.
|
|
73
|
+
#
|
|
74
|
+
# This method loads lines from the index file path, removes trailing
|
|
75
|
+
# whitespace, and filters out directory entries if the debug flag is not set.
|
|
76
|
+
# It returns create_paths if the index file is empty or missing,
|
|
77
|
+
# otherwise it returns the processed list of file paths.
|
|
78
|
+
#
|
|
79
|
+
# @return [ Array<String> ] an array of file paths loaded from the index
|
|
80
|
+
def load_paths
|
|
81
|
+
if @files
|
|
82
|
+
return @files
|
|
83
|
+
end
|
|
84
|
+
lines = File.readlines(index_path)
|
|
85
|
+
@args[?v] and warn "Loaded index #{index_path.inspect}."
|
|
86
|
+
lines.empty? and raise Errno::ENOENT
|
|
87
|
+
@args[?d] or lines = lines.grep_v(%r{/$})
|
|
88
|
+
lines.map(&:chomp!)
|
|
89
|
+
rescue Errno::ENOENT
|
|
90
|
+
return create_paths
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The reset_index method resets the index file by removing it if the reset
|
|
94
|
+
# flag is set or if the index has expired.
|
|
95
|
+
#
|
|
96
|
+
# This method checks whether the reset argument flag is set or if the index
|
|
97
|
+
# file has expired based on its modification time.
|
|
98
|
+
# If either condition is true, it removes the index file from the filesystem
|
|
99
|
+
# and clears the mize cache. The method then returns the instance itself to
|
|
100
|
+
# allow for method chaining.
|
|
101
|
+
#
|
|
102
|
+
# @return [ Utils::Finder ] returns self to allow for method chaining
|
|
103
|
+
def reset_index
|
|
104
|
+
@files and return
|
|
105
|
+
path = index_path
|
|
106
|
+
if @args[?r] || index_expired?(path)
|
|
107
|
+
@args[?v] and warn "Resetting index #{path.inspect}."
|
|
108
|
+
FileUtils.rm_f path
|
|
109
|
+
mize_cache_clear
|
|
110
|
+
end
|
|
111
|
+
self
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The index_expired? method checks whether the index file has exceeded its
|
|
115
|
+
# expiration duration.
|
|
116
|
+
#
|
|
117
|
+
# This method determines if the specified index file is considered expired
|
|
118
|
+
# based on the configured discovery index expiration time. It compares the
|
|
119
|
+
# current time with the modification time of the file to make this
|
|
120
|
+
# determination.
|
|
121
|
+
#
|
|
122
|
+
# @param path [ String ] the filesystem path to the index file being checked
|
|
123
|
+
#
|
|
124
|
+
# @return [ TrueClass, FalseClass ] true if the index file has expired, false otherwise
|
|
125
|
+
def index_expired?(path)
|
|
126
|
+
@files and return false
|
|
127
|
+
if duration = @config.discover.index_expire_after
|
|
128
|
+
Time.now - duration >= File.mtime(path)
|
|
129
|
+
else
|
|
130
|
+
false
|
|
131
|
+
end
|
|
132
|
+
rescue Errno::ENOENT
|
|
133
|
+
false
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The each_path_from_roots method iterates through root directories and
|
|
137
|
+
# processes each file or directory while applying filtering logic based on
|
|
138
|
+
# configuration settings.
|
|
139
|
+
#
|
|
140
|
+
# This method sets up a visit lambda that checks whether each file or
|
|
141
|
+
# directory should be pruned or skipped based on the discover configuration's
|
|
142
|
+
# prune and skip patterns. It uses the find utility to traverse the
|
|
143
|
+
# filesystem starting from the configured root directories, applying the
|
|
144
|
+
# visit logic to determine which entries to process and which to ignore. When
|
|
145
|
+
# a directory is encountered, it appends a trailing slash to the filename for
|
|
146
|
+
# distinction. The method yields each qualifying filename to the provided
|
|
147
|
+
# block for further processing.
|
|
148
|
+
#
|
|
149
|
+
# @param block [ Proc ] the block to be executed for each qualifying filename
|
|
150
|
+
#
|
|
151
|
+
# @yield [ filename ] yields the filename to the provided block
|
|
152
|
+
# @yieldparam filename [ String ] the path to the file or directory being processed
|
|
153
|
+
def each_path_from_roots(&block)
|
|
154
|
+
visit = -> filename {
|
|
155
|
+
s = filename.stat
|
|
156
|
+
bn = filename.pathname.basename
|
|
157
|
+
if !s ||
|
|
158
|
+
s.directory? && @config.discover.prune?(bn) ||
|
|
159
|
+
s.file? && @config.discover.skip?(bn)
|
|
160
|
+
then
|
|
161
|
+
@args[?v] and warn "Pruning #{filename.inspect}."
|
|
162
|
+
prune
|
|
163
|
+
end
|
|
164
|
+
true
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
find(*@roots, visit:) do |filename|
|
|
168
|
+
filename.directory? and filename << ?/
|
|
169
|
+
yield filename
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# The each_path_from_files method iterates through a collection of file paths
|
|
174
|
+
# and processes each one according to configured filtering rules
|
|
175
|
+
#
|
|
176
|
+
# This method takes an array of file paths and applies filtering logic based
|
|
177
|
+
# on the discover configuration's skip patterns to determine whether each
|
|
178
|
+
# file should be processed or skipped. It extends each filename with path
|
|
179
|
+
# extension functionality and handles both regular files and directories
|
|
180
|
+
# differently, appending a trailing slash to directory entries
|
|
181
|
+
# before yielding them to the provided block for further processing
|
|
182
|
+
#
|
|
183
|
+
# @param block [ Proc ] the block to be executed for each qualifying filename
|
|
184
|
+
#
|
|
185
|
+
# @yield [ filename ] yields the filename to the provided block
|
|
186
|
+
# @yieldparam filename [ String ] the path to the file or directory being processed
|
|
187
|
+
def each_path_from_files(&block)
|
|
188
|
+
@files.each do |filename|
|
|
189
|
+
filename.extend(::Tins::Find::Finder::PathExtension)
|
|
190
|
+
if filename.file?
|
|
191
|
+
@config.discover.skip?(filename.pathname.basename) and next
|
|
192
|
+
elsif filename.directory?
|
|
193
|
+
filename << ?/
|
|
194
|
+
end
|
|
195
|
+
block.(filename)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# The each_path method iterates through file paths based on configured roots
|
|
200
|
+
# or pre-loaded files
|
|
201
|
+
#
|
|
202
|
+
# This method determines whether to process file paths from configured root
|
|
203
|
+
# directories or from a pre-loaded collection of files, delegating to the
|
|
204
|
+
# appropriate internal method based on the presence of root directories
|
|
205
|
+
#
|
|
206
|
+
# @param block [ Proc ] the block to be executed for each qualifying filename
|
|
207
|
+
#
|
|
208
|
+
# @yield [ filename ] yields the filename to the provided block
|
|
209
|
+
# @yieldparam filename [ String ] the path to the file or directory being
|
|
210
|
+
# processed
|
|
211
|
+
def each_path(&block)
|
|
212
|
+
if @roots
|
|
213
|
+
each_path_from_roots(&block)
|
|
214
|
+
else
|
|
215
|
+
each_path_from_files(&block)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
data/lib/utils/finder.rb
CHANGED
|
@@ -3,6 +3,9 @@ require 'tempfile'
|
|
|
3
3
|
require 'digest/md5'
|
|
4
4
|
require 'fileutils'
|
|
5
5
|
require 'mize'
|
|
6
|
+
class Utils::Finder
|
|
7
|
+
end
|
|
8
|
+
require 'utils/finder/files'
|
|
6
9
|
|
|
7
10
|
# A class for finding and searching files with configurable patterns and
|
|
8
11
|
# filters.
|
|
@@ -17,25 +20,43 @@ require 'mize'
|
|
|
17
20
|
# finder = Utils::Finder.new(args: { l: true }, roots: ['.'])
|
|
18
21
|
# finder.search
|
|
19
22
|
class Utils::Finder
|
|
20
|
-
include Tins::Find
|
|
21
|
-
include Utils::Patterns
|
|
22
|
-
include Utils::XDG
|
|
23
23
|
include Term::ANSIColor
|
|
24
|
+
include Utils::Patterns
|
|
25
|
+
include Utils::Finder::Files
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
|
|
28
|
+
# Initializes a new Finder instance with the specified options.
|
|
29
|
+
#
|
|
30
|
+
# Configures the finder to search either specific files or directories based on
|
|
31
|
+
# the provided arguments. Handles command-line argument parsing and pattern
|
|
32
|
+
# configuration for search operations.
|
|
33
|
+
#
|
|
34
|
+
# @example Basic usage with root directories
|
|
35
|
+
# finder = Utils::Finder.new(args: { l: true }, roots: ['.'])
|
|
26
36
|
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
# the pattern matcher based on the specified options and prepares the index
|
|
30
|
-
# for searching.
|
|
37
|
+
# @example Usage with specific files
|
|
38
|
+
# finder = Utils::Finder.new(args: { l: true }, files: ['file1.rb', 'file2.rb'])
|
|
31
39
|
#
|
|
32
|
-
# @param opts [
|
|
33
|
-
# @option opts [
|
|
34
|
-
# @option opts [
|
|
35
|
-
# @option opts [
|
|
40
|
+
# @param opts [Hash] configuration options
|
|
41
|
+
# @option opts [Hash] :args Command-line arguments hash
|
|
42
|
+
# @option opts [Array<String>] :roots Root directories to search
|
|
43
|
+
# @option opts [Array<String>] :files Specific files to process
|
|
44
|
+
# @option opts [Utils::ConfigFile] :config (Utils::ConfigFile.new) Configuration object
|
|
45
|
+
# @option opts [Hash] :pattern Pattern-related options
|
|
46
|
+
# @option opts [String] :pattern :cset Character set for pattern matching
|
|
47
|
+
# @option opts [Boolean] :pattern :icase Case insensitive matching
|
|
48
|
+
#
|
|
49
|
+
# @raise [ArgumentError] When both :roots and :files are specified
|
|
50
|
+
#
|
|
51
|
+
# @return [Utils::Finder] A configured finder instance ready for search operations
|
|
36
52
|
def initialize(opts = {})
|
|
37
53
|
@args = opts[:args] || {}
|
|
38
|
-
|
|
54
|
+
if opts[:files]
|
|
55
|
+
opts[:roots] and raise ArgumentError, "Require :roots xor :files argument"
|
|
56
|
+
@files = opts[:files]
|
|
57
|
+
else
|
|
58
|
+
@roots = discover_roots(opts[:roots])
|
|
59
|
+
end
|
|
39
60
|
@config = opts[:config] || Utils::ConfigFile.new
|
|
40
61
|
if @args[?l] || @args[?L]
|
|
41
62
|
@pattern = nil
|
|
@@ -75,97 +96,12 @@ class Utils::Finder
|
|
|
75
96
|
# entries are properly marked with a trailing slash for distinction. The
|
|
76
97
|
# resulting paths are deduplicated before being returned.
|
|
77
98
|
#
|
|
78
|
-
# @return [ Array ] an array of file system path strings, with directories
|
|
99
|
+
# @return [ Array<String> ] an array of file system path strings, with directories
|
|
79
100
|
# marked by a trailing slash
|
|
80
101
|
def build_paths
|
|
81
|
-
paths = []
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
s = filename.stat
|
|
85
|
-
bn = filename.pathname.basename
|
|
86
|
-
if !s ||
|
|
87
|
-
s.directory? && @config.discover.prune?(bn) ||
|
|
88
|
-
s.file? && @config.discover.skip?(bn)
|
|
89
|
-
then
|
|
90
|
-
@args[?v] and warn "Pruning #{filename.inspect}."
|
|
91
|
-
prune
|
|
92
|
-
end
|
|
93
|
-
true
|
|
94
|
-
}
|
|
95
|
-
find(*@roots, visit:) do |filename|
|
|
96
|
-
filename.stat.directory? and filename << ?/
|
|
97
|
-
paths << filename
|
|
98
|
-
end
|
|
99
|
-
paths.uniq!
|
|
100
|
-
paths
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
# The index_path method generates a unique file path for storing finder
|
|
104
|
-
# results.
|
|
105
|
-
#
|
|
106
|
-
# This method creates a standardized location in the temporary directory for
|
|
107
|
-
# caching finder path data based on the root directories being processed.
|
|
108
|
-
# It ensures uniqueness by hashing the sorted root paths and uses the current
|
|
109
|
-
# script name as part of the directory structure.
|
|
110
|
-
#
|
|
111
|
-
# @return [ String ] the full file path where finder results should be stored
|
|
112
|
-
def index_path
|
|
113
|
-
roots = @roots.map { |r| File.expand_path(r) }.uniq.sort
|
|
114
|
-
filename = "finder-paths-" + Digest::MD5.new.update(roots.inspect).hexdigest
|
|
115
|
-
XDG_CACHE_HOME.sub_dir_path('utils') + filename
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
# The create_paths method generates and stores path information by building a
|
|
119
|
-
# list of paths, writing them to a secure file, and then returning the list
|
|
120
|
-
# of paths.
|
|
121
|
-
#
|
|
122
|
-
# @return [ Array ] an array containing the paths that were built and written
|
|
123
|
-
# to the index file
|
|
124
|
-
def create_paths
|
|
125
|
-
paths = build_paths
|
|
126
|
-
File.secure_write(index_path) do |output|
|
|
127
|
-
output.puts paths
|
|
128
|
-
end
|
|
129
|
-
paths
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
# The load_paths method reads and processes indexed file paths from disk.
|
|
133
|
-
#
|
|
134
|
-
# This method loads lines from the index file path, removes trailing
|
|
135
|
-
# whitespace, and filters out directory entries if the debug flag is not set.
|
|
136
|
-
# It returns create_paths if the index file is empty or missing,
|
|
137
|
-
# otherwise it returns the processed list of file paths.
|
|
138
|
-
#
|
|
139
|
-
# @return [ Array<String> ] an array of file paths loaded from the index
|
|
140
|
-
memoize method:
|
|
141
|
-
def load_paths
|
|
142
|
-
lines = File.readlines(index_path)
|
|
143
|
-
@args[?v] and warn "Loaded index #{index_path.inspect}."
|
|
144
|
-
lines.empty? and raise Errno::ENOENT
|
|
145
|
-
@args[?d] or lines = lines.grep_v(%r{/$})
|
|
146
|
-
lines.map(&:chomp!)
|
|
147
|
-
rescue Errno::ENOENT
|
|
148
|
-
return create_paths
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
# The reset_index method resets the index file by removing it if the reset
|
|
152
|
-
# flag is set or if the index has expired.
|
|
153
|
-
#
|
|
154
|
-
# This method checks whether the reset argument flag is set or if the index
|
|
155
|
-
# file has expired based on its modification time.
|
|
156
|
-
# If either condition is true, it removes the index file from the filesystem
|
|
157
|
-
# and clears the mize cache. The method then returns the instance itself to
|
|
158
|
-
# allow for method chaining.
|
|
159
|
-
#
|
|
160
|
-
# @return [ Utils::Finder ] returns self to allow for method chaining
|
|
161
|
-
def reset_index
|
|
162
|
-
path = index_path
|
|
163
|
-
if @args[?r] || index_expired?(path)
|
|
164
|
-
@args[?v] and warn "Resetting index #{path.inspect}."
|
|
165
|
-
FileUtils.rm_f path
|
|
166
|
-
mize_cache_clear
|
|
167
|
-
end
|
|
168
|
-
self
|
|
102
|
+
paths = Set[]
|
|
103
|
+
each_path { |path| paths << path }
|
|
104
|
+
paths.sort
|
|
169
105
|
end
|
|
170
106
|
|
|
171
107
|
# The search_paths method processes and filters a collection of file paths
|
|
@@ -184,7 +120,7 @@ class Utils::Finder
|
|
|
184
120
|
suffixes.full? do |s|
|
|
185
121
|
paths.select! { |path| s.include?(File.extname(path)[1..-1]) }
|
|
186
122
|
end
|
|
187
|
-
paths = paths.map
|
|
123
|
+
paths = paths.map do |path|
|
|
188
124
|
if @pattern.nil?
|
|
189
125
|
[ [ path.count(?/), path ], path, path ]
|
|
190
126
|
elsif match = @pattern.match(path)
|
|
@@ -221,68 +157,16 @@ class Utils::Finder
|
|
|
221
157
|
self
|
|
222
158
|
end
|
|
223
159
|
|
|
224
|
-
# The
|
|
225
|
-
#
|
|
160
|
+
# The search method executes a file search operation using pre-loaded paths
|
|
161
|
+
# and configured patterns
|
|
226
162
|
#
|
|
227
|
-
# This method
|
|
228
|
-
#
|
|
229
|
-
# search
|
|
163
|
+
# This method leverages previously loaded file paths and applies the
|
|
164
|
+
# configured search patterns to filter and process the files. It serves as
|
|
165
|
+
# the main entry point for performing search operations within the configured
|
|
166
|
+
# file system scope.
|
|
230
167
|
#
|
|
231
|
-
# @return [
|
|
232
|
-
def
|
|
233
|
-
search_paths
|
|
234
|
-
end
|
|
235
|
-
|
|
236
|
-
# The search_index method performs a pattern search across previously loaded
|
|
237
|
-
# paths.
|
|
238
|
-
#
|
|
239
|
-
# This method utilizes the loaded paths from the internal storage to execute
|
|
240
|
-
# a search operation, applying the configured pattern matching criteria to
|
|
241
|
-
# filter and return relevant results based on the current search
|
|
242
|
-
# configuration.
|
|
243
|
-
def search_index
|
|
244
|
-
search_paths load_paths
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
alias search search_index
|
|
248
|
-
|
|
249
|
-
private
|
|
250
|
-
|
|
251
|
-
# The index_expired? method checks whether the index file has exceeded its
|
|
252
|
-
# expiration duration.
|
|
253
|
-
#
|
|
254
|
-
# This method determines if the specified index file is considered expired
|
|
255
|
-
# based on the configured discovery index expiration time. It compares the
|
|
256
|
-
# current time with the modification time of the file to make this
|
|
257
|
-
# determination.
|
|
258
|
-
#
|
|
259
|
-
# @param path [ String ] the filesystem path to the index file being checked
|
|
260
|
-
#
|
|
261
|
-
# @return [ TrueClass, FalseClass ] true if the index file has expired, false otherwise
|
|
262
|
-
def index_expired?(path)
|
|
263
|
-
if duration = @config.discover.index_expire_after
|
|
264
|
-
Time.now - duration >= File.mtime(path)
|
|
265
|
-
else
|
|
266
|
-
false
|
|
267
|
-
end
|
|
268
|
-
rescue Errno::ENOENT
|
|
269
|
-
false
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
# The discover_roots method processes an array of root patterns and expands
|
|
273
|
-
# them into actual directory paths.
|
|
274
|
-
#
|
|
275
|
-
# This method takes an array of root patterns, which may include glob patterns,
|
|
276
|
-
# and uses Dir[r] to expand each pattern into matching directory paths.
|
|
277
|
-
# It handles the case where the input roots array is nil by defaulting to an
|
|
278
|
-
# empty array.
|
|
279
|
-
#
|
|
280
|
-
# @param roots [ Array<String>, nil ] an array of root patterns or nil
|
|
281
|
-
#
|
|
282
|
-
# @return [ Array<String> ] an array of expanded directory paths that match
|
|
283
|
-
# the input patterns
|
|
284
|
-
def discover_roots(roots)
|
|
285
|
-
roots ||= []
|
|
286
|
-
roots.inject([]) { |rs, r| rs.concat Dir[r] }
|
|
168
|
+
# @return [ Utils::Finder ] returns self to allow for method chaining
|
|
169
|
+
def search
|
|
170
|
+
search_paths current_paths
|
|
287
171
|
end
|
|
288
172
|
end
|
data/lib/utils/version.rb
CHANGED
data/utils.gemspec
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
# stub: utils 0.
|
|
2
|
+
# stub: utils 0.89.0 ruby lib
|
|
3
3
|
|
|
4
4
|
Gem::Specification.new do |s|
|
|
5
5
|
s.name = "utils".freeze
|
|
6
|
-
s.version = "0.
|
|
6
|
+
s.version = "0.89.0".freeze
|
|
7
7
|
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
|
9
9
|
s.require_paths = ["lib".freeze]
|
|
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
|
|
|
12
12
|
s.description = "This ruby gem provides some useful command line utilities".freeze
|
|
13
13
|
s.email = "flori@ping.de".freeze
|
|
14
14
|
s.executables = ["ascii7".freeze, "blameline".freeze, "changes".freeze, "classify".freeze, "code_comment".freeze, "code_indexer".freeze, "commit_message".freeze, "discover".freeze, "edit".freeze, "edit_wait".freeze, "enum".freeze, "git-empty".freeze, "git-versions".freeze, "irb_client".freeze, "json_check".freeze, "long_lines".freeze, "myex".freeze, "on_change".freeze, "path".freeze, "print_method".freeze, "probe".freeze, "rainbow".freeze, "rd2md".freeze, "search".freeze, "sedit".freeze, "serve".freeze, "ssh-tunnel".freeze, "strip_spaces".freeze, "sync_dir".freeze, "untest".freeze, "utils-utilsrc".freeze, "vcf2alias".freeze, "yaml_check".freeze]
|
|
15
|
-
s.extra_rdoc_files = ["README.md".freeze, "lib/utils.rb".freeze, "lib/utils/config_file.rb".freeze, "lib/utils/config_file/block_config.rb".freeze, "lib/utils/editor.rb".freeze, "lib/utils/finder.rb".freeze, "lib/utils/grepper.rb".freeze, "lib/utils/irb.rb".freeze, "lib/utils/irb/irb_server.rb".freeze, "lib/utils/line_blamer.rb".freeze, "lib/utils/line_formatter.rb".freeze, "lib/utils/md5.rb".freeze, "lib/utils/patterns.rb".freeze, "lib/utils/probe.rb".freeze, "lib/utils/probe/probe_client.rb".freeze, "lib/utils/probe/probe_server.rb".freeze, "lib/utils/probe/process_job.rb".freeze, "lib/utils/probe/server_handling.rb".freeze, "lib/utils/ssh_tunnel_specification.rb".freeze, "lib/utils/version.rb".freeze, "lib/utils/xdg.rb".freeze, "lib/utils/xt/source_location_extension.rb".freeze]
|
|
16
|
-
s.files = ["Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "bin/ascii7".freeze, "bin/blameline".freeze, "bin/changes".freeze, "bin/classify".freeze, "bin/code_comment".freeze, "bin/code_indexer".freeze, "bin/commit_message".freeze, "bin/discover".freeze, "bin/edit".freeze, "bin/edit_wait".freeze, "bin/enum".freeze, "bin/git-empty".freeze, "bin/git-versions".freeze, "bin/irb_client".freeze, "bin/json_check".freeze, "bin/long_lines".freeze, "bin/myex".freeze, "bin/on_change".freeze, "bin/path".freeze, "bin/print_method".freeze, "bin/probe".freeze, "bin/rainbow".freeze, "bin/rd2md".freeze, "bin/search".freeze, "bin/sedit".freeze, "bin/serve".freeze, "bin/ssh-tunnel".freeze, "bin/strip_spaces".freeze, "bin/sync_dir".freeze, "bin/untest".freeze, "bin/utils-utilsrc".freeze, "bin/vcf2alias".freeze, "bin/yaml_check".freeze, "lib/utils.rb".freeze, "lib/utils/config_file.rb".freeze, "lib/utils/config_file/block_config.rb".freeze, "lib/utils/editor.rb".freeze, "lib/utils/finder.rb".freeze, "lib/utils/grepper.rb".freeze, "lib/utils/irb.rb".freeze, "lib/utils/irb/irb_server.rb".freeze, "lib/utils/line_blamer.rb".freeze, "lib/utils/line_formatter.rb".freeze, "lib/utils/md5.rb".freeze, "lib/utils/patterns.rb".freeze, "lib/utils/probe.rb".freeze, "lib/utils/probe/probe_client.rb".freeze, "lib/utils/probe/probe_server.rb".freeze, "lib/utils/probe/process_job.rb".freeze, "lib/utils/probe/server_handling.rb".freeze, "lib/utils/ssh_tunnel_specification.rb".freeze, "lib/utils/version.rb".freeze, "lib/utils/xdg.rb".freeze, "lib/utils/xt/source_location_extension.rb".freeze, "tests/test_helper.rb".freeze, "tests/utils_test.rb".freeze, "utils.gemspec".freeze]
|
|
15
|
+
s.extra_rdoc_files = ["README.md".freeze, "lib/utils.rb".freeze, "lib/utils/config_file.rb".freeze, "lib/utils/config_file/block_config.rb".freeze, "lib/utils/editor.rb".freeze, "lib/utils/finder.rb".freeze, "lib/utils/finder/files.rb".freeze, "lib/utils/grepper.rb".freeze, "lib/utils/irb.rb".freeze, "lib/utils/irb/irb_server.rb".freeze, "lib/utils/line_blamer.rb".freeze, "lib/utils/line_formatter.rb".freeze, "lib/utils/md5.rb".freeze, "lib/utils/patterns.rb".freeze, "lib/utils/probe.rb".freeze, "lib/utils/probe/probe_client.rb".freeze, "lib/utils/probe/probe_server.rb".freeze, "lib/utils/probe/process_job.rb".freeze, "lib/utils/probe/server_handling.rb".freeze, "lib/utils/ssh_tunnel_specification.rb".freeze, "lib/utils/version.rb".freeze, "lib/utils/xdg.rb".freeze, "lib/utils/xt/source_location_extension.rb".freeze]
|
|
16
|
+
s.files = ["Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "bin/ascii7".freeze, "bin/blameline".freeze, "bin/changes".freeze, "bin/classify".freeze, "bin/code_comment".freeze, "bin/code_indexer".freeze, "bin/commit_message".freeze, "bin/discover".freeze, "bin/edit".freeze, "bin/edit_wait".freeze, "bin/enum".freeze, "bin/git-empty".freeze, "bin/git-versions".freeze, "bin/irb_client".freeze, "bin/json_check".freeze, "bin/long_lines".freeze, "bin/myex".freeze, "bin/on_change".freeze, "bin/path".freeze, "bin/print_method".freeze, "bin/probe".freeze, "bin/rainbow".freeze, "bin/rd2md".freeze, "bin/search".freeze, "bin/sedit".freeze, "bin/serve".freeze, "bin/ssh-tunnel".freeze, "bin/strip_spaces".freeze, "bin/sync_dir".freeze, "bin/untest".freeze, "bin/utils-utilsrc".freeze, "bin/vcf2alias".freeze, "bin/yaml_check".freeze, "lib/utils.rb".freeze, "lib/utils/config_file.rb".freeze, "lib/utils/config_file/block_config.rb".freeze, "lib/utils/editor.rb".freeze, "lib/utils/finder.rb".freeze, "lib/utils/finder/files.rb".freeze, "lib/utils/grepper.rb".freeze, "lib/utils/irb.rb".freeze, "lib/utils/irb/irb_server.rb".freeze, "lib/utils/line_blamer.rb".freeze, "lib/utils/line_formatter.rb".freeze, "lib/utils/md5.rb".freeze, "lib/utils/patterns.rb".freeze, "lib/utils/probe.rb".freeze, "lib/utils/probe/probe_client.rb".freeze, "lib/utils/probe/probe_server.rb".freeze, "lib/utils/probe/process_job.rb".freeze, "lib/utils/probe/server_handling.rb".freeze, "lib/utils/ssh_tunnel_specification.rb".freeze, "lib/utils/version.rb".freeze, "lib/utils/xdg.rb".freeze, "lib/utils/xt/source_location_extension.rb".freeze, "tests/test_helper.rb".freeze, "tests/utils_test.rb".freeze, "utils.gemspec".freeze]
|
|
17
17
|
s.homepage = "http://github.com/flori/utils".freeze
|
|
18
18
|
s.licenses = ["GPL-2.0".freeze]
|
|
19
19
|
s.rdoc_options = ["--title".freeze, "Utils - Some useful command line utilities".freeze, "--main".freeze, "README.md".freeze]
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: utils
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.89.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Florian Frank
|
|
@@ -319,6 +319,7 @@ extra_rdoc_files:
|
|
|
319
319
|
- lib/utils/config_file/block_config.rb
|
|
320
320
|
- lib/utils/editor.rb
|
|
321
321
|
- lib/utils/finder.rb
|
|
322
|
+
- lib/utils/finder/files.rb
|
|
322
323
|
- lib/utils/grepper.rb
|
|
323
324
|
- lib/utils/irb.rb
|
|
324
325
|
- lib/utils/irb/irb_server.rb
|
|
@@ -378,6 +379,7 @@ files:
|
|
|
378
379
|
- lib/utils/config_file/block_config.rb
|
|
379
380
|
- lib/utils/editor.rb
|
|
380
381
|
- lib/utils/finder.rb
|
|
382
|
+
- lib/utils/finder/files.rb
|
|
381
383
|
- lib/utils/grepper.rb
|
|
382
384
|
- lib/utils/irb.rb
|
|
383
385
|
- lib/utils/irb/irb_server.rb
|