module Mustermann::FileUtils

Implements handy file operations using patterns.

Public Instance Methods

[](*pattern, **options, &block)
Alias for: glob
copy(map = {}, recursive: false, **options)
Alias for: cp
cp(map = {}, recursive: false, **options) click to toggle source

Copies files based on a pattern mapping.

@example

require 'mustermann/file_utils'

# copies example.txt to example.bak.txt
Mustermann::FileUtils.cp(':base.:ext' => ':base.bak.:ext')

@see glob_map

# File lib/mustermann/file_utils.rb, line 75
def cp(map = {}, recursive: false, **options)
  utils_opts, opts = split_options(:preserve, :dereference_root, :remove_destination, **options)
  cp_method        = recursive ? :cp_r : :cp
  glob_map(map, **opts) { |o,n| f.send(cp_method, o, n, **utils_opts) }
end
Also aliased as: copy
cp_r(map = {}, **options) click to toggle source

Copies files based on a pattern mapping, recursively.

@example

require 'mustermann/file_utils'

# copies Foo.app/example.txt to Foo.back.app/example.txt
Mustermann::FileUtils.cp_r(':base.:ext' => ':base.bak.:ext')

@see glob_map

# File lib/mustermann/file_utils.rb, line 91
def cp_r(map = {}, **options)
  cp(map, recursive: true, **options)
end
glob(*pattern, **options, &block) click to toggle source

Uses the given pattern(s) to search for files and directories.

@example

require 'mustermann/file_utils'
Mustermann::FileUtils.glob(':base.:ext') # => ['example.txt']

Mustermann::FileUtils.glob(':base.:ext') do |file, params|
  file   # => "example.txt"
  params # => {"base"=>"example", "ext"=>"txt"}
end
# File lib/mustermann/file_utils.rb, line 40
def glob(*pattern, **options, &block)
  raise ArgumentError, "no pattern given" if pattern.empty?
  pattern, glob_pattern = pattern_with_glob_pattern(*pattern, **options)
  results               = [] unless block
  Dir.glob(glob_pattern) do |result|
    next unless params = pattern.params(result)
    block ? block[result, params] : results << result
  end
  results
end
Also aliased as: []
glob_map(map = {}, **options, &block) click to toggle source

Allows to search for files an map these onto other strings.

@example

require 'mustermann/file_utils'

Mustermann::FileUtils.glob_map(':base.:ext' => ':base.bak.:ext') # => {'example.txt' => 'example.bak.txt'}
Mustermann::FileUtils.glob_map(':base.:ext' => :base) { |file, mapped| mapped } # => ['example']

@see Mustermann::Mapper

# File lib/mustermann/file_utils.rb, line 60
def glob_map(map = {}, **options, &block)
  map    = Mapper === map ? map : Mapper.new(map, **options)
  mapped = glob(*map.to_h.keys).map { |f| [f, unescape(map[f])] }
  block ? mapped.map(&block) : Hash[mapped]
end
glob_pattern(*pattern, **options) click to toggle source

Turn a Mustermann pattern into glob pattern.

@example

require 'mustermann/file_utils'

Mustermann::FileUtils.glob_pattern('/:name')                  # => '/*'
Mustermann::FileUtils.glob_pattern('src/:path/:file.(js|rb)') # => 'src/**/*/*.{js,rb}'
Mustermann::FileUtils.glob_pattern('{a,b}/*', type: :shell)   # => '{a,b}/*'

pattern = Mustermann.new('/foo/:page', '/bar/:page') # => #<Mustermann::Composite:...>
Mustermann::FileUtils.glob_pattern(pattern)          # => "{/foo/*,/bar/*}"

@param [Object] pattern the object to turn into a glob pattern. @return [String] the glob pattern

# File lib/mustermann/file_utils.rb, line 26
def glob_pattern(*pattern, **options)
  pattern_with_glob_pattern(*pattern, **options).last
end
ln(map = {}, symbolic: false, **options) click to toggle source

Creates links based on a pattern mapping.

@example

require 'mustermann/file_utils'

# creates a link from bin/example to lib/example.rb
Mustermann::FileUtils.ln('lib/:name.rb' => 'bin/:name')

@see glob_map

# File lib/mustermann/file_utils.rb, line 119
def ln(map = {}, symbolic: false, **options)
  utils_opts, opts = split_options(**options)
  link_method      = symbolic ? :ln_s : :ln
  glob_map(map, **opts) { |o,n| f.send(link_method, o, n, **utils_opts) }
end
Also aliased as: link
ln_s(map = {}, **options) click to toggle source

Creates symbolic links based on a pattern mapping.

@example

require 'mustermann/file_utils'

# creates a symbolic link from bin/example to lib/example.rb
Mustermann::FileUtils.ln_s('lib/:name.rb' => 'bin/:name')

@see glob_map

# File lib/mustermann/file_utils.rb, line 134
def ln_s(map = {}, **options)
  ln(map, symbolic: true, **options)
end
Also aliased as: symlink
ln_sf(map = {}, **options) click to toggle source

Creates symbolic links based on a pattern mapping. Overrides potentailly existing files.

@example

require 'mustermann/file_utils'

# creates a symbolic link from bin/example to lib/example.rb
Mustermann::FileUtils.ln_sf('lib/:name.rb' => 'bin/:name')

@see glob_map

# File lib/mustermann/file_utils.rb, line 148
def ln_sf(map = {}, **options)
  ln(map, symbolic: true, force: true, **options)
end
move(map = {}, **options)
Alias for: mv
mv(map = {}, **options) click to toggle source

Moves files based on a pattern mapping.

@example

require 'mustermann/file_utils'

# moves example.txt to example.bak.txt
Mustermann::FileUtils.mv(':base.:ext' => ':base.bak.:ext')

@see glob_map

# File lib/mustermann/file_utils.rb, line 104
def mv(map = {}, **options)
  utils_opts, opts = split_options(**options)
  glob_map(map, **opts) { |o,n| f.mv(o, n, **utils_opts) }
end
Also aliased as: move
with_file_utils(&block) click to toggle source

Create a new version of Mustermann::FileUtils using a different ::FileUtils module. @see DryRun @!visibility private

# File lib/mustermann/file_utils.rb, line 197
def with_file_utils(&block)
  Module.new do
    include Mustermann::FileUtils
    define_method(:f, &block)
    private(:f)
    extend self
  end
end

Private Instance Methods

f() click to toggle source

The FileUtils method to use. @!visibility private

# File lib/mustermann/file_utils.rb, line 183
def f
  ::FileUtils
end
pattern_with_glob_pattern(*pattern, **options) click to toggle source

Create a Mustermann pattern from whatever the input is and turn it into a glob pattern.

@!visibility private

# File lib/mustermann/file_utils.rb, line 173
def pattern_with_glob_pattern(*pattern, **options)
  options[:uri_decode]    ||= false
  pattern                   = Mustermann.new(*pattern.flatten, **options)
  @glob_patterns          ||= {}
  @glob_patterns[pattern] ||= GlobPattern.generate(pattern)
  [pattern, @glob_patterns[pattern]]
end
split_options(*utils_option_names, **options) click to toggle source

Splits options into those meant for Mustermann and those meant for ::FileUtils.

@!visibility private

# File lib/mustermann/file_utils.rb, line 157
def split_options(*utils_option_names, **options)
  utils_options, pattern_options = {}, {}
  utils_option_names += %i[force noop verbose]

  options.each do |key, value|
    list = utils_option_names.include?(key) ? utils_options : pattern_options
    list[key] = value
  end

  [utils_options, pattern_options]
end
unescape(string) click to toggle source

Unescape an URI escaped string. @!visibility private

# File lib/mustermann/file_utils.rb, line 189
def unescape(string)
  @uri ||= URI::Parser.new
  @uri.unescape(string)
end