Sha256: d3add8b4034c7824e6d0a78ad28a0ae72e1a7060661b0844aa84c13150879158

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'fileutils'
require 'logger'

module Ryonan
  class Filer
    attr_accessor :root

    def initialize(root)
      self.root = root
    end

    def files
      Dir.glob("#{root}/**/*")
    end

    def dirs
      files.select { |file_path| FileTest.directory?(file_path) }
    end

    def templatable_dirs
      dirs.select { |dir| Dir::entries(dir).any? { |file| file == '.template' } }
    end

    def erbs
      files.select { |file_path| file_path.match(/\.erb$/) }
    end

    def recursive_rename_files(variable_hash)
      variable_hash.each do |key, value|
        from = "__#{key}__"
        while files.any? { |dir| dir.match(/#{from}/) }
          src  = files.select { |dir| dir.match(/#{from}/) }.first
          dest = src.gsub(/#{from}/, value)
          Filer.mv(src, dest)
        end
      end
    end

    def render_erbs(variable_hash)
      erbs.each do |erb|
        Filer.render_erb(erb, variable_hash)
      end
    end

    class << self
      attr_writer :logger

      def logger
        @logger ||= Logger.new(STDOUT)
      end

      def cp_r(src, dest)
        logger.info("cp -r #{src} #{dest}")
        FileUtils.cp_r(src, dest)
      end

      def mv(src, dest)
        logger.info("mv #{src} #{dest}")
        FileUtils.mv(src, dest)
      end

      def rm(file)
        logger.info("rm #{file}")
        FileUtils.rm(file)
      end
      
      def render_erb(erb, hash)
        result = Renderer.new(hash).render(open(erb).read)
        Filer.logger.info("render_erb #{erb} to #{erb.gsub(/\.erb$/, '')}")
        open(erb.gsub(/\.erb$/, ''), 'w') { |file| file.puts(result) }
        Filer.rm(erb)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ryonan-0.0.1 lib/ryonan/filer.rb