Class: Folio::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/folio/shell.rb

Overview

Prompt class

A Folio Shell object provides a limited file system shell in code.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*path) ⇒ Shell

New Shell object.

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/folio/shell.rb', line 15

def initialize(*path)
  opts = (Hash===path.last ? path.pop : {})
  mode(opts)

  if path.empty?
    path = Dir.pwd
  else
    path = File.join(*path)
  end

  raise FileNotFound, "#{path}" unless ::File.exist?(path)
  raise FileNotFound, "#{path}" unless ::File.directory?(path)

  @work = dir(path)
end

Instance Attribute Details

#workObject (readonly)

Present working directory as a Directory object.



52
53
54
# File 'lib/folio/shell.rb', line 52

def work
  @work
end

Class Method Details

.[](path) ⇒ Object



554
555
556
# File 'lib/folio/shell.rb', line 554

def self.[](path)
  new(path)
end

Instance Method Details

#+(fname) ⇒ Object Also known as: /

Join paths.



91
92
93
# File 'lib/folio/shell.rb', line 91

def +(fname)
  @work =+ fname
end

#[](name) ⇒ Object

Get the FileObject for the given file name.

TODO: Should there be methods like this for each

type, and raise an error if not the right type?


70
71
72
# File 'lib/folio/shell.rb', line 70

def [](name)
  FileObject[name]
end

#absolute?(path) ⇒ Boolean

Returns:

  • (Boolean)


202
# File 'lib/folio/shell.rb', line 202

def absolute?(path) ; FileTest.absolute?(path) ; end

#blockdev?(path) ⇒ Boolean

Returns:

  • (Boolean)


179
# File 'lib/folio/shell.rb', line 179

def blockdev?(path)   ; FileTest.blockdev?(localize(path))  ; end

#bzip2(file = nil, options = {}) ⇒ Object



382
383
384
385
# File 'lib/folio/shell.rb', line 382

def bzip2(file=nil, options={})
  file   = localize(file)
  ziputils.bzip2(file, options)
end

#cache(path) ⇒ Object

Look up a cache file.



461
462
463
# File 'lib/folio/shell.rb', line 461

def cache(path)
  file(XDG.xdg_cache_file(path))
end

#cd(dir, &block) ⇒ Object Also known as: chdir

Chage working directory.



152
153
154
155
156
157
158
# File 'lib/folio/shell.rb', line 152

def cd(dir, &block)
  if block
    Dir.chdir(localize(dir), &block)
  else
    @work += dir
  end
end

#chardev?(path) ⇒ Boolean

Returns:

  • (Boolean)


172
# File 'lib/folio/shell.rb', line 172

def chardev?(path)    ; FileTest.chardev?(localize(path))   ; end

#chmod(mode, list, options = {}) ⇒ Object



331
332
333
334
# File 'lib/folio/shell.rb', line 331

def chmod(mode, list, options={})
  list = localize(list)
  fileutils.chmod(mode, list, options)
end

#chmod_r(mode, list, options = {}) ⇒ Object



336
337
338
339
# File 'lib/folio/shell.rb', line 336

def chmod_r(mode, list, options={})
  list = localize(list)
  fileutils.chmod_r(mode, list, options)
end

#chown(user, group, list, options = {}) ⇒ Object

alias_method :chmod_R, :chmod_r



342
343
344
345
# File 'lib/folio/shell.rb', line 342

def chown(user, group, list, options={})
  list = localize(list)
  fileutils.chown(user, group, list, options)
end

#chown_r(user, group, list, options = {}) ⇒ Object



347
348
349
350
# File 'lib/folio/shell.rb', line 347

def chown_r(user, group, list, options={})
  list = localize(list)
  fileutils.chown_r(user, group, list, options)
end

#cmp(a, b) ⇒ Object

Same as identical?



231
232
233
# File 'lib/folio/shell.rb', line 231

def cmp(a,b)
  fileutils.cmp(a,b)
end

#compress(folder, file, options = {}) ⇒ Object

Compress directory to file. Format is determined by file extension.



371
372
373
# File 'lib/folio/shell.rb', line 371

def compress(folder, file, options={})
  ziputils.compress(folder, file, options)
end

#config(path) ⇒ Object

Look up a config file.



451
452
453
# File 'lib/folio/shell.rb', line 451

def config(path)
  file(XDG.xdg_config_file(path))
end

#cp(src, dest, options = {}) ⇒ Object Also known as: copy

cp(list, dir, options={})



282
283
284
285
286
# File 'lib/folio/shell.rb', line 282

def cp(src, dest, options={})
  src  = localize(src)
  dest = localize(dest)
  fileutils.cp(src, dest, options)
end

#cp_r(src, dest, options = {}) ⇒ Object

cp_r(list, dir, options={})



290
291
292
293
294
# File 'lib/folio/shell.rb', line 290

def cp_r(src, dest, options={})
  src  = localize(src)
  dest = localize(dest)
  fileutils.cp_r(src, dest, options)
end

#data(path) ⇒ Object

Look up a data file.



456
457
458
# File 'lib/folio/shell.rb', line 456

def data(path)
  file(XDG.xdg_data_file(path))
end

#dir(name) ⇒ Object



86
87
88
# File 'lib/folio/shell.rb', line 86

def dir(name)
  Directory.new(name)
end

#directoriesObject

Returns Enumerator of documents.



119
# File 'lib/folio/shell.rb', line 119

def directories ; work.directories ; end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


168
# File 'lib/folio/shell.rb', line 168

def directory?(path)  ; FileTest.directory?(localize(path)) ; end

#directory_entriesObject

Lists directory entries.



103
104
105
# File 'lib/folio/shell.rb', line 103

def directory_entries
  work.directory_entries
end

#doc(name) ⇒ Object



82
83
84
# File 'lib/folio/shell.rb', line 82

def doc(name)
  Document.new(name)
end

#document_entriesObject

Lists directory entries.



108
109
110
# File 'lib/folio/shell.rb', line 108

def document_entries
  work.document_entries
end

#documentsObject

Returns Enumerator of directories.



116
# File 'lib/folio/shell.rb', line 116

def documents ; work.documents ; end

#entriesObject Also known as: ls

Lists all entries.



97
98
99
# File 'lib/folio/shell.rb', line 97

def entries
  work.entries
end

#executable?(path) ⇒ Boolean

Returns:

  • (Boolean)


188
# File 'lib/folio/shell.rb', line 188

def executable?(path) ; FileTest.executable?(localize(path))  ; end

#executable_real?(path) ⇒ Boolean

Returns:

  • (Boolean)


191
# File 'lib/folio/shell.rb', line 191

def executable_real?(path) ; FileTest.executable_real?(localize(path)) ; end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


173
# File 'lib/folio/shell.rb', line 173

def exist?(path)      ; FileTest.exist?(localize(path))     ; end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


174
# File 'lib/folio/shell.rb', line 174

def exists?(path)     ; FileTest.exists?(localize(path))    ; end

#file(name) ⇒ Object



74
75
76
# File 'lib/folio/shell.rb', line 74

def file(name)
  FileObject[name]
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


177
# File 'lib/folio/shell.rb', line 177

def file?(path)       ; FileTest.file?(localize(path))      ; end

#filesObject

Returns Enumerator of files objects.



113
# File 'lib/folio/shell.rb', line 113

def files ; work.files ; end

#glob(*patterns, &block) ⇒ Object

Glob pattern. Returns matches as strings.



122
123
124
125
126
127
128
129
# File 'lib/folio/shell.rb', line 122

def glob(*patterns, &block)
  opts = (::Integer===patterns.last ? patterns.pop : 0)
  patterns = localize(patterns)
  matches  = patterns.map{ |pattern| ::Dir.glob(pattern, opts) }.flatten
  if block_given?
    matches.each(&block)
  end
end

#grpowned?(path) ⇒ Boolean

Returns:

  • (Boolean)


180
# File 'lib/folio/shell.rb', line 180

def grpowned?(path)   ; FileTest.grpowned?(localize(path))  ; end

#gzip(file = nil, options = {}) ⇒ Object



376
377
378
379
# File 'lib/folio/shell.rb', line 376

def gzip(file=nil, options={})
  file   = localize(file)
  ziputils.gzip(file, options)
end

#homeObject

Home location.



55
# File 'lib/folio/shell.rb', line 55

def home ; dir('~') ; end

#home_cacheObject

Return the home cache directory.



486
487
488
# File 'lib/folio/shell.rb', line 486

def home_cache
  dir(XDG.xdg_cache_home)
end

#home_configObject

Return the home config directory.



476
477
478
# File 'lib/folio/shell.rb', line 476

def home_config
  dir(XDG.xdg_config_home)
end

#home_dataObject

Return the home data directory.



481
482
483
# File 'lib/folio/shell.rb', line 481

def home_data
  dir(XDG.xdg_data_home)
end

#identical?(path) ⇒ Boolean

Returns:

  • (Boolean)


186
# File 'lib/folio/shell.rb', line 186

def identical?(path)  ; FileTest.identical?(localize(path)) ; end

#install(src, dest, mode, options = {}) ⇒ Object



325
326
327
328
329
# File 'lib/folio/shell.rb', line 325

def install(src, dest, mode, options={})
  src  = localize(src)
  dest = localize(dest)
  fileutils.install(src, dest, mode, options)
end

#ln(old, new, options = {}) ⇒ Object Also known as: link

ln(list, destdir, options={})



260
261
262
263
264
# File 'lib/folio/shell.rb', line 260

def ln(old, new, options={})
  old = localize(old)
  new = localize(new)
  fileutils.ln(old, new, options)
end

#ln_s(old, new, options = {}) ⇒ Object Also known as: symlink

ln_s(list, destdir, options={})



268
269
270
271
272
# File 'lib/folio/shell.rb', line 268

def ln_s(old, new, options={})
  old = localize(old)
  new = localize(new)
  fileutils.ln_s(old, new, options)
end

#ln_sf(old, new, options = {}) ⇒ Object



275
276
277
278
279
# File 'lib/folio/shell.rb', line 275

def ln_sf(old, new, options={})
  old = localize(old)
  new = localize(new)
  fileutils.ln_sf(old, new, options)
end

#match(*patterns, &block) ⇒ Object

Match pattern. Like #glob but returns file objects.



141
142
143
144
145
146
147
148
149
# File 'lib/folio/shell.rb', line 141

def match(*patterns, &block)
  opts = (::Integer===patterns.last ? patterns.pop : 0)
  patterns = localize(patterns)
  matches  = patterns.map{ |pattern| ::Dir.glob(pattern, opts) }.flatten
  matches  = matches.map{ |f| FileObject[f] }
  if block_given?
    matches.each(&block)
  end
end

#mkdir(dir, options = {}) ⇒ Object



243
244
245
246
# File 'lib/folio/shell.rb', line 243

def mkdir(dir, options={})
  dir = localize(dir)
  fileutils.mkdir(dir, options)
end

#mkdir_p(dir, options = {}) ⇒ Object Also known as: mkpath



248
249
250
251
# File 'lib/folio/shell.rb', line 248

def mkdir_p(dir, options={})
  dir = localize(dir)
  fileutils.mkdir_p(dir, options)
end

#mode(opts = nil) ⇒ Object

Opertaton mode. This can be :noop, :verbose or :dryrun. The later is the same as the first two combined.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/folio/shell.rb', line 33

def mode(opts=nil)
  return @mode unless opts
  opts.each do |key, val|
    next unless val
    case key
    when :noop
      @mode = (@mode == :verbose ? :dryrun : :noop)
    when :verbose
      @mode = (@mode == :noop ? :dryrun : :verbose)
    when :dryrun
      @mode = :dryrun
    end
  end
end

#multiglob(*a) ⇒ Object

TODO: Ultimately merge #glob and #multiglob.



132
133
134
# File 'lib/folio/shell.rb', line 132

def multiglob(*a)
  Dir.multiglob(*a)
end

#multiglob_r(*a) ⇒ Object



136
137
138
# File 'lib/folio/shell.rb', line 136

def multiglob_r(*a)
  Dir.multiglob_r(*a)
end

#mv(src, dest, options = {}) ⇒ Object Also known as: move

mv(list, dir, options={})



297
298
299
300
301
# File 'lib/folio/shell.rb', line 297

def mv(src, dest, options={})
  src  = localize(src)
  dest = localize(dest)
  fileutils.mv(src, dest, options)
end

#out_of_date?(path, *sources) ⇒ Boolean

TODO: Is #out_of_date? this the same as #uptodate?

Returns:

  • (Boolean)


197
198
199
# File 'lib/folio/shell.rb', line 197

def out_of_date?(path, *sources)
  FileTest.out_of_date?(localize(path), *localize(sources))
end

#owned?(path) ⇒ Boolean

Returns:

  • (Boolean)


185
# File 'lib/folio/shell.rb', line 185

def owned?(path)      ; FileTest.owned?(localize(path))     ; end

#pathname(name) ⇒ Object



78
79
80
# File 'lib/folio/shell.rb', line 78

def pathname(name)
  Pathname.new(name)
end

#pipe?(path) ⇒ Boolean

Returns:

  • (Boolean)


176
# File 'lib/folio/shell.rb', line 176

def pipe?(path)       ; FileTest.pipe?(localize(path))      ; end

#pwdObject

Present working directory.



226
227
228
# File 'lib/folio/shell.rb', line 226

def pwd
  work.to_s
end

#readable?(path) ⇒ Boolean

Returns:

  • (Boolean)


170
# File 'lib/folio/shell.rb', line 170

def readable?(path)   ; FileTest.readable?(localize(path))  ; end

#readable_real?(path) ⇒ Boolean

Returns:

  • (Boolean)


192
# File 'lib/folio/shell.rb', line 192

def readable_real?(path)   ; FileTest.readable_real?(localize(path))   ; end

#relative?(path) ⇒ Boolean

Returns:

  • (Boolean)


201
# File 'lib/folio/shell.rb', line 201

def relative?(path) ; FileTest.relative?(path) ; end

#rm(list, options = {}) ⇒ Object Also known as: remove



304
305
306
307
# File 'lib/folio/shell.rb', line 304

def rm(list, options={})
  list = localize(list)
  fileutils.rm(list, options)
end

#rm_f(list, options = {}) ⇒ Object



315
316
317
318
# File 'lib/folio/shell.rb', line 315

def rm_f(list, options={})
  list = localize(list)
  fileutils.rm_f(list, options)
end

#rm_r(list, options = {}) ⇒ Object



310
311
312
313
# File 'lib/folio/shell.rb', line 310

def rm_r(list, options={})
  list = localize(list)
  fileutils.rm_r(list, options)
end

#rm_rf(list, options = {}) ⇒ Object



320
321
322
323
# File 'lib/folio/shell.rb', line 320

def rm_rf(list, options={})
  list = localize(list)
  fileutils.rm_rf(list, options)
end

#rmdir(dir, options = {}) ⇒ Object



254
255
256
257
# File 'lib/folio/shell.rb', line 254

def rmdir(dir, options={})
  dir = localize(dir)
  fileutils.rmdir(dir, options)
end

#rootObject

Root location.



58
# File 'lib/folio/shell.rb', line 58

def root ; dir('/') ; end

#root_configObject

Return a enumertor of system config directories.



466
467
468
# File 'lib/folio/shell.rb', line 466

def root_config() #_directories
  XDG.xdg_config_dirs.to_enum(:each){ |f| dir(f) }
end

#root_dataObject

Return a enumertor of system data directories.



471
472
473
# File 'lib/folio/shell.rb', line 471

def root_data() #_directories
  XDG.xdg_data_dirs.to_enum(:each){ |f| dir(f) }
end

#safe?(path) ⇒ Boolean

Returns:

  • (Boolean)


194
# File 'lib/folio/shell.rb', line 194

def safe?(path)  ; FileTest.safe?(localize(path))   ; end

#setgid?(path) ⇒ Boolean

Returns:

  • (Boolean)


181
# File 'lib/folio/shell.rb', line 181

def setgid?(path)     ; FileTest.setgid?(localize(path))    ; end

#setuid?(path) ⇒ Boolean

Returns:

  • (Boolean)


182
# File 'lib/folio/shell.rb', line 182

def setuid?(path)     ; FileTest.setuid?(localize(path))    ; end

#size(path) ⇒ Object



166
# File 'lib/folio/shell.rb', line 166

def size(path)        ; FileTest.size(localize(path))       ; end

#size?(path) ⇒ Boolean

Returns:

  • (Boolean)


167
# File 'lib/folio/shell.rb', line 167

def size?(path)       ; FileTest.size(localize(path))       ; end

#socket?(path) ⇒ Boolean

Returns:

  • (Boolean)


183
# File 'lib/folio/shell.rb', line 183

def socket?(path)     ; FileTest.socket?(localize(path))    ; end

#stage(stage_directory, files) ⇒ Object



359
360
361
362
363
# File 'lib/folio/shell.rb', line 359

def stage(stage_directory, files)
  dir   = localize(stage_directory)
  files = localize(files)
  fileutils.stage(dir, files)
end

#sticky?(path) ⇒ Boolean

Returns:

  • (Boolean)


178
# File 'lib/folio/shell.rb', line 178

def sticky?(path)     ; FileTest.sticky?(localize(path))    ; end

#symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


169
# File 'lib/folio/shell.rb', line 169

def symlink?(path)    ; FileTest.symlink?(localize(path))   ; end

#tar(folder, file = nil, options = {}) ⇒ Object



395
396
397
398
399
# File 'lib/folio/shell.rb', line 395

def tar(folder, file=nil, options={})
  folder = localize(folder)
  file   = localize(file)
  ziputils.tar_gzip(folder, file, options)
end

#tar_bzip2(folder, file = nil, options = {}) ⇒ Object

Create a tar.bz2 file of a directory.



410
411
412
413
414
# File 'lib/folio/shell.rb', line 410

def tar_bzip2(folder, file=nil, options={})
  folder = localize(folder)
  file   = localize(file)
  ziputils.tar_bzip2(folder, file, options)
end

#tar_gzip(folder, file = nil, options = {}) ⇒ Object Also known as: tgz

Create a tgz file of a directory.



402
403
404
405
406
# File 'lib/folio/shell.rb', line 402

def tar_gzip(folder, file=nil, options={})
  folder = localize(folder)
  file   = localize(file)
  ziputils.tar_gzip(folder, file, options)
end

#to_sObject

String representation is work directory path.



49
# File 'lib/folio/shell.rb', line 49

def to_s ; work.to_s ; end

#touch(list, options = {}) ⇒ Object

alias_method :chown_R, :chown_r



353
354
355
356
# File 'lib/folio/shell.rb', line 353

def touch(list, options={})
  list = localize(list)
  fileutils.touch(list, options)
end

#unbzip2(file, options) ⇒ Object



421
422
423
424
# File 'lib/folio/shell.rb', line 421

def unbzip2(file, options)
  file   = localize(file)
  ziputils.unbzip2(file, options)
end

#ungzip(file, options) ⇒ Object



416
417
418
419
# File 'lib/folio/shell.rb', line 416

def ungzip(file, options)
  file   = localize(file)
  ziputils.ungzip(file, options)
end

#untar(file, options) ⇒ Object



431
432
433
434
# File 'lib/folio/shell.rb', line 431

def untar(file, options)
  file   = localize(file)
  ziputils.untar(file, options)
end

#untar_bzip2(file, options) ⇒ Object



441
442
443
444
# File 'lib/folio/shell.rb', line 441

def untar_bzip2(file, options)
  file   = localize(file)
  ziputils.untar_bzip2(file, options)
end

#untar_gzip(file, options) ⇒ Object



436
437
438
439
# File 'lib/folio/shell.rb', line 436

def untar_gzip(file, options)
  file   = localize(file)
  ziputils.untar_gzip(file, options)
end

#unzip(file, options) ⇒ Object



426
427
428
429
# File 'lib/folio/shell.rb', line 426

def unzip(file, options)
  file   = localize(file)
  ziputils.unzip(file, options)
end

#uptodate?(new, old_list, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
239
240
# File 'lib/folio/shell.rb', line 236

def uptodate?(new, old_list, options=nil)
  new = localize(new)
  old = localize(old_list)
  fileutils.uptodate?(new, old, options)
end

#work_cacheObject

Return the work cache directory.



496
497
498
# File 'lib/folio/shell.rb', line 496

def work_cache
  dir(XDG.xdg_cache_work)
end

#work_configObject

Return the work config directory.



491
492
493
# File 'lib/folio/shell.rb', line 491

def work_config
  dir(XDG.xdg_config_work)
end

#writable?(path) ⇒ Boolean

Returns:

  • (Boolean)


187
# File 'lib/folio/shell.rb', line 187

def writable?(path)   ; FileTest.writable?(localize(path))  ; end

#writable_real?(path) ⇒ Boolean

Returns:

  • (Boolean)


190
# File 'lib/folio/shell.rb', line 190

def writable_real?(path)   ; FileTest.writable_real?(localize(path))   ; end

#zero?(path) ⇒ Boolean

Returns:

  • (Boolean)


175
# File 'lib/folio/shell.rb', line 175

def zero?(path)       ; FileTest.zero?(localize(path))      ; end

#zip(folder, file = nil, options = {}) ⇒ Object

Create a zip file of a directory.



388
389
390
391
392
# File 'lib/folio/shell.rb', line 388

def zip(folder, file=nil, options={})
  folder = localize(folder)
  file   = localize(file)
  ziputils.zip(folder, file, options)
end