Module: Autoshell::Filestuff
- Included in:
- Base
- Defined in:
- lib/autoshell/filestuff.rb
Instance Method Summary collapse
-
#cd(path = nil) ⇒ Object
Change directories.
-
#copy_to(path = nil, force: nil) ⇒ Object
Copy this working dir to another path.
-
#cp(path, dest, force: false) ⇒ Object
copy a path from the working directory to another location.
-
#dir?(path = '.') ⇒ Boolean
Check if the param is a directory.
-
#exist?(path = '.') ⇒ Boolean
Check if the param exists on the system.
-
#expand(path) ⇒ String
expand a local path for this working directory.
-
#glob(pattern) ⇒ Array
Get matching files.
-
#ls(path = '.') ⇒ Array<String>
Return an array of filenames.
-
#mime(path) ⇒ String
Get mime for a file.
-
#mkdir(path = '.') ⇒ Object
Make all the directories for a path.
-
#mkpdir(path = '.') ⇒ Object
Make all the parent directories for a path.
-
#move_to(path) ⇒ Object
Move this working dir to another path.
-
#mv(path, dest, force: false) ⇒ Object
Move something from the working directory to another location.
-
#read(path) ⇒ String
Read and parse a file.
-
#read_binary(path) ⇒ String
return the binary contents of a local path.
-
#read_json(path) ⇒ Object
Read and parse JSON from a local path.
-
#read_text(path) ⇒ String
return the contents of a local path.
-
#read_yaml(path) ⇒ Object
Read and parse YAML from a local path.
-
#rm(path = '.') ⇒ Object
Delete a path in the working directory.
Instance Method Details
#cd(path = nil) ⇒ Object
Change directories
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/autoshell/filestuff.rb', line 74 def cd(path = nil) unless path.nil? original_dir = working_dir @working_dir = (path) end unless Dir.exist? working_dir raise CommandError, "cd: The directory '#{working_dir}' does not exist" end ret = nil Dir.chdir(working_dir) do ret = yield self end @working_dir = original_dir unless path.nil? ret # return end |
#copy_to(path = nil, force: nil) ⇒ Object
Copy this working dir to another path
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/autoshell/filestuff.rb', line 118 def copy_to(path = nil, force: nil) return super if path.nil? unless Dir.exist? working_dir raise CommandError, "copy_to: The directory '#{working_dir}' does not exist" end cp '.', path, force: force self.class.new( path) end |
#cp(path, dest, force: false) ⇒ Object
copy a path from the working directory to another location
44 45 46 47 48 49 50 |
# File 'lib/autoshell/filestuff.rb', line 44 def cp(path, dest, force: false) rm(dest) if force raise CommandError, 'Destination exists' if exist?(dest) mkpdir dest FileUtils.cp_r((path), (dest)) end |
#dir?(path = '.') ⇒ Boolean
Check if the param is a directory
24 25 26 |
# File 'lib/autoshell/filestuff.rb', line 24 def dir?(path = '.') Dir.exist?( path) end |
#exist?(path = '.') ⇒ Boolean
Check if the param exists on the system
17 18 19 |
# File 'lib/autoshell/filestuff.rb', line 17 def exist?(path = '.') File.exist?( path) end |
#expand(path) ⇒ String
expand a local path for this working directory
10 11 12 |
# File 'lib/autoshell/filestuff.rb', line 10 def (path) File.(path, working_dir) end |
#glob(pattern) ⇒ Array
Get matching files
31 32 33 |
# File 'lib/autoshell/filestuff.rb', line 31 def glob(pattern) cd { Dir.glob(pattern) } end |
#ls(path = '.') ⇒ Array<String>
Return an array of filenames
66 67 68 |
# File 'lib/autoshell/filestuff.rb', line 66 def ls(path = '.') Dir.entries( path) end |
#mime(path) ⇒ String
Get mime for a file
133 134 135 |
# File 'lib/autoshell/filestuff.rb', line 133 def mime(path) MIME::Types.type_for( path).first end |
#mkdir(path = '.') ⇒ Object
Make all the directories for a path
103 104 105 106 |
# File 'lib/autoshell/filestuff.rb', line 103 def mkdir(path = '.') FileUtils.mkdir_p( path) self end |
#mkpdir(path = '.') ⇒ Object
Make all the parent directories for a path
96 97 98 99 |
# File 'lib/autoshell/filestuff.rb', line 96 def mkpdir(path = '.') mkdir(File.dirname( path)) self end |
#move_to(path) ⇒ Object
Move this working dir to another path
110 111 112 113 114 |
# File 'lib/autoshell/filestuff.rb', line 110 def move_to(path) mv '.', path self.working_dir = path self end |
#mv(path, dest, force: false) ⇒ Object
Move something from the working directory to another location
55 56 57 58 59 60 61 |
# File 'lib/autoshell/filestuff.rb', line 55 def mv(path, dest, force: false) rm(dest) if force raise CommandError, 'Destination exists' if exist?(dest) mkpdir dest FileUtils.mv((path), (dest)) end |
#read(path) ⇒ String
Read and parse a file
140 141 142 143 144 145 146 147 |
# File 'lib/autoshell/filestuff.rb', line 140 def read(path) m = mime(path) return read_text(path) unless m return read_json(path) if m.content_type == 'application/json' return read_yaml(path) if m.content_type == 'text/x-yaml' return read_binary(path) if m.binary? read_text(path) end |
#read_binary(path) ⇒ String
return the binary contents of a local path
182 183 184 |
# File 'lib/autoshell/filestuff.rb', line 182 def read_binary(path) File.binread( path) if exist? path end |
#read_json(path) ⇒ Object
Read and parse JSON from a local path
152 153 154 155 156 157 158 |
# File 'lib/autoshell/filestuff.rb', line 152 def read_json(path) text = read_text(path) return nil if text.nil? JSON.parse(text) rescue JSON::ParserError nil end |
#read_text(path) ⇒ String
return the contents of a local path
175 176 177 |
# File 'lib/autoshell/filestuff.rb', line 175 def read_text(path) File.read( path) if exist? path end |
#read_yaml(path) ⇒ Object
Read and parse YAML from a local path
163 164 165 166 167 168 169 170 |
# File 'lib/autoshell/filestuff.rb', line 163 def read_yaml(path) require 'yaml' text = read_text(path) return nil if text.nil? YAML.parse(text).to_h rescue Psych::SyntaxError nil end |
#rm(path = '.') ⇒ Object
Delete a path in the working directory
37 38 39 |
# File 'lib/autoshell/filestuff.rb', line 37 def rm(path = '.') FileUtils.rm_rf( path) end |