Sha256: 6bb16c35f739f2fb5ef3a50bcaee7e311ac397666b6a34fd4679c60c0d348db5

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

module Hem
  module Lib
    module Seed
      class Replacer
        # Matching files/directories to be excluded from replacements
        EXCLUDES = ["\\.git/", "^./bin", "^./lib", "^./spec"]

        def replace(path, tokens)
          if tokens.instance_of? Hash
            tokens = flat_hash(tokens)
          elsif !tokens.instance_of? Array
            raise "Invalid token list (expected Array or Hash)"
          end

          return search_replace(path, tokens)
        end

        private

        def search_replace(path, tokens, &block)
          require 'find'
          files = []
          excludes = Regexp.new(EXCLUDES.join("|"))
          Find.find(path) do |candidate|
            Find.prune if candidate =~ excludes  # Skip excluded
            next unless FileTest.file? candidate # Skip unless file

            content = File.read(candidate)
            next unless content.force_encoding("UTF-8").valid_encoding? # Skip unless file can be valid UTF-8

            match = false
            tokens.each do |token, replacement|
              token = "{{#{token.join('.')}}}"
              match = content.match(token)
              if match
                content.gsub!(token, replacement)
                files.push(candidate)
              end
            end

            File.write(candidate, content) if files.include? candidate
          end
          return files.uniq
        end

        # http://stackoverflow.com/questions/9647997/converting-a-nested-hash-into-a-flat-hash
        def flat_hash(hash, k = [])
          return {k => hash} unless hash.is_a?(Hash)
          hash.inject({}) do |h, v|
            h.merge! flat_hash(v[-1], k + [v[0]])
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hem-1.0.1.beta6 lib/hem/lib/seed/replacer.rb
hem-1.0.1.beta5 lib/hem/lib/seed/replacer.rb
hem-1.0.1.beta4 lib/hem/lib/seed/replacer.rb
hem-1.0.1.beta3 lib/hem/lib/seed/replacer.rb
hem-1.0.1.beta2 lib/hem/lib/seed/replacer.rb
hem-1.0.1.beta1 lib/hem/lib/seed/replacer.rb