Module: FeduxOrgStdlib::Filesystem
- Defined in:
- lib/fedux_org_stdlib/filesystem.rb
Instance Method Summary collapse
-
#cleanup_working_directory ⇒ Object
Clean up test directory.
-
#create_directory(*dirs) ⇒ String, Array
Create directory(ies).
-
#create_file(path, content = '', mode = 0644) ⇒ String
Create a single file.
-
#create_working_directory ⇒ Object
Create temporary directory.
-
#delete_directory(*dirs) ⇒ String, Array
Delete directory(ies).
-
#delete_file(*files) ⇒ String
Delete a single file.
-
#delete_working_directory ⇒ Object
Delete temporary directory.
-
#expand_path(*paths) ⇒ Array, String
Expand path based on temporary directory.
-
#path_does_not_exist?(*paths) ⇒ TrueClass, FalseClass
Check absence of path(s).
-
#path_exists?(*paths) ⇒ TrueClass, FalseClass
Check existence of path(s).
-
#path_matches?(strings, regex, *paths) ⇒ TrueClass, FalseClass
Check if path matches.
-
#raise_if_forbidden_path_for_create_operation(*paths) ⇒ TrueClass, FalseClass
Check if path is forbidden for delete operation.
-
#raise_if_forbidden_path_for_delete_operation(*paths) ⇒ TrueClass, FalseClass
Check if path is forbidden for delete operation.
-
#read_file(path) ⇒ String, Binary
Read the content of a file.
-
#root_directory ⇒ String
The root directory of the project.
-
#switch_to_working_directory(&block) ⇒ Object
(also: #in_working_directory)
Switch the current working directory to the temporary one and execute code block.
-
#working_directory ⇒ String
The temporary directory for the project.
Instance Method Details
#cleanup_working_directory ⇒ Object
Clean up test directory
39 40 41 42 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 39 def cleanup_working_directory delete_working_directory create_working_directory end |
#create_directory(*dirs) ⇒ String, Array
Create directory(ies)
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 59 def create_directory(*dirs) raise_if_forbidden_path_for_create_operation(dirs) directories = (dirs.flatten) FileUtils.mkdir_p(directories) if directories.size == 1 return directories.first else return directories end end |
#create_file(path, content = '', mode = 0644) ⇒ String
Create a single file
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 128 def create_file(path, content='', mode=0644) raise_if_forbidden_path_for_create_operation(path) file = (path).first directory = ::File.dirname(file) FileUtils.mkdir_p(directory) unless directory == '.' ::File.open(file, "wb") do |f| f.write content end FileUtils.chmod(mode, file) file end |
#create_working_directory ⇒ Object
Create temporary directory
29 30 31 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 29 def create_working_directory FileUtils.mkdir_p(working_directory) unless ::File.exists? working_directory end |
#delete_directory(*dirs) ⇒ String, Array
Delete directory(ies)
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 80 def delete_directory(*dirs) raise_if_forbidden_path_for_delete_operation(dirs) directories = (dirs.flatten) FileUtils.rm_r(directories) if directories.size == 1 return directories.first else return directories end end |
#delete_file(*files) ⇒ String
Delete a single file
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 151 def delete_file(*files) raise_if_forbidden_path_for_delete_operation(files) files_to_be_deleted = (files.flatten) FileUtils.rm(files_to_be_deleted) if files_to_be_deleted.size == 1 return files_to_be_deleted.first else return files_to_be_deleted end end |
#delete_working_directory ⇒ Object
Delete temporary directory
34 35 36 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 34 def delete_working_directory FileUtils.rm_rf(working_directory) if ::File.exists? working_directory end |
#expand_path(*paths) ⇒ Array, String
Expand path based on temporary directory
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 185 def (*paths) raise_if_forbidden_path_for_create_operation(paths) paths.flatten.map do |p| case p when /^~/ ::File.(p) else ::File.join(working_directory, p ) end end end |
#path_does_not_exist?(*paths) ⇒ TrueClass, FalseClass
Check absence of path(s)
114 115 116 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 114 def path_does_not_exist?(*paths) not path_exists?(paths) end |
#path_exists?(*paths) ⇒ TrueClass, FalseClass
Check existence of path(s)
100 101 102 103 104 105 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 100 def path_exists?(*paths) raise_if_forbidden_path_for_create_operation(paths) = (paths.flatten) .flatten.all? { |p| ::File.exists?(p) } end |
#path_matches?(strings, regex, *paths) ⇒ TrueClass, FalseClass
Check if path matches
241 242 243 244 245 246 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 241 def path_matches?(strings, regex, *paths) flattend_paths = paths.flatten flattend_strings = strings.flatten flattend_paths.any? { |f| f =~ regex or flattend_strings.include?(f) } end |
#raise_if_forbidden_path_for_create_operation(*paths) ⇒ TrueClass, FalseClass
Check if path is forbidden for delete operation
205 206 207 208 209 210 211 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 205 def raise_if_forbidden_path_for_create_operation(*paths) flattend_paths = paths.flatten strings = [] regex = %r[\.\.] raise FileSystem::Exceptions::InvalidPath , "Sorry, but you cannot use paths matching \"#{strings.join(', ')}\" or \"#{regex.inspect}\" here!" if path_matches?(strings, regex, flattend_paths) end |
#raise_if_forbidden_path_for_delete_operation(*paths) ⇒ TrueClass, FalseClass
Check if path is forbidden for delete operation
220 221 222 223 224 225 226 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 220 def raise_if_forbidden_path_for_delete_operation(*paths) flattend_paths = paths.flatten strings = %w[ / ] regex = %r[\.\.] raise FileSystem::Exceptions::InvalidPath , "Sorry, but you cannot use paths matching \"#{strings.join(', ')}\" or \"#{regex.to_s}\" here!" if path_matches?(strings, regex, flattend_paths) end |
#read_file(path) ⇒ String, Binary
Read the content of a file
171 172 173 174 175 176 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 171 def read_file(path) raise_if_forbidden_path_for_create_operation(path) file_path = (path).first return ::File.read(file_path) end |
#root_directory ⇒ String
The root directory of the project
15 16 17 18 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 15 def root_directory #::File.expand_path('../../../', __FILE__) raise Exceptions::InvalidUsageOfLibrary , "Sorry, but you need to define the root directory yourself" end |
#switch_to_working_directory(&block) ⇒ Object Also known as: in_working_directory
Switch the current working directory to the temporary one and execute code block
46 47 48 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 46 def switch_to_working_directory(&block) Dir.chdir(working_directory, &block) end |
#working_directory ⇒ String
The temporary directory for the project
24 25 26 |
# File 'lib/fedux_org_stdlib/filesystem.rb', line 24 def working_directory ::File.join(root_directory, 'tmp', 'test') end |