Sha256: 85a55664e46f9691f261660bc722a66b3b15fce1a8bcd5b7b3831be2ea24d4e3

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'fileutils'
require 'rails'
require 'yaml'

module Chlorine
  class Railtie < Rails::Railtie
    
    initializer "chlorine.configure_rails_initialization" do
      begin
        @config = YAML::load_file File.expand_path('config/chlorine.yml', Rails.root)
        return unless @config && @config['envs'] && @config['dirs']
        require_clean = (@config['envs'].collect {|env| env.downcase}).include? Rails.env
      rescue Exception => e
        puts "Chlorine was unable to read its configuration: #{e}"
      end
      
      do_clean if require_clean
    end
    
    private
    
    def do_clean
      @config['dirs'].each do |dir_name, dir_config|
        begin
          dir_config['excludes'] = dir_config['excludes'].collect {|x| x[-1] == '/' ? x[0...-1] : x}
          next unless valid_dir_config? dir_config
          next unless Dir.exists? File.expand_path(dir_name, Rails.root)
          dir = Dir.new File.expand_path(dir_name, Rails.root)
          dir.each { |filename|
            unless (['.', '..'] + dir_config['excludes']).include?(filename)
              filepath = File.expand_path(dir_name + '/' + filename, Rails.root)
              if File.file?(filepath)
                File.delete filepath
              elsif File.directory?(filepath)
                FileUtils.rm_rf filepath
              end
            end
          }
        rescue Exception => e
          puts "Chlorine hit an exception while cleaning: #{e}"
        end
      end
    end
    
    def valid_dir_config?(dir_config)
      dir_config['excludes'].each do |exclude|
        next if exclude == ''
        if exclude[0...-1].match '/'
          puts "Invalid exclude '#{exclude}' in chlorine.yml: excludes must be root-level"
          return false
        end
      end
      return true
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chlorine-0.1.7 lib/chlorine/clean.rb