Skip to content

Commit

Permalink
Introduce path type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jan 17, 2021
1 parent e7504e6 commit 929ecc8
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 126 deletions.
49 changes: 26 additions & 23 deletions stdlib/fileutils/0/fileutils.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@
# This module has all methods of FileUtils module, but never changes
# files/directories. This equates to passing the `:noop` and `:verbose` flags
# to methods in FileUtils.
#
module FileUtils
VERSION: String

type mode = Integer | String

type pathlist = String | Array[String]
type path = string | _ToPath

type pathlist = path | Array[path]

# Changes the current directory to the directory `dir`.
#
Expand All @@ -114,8 +117,8 @@ module FileUtils
# # ... # do something
# end # return to original directory
#
def self.cd: (String dir, ?verbose: boolish) -> void
| [X] (String dir, ?verbose: boolish) { (String) -> X } -> X
def self.cd: (path dir, ?verbose: boolish) -> void
| [X] (path dir, ?verbose: boolish) { (String) -> X } -> X

alias self.chdir self.cd

Expand Down Expand Up @@ -210,7 +213,7 @@ module FileUtils
# FileUtils.compare_file('somefile', 'somefile') #=> true
# FileUtils.compare_file('/dev/null', '/dev/urandom') #=> false
#
def self.compare_file: (String a, String b) -> bool
def self.compare_file: (path a, path b) -> bool

alias self.cmp self.compare_file

Expand All @@ -235,12 +238,12 @@ module FileUtils
# If `remove_destination` is true, this method removes each destination file
# before copy.
#
def self.copy_entry: (String src, String dest, ?boolish preserve, ?boolish dereference_root, ?boolish remove_destination) -> void
def self.copy_entry: (path src, path dest, ?boolish preserve, ?boolish dereference_root, ?boolish remove_destination) -> void

# Copies file contents of `src` to `dest`. Both of `src` and `dest` must be a
# path name.
#
def self.copy_file: (String src, String dest, ?boolish preserve, ?boolish dereference) -> void
def self.copy_file: (path src, path dest, ?boolish preserve, ?boolish dereference) -> void

# Copies stream `src` to `dest`. `src` must respond to #read(n) and `dest` must
# respond to #write(str).
Expand All @@ -257,7 +260,7 @@ module FileUtils
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', verbose: true
# FileUtils.cp 'symlink', 'dest' # copy content, "dest" is not a symlink
#
def self.cp: (pathlist src, String dest, ?preserve: boolish, ?noop: boolish, ?verbose: boolish) -> void
def self.cp: (pathlist src, path dest, ?preserve: boolish, ?noop: boolish, ?verbose: boolish) -> void

alias self.copy self.cp

Expand All @@ -283,7 +286,7 @@ module FileUtils
# # use the following code.
# FileUtils.cp_lr 'src/.', 'dest' # cp_lr('src', 'dest') makes dest/src, but this doesn't.
#
def self.cp_lr: (pathlist src, String dest, ?noop: boolish, ?verbose: boolish, ?dereference_root: boolish, ?remove_destination: boolish) -> void
def self.cp_lr: (pathlist src, path dest, ?noop: boolish, ?verbose: boolish, ?dereference_root: boolish, ?remove_destination: boolish) -> void

# Copies `src` to `dest`. If `src` is a directory, this method copies all its
# contents recursively. If `dest` is a directory, copies `src` to `dest/src`.
Expand All @@ -309,7 +312,7 @@ module FileUtils
# FileUtils.cp_r 'src/.', 'dest' # cp_r('src', 'dest') makes dest/src,
# # but this doesn't.
#
def self.cp_r: (pathlist src, String dest, ?preserve: boolish, ?noop: boolish, ?verbose: boolish, ?dereference_root: boolish, ?remove_destination: boolish) -> void
def self.cp_r: (pathlist src, path dest, ?preserve: boolish, ?noop: boolish, ?verbose: boolish, ?dereference_root: boolish, ?remove_destination: boolish) -> void

# Returns true if the method `mid` have an option `opt`.
#
Expand All @@ -326,7 +329,7 @@ module FileUtils
# FileUtils.install 'ruby', '/usr/local/bin/ruby', mode: 0755, verbose: true
# FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', verbose: true
#
def self.install: (String src, String dest, ?mode: mode?, ?owner: String?, ?group: String?, ?preserve: boolish, ?noop: boolish, ?verbose: boolish) -> void
def self.install: (path src, path dest, ?mode: mode?, ?owner: String?, ?group: String?, ?preserve: boolish, ?noop: boolish, ?verbose: boolish) -> void

# Hard links a file system entry `src` to `dest`. If `src` is a directory, this
# method links its contents recursively.
Expand All @@ -339,7 +342,7 @@ module FileUtils
# If `remove_destination` is true, this method removes each destination file
# before copy.
#
def self.link_entry: (String src, String dest, ?boolish dereference_root, ?boolish remove_destination) -> void
def self.link_entry: (path src, path dest, ?boolish dereference_root, ?boolish remove_destination) -> void

# In the first form, creates a hard link `link` which points to `target`. If
# `link` already exists, raises Errno::EEXIST. But if the `force` option is set,
Expand All @@ -355,7 +358,7 @@ module FileUtils
# FileUtils.cd '/sbin'
# FileUtils.ln %w(cp mv mkdir), '/bin' # Now /sbin/cp and /bin/cp are linked.
#
def self.ln: (pathlist src, String dest, ?force: boolish, ?noop: boolish, ?verbose: boolish) -> void
def self.ln: (pathlist src, path dest, ?force: boolish, ?noop: boolish, ?verbose: boolish) -> void

alias self.link self.ln

Expand All @@ -372,15 +375,15 @@ module FileUtils
#
# FileUtils.ln_s Dir.glob('/bin/*.rb'), '/home/foo/bin'
#
def self.ln_s: (pathlist src, String dest, ?force: boolish, ?noop: boolish, ?verbose: boolish) -> void
def self.ln_s: (pathlist src, path dest, ?force: boolish, ?noop: boolish, ?verbose: boolish) -> void

alias self.symlink self.ln_s

# Same as
#
# FileUtils.ln_s(*args, force: true)
#
def self.ln_sf: (pathlist src, String dest, ?noop: boolish, ?verbose: boolish) -> void
def self.ln_sf: (pathlist src, path dest, ?noop: boolish, ?verbose: boolish) -> void

# Creates one or more directories.
#
Expand All @@ -389,7 +392,7 @@ module FileUtils
# FileUtils.mkdir 'notexist', noop: true # Does not really create.
# FileUtils.mkdir 'tmp', mode: 0700
#
def self.mkdir: (pathlist list, ?mode: mode?, ?noop: boolish, ?verbose: boolish) -> void
def self.mkdir: (pathlist list, ?mode: Integer?, ?noop: boolish, ?verbose: boolish) -> void

# Creates a directory and all its parent directories. For example,
#
Expand Down Expand Up @@ -420,7 +423,7 @@ module FileUtils
# FileUtils.mv %w(junk.txt dust.txt), '/home/foo/.trash/'
# FileUtils.mv Dir.glob('test*.rb'), 'test', noop: true, verbose: true
#
def self.mv: (pathlist src, String dest, ?force: boolish, ?noop: boolish, ?verbose: boolish, ?secure: boolish) -> void
def self.mv: (pathlist src, path dest, ?force: boolish, ?noop: boolish, ?verbose: boolish, ?secure: boolish) -> void

alias self.move self.mv

Expand All @@ -445,15 +448,15 @@ module FileUtils
# Removes a directory `dir` and its contents recursively. This method ignores
# StandardError if `force` is true.
#
def self.remove_dir: (String path, ?boolish force) -> void
def self.remove_dir: (path path, ?boolish force) -> void

# This method removes a file system entry `path`. `path` might be a regular
# file, a directory, or something. If `path` is a directory, remove it
# recursively.
#
# See also remove_entry_secure.
#
def self.remove_entry: (String path, ?boolish force) -> void
def self.remove_entry: (path path, ?boolish force) -> void

# This method removes a file system entry `path`. `path` shall be a regular
# file, a directory, or something. If `path` is a directory, remove it
Expand Down Expand Up @@ -487,11 +490,11 @@ module FileUtils
#
# For fileutils.rb, this vulnerability is reported in [ruby-dev:26100].
#
def self.remove_entry_secure: (String path, ?boolish force) -> void
def self.remove_entry_secure: (path path, ?boolish force) -> void

# Removes a file `path`. This method ignores StandardError if `force` is true.
#
def self.remove_file: (String path, ?void force) -> void
def self.remove_file: (path path, ?void force) -> void

# Remove file(s) specified in `list`. This method cannot remove directories.
# All StandardErrors are ignored when the :force option is set.
Expand Down Expand Up @@ -540,6 +543,8 @@ module FileUtils
#
def self.rm_rf: (pathlist list, ?noop: boolish, ?verbose: boolish, ?secure: boolish) -> void

alias self.rmtree self.rm_rf

# Removes one or more directories.
#
# FileUtils.rmdir 'somedir'
Expand All @@ -549,8 +554,6 @@ module FileUtils
#
def self.rmdir: (pathlist list, ?parents: boolish, ?noop: boolish, ?verbose: boolish) -> void

alias self.rmtree self.rm_rf

# Updates modification time (mtime) and access time (atime) of file(s) in
# `list`. Files are created if they don't exist.
#
Expand All @@ -565,5 +568,5 @@ module FileUtils
# FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \
# system 'make hello.o'
#
def self.uptodate?: (String new, Array[String] old_list) -> bool
def self.uptodate?: (path new, pathlist old_list) -> bool
end
Loading

0 comments on commit 929ecc8

Please sign in to comment.