Module: Mustermann::FileUtils
- Extended by:
- FileUtils
- Included in:
- FileUtils
- Defined in:
- lib/mustermann/file_utils.rb,
lib/mustermann/file_utils/glob_pattern.rb
Overview
Implements handy file operations using patterns.
Instance Method Summary collapse
-
#cp(map = {}, recursive: false, **options) ⇒ Object
(also: #copy)
Copies files based on a pattern mapping.
-
#cp_r(map = {}, **options) ⇒ Object
Copies files based on a pattern mapping, recursively.
-
#glob(*pattern, **options, &block) ⇒ Object
(also: #[])
Uses the given pattern(s) to search for files and directories.
-
#glob_map(map = {}, **options, &block) ⇒ Object
Allows to search for files an map these onto other strings.
-
#glob_pattern(*pattern, **options) ⇒ String
Turn a Mustermann pattern into glob pattern.
-
#ln(map = {}, symbolic: false, **options) ⇒ Object
(also: #link)
Creates links based on a pattern mapping.
-
#ln_s(map = {}, **options) ⇒ Object
(also: #symlink)
Creates symbolic links based on a pattern mapping.
-
#ln_sf(map = {}, **options) ⇒ Object
Creates symbolic links based on a pattern mapping.
-
#mv(map = {}, **options) ⇒ Object
(also: #move)
Moves files based on a pattern mapping.
Instance Method Details
#cp(map = {}, recursive: false, **options) ⇒ Object Also known as: copy
Copies files based on a pattern mapping.
74 75 76 77 78 |
# File 'lib/mustermann/file_utils.rb', line 74 def cp(map = {}, recursive: false, **) utils_opts, opts = (:preserve, :dereference_root, :remove_destination, **) cp_method = recursive ? :cp_r : :cp glob_map(map, **opts) { |o,n| f.send(cp_method, o, n, **utils_opts) } end |
#cp_r(map = {}, **options) ⇒ Object
Copies files based on a pattern mapping, recursively.
90 91 92 |
# File 'lib/mustermann/file_utils.rb', line 90 def cp_r(map = {}, **) cp(map, recursive: true, **) end |
#glob(*pattern, **options, &block) ⇒ Object Also known as: []
Uses the given pattern(s) to search for files and directories.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mustermann/file_utils.rb', line 39 def glob(*pattern, **, &block) raise ArgumentError, "no pattern given" if pattern.empty? pattern, glob_pattern = pattern_with_glob_pattern(*pattern, **) results = [] unless block Dir.glob(glob_pattern) do |result| next unless params = pattern.params(result) block ? block[result, params] : results << result end results end |
#glob_map(map = {}, **options, &block) ⇒ Object
Allows to search for files an map these onto other strings.
59 60 61 62 63 |
# File 'lib/mustermann/file_utils.rb', line 59 def glob_map(map = {}, **, &block) map = Mapper === map ? map : Mapper.new(map, **) mapped = glob(*map.to_h.keys).map { |f| [f, unescape(map[f])] } block ? mapped.map(&block) : Hash[mapped] end |
#glob_pattern(*pattern, **options) ⇒ String
Turn a Mustermann pattern into glob pattern.
25 26 27 |
# File 'lib/mustermann/file_utils.rb', line 25 def glob_pattern(*pattern, **) pattern_with_glob_pattern(*pattern, **).last end |
#ln(map = {}, symbolic: false, **options) ⇒ Object Also known as: link
Creates links based on a pattern mapping.
118 119 120 121 122 |
# File 'lib/mustermann/file_utils.rb', line 118 def ln(map = {}, symbolic: false, **) utils_opts, opts = (**) link_method = symbolic ? :ln_s : :ln glob_map(map, **opts) { |o,n| f.send(link_method, o, n, **utils_opts) } end |
#ln_s(map = {}, **options) ⇒ Object Also known as: symlink
Creates symbolic links based on a pattern mapping.
133 134 135 |
# File 'lib/mustermann/file_utils.rb', line 133 def ln_s(map = {}, **) ln(map, symbolic: true, **) end |
#ln_sf(map = {}, **options) ⇒ Object
Creates symbolic links based on a pattern mapping. Overrides potentailly existing files.
147 148 149 |
# File 'lib/mustermann/file_utils.rb', line 147 def ln_sf(map = {}, **) ln(map, symbolic: true, force: true, **) end |
#mv(map = {}, **options) ⇒ Object Also known as: move
Moves files based on a pattern mapping.
103 104 105 106 |
# File 'lib/mustermann/file_utils.rb', line 103 def mv(map = {}, **) utils_opts, opts = (**) glob_map(map, **opts) { |o,n| f.mv(o, n, **utils_opts) } end |