Permalink
Browse files

added basic file checks

  • Loading branch information...
Simon Chiang
Simon Chiang committed May 3, 2011
1 parent 749fdf5 commit 6a06d7f8330631f7f5e4236a87f826248f01fc38
@@ -1,3 +1,4 @@
-(path)
+Checks that dir exists and is a directory.
+(dir)
--
-[ -d "<%= path %>" ]
+[ -d "<%= dir %>" ]
@@ -0,0 +1,4 @@
+Checks that file exists and is executable, or file is a directory that can be searched.
+(file)
+--
+[ -x "<%= file %>" ]
@@ -1,3 +1,4 @@
-(path)
+Checks that file exists.
+(file)
--
-[ -e "<%= path %>" ]
+[ -e "<%= file %>" ]
@@ -1,3 +1,4 @@
-(path)
+Checks that file exists and is a regular file.
+(file)
--
-[ -f "<%= path %>" ]
+[ -f "<%= file %>" ]
@@ -0,0 +1,4 @@
+Checks that file exists and is not empty.
+(file)
+--
+[ -s "<%= file %>" ]
@@ -0,0 +1,4 @@
+Checks that file exists and is a symbolic link.
+(file)
+--
+[ -L "<%= file %>" ]
@@ -0,0 +1,4 @@
+Checks that file exists and is readable.
+(file)
+--
+[ -r "<%= file %>" ]
@@ -0,0 +1,4 @@
+Checks that file exists and is writable.
+(file)
+--
+[ -w "<%= file %>" ]
View
@@ -99,42 +99,6 @@ def _cp(*args, &block) # :nodoc:
str
end
- # Copy source to target, with -f.
- def cp_f(source, target)
- cp source, target, '-f' => true
- chain_proxy
- end
-
- def _cp_f(*args, &block) # :nodoc:
- str = capture_str { cp_f(*args, &block) }
- str.strip!
- str
- end
-
- # Copy source to target, with -r.
- def cp_r(source, target)
- cp source, target, '-r'=> true
- chain_proxy
- end
-
- def _cp_r(*args, &block) # :nodoc:
- str = capture_str { cp_r(*args, &block) }
- str.strip!
- str
- end
-
- # Copy source to target, with -rf.
- def cp_rf(source, target)
- cp source, target, '-rf' => true
- chain_proxy
- end
-
- def _cp_rf(*args, &block) # :nodoc:
- str = capture_str { cp_rf(*args, &block) }
- str.strip!
- str
- end
-
# Returns the current system time. A format string may be provided, as well as
# a hash of command line options.
def date(format=nil, options={})
@@ -152,9 +116,12 @@ def _date(*args, &block) # :nodoc:
str
end
- def directory?(path)
- # [ -d "<%= path %>" ]
- write "[ -d \""; write(( path ).to_s); write "\" ]"
+ # Checks that dir exists and is a directory.
+ def directory?(dir)
+ # [ -d "<%= dir %>" ]
+ #
+ write "[ -d \""; write(( dir ).to_s); write "\" ]\n"
+
chain_proxy
end
@@ -176,9 +143,27 @@ def _echo(*args, &block) # :nodoc:
str
end
- def exists?(path)
- # [ -e "<%= path %>" ]
- write "[ -e \""; write(( path ).to_s); write "\" ]"
+ # Checks that file exists and is executable, or file is a directory that can be searched.
+ def executable?(file)
+ # [ -x "<%= file %>" ]
+ #
+ write "[ -x \""; write(( file ).to_s); write "\" ]\n"
+
+ chain_proxy
+ end
+
+ def _executable?(*args, &block) # :nodoc:
+ str = capture_str { executable?(*args, &block) }
+ str.strip!
+ str
+ end
+
+ # Checks that file exists.
+ def exists?(file)
+ # [ -e "<%= file %>" ]
+ #
+ write "[ -e \""; write(( file ).to_s); write "\" ]\n"
+
chain_proxy
end
@@ -188,9 +173,12 @@ def _exists?(*args, &block) # :nodoc:
str
end
- def file?(path)
- # [ -f "<%= path %>" ]
- write "[ -f \""; write(( path ).to_s); write "\" ]"
+ # Checks that file exists and is a regular file.
+ def file?(file)
+ # [ -f "<%= file %>" ]
+ #
+ write "[ -f \""; write(( file ).to_s); write "\" ]\n"
+
chain_proxy
end
@@ -216,50 +204,56 @@ def _gsub(*args, &block) # :nodoc:
str
end
- # Link source to target. Accepts a hash of command line options.
- def ln(source, target, options={})
- execute 'ln', source, target, options
+ # Checks that file exists and is not empty.
+ def has_content?(file)
+ # [ -s "<%= file %>" ]
+ #
+ write "[ -s \""; write(( file ).to_s); write "\" ]\n"
+
chain_proxy
end
- def _ln(*args, &block) # :nodoc:
- str = capture_str { ln(*args, &block) }
+ def _has_content?(*args, &block) # :nodoc:
+ str = capture_str { has_content?(*args, &block) }
str.strip!
str
end
- # Copy source to target, with -s.
- def ln_s(source, target)
- ln source, target, '-s' => true
+ # Checks that file exists and is a symbolic link.
+ def link?(file)
+ # [ -L "<%= file %>" ]
+ #
+ write "[ -L \""; write(( file ).to_s); write "\" ]\n"
+
chain_proxy
end
- def _ln_s(*args, &block) # :nodoc:
- str = capture_str { ln_s(*args, &block) }
+ def _link?(*args, &block) # :nodoc:
+ str = capture_str { link?(*args, &block) }
str.strip!
str
end
- # Make a directory. Accepts a hash of command line options.
- def mkdir(path, options={})
- execute 'mkdir', path, options
+ # Link source to target. Accepts a hash of command line options.
+ def ln(source, target, options={})
+ execute 'ln', source, target, options
chain_proxy
end
- def _mkdir(*args, &block) # :nodoc:
- str = capture_str { mkdir(*args, &block) }
+ def _ln(*args, &block) # :nodoc:
+ str = capture_str { ln(*args, &block) }
str.strip!
str
end
- # Make a directory, and parent directories as needed.
- def mkdir_p(path)
- mkdir path, '-p' => true
+ # Make a directory. Accepts a hash of command line options.
+ def mkdir(path, options={})
+ execute 'mkdir', path, options
chain_proxy
end
- def _mkdir_p(*args, &block) # :nodoc:
- str = capture_str { mkdir_p(*args, &block) }
+ def _mkdir(*args, &block) # :nodoc:
+ str = capture_str { mkdir(*args, &block) }
str.strip!
str
end
@@ -276,14 +270,17 @@ def _mv(*args, &block) # :nodoc:
str
end
- # Move source to target, with -f.
- def mv_f(source, target)
- mv source, target, '-f' => true
+ # Checks that file exists and is readable.
+ def readable?(file)
+ # [ -r "<%= file %>" ]
+ #
+ write "[ -r \""; write(( file ).to_s); write "\" ]\n"
+
chain_proxy
end
- def _mv_f(*args, &block) # :nodoc:
- str = capture_str { mv_f(*args, &block) }
+ def _readable?(*args, &block) # :nodoc:
+ str = capture_str { readable?(*args, &block) }
str.strip!
str
end
@@ -300,30 +297,6 @@ def _rm(*args, &block) # :nodoc:
str
end
- # Unlink a file or directory, with -r.
- def rm_r(path)
- rm path, '-r' => true
- chain_proxy
- end
-
- def _rm_r(*args, &block) # :nodoc:
- str = capture_str { rm_r(*args, &block) }
- str.strip!
- str
- end
-
- # Unlink a file or directory, with -rf.
- def rm_rf(path)
- rm path, '-rf' => true
- chain_proxy
- end
-
- def _rm_rf(*args, &block) # :nodoc:
- str = capture_str { rm_rf(*args, &block) }
- str.strip!
- str
- end
-
def section(comment="")
n = (78 - comment.length)/2
str = "-" * n
@@ -457,6 +430,21 @@ def _shebang(*args, &block) # :nodoc:
str.strip!
str
end
+
+ # Checks that file exists and is writable.
+ def writable?(file)
+ # [ -w "<%= file %>" ]
+ #
+ write "[ -w \""; write(( file ).to_s); write "\" ]\n"
+
+ chain_proxy
+ end
+
+ def _writable?(*args, &block) # :nodoc:
+ str = capture_str { writable?(*args, &block) }
+ str.strip!
+ str
+ end
end
end
end
Oops, something went wrong.

0 comments on commit 6a06d7f

Please sign in to comment.