Sha256: f364e55785c5afabab5139bc0a9b6ac831f3aa778d37d56e25d105d6e70092bc

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 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)
      rescue Exception => e
        puts "Chlorine was unable to read its configuration: #{e}"
      end
      
      return unless @config['envs'] and @config['envs'].include? Rails.env
      
      @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: #{e}"
        end
      end
    end
    
    private
    
    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.1 lib/chlorine/clean.rb