Class: FileSys

Inherits:
Object
  • Object
show all
Defined in:
lib/atk/extra_file_utils.rb

Class Method Summary collapse

Class Method Details

.abs?(path) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/atk/extra_file_utils.rb', line 143

def self.abs?(path)
    Pathname.new(path).absolute?
end

.absolute_path(*args) ⇒ Object

inherit from File



196
197
198
# File 'lib/atk/extra_file_utils.rb', line 196

def self.absolute_path(*args)
    File.absolute_path(*args)
end

.absolute_path?(path) ⇒ Boolean

Pathname aliases

Returns:

  • (Boolean)


140
141
142
# File 'lib/atk/extra_file_utils.rb', line 140

def self.absolute_path?(path)
    Pathname.new(path).absolute?
end

.basename(*args) ⇒ Object



202
203
204
# File 'lib/atk/extra_file_utils.rb', line 202

def self.basename(*args)
    File.basename(*args)
end

.cd(path, verbose: false) ⇒ Object



172
173
174
# File 'lib/atk/extra_file_utils.rb', line 172

def self.cd(path, verbose: false)
    FileUtils.cd(path, verbose: verbose)
end

.chdir(path, verbose: false) ⇒ Object



175
176
177
# File 'lib/atk/extra_file_utils.rb', line 175

def self.chdir(path, verbose: false)
    FileUtils.cd(path, verbose: verbose)
end

.copy(from: nil, to: nil, new_name: "", force: true, preserve: false, dereference_root: false) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/atk/extra_file_utils.rb', line 96

def self.copy(from:nil, to:nil, new_name:"", force: true, preserve: false, dereference_root: false)
    if new_name == ""
        raise "\n\nFileSys.copy() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSys.copy(from:'place/thing', to:'place', new_name:nil)"
    end
    # make sure the "to" path exists
    FileSys.touch_dir(to)
    # perform the copy
    FileUtils.copy_entry(from, to/new_name, preserve, dereference_root, force)
end

.delete(path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/atk/extra_file_utils.rb', line 64

def self.delete(path)
    if File.file?(path)
        File.delete(path)
    elsif File.directory?(path)
        FileUtils.rm_rf(path)
    end
end

.dir?(*args) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/atk/extra_file_utils.rb', line 188

def self.dir?(*args)
    File.directory?(*args)
end

.directory?(*args) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/atk/extra_file_utils.rb', line 208

def self.directory?(*args)
    File.directory?(*args)
end

.dirname(*args) ⇒ Object



199
200
201
# File 'lib/atk/extra_file_utils.rb', line 199

def self.dirname(*args)
    File.dirname(*args)
end

.download(input = nil, from: nil, url: nil, to: nil) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/atk/extra_file_utils.rb', line 263

def self.download(input=nil, from:nil, url:nil, to:nil)
    # if only one argument, either input or url
    if ((input!=nil) != (url!=nil)) && (from==nil) && (to==nil)
        # this covers:
        #    download     'site.com/file'
        the_url = url || input
        file_name = the_url.match /(?<=\/)[^\/]+\z/ 
        file_name = file_name[0]
    elsif (to != nil) && ((input!=nil)!=(url!=nil))
        # this covers:
        #    download     'site.com/file' to:'file'
        #    download url:'site.com/file' to:'file'
        the_url = url || input
        file_name = to
    elsif ((from!=nil) != (url!=nil)) && input!=nil
        # this covers:
        #    download 'file' from:'site.com/file'
        #    download 'file'  url:'site.com/file'
        the_url = from || url
        file_name = input
    else
        raise <<-HEREDOC.remove_indent
            I'm not sure how you're using the download function.
            Please use one of the following methods:
                download     'site.com/file'
                download     'site.com/file', to:'file'
                download url:'site.com/file', to:'file'
                download 'file', from:'site.com/file'
                download 'file',  url:'site.com/file'
        HEREDOC
    end
    FileSys.write(open(URI.encode(the_url)).read, to: file_name)
end

.empty?(*args) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/atk/extra_file_utils.rb', line 214

def self.empty?(*args)
    File.empty?(*args)
end

.executable?(*args) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/atk/extra_file_utils.rb', line 220

def self.executable?(*args)
    File.executable?(*args)
end

.exist?(*args) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/atk/extra_file_utils.rb', line 217

def self.exist?(*args)
    File.exist?(*args)
end

.exists?(*args) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/atk/extra_file_utils.rb', line 191

def self.exists?(*args)
    File.exist?(*args)
end

.expand_path(*args) ⇒ Object



253
254
255
# File 'lib/atk/extra_file_utils.rb', line 253

def self.expand_path(*args)
    File.expand_path(*args)
end

.extname(*args) ⇒ Object



205
206
207
# File 'lib/atk/extra_file_utils.rb', line 205

def self.extname(*args)
    File.extname(*args)
end

.file?(*args) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/atk/extra_file_utils.rb', line 211

def self.file?(*args)
    File.file?(*args)
end

.glob(path) ⇒ Object



157
158
159
# File 'lib/atk/extra_file_utils.rb', line 157

def self.glob(path)
    Dir.glob(path, File::FNM_DOTMATCH) - %w[. ..]
end

.homeObject

dir aliases



154
155
156
# File 'lib/atk/extra_file_utils.rb', line 154

def self.home
    HOME
end

.in_dir(path_to_somewhere) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/atk/extra_file_utils.rb', line 84

def self.in_dir(path_to_somewhere)
    # save the current working dir
    current_dir = Dir.pwd
    # switch dirs
    Dir.chdir(path_to_somewhere)
    # do the thing
    output = yield
    # switch back
    Dir.chdir(current_dir)
    return output
end

.list_files(path = ".") ⇒ Object



160
161
162
# File 'lib/atk/extra_file_utils.rb', line 160

def self.list_files(path=".")
    Dir.children(path).select {|each| FileSys.file?(each)}
end

.list_folders(path = ".") ⇒ Object



163
164
165
# File 'lib/atk/extra_file_utils.rb', line 163

def self.list_folders(path=".")
    Dir.children(path).select {|each| FileSys.directory?(each)}
end

.ls(path) ⇒ Object



166
167
168
# File 'lib/atk/extra_file_utils.rb', line 166

def self.ls(path)
    Dir.children(path)
end

.makedirs(path) ⇒ Object



80
81
82
# File 'lib/atk/extra_file_utils.rb', line 80

def self.makedirs(path)
    FileUtils.makedirs(path)
end

.mkfifo(*args) ⇒ Object



256
257
258
# File 'lib/atk/extra_file_utils.rb', line 256

def self.mkfifo(*args)
    File.mkfifo(*args)
end

.move(from: nil, to: nil, new_name: "", force: true, noop: nil, verbose: nil, secure: nil) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/atk/extra_file_utils.rb', line 106

def self.move(from:nil, to:nil, new_name:"", force: true, noop: nil, verbose: nil, secure: nil)
    if new_name == ""
        raise "\n\nFileSys.move() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSys.move(from:'place/thing', to:'place', new_name:nil)"
    end
    # make sure the "to" path exists
    FileSys.touch_dir(to)
    # perform the move
    FileUtils.move(from, to/new_name, force: force, noop: noop, verbose: verbose, secure: secure)
end

.owned?(*args) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/atk/extra_file_utils.rb', line 226

def self.owned?(*args)
    File.owned?(*args)
end

.pipe?(*args) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/atk/extra_file_utils.rb', line 229

def self.pipe?(*args)
    File.pipe?(*args)
end

.pwdObject



169
170
171
# File 'lib/atk/extra_file_utils.rb', line 169

def self.pwd
    Dir.pwd
end

.read(filepath) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/atk/extra_file_utils.rb', line 56

def self.read(filepath)
    begin
        return IO.read(filepath)
    rescue Errno::ENOENT => exception
        return nil
    end
end

.readable?(*args) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/atk/extra_file_utils.rb', line 232

def self.readable?(*args)
    File.readable?(*args)
end

.rel?(path) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/atk/extra_file_utils.rb', line 149

def self.rel?(path)
    Pathname.new(path).relative?
end

.relative_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/atk/extra_file_utils.rb', line 146

def self.relative_path?(path)
    Pathname.new(path).relative?
end

.rename(from: nil, to: nil, force: true) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/atk/extra_file_utils.rb', line 116

def self.rename(from:nil, to:nil, force: true)
    # if the directories are different, then throw an error
    if not File.identical?(File.dirname(from), File.dirname(to))
        raise "\n\nFileSys.rename() requires that the the file stay in the same place and only change names.\nIf you want to move a file, use FileSys.move()"
    end
    # make sure the path is clear
    if force
        FileSys.delete(to)
    end
    # perform the copy
    File.rename(from, to)
end

.size?(*args) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/atk/extra_file_utils.rb', line 235

def self.size?(*args)
    File.size?(*args)
end

.socket?(*args) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/atk/extra_file_utils.rb', line 238

def self.socket?(*args)
    File.socket?(*args)
end

.stat(*args) ⇒ Object



259
260
261
# File 'lib/atk/extra_file_utils.rb', line 259

def self.stat(*args)
    File.stat(*args)
end

.symlink?(*args) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/atk/extra_file_utils.rb', line 223

def self.symlink?(*args)
    File.symlink?(*args)
end

.time_access(*args) ⇒ Object

File aliases



180
181
182
# File 'lib/atk/extra_file_utils.rb', line 180

def self.time_access(*args)
    File.atime(*args)
end

.time_created(*args) ⇒ Object



183
184
185
# File 'lib/atk/extra_file_utils.rb', line 183

def self.time_created(*args)
    File.birthtime(*args)
end

.time_modified(*args) ⇒ Object



186
187
# File 'lib/atk/extra_file_utils.rb', line 186

def self.time_modified(*args)
end

.touch(*args) ⇒ Object



129
130
131
# File 'lib/atk/extra_file_utils.rb', line 129

def self.touch(*args)
    return FileUtils.touch(*args)
end

.touch_dir(path) ⇒ Object



133
134
135
136
137
# File 'lib/atk/extra_file_utils.rb', line 133

def self.touch_dir(path)
    if not FileSys.directory?(path)
        FileUtils.makedirs(path)
    end
end

.usernameObject



72
73
74
75
76
77
78
# File 'lib/atk/extra_file_utils.rb', line 72

def self.username
    if OS.is?(:windows)
        return File.basename(ENV["userprofile"])
    else
        return Etc.getlogin
    end
end

.world_readable?(*args) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/atk/extra_file_utils.rb', line 241

def self.world_readable?(*args)
    File.world_readable?(*args)
end

.world_writable?(*args) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/atk/extra_file_utils.rb', line 244

def self.world_writable?(*args)
    File.world_writable?(*args)
end

.writable?(*args) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/atk/extra_file_utils.rb', line 247

def self.writable?(*args)
    File.writable?(*args)
end

.writable_real?(*args) ⇒ Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/atk/extra_file_utils.rb', line 250

def self.writable_real?(*args)
    File.writable_real?(*args)
end

.write(data, to: nil) ⇒ Object

change_owner set_permissions relative_path_between relative_path_to add a force: true option to most of the commands



49
50
51
52
53
54
# File 'lib/atk/extra_file_utils.rb', line 49

def self.write(data, to:nil)
    # make sure the containing folder exists
    FileSys.makedirs(File.dirname(to))
    # actually download the file
    IO.write(to, data)
end