From b4b287f17f97b771fe75c3325e666237e22f44d7 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 4 Jan 2011 16:58:16 -0500 Subject: [PATCH] now generates symlinked dirs in rake task, with option to turn off for specific configs --- autotest/discover.rb | 5 +++ lib/apache/config.rb | 14 ++++++++- lib/apache/rake/apache/create.rb | 9 +++--- lib/apache/rake/apache/symlink.rb | 0 lib/apache/rake/support.rb | 33 ++++++++++++++++++-- spec/apache/config_spec.rb | 26 ++++++++++++++++ spec/apache/rake/support_spec.rb | 52 ++++++++++++++++++++++++------- 7 files changed, 119 insertions(+), 20 deletions(-) delete mode 100644 lib/apache/rake/apache/symlink.rb diff --git a/autotest/discover.rb b/autotest/discover.rb index cd6892c..3a7ecb8 100644 --- a/autotest/discover.rb +++ b/autotest/discover.rb @@ -1 +1,6 @@ Autotest.add_discovery { "rspec2" } + +Autotest.add_hook(:initialize) do |at| + at.add_exception(%r{^./test/.*}) +end + diff --git a/lib/apache/config.rb b/lib/apache/config.rb index e8a4e43..5307921 100644 --- a/lib/apache/config.rb +++ b/lib/apache/config.rb @@ -93,15 +93,27 @@ module Apache config = build_and_return(&block) FileUtils.mkdir_p File.split(target).first - File.open(target, 'w') { |file| file.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", config ].flatten * "\n" } + File.open(target, 'w') { |file| file.puts generate_config_file(config) * "\n" } config end + # If included in a configuration, will not generate the symlink in the Rake task + def disable_symlink! + @is_disabled = true + end + + def generate_config_file(config) + output = [ "# Generated by apache-config-generator #{Time.now.to_s}", config ] + output.unshift('# disabled') if @is_disabled + output.flatten + end + # Reset the current settings def reset! @config = [] @line_indent = 0 + @is_disabled = false end # Indent the string by the current @line_indent level diff --git a/lib/apache/rake/apache/create.rb b/lib/apache/rake/apache/create.rb index c5c911e..1ef604f 100644 --- a/lib/apache/rake/apache/create.rb +++ b/lib/apache/rake/apache/create.rb @@ -16,13 +16,10 @@ namespace :apache do APACHE_ENV = (args[:environment] || get_default_environment).to_sym end - config[:source_path] = File.expand_path(config[:source]) - config[:dest_path] = File.expand_path(config[:destination]) - Apache::Config.rotate_logs_path = config[:rotate_logs_path] - FileUtils.mkdir_p config[:dest_path] - Dir.chdir config[:dest_path] + FileUtils.mkdir_p config[:destination_path] + Dir.chdir config[:destination_path] # using CONFIG is deprecated CONFIG = config @@ -31,6 +28,8 @@ namespace :apache do puts file.foreground(:green) require file end + + symlink_configs! end desc "List all possible environments" diff --git a/lib/apache/rake/apache/symlink.rb b/lib/apache/rake/apache/symlink.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/apache/rake/support.rb b/lib/apache/rake/support.rb index b846221..100bc0e 100644 --- a/lib/apache/rake/support.rb +++ b/lib/apache/rake/support.rb @@ -5,7 +5,36 @@ module Apache module Rake module Support def config - @config ||= Hash[YAML.load_file('config.yml').collect { |k,v| [ k.to_sym, v ] }] + if !@config + @config = Hash[YAML.load_file('config.yml').collect { |k,v| [ k.to_sym, v ] }] + config_paths! + + class << @config + def [](which) + if which == :dest_path + print "config[:dest_path] is deprecated.".foreground(:red).bright + puts " Use config[:destination_path] instead.".foreground(:red) + + self[:destination_path] + else + super + end + end + end + end + + @config + end + + def config_paths! + [ :source, :destination, :symlink ].each do |which| + begin + @config[:"#{which}_path"] = File.expand_path(@config[which]) + rescue StandardError + puts "#{which.to_s.bright} is not defined in the configuration file.".foreground(:red) + exit 1 + end + end end def get_environments @@ -37,7 +66,7 @@ module Apache FileUtils.rm_rf(config[:symlink_path]) FileUtils.mkdir_p(config[:symlink_path]) - Dir[File.join(config[:destination_path], '**/*')].each do |file| + Dir[File.join(config[:destination_path], '**/*')].find_all { |file| File.file?(file) }.each do |file| if line = File.read(file).first if !line['# disabled'] target = file.gsub(config[:destination_path], config[:symlink_path]) diff --git a/spec/apache/config_spec.rb b/spec/apache/config_spec.rb index 48c93ff..ac1964b 100644 --- a/spec/apache/config_spec.rb +++ b/spec/apache/config_spec.rb @@ -33,6 +33,32 @@ describe Apache::Config, "builds configurations" do end end + describe '.disable_symlink!' do + context 'is enabled by default' do + it { apache.instance_variable_get(:@is_disabled).should be_false } + end + + context 'disable' do + before { apache.disable_symlink! } + + it { apache.instance_variable_get(:@is_disabled).should be_true } + end + end + + describe '.generate_config_file' do + subject { apache.generate_config_file(%w{config}) } + + context 'with symlink' do + its(:first) { should_not == '# disabled' } + end + + context 'without symlink' do + before { apache.disable_symlink! } + + its(:first) { should == '# disabled' } + end + end + it "should handle indent" do apache.line_indent = 1 diff --git a/spec/apache/rake/support_spec.rb b/spec/apache/rake/support_spec.rb index d80f9a7..12904cc 100644 --- a/spec/apache/rake/support_spec.rb +++ b/spec/apache/rake/support_spec.rb @@ -8,6 +8,22 @@ describe Apache::Rake::Support do let(:destination) { '/destination/available' } let(:symlink) { '/destination/enabled' } + describe 'config_paths!' do + before { + @config = { + :source => 'cats', + :destination => 'dogs', + :symlink => 'cows' + } + } + + subject { config_paths!; @config } + + its([:source_path]) { should == File.expand_path('cats') } + its([:destination_path]) { should == File.expand_path('dogs') } + its([:symlink_path]) { should == File.expand_path('cows') } + end + describe 'symlink_configs!' do before { @config = { @@ -45,25 +61,37 @@ describe Apache::Rake::Support do let(:filename) { File.join(destination, 'dogs/cats') } let(:dir_return) { [ filename ] } - before { File.expects(:read).with(filename).returns(read_result) } + before { File.expects(:file?).with(filename).returns(is_file_result) } - context 'config should not be symlinked' do - let(:read_result) { ['# disabled'] } - - before { FileUtils.expects(:ln_sf).never } + context 'is a directory' do + let(:is_file_result) { false } it { subject } end - context 'config should be symlinked' do - let(:read_result) { ['# whatever'] } + context 'is a file' do + let(:is_file_result) { true } - before { - FileUtils.expects(:mkdir_p).with(File.join(symlink, 'dogs')) - FileUtils.expects(:ln_sf).with(filename, filename.gsub(destination, symlink)) - } + before { File.expects(:read).with(filename).returns(read_result) } - it { subject } + context 'config should not be symlinked' do + let(:read_result) { ['# disabled'] } + + before { FileUtils.expects(:ln_sf).never } + + it { subject } + end + + context 'config should be symlinked' do + let(:read_result) { ['# whatever'] } + + before { + FileUtils.expects(:mkdir_p).with(File.join(symlink, 'dogs')) + FileUtils.expects(:ln_sf).with(filename, filename.gsub(destination, symlink)) + } + + it { subject } + end end end end