staticky-files 0.1.0 → 0.2.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/CHANGELOG.md +6 -0
- data/README.md +1 -0
- data/lib/staticky/files/file_system.rb +10 -0
- data/lib/staticky/files/memory_file_system/node.rb +23 -11
- data/lib/staticky/files/memory_file_system.rb +31 -4
- data/lib/staticky/files/version.rb +1 -1
- data/lib/staticky/files.rb +20 -9
- metadata +3 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad0e4764cd0d5ab60c632d49209c6cd374948538c973224a900011142349bf3c
|
4
|
+
data.tar.gz: f86bc8ded9348bf8e140e5d8c5a547201dd53d4cddb805f6edc36a6ad362ae33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e16a18e11d0432dfdad9ffbcd74a0e89d2e25bb2c62f2d2ad30977d95088809e786ee54df2b8d2c2f7930ff24e60b59f8542b06fc489995208a9cc1d387582a
|
7
|
+
data.tar.gz: bdde8d06c9bcf7338ac7afc19f195ed302289523314f798ba06679c19717ac29947e1bbc0b5c8b19e26718a74dd3b57e6ed832f4512412e49b93320c543d4eed
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -312,6 +312,16 @@ module Staticky
|
|
312
312
|
end
|
313
313
|
end
|
314
314
|
|
315
|
+
# Returns an array of file paths that match the given pattern.
|
316
|
+
#
|
317
|
+
# @param pattern [String] the glob pattern to match
|
318
|
+
# @return [Array<String>] the matching file paths
|
319
|
+
def glob(pattern)
|
320
|
+
with_error_handling do
|
321
|
+
Dir.glob(pattern)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
315
325
|
private
|
316
326
|
|
317
327
|
# Catch `SystemCallError` and re-raise a `Staticky::Files::IOError`.
|
@@ -20,15 +20,23 @@ module Staticky
|
|
20
20
|
MODE_OTHERS_EXECUTE = 0b000000001
|
21
21
|
|
22
22
|
# Default directory mode: 0755
|
23
|
-
DEFAULT_DIRECTORY_MODE = MODE_USER_READ |
|
24
|
-
|
25
|
-
|
23
|
+
DEFAULT_DIRECTORY_MODE = MODE_USER_READ |
|
24
|
+
MODE_USER_WRITE |
|
25
|
+
MODE_USER_EXECUTE |
|
26
|
+
MODE_GROUP_READ |
|
27
|
+
MODE_GROUP_EXECUTE |
|
28
|
+
MODE_OTHERS_READ |
|
29
|
+
MODE_GROUP_EXECUTE
|
26
30
|
|
27
31
|
# Default file mode: 0644
|
28
|
-
DEFAULT_FILE_MODE = MODE_USER_READ |
|
32
|
+
DEFAULT_FILE_MODE = MODE_USER_READ |
|
33
|
+
MODE_USER_WRITE |
|
34
|
+
MODE_GROUP_READ |
|
35
|
+
MODE_OTHERS_READ
|
29
36
|
|
30
37
|
MODE_BASE = 16
|
31
38
|
ROOT_PATH = "/"
|
39
|
+
EMPTY_CONTENT = ""
|
32
40
|
|
33
41
|
# Instantiate a root node
|
34
42
|
#
|
@@ -132,12 +140,12 @@ module Staticky
|
|
132
140
|
# @raise [Staticky::Files::NotMemoryFileError] if node isn't a file
|
133
141
|
def write(content)
|
134
142
|
content = case content
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
143
|
+
when String
|
144
|
+
content
|
145
|
+
when Array
|
146
|
+
array_to_string(content)
|
147
|
+
when NilClass
|
148
|
+
EMPTY_CONTENT
|
141
149
|
end
|
142
150
|
|
143
151
|
@content = StringIO.new(content)
|
@@ -155,8 +163,12 @@ module Staticky
|
|
155
163
|
# Check if node is executable for user
|
156
164
|
#
|
157
165
|
# @return [TrueClass,FalseClass] the result of the check
|
166
|
+
def readable?
|
167
|
+
mode.anybits?(MODE_USER_READ)
|
168
|
+
end
|
169
|
+
|
158
170
|
def executable?
|
159
|
-
(
|
171
|
+
mode.anybits?(MODE_USER_EXECUTE)
|
160
172
|
end
|
161
173
|
|
162
174
|
def array_to_string(content)
|
@@ -59,13 +59,14 @@ module Staticky
|
|
59
59
|
# @return [Array<String>] the file contents
|
60
60
|
#
|
61
61
|
# @raise [Staticky::Files::IOError] in case the target path is a directory
|
62
|
-
# or if the file cannot be found
|
62
|
+
# or if the file cannot be found, or if the file is not readable
|
63
63
|
def readlines(path)
|
64
64
|
path = Path[path]
|
65
65
|
node = find(path)
|
66
66
|
|
67
67
|
raise IOError, Errno::ENOENT.new(path.to_s) if node.nil?
|
68
68
|
raise IOError, Errno::EISDIR.new(path.to_s) if node.directory?
|
69
|
+
raise IOError, Errno::EACCES.new(path.to_s) unless node.readable?
|
69
70
|
|
70
71
|
node.readlines
|
71
72
|
end
|
@@ -91,12 +92,17 @@ module Staticky
|
|
91
92
|
#
|
92
93
|
# @param path [String, Array<String>] the target path
|
93
94
|
# @param content [String, Array<String>] the content to write
|
95
|
+
#
|
96
|
+
# @raise [Staticky::Files::IOError] in case the target path is a directory
|
94
97
|
def write(path, *content)
|
95
98
|
path = Path[path]
|
99
|
+
raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path)
|
100
|
+
|
96
101
|
node = @root
|
97
102
|
|
98
103
|
for_each_segment(path) do |segment|
|
99
104
|
node = node.set(segment)
|
105
|
+
raise IOError, Errno::EACCES.new(path.to_s) unless node.readable?
|
100
106
|
end
|
101
107
|
|
102
108
|
node.write(*content)
|
@@ -164,7 +170,7 @@ module Staticky
|
|
164
170
|
# @param path [String,Array<String>] the directory to create
|
165
171
|
#
|
166
172
|
# @raise [Staticky::Files::IOError] in case path is an already existing
|
167
|
-
# file
|
173
|
+
# file, or the file system cannot be accessed
|
168
174
|
def mkdir(path)
|
169
175
|
path = Path[path]
|
170
176
|
node = @root
|
@@ -172,6 +178,7 @@ module Staticky
|
|
172
178
|
for_each_segment(path) do |segment|
|
173
179
|
node = node.set(segment)
|
174
180
|
raise IOError, Errno::EEXIST.new(path.to_s) if node.file?
|
181
|
+
raise IOError, Errno::EACCES.new(path.to_s) unless node.readable?
|
175
182
|
end
|
176
183
|
end
|
177
184
|
|
@@ -337,11 +344,31 @@ module Staticky
|
|
337
344
|
[".", ".."] + Array(node.children&.keys)
|
338
345
|
end
|
339
346
|
|
347
|
+
# Returns an array of file paths that match the given pattern.
|
348
|
+
#
|
349
|
+
# @param pattern [Pathname, String] the glob pattern to match
|
350
|
+
# @return [Array<String>] the matching file paths
|
351
|
+
def glob(pattern)
|
352
|
+
matches = []
|
353
|
+
traverse(@root, "/", pattern.to_s, matches)
|
354
|
+
matches
|
355
|
+
end
|
356
|
+
|
340
357
|
private
|
341
358
|
|
342
|
-
def
|
359
|
+
def traverse(node, current_path, pattern, matches)
|
360
|
+
if node.file? && File.fnmatch(pattern, current_path, File::FNM_PATHNAME)
|
361
|
+
matches << current_path
|
362
|
+
elsif node.directory?
|
363
|
+
node.children.each do |name, child|
|
364
|
+
traverse(child, File.join(current_path, name), pattern, matches)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
def for_each_segment(path, &)
|
343
370
|
segments = Path.split(path)
|
344
|
-
segments.each(&
|
371
|
+
segments.each(&)
|
345
372
|
end
|
346
373
|
|
347
374
|
def find_directory(path)
|
data/lib/staticky/files.rb
CHANGED
@@ -58,9 +58,10 @@ module Staticky
|
|
58
58
|
|
59
59
|
# Sets UNIX permissions of the file at the given path.
|
60
60
|
#
|
61
|
-
# Accepts permissions in numeric mode only, best provided as octal numbers
|
62
|
-
# standard UNIX octal permission modes, such as `0o544` for a
|
63
|
-
# readable by others, or `0o755` for a
|
61
|
+
# Accepts permissions in numeric mode only, best provided as octal numbers
|
62
|
+
# matching the standard UNIX octal permission modes, such as `0o544` for a
|
63
|
+
# file writeable by its owner and readable by others, or `0o755` for a
|
64
|
+
# file writeable by its owner and executable by everyone.
|
64
65
|
#
|
65
66
|
# @param path [String,Pathname] the path to the file
|
66
67
|
# @param mode [Integer] the UNIX permissions mode
|
@@ -69,7 +70,7 @@ module Staticky
|
|
69
70
|
def chmod(path, mode)
|
70
71
|
unless mode.is_a?(Integer)
|
71
72
|
raise Staticky::Files::Error,
|
72
|
-
|
73
|
+
"mode should be an integer (e.g. 0o755)"
|
73
74
|
end
|
74
75
|
|
75
76
|
adapter.chmod(path, mode)
|
@@ -128,8 +129,8 @@ module Staticky
|
|
128
129
|
# @param blk [Proc] the code to execute with the target directory
|
129
130
|
#
|
130
131
|
# @raise [Staticky::Files::IOError] in case of I/O error
|
131
|
-
def chdir(path, &
|
132
|
-
adapter.chdir(path, &
|
132
|
+
def chdir(path, &)
|
133
|
+
adapter.chdir(path, &)
|
133
134
|
end
|
134
135
|
|
135
136
|
# Creates a directory for the given path.
|
@@ -837,12 +838,22 @@ module Staticky
|
|
837
838
|
#
|
838
839
|
# @raise [Staticky::Files::IOError] in case of I/O error
|
839
840
|
#
|
840
|
-
# @since 1.0
|
841
|
+
# @since 0.1.0
|
841
842
|
# @api public
|
842
843
|
def entries(path)
|
843
844
|
adapter.entries(path)
|
844
845
|
end
|
845
846
|
|
847
|
+
# Reads files matching the given glob pattern
|
848
|
+
#
|
849
|
+
# @param pattern [String,Pathname] the glob pattern
|
850
|
+
#
|
851
|
+
# @since 0.1.0
|
852
|
+
# @api public
|
853
|
+
def glob(pattern)
|
854
|
+
adapter.glob(pattern)
|
855
|
+
end
|
856
|
+
|
846
857
|
private
|
847
858
|
|
848
859
|
class Delimiter
|
@@ -925,9 +936,9 @@ module Staticky
|
|
925
936
|
delimiter,
|
926
937
|
count_offset = 0
|
927
938
|
)
|
928
|
-
blocks_count = content.count
|
939
|
+
blocks_count = content.count do |line|
|
929
940
|
line.match?(delimiter.opening_matcher)
|
930
|
-
|
941
|
+
end + count_offset
|
931
942
|
matching_line = content.find do |line|
|
932
943
|
blocks_count -= 1 if line.match?(delimiter.closing_matcher)
|
933
944
|
line if blocks_count.zero?
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: staticky-files
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nolan J Tait
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies: []
|
13
12
|
description: Better file management with adapters for testing and production
|
14
13
|
email:
|
@@ -40,7 +39,6 @@ metadata:
|
|
40
39
|
source_code_uri: https://github.com/nolantait/staticky-files
|
41
40
|
changelog_uri: https://github.com/nolantait/staticky-files
|
42
41
|
rubygems_mfa_required: 'true'
|
43
|
-
post_install_message:
|
44
42
|
rdoc_options: []
|
45
43
|
require_paths:
|
46
44
|
- lib
|
@@ -55,8 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
53
|
- !ruby/object:Gem::Version
|
56
54
|
version: '0'
|
57
55
|
requirements: []
|
58
|
-
rubygems_version: 3.
|
59
|
-
signing_key:
|
56
|
+
rubygems_version: 3.7.2
|
60
57
|
specification_version: 4
|
61
58
|
summary: Better file management with adapters for testing and production
|
62
59
|
test_files: []
|