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)

215
216
217
# File 'lib/fs/base.rb', line 215

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

#basename(path) ⇒ Object

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


227
228
229
# File 'lib/fs/base.rb', line 227

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

#changedir(dir) ⇒ Object

Dir#chdir


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

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


202
203
204
205
206
207
208
209
210
211
212
# File 'lib/fs/base.rb', line 202

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


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

def currentdir
  Dir.pwd
end

#directory?(path) ⇒ Boolean

File.directory?

Returns:

  • (Boolean)

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

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

#dirname(path) ⇒ Object

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


221
222
223
# File 'lib/fs/base.rb', line 221

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)

Raises:

  • (Errno::ENOENT)

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

def empty?(path)
  raise Errno::ENOENT unless File.exist?(path)
  if File.directory?(path)
    files = Dir.entries(path)
    files.size == 2 && files.sort == ['.', '..']
  else
    File.size(path) == 0
  end
end

#exist?(path) ⇒ Boolean

File.exist?

Returns:

  • (Boolean)

164
165
166
# File 'lib/fs/base.rb', line 164

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

#expand_path(path, base = nil) ⇒ Object

File.expand_path


196
197
198
# File 'lib/fs/base.rb', line 196

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

#extname(path) ⇒ Object

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


233
234
235
# File 'lib/fs/base.rb', line 233

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

#file?(path) ⇒ Boolean

File.file?

Returns:

  • (Boolean)

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

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

#filename(path) ⇒ Object

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


238
239
240
241
242
243
244
# File 'lib/fs/base.rb', line 238

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

#grep(file, regexp, &block) ⇒ Object

File#open(file, 'r') =~ /REGEX/


109
110
111
112
113
114
115
116
117
# File 'lib/fs/base.rb', line 109

def grep(file, regexp, &block)
  if block_given?
    File.open(file, 'r').each {|line| line.chomp!; block[line] if line =~ regexp }
  else
    lines = []
    File.open(file, 'r').each {|line| line.chomp!; lines << line if line =~ regexp }
    lines
  end
end

#home(user = nil) ⇒ Object

Dir#home the path is always expanded


121
122
123
# File 'lib/fs/base.rb', line 121

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

#join(*args) ⇒ Object

File.join


191
192
193
# File 'lib/fs/base.rb', line 191

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)
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


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

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


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

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').read


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') {|f| block[f.read] }
  else
    content = nil
    File.open(file, 'r') {|f| content = f.read }
    content
  end
end

#read_lines(file, &block) ⇒ Object

File#open(file, 'r').each


98
99
100
101
102
103
104
105
106
# File 'lib/fs/base.rb', line 98

def read_lines(file, &block)
  if block_given?
    File.open(file, 'r').each {|line| line.chomp!; block[line] }
  else
    lines = []
    File.open(file, 'r').each {|line| line.chomp!; lines << line }
    lines
  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 empty?(dir)
  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 '/'


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

def root
  '/'
end

#splitname(path) ⇒ Object

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


247
248
249
# File 'lib/fs/base.rb', line 247

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

#tempdirObject

tmpdir / Dir.tmpdir


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

def tempdir
  Dir.tmpdir
end

#this_dirObject

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


259
260
261
# File 'lib/fs/base.rb', line 259

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

#this_fileObject

__FILE__ of the caller the path is always expanded


253
254
255
# File 'lib/fs/base.rb', line 253

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


157
158
159
160
161
# File 'lib/fs/base.rb', line 157

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