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.
75 76 77 78 79 |
# File 'lib/mustermann/file_utils.rb', line 75 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.
91 92 93 |
# File 'lib/mustermann/file_utils.rb', line 91 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.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mustermann/file_utils.rb', line 40 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.
60 61 62 63 64 |
# File 'lib/mustermann/file_utils.rb', line 60 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.
26 27 28 |
# File 'lib/mustermann/file_utils.rb', line 26 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.
119 120 121 122 123 |
# File 'lib/mustermann/file_utils.rb', line 119 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.
134 135 136 |
# File 'lib/mustermann/file_utils.rb', line 134 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.
148 149 150 |
# File 'lib/mustermann/file_utils.rb', line 148 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.
104 105 106 107 |
# File 'lib/mustermann/file_utils.rb', line 104 def mv(map = {}, **) utils_opts, opts = (**) glob_map(map, **opts) { |o,n| f.mv(o, n, **utils_opts) } end |