Module: FS::Base

Included in:
FS
Defined in:
lib/fs/base.rb

Instance Method Summary collapse

Instance Method Details

#absolute?(path) ⇒ Boolean

checks for a slash at the beginning

Returns:

  • (Boolean)


193
194
195
# File 'lib/fs/base.rb', line 193

def absolute?(path)
  %r{\A/} =~ path ? true : false
end

#basename(path) ⇒ Object

File.basename “tmp/foo/bar.todo” => “bar.todo”



205
206
207
# File 'lib/fs/base.rb', line 205

def basename(path)
  File.basename(path)
end

#changedir(dir) ⇒ Object

Dir#chdir



109
110
111
# File 'lib/fs/base.rb', line 109

def changedir(dir)
  Dir.chdir(dir)
end

#chop_path(path, base = '.') ⇒ Object

chop base from the path if it’s not a subdir the absolute path will be returned



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fs/base.rb', line 180

def chop_path(path, base='.')
  full_base = File.expand_path(base)
  full_path = File.expand_path(path)
  if full_path == full_base
    '.'
  elsif full_path.start_with?(full_base)
    full_path[full_base.size+1..-1]
  else
    full_path
  end
end

#copy(*froms, to) ⇒ Object

TODO: find time to make this cool, not work only FileUtils#cp



55
56
57
58
59
# File 'lib/fs/base.rb', line 55

def copy(*froms, to)
  froms.each do |from|
    FileUtils.cp(from, to)
  end
end

#currentdirObject

Dir#pwd



114
115
116
# File 'lib/fs/base.rb', line 114

def currentdir
  Dir.pwd
end

#directory?(path) ⇒ Boolean

File.directory?

Returns:

  • (Boolean)


147
148
149
# File 'lib/fs/base.rb', line 147

def directory?(path)
  File.directory?(path)
end

#dirname(path) ⇒ Object

File.dirname “tmp/foo/bar.todo” => “tmp/foo”



199
200
201
# File 'lib/fs/base.rb', line 199

def dirname(path)
  File.dirname(path)
end

#empty?(path) ⇒ Boolean

uses File.size and Dir.entries for files it returns ‘nil` if file does not exist, `true` if it’s empty

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
# File 'lib/fs/base.rb', line 158

def empty?(path)
  if !File.exist?(path)
    nil
  elsif File.directory?(path)
    Dir.entries(path) == ['.', '..']
  else
    File.size(path) == 0
  end
end

#exist?(path) ⇒ Boolean

File.exist?

Returns:

  • (Boolean)


142
143
144
# File 'lib/fs/base.rb', line 142

def exist?(path)
  File.exist?(path)
end

#expand_path(path, base = nil) ⇒ Object

File.expand_path



174
175
176
# File 'lib/fs/base.rb', line 174

def expand_path(path, base=nil)
  File.expand_path(path, base)
end

#extname(path) ⇒ Object

File.extname “tmp/foo/bar.todo” => “.todo”



211
212
213
# File 'lib/fs/base.rb', line 211

def extname(path)
  File.extname(path)
end

#file?(path) ⇒ Boolean

File.file?

Returns:

  • (Boolean)


152
153
154
# File 'lib/fs/base.rb', line 152

def file?(path)
  File.file?(path)
end

#filename(path) ⇒ Object

“tmp/foo/bar.todo” => “bar”



216
217
218
219
220
221
# File 'lib/fs/base.rb', line 216

def filename(path)
  return '' if path == '/' || path == '.'
  base = File.basename(path)
  ext = File.extname(path)
  ext.empty? ? base :base[0...-ext.size]
end

#home(user = nil) ⇒ Object

Dir#home the path is always expanded



99
100
101
# File 'lib/fs/base.rb', line 99

def home(user=nil)
  File.expand_path(Dir.home(user))
end

#join(*args) ⇒ Object

File.join



169
170
171
# File 'lib/fs/base.rb', line 169

def join(*args)
  File.join(*args)
end

Creates hard links for files and symbolic links for dirs

FileUtils#ln or FileUtils#ln_s



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

def link(from, to)
  if File.directory?(from)
    FileUtils.ln_s(from, to)
  else
    FileUtils.ln(from, to)
  end
end

#list(dir = '.', pattern = '*') ⇒ Object

Dir#glob



33
34
35
# File 'lib/fs/base.rb', line 33

def list(dir='.', pattern='*')
  glob(dir, pattern, nil)
end

#list_dirs(dir = '.', pattern = '*') ⇒ Object



37
38
39
# File 'lib/fs/base.rb', line 37

def list_dirs(dir='.', pattern='*')
  glob(dir, pattern, ->(path){FS.directory?(path)})
end

#list_files(dir = '.', pattern = '*') ⇒ Object



41
42
43
# File 'lib/fs/base.rb', line 41

def list_files(dir='.', pattern='*')
  glob(dir, pattern, ->(path){FS.file?(path)})
end

#makedir(*dirs) ⇒ Object

FileUtils#mkdir



10
11
12
# File 'lib/fs/base.rb', line 10

def makedir(*dirs)
  FileUtils.mkdir(dirs)
end

#makedirs(*dirs) ⇒ Object

FileUtils#mkdir_p



15
16
17
# File 'lib/fs/base.rb', line 15

def makedirs(*dirs)
  FileUtils.mkdir_p(dirs)
end

#maketempdir(prefix_suffix = nil, parent_dir = nil) ⇒ Object

TODO: use separate options for prefix, suffix and target_dir tmpdir / Dir.mktmpdir



125
126
127
# File 'lib/fs/base.rb', line 125

def maketempdir(prefix_suffix=nil, parent_dir=nil)
  Dir.mktmpdir(prefix_suffix, parent_dir)
end

#maketempfile(prefix_suffix = nil, parent_dir = nil) ⇒ Object

uses the methods of the tmpdir library to touch a new file in tempdir



130
131
132
# File 'lib/fs/base.rb', line 130

def maketempfile(prefix_suffix=nil, parent_dir=nil)
  Dir::Tmpname.create(prefix_suffix || "f", parent_dir || Dir.tmpdir) {|n| FileUtils.touch(n)}
end

#move(*froms, to) ⇒ Object

TODO: find time to make this cool, not work only FileUtils#mv



47
48
49
50
51
# File 'lib/fs/base.rb', line 47

def move(*froms, to)
  froms.each do |from|
    FileUtils.mv(from, to)
  end
end

#read(file, &block) ⇒ Object

File#open(file, ‘r’)



87
88
89
90
91
92
93
94
95
# File 'lib/fs/base.rb', line 87

def read(file, &block)
  if block_given?
    File.open(file, 'r', &block)
  else
    content = nil
    File.open(file, 'r') {|f| content = f.read }
    content
  end
end

#remove(*pathes) ⇒ Object

FileUtils#rm



73
74
75
# File 'lib/fs/base.rb', line 73

def remove(*pathes)
  FileUtils.rm(pathes)
end

#removedir(*dirs) ⇒ Object

FileUtils#rmdir



20
21
22
23
24
25
# File 'lib/fs/base.rb', line 20

def removedir(*dirs)
  dirs.each do |dir|
    raise Errno::ENOTEMPTY unless list(dir).empty?
  end
  FileUtils.rmdir(dirs)
end

#removedirs(*dirs) ⇒ Object

FileUtils#rm_r



28
29
30
# File 'lib/fs/base.rb', line 28

def removedirs(*dirs)
  FileUtils.rm_r(dirs)
end

#rootObject

always returns ‘/’



104
105
106
# File 'lib/fs/base.rb', line 104

def root
  '/'
end

#splitname(path) ⇒ Object

“tmp/foo/bar.todo” => [“tmp/foo”, “bar”, “.todo”]



224
225
226
# File 'lib/fs/base.rb', line 224

def splitname(path)
  [dirname(path), filename(path), extname(path)]
end

#tempdirObject

tmpdir / Dir.tmpdir



119
120
121
# File 'lib/fs/base.rb', line 119

def tempdir
  Dir.tmpdir
end

#this_dirObject

File.dirname(__FILE__) of the caller the path is always expanded



236
237
238
# File 'lib/fs/base.rb', line 236

def this_dir
  File.expand_path(File.dirname(caller_file(caller)))
end

#this_fileObject

__FILE__ of the caller the path is always expanded



230
231
232
# File 'lib/fs/base.rb', line 230

def this_file
  File.expand_path(caller_file(caller))
end

#touch(*files) ⇒ Object

FileUtils.touch



5
6
7
# File 'lib/fs/base.rb', line 5

def touch(*files)
  FileUtils.touch(files)
end

#tree(dir = '.') ⇒ Object

same as the ‘tree` shell command



135
136
137
138
139
# File 'lib/fs/base.rb', line 135

def tree(dir='.')
  output = []
  visit_tree(output, '.', '', '', '', dir)
  output.join("\n")
end

#write(file, content = nil, &block) ⇒ Object

File#open(file, ‘w’)



78
79
80
81
82
83
84
# File 'lib/fs/base.rb', line 78

def write(file, content=nil, &block)
  if block_given?
    File.open(file, 'w', &block)
  else
    File.open(file, 'w') {|f| f.write(content) }
  end
end