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

Instance Method Details

#cp(map = {}, recursive: false, **options) ⇒ Object Also known as: copy

Copies files based on a pattern mapping.

Examples:

require 'mustermann/file_utils'

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

See Also:



75
76
77
78
79
# 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

#cp_r(map = {}, **options) ⇒ Object

Copies files based on a pattern mapping, recursively.

Examples:

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 Also:



91
92
93
# File 'lib/mustermann/file_utils.rb', line 91

def cp_r(map = {}, **options)
  cp(map, recursive: true, **options)
end

#glob(*pattern, **options, &block) ⇒ Object Also known as: []

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

Examples:

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

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
# 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

#glob_map(map = {}, **options, &block) ⇒ Object

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

Examples:

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 Also:

  • Mapper


60
61
62
63
64
# 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) ⇒ String

Turn a Mustermann pattern into glob pattern.

Examples:

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/*}"

Parameters:

  • pattern (Object)

    the object to turn into a glob pattern.

Returns:

  • (String)

    the glob pattern



26
27
28
# 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) ⇒ Object Also known as: link

Creates links based on a pattern mapping.

Examples:

require 'mustermann/file_utils'

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

See Also:



119
120
121
122
123
# 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

#ln_s(map = {}, **options) ⇒ Object Also known as: symlink

Creates symbolic links based on a pattern mapping.

Examples:

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 Also:



134
135
136
# File 'lib/mustermann/file_utils.rb', line 134

def ln_s(map = {}, **options)
  ln(map, symbolic: true, **options)
end

#ln_sf(map = {}, **options) ⇒ Object

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

Examples:

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 Also:



148
149
150
# File 'lib/mustermann/file_utils.rb', line 148

def ln_sf(map = {}, **options)
  ln(map, symbolic: true, force: true, **options)
end

#mv(map = {}, **options) ⇒ Object Also known as: move

Moves files based on a pattern mapping.

Examples:

require 'mustermann/file_utils'

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

See Also:



104
105
106
107
# 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