From d37373845178a3a27e032aa8cf753e979dce62a6 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 5 Jan 2011 17:44:45 -0500 Subject: [PATCH] a bunch of updates, especially to the rake apache:create task --- bin/apache-configurator | 16 ++++++++++++-- lib/apache/config.rb | 10 +++++++++ lib/apache/hash.rb | 8 +++++++ lib/apache/rake/apache/create.rb | 25 +++++++++++++++++++--- lib/apache/rake/support.rb | 3 ++- skel/Gemfile | 5 +++++ skel/Rakefile | 5 +---- spec/apache/config_spec.rb | 6 ++++++ spec/apache/hash_spec.rb | 36 ++++++++++++++++++++++++++++++++ 9 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 lib/apache/hash.rb create mode 100644 skel/Gemfile create mode 100644 spec/apache/hash_spec.rb diff --git a/bin/apache-configurator b/bin/apache-configurator index 22e8def..aef55ae 100755 --- a/bin/apache-configurator +++ b/bin/apache-configurator @@ -1,17 +1,29 @@ #!/usr/bin/env ruby require 'fileutils' +require 'rubygems' +require 'rainbow' if !ARGV[0] - puts "Directory name required." + puts "Directory name required.".foreground(:red) exit 1 end if File.directory?(ARGV[0]) - puts "You can't overwrite an existing directory." + puts "You can't overwrite an existing directory.".foreground(:red) exit 1 end +puts "Copying skel files...".foreground(:green) FileUtils.cp_r File.expand_path(File.join(File.dirname(__FILE__), '..', 'skel')), ARGV[0] + +puts "Making config directories...".foreground(:green) FileUtils.mkdir_p File.join(ARGV[0], 'source') +FileUtils.mkdir_p File.join(ARGV[0], 'conf.d-original') FileUtils.mkdir_p File.join(ARGV[0], 'conf.d') + +print "Complete! ".bright.foreground(:green) +print "cd #{ARGV[0]}".bright.foreground(:yellow) +print " and run ".foreground(:green) +print "bundle exec rake -T ".bright.foreground(:yellow) +puts "to get started.".foreground(:green) diff --git a/lib/apache/config.rb b/lib/apache/config.rb index 5307921..dec0f7d 100644 --- a/lib/apache/config.rb +++ b/lib/apache/config.rb @@ -94,6 +94,7 @@ module Apache FileUtils.mkdir_p File.split(target).first File.open(target, 'w') { |file| file.puts generate_config_file(config) * "\n" } + @was_written = true config end @@ -103,6 +104,14 @@ module Apache @is_disabled = true end + def disabled? + @is_disabled + end + + def written? + @was_written + end + def generate_config_file(config) output = [ "# Generated by apache-config-generator #{Time.now.to_s}", config ] output.unshift('# disabled') if @is_disabled @@ -114,6 +123,7 @@ module Apache @config = [] @line_indent = 0 @is_disabled = false + @was_written = false end # Indent the string by the current @line_indent level diff --git a/lib/apache/hash.rb b/lib/apache/hash.rb new file mode 100644 index 0000000..94013ff --- /dev/null +++ b/lib/apache/hash.rb @@ -0,0 +1,8 @@ +class Hash + def to_sym_keys + Hash[self.collect { |key, value| + value = value.to_sym_keys if value.kind_of?(Hash) + [ key.to_sym, value ] + }] + end +end \ No newline at end of file diff --git a/lib/apache/rake/apache/create.rb b/lib/apache/rake/apache/create.rb index 1ef604f..0d5c7c7 100644 --- a/lib/apache/rake/apache/create.rb +++ b/lib/apache/rake/apache/create.rb @@ -5,6 +5,15 @@ include Apache::Rake::Support task :default => 'apache:create' +def capture_stdout + buffer = StringIO.new + $stdout = buffer + yield + $stdout = STDOUT + buffer.rewind + buffer.read +end + namespace :apache do desc "Create all defined configs for the specified environment" task :create, :environment do |t, args| @@ -21,12 +30,22 @@ namespace :apache do FileUtils.mkdir_p config[:destination_path] Dir.chdir config[:destination_path] - # using CONFIG is deprecated CONFIG = config + ENVIRONMENT_CONFIG = (config[:environments][APACHE_ENV] rescue nil) + Dir[File.join(config[:source_path], '**', '*.rb')].each do |file| - puts file.foreground(:green) - require file + Apache::Config.reset! + output = capture_stdout { require file } + + if Apache::Config.written? + if Apache::Config.disabled? + puts file.foreground(:blue) + else + puts file.foreground(:green) + end + puts output + end end symlink_configs! diff --git a/lib/apache/rake/support.rb b/lib/apache/rake/support.rb index 100bc0e..de5d12e 100644 --- a/lib/apache/rake/support.rb +++ b/lib/apache/rake/support.rb @@ -1,12 +1,13 @@ require 'yaml' require 'fileutils' +require 'apache/hash' module Apache module Rake module Support def config if !@config - @config = Hash[YAML.load_file('config.yml').collect { |k,v| [ k.to_sym, v ] }] + @config = YAML.load_file('config.yml').to_sym_keys config_paths! class << @config diff --git a/skel/Gemfile b/skel/Gemfile new file mode 100644 index 0000000..e53448b --- /dev/null +++ b/skel/Gemfile @@ -0,0 +1,5 @@ +source :rubygems + +gem 'apache-config-generator', '>= 0.2.7' +gem 'rake' + diff --git a/skel/Rakefile b/skel/Rakefile index ce47e50..3021788 100644 --- a/skel/Rakefile +++ b/skel/Rakefile @@ -1,4 +1 @@ -require 'rubygems' - -require 'apache-config-generator' -require 'apache/rake/create' +require 'apache/rake/apache/create' diff --git a/spec/apache/config_spec.rb b/spec/apache/config_spec.rb index ac1964b..f070ae8 100644 --- a/spec/apache/config_spec.rb +++ b/spec/apache/config_spec.rb @@ -36,12 +36,14 @@ describe Apache::Config, "builds configurations" do describe '.disable_symlink!' do context 'is enabled by default' do it { apache.instance_variable_get(:@is_disabled).should be_false } + it { apache.disabled?.should be_false } end context 'disable' do before { apache.disable_symlink! } it { apache.instance_variable_get(:@is_disabled).should be_true } + it { apache.disabled?.should be_true } end end @@ -110,9 +112,13 @@ describe Apache::Config, "builds configurations" do end it "should handle a build" do + apache.written?.should be_false + FileUtils.mkdir_p 'test' apache.build('test/fake.conf') { my_test "this" }.should == [ 'MyTest "this"' ] FileUtils.rm 'test/fake.conf' + + apache.written?.should be_true end it "should handle building if the environment is correct" do diff --git a/spec/apache/hash_spec.rb b/spec/apache/hash_spec.rb new file mode 100644 index 0000000..7eea9d3 --- /dev/null +++ b/spec/apache/hash_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' +require 'lib/apache/hash' + +describe Hash do + describe '#to_sym_keys' do + subject { test_hash.to_sym_keys } + + context 'no nested hashes' do + let(:test_hash) { { + 'hello' => 'goodbye', + :other => 'this' + } } + + it { should == { + :hello => 'goodbye', + :other => 'this' + } } + end + + context 'nested hash' do + let(:test_hash) { { + 'hello' => 'goodbye', + :other => { + 'this' => 'that' + } + } } + + it { should == { + :hello => 'goodbye', + :other => { + :this => 'that' + } + } } + end + end +end