blob: 5a9c3bdba1bcbab65702209dcd9a14bed73b2512 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
class Object
# Copies a file
def cp(source, dest)
File.open(dest, "w") do |d|
File.open(source, "r") do |s|
while data = s.read(1024)
d.write data
end
end
end
end
# Creates each directory in path that does not exist.
def mkdir_p(path)
parts = File.expand_path(path).split %r[/|\\]
name = parts.shift
parts.each do |part|
name = File.join name, part
stat = File.stat name rescue nil
if stat and stat.file?
raise ArgumentError, "path component of #{path} is a file"
end
unless stat and stat.directory?
begin
Dir.mkdir name
rescue Errno::EEXIST
raise unless File.directory? name
end
end
end
end
# Recursively removes all files and directories in +path+
# if +path+ is a directory. Removes the file if +path+ is
# a file.
def rm_r(*paths)
paths.each do |path|
path = File.expand_path path
prefix = SPEC_TEMP_DIR
unless path[0, prefix.size] == prefix
raise ArgumentError, "#{path} is not prefixed by #{prefix}"
end
# File.symlink? needs to be checked first as
# File.exist? returns false for dangling symlinks
if File.symlink? path
File.unlink path
elsif File.directory? path
Dir.entries(path).each { |x| rm_r "#{path}/#{x}" unless x =~ /^\.\.?$/ }
Dir.rmdir path
elsif File.exist? path
File.delete path
end
end
end
# Creates a file +name+. Creates the directory for +name+
# if it does not exist.
def touch(name, mode="w")
mkdir_p File.dirname(name)
File.open(name, mode) do |f|
yield f if block_given?
end
end
end
|