diff --git a/Gemfile.lock b/Gemfile.lock index 5a1e21e..369c5b9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,7 +9,7 @@ GEM specs: autotest (4.4.2) diff-lcs (1.1.2) - mocha (0.9.9) + mocha (0.9.10) rake nokogiri (1.4.3.1) rainbow (1.1) diff --git a/lib/apache/config.rb b/lib/apache/config.rb index dec0f7d..9b08e4b 100644 --- a/lib/apache/config.rb +++ b/lib/apache/config.rb @@ -92,15 +92,17 @@ module Apache def build(target, &block) config = build_and_return(&block) - FileUtils.mkdir_p File.split(target).first - File.open(target, 'w') { |file| file.puts generate_config_file(config) * "\n" } - @was_written = true + if !disabled? + FileUtils.mkdir_p File.split(target).first + File.open(target, 'w') { |file| file.puts generate_config_file(config) * "\n" } + @was_written = true + end config end - # If included in a configuration, will not generate the symlink in the Rake task - def disable_symlink! + # If included in a configuration, will not generate the config file in the Rake task + def disable! @is_disabled = true end @@ -113,9 +115,7 @@ module Apache 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 + [ "# Generated by apache-config-generator #{Time.now.to_s}", config ].flatten end # Reset the current settings diff --git a/lib/apache/rake/apache/create.rb b/lib/apache/rake/apache/create.rb index 0d5c7c7..9b8f915 100644 --- a/lib/apache/rake/apache/create.rb +++ b/lib/apache/rake/apache/create.rb @@ -27,6 +27,7 @@ namespace :apache do Apache::Config.rotate_logs_path = config[:rotate_logs_path] + FileUtils.rm_rf config[:destination_path] FileUtils.mkdir_p config[:destination_path] Dir.chdir config[:destination_path] @@ -47,8 +48,6 @@ namespace :apache do puts output end end - - symlink_configs! end desc "List all possible environments" diff --git a/lib/apache/rake/support.rb b/lib/apache/rake/support.rb index de5d12e..49c6f6f 100644 --- a/lib/apache/rake/support.rb +++ b/lib/apache/rake/support.rb @@ -28,7 +28,7 @@ module Apache end def config_paths! - [ :source, :destination, :symlink ].each do |which| + [ :source, :destination ].each do |which| begin @config[:"#{which}_path"] = File.expand_path(@config[which]) rescue StandardError @@ -39,8 +39,6 @@ module Apache end def get_environments - config[:source_path] = File.expand_path(config[:source]) - Dir[File.join(config[:source_path], '**', '*.rb')].collect { |file| File.readlines(file).find_all { |line| line[%r{(if_environment|build_if)}] }.collect { |line| line.scan(%r{:[a-z_]+}) } }.flatten.uniq.sort.collect { |name| name[1..-1] } @@ -60,23 +58,6 @@ module Apache puts "rake apache:default[#{get_environments.first}]" exit 1 end - - def symlink_configs! - raise Errno::ENOENT if !File.directory?(config[:destination_path]) - - FileUtils.rm_rf(config[:symlink_path]) - FileUtils.mkdir_p(config[:symlink_path]) - - 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]) - FileUtils.mkdir_p(File.split(target).first) - FileUtils.ln_sf(file, target) - end - end - end - end end end end diff --git a/skel/config.yml b/skel/config.yml index e5b1d0d..725c336 100644 --- a/skel/config.yml +++ b/skel/config.yml @@ -1,4 +1,3 @@ source: "source" -destination: "conf.d-original" -symlink: "conf.d" +destination: "conf.d" rotate_logs_path: '/path/to/apache/bin/rotatelogs' diff --git a/spec/apache/config_spec.rb b/spec/apache/config_spec.rb index f070ae8..6a06202 100644 --- a/spec/apache/config_spec.rb +++ b/spec/apache/config_spec.rb @@ -33,31 +33,37 @@ describe Apache::Config, "builds configurations" do end end - describe '.disable_symlink!' do + describe '.disable!' do context 'is enabled by default' do it { apache.instance_variable_get(:@is_disabled).should be_false } it { apache.disabled?.should be_false } + + context 'writes config' do + before { + FileUtils.expects(:mkdir_p).once + File.expects(:open).once + apache.build("here") { cats } + } + + it { apache.written?.should == true } + end end context 'disable' do - before { apache.disable_symlink! } + before { apache.disable! } it { apache.instance_variable_get(:@is_disabled).should be_true } it { apache.disabled?.should be_true } - end - end - describe '.generate_config_file' do - subject { apache.generate_config_file(%w{config}) } + context 'does not write config' do + before { + FileUtils.expects(:mkdir_p).never + File.expects(:open).never + apache.build("here") { disable!; cats } + } - context 'with symlink' do - its(:first) { should_not == '# disabled' } - end - - context 'without symlink' do - before { apache.disable_symlink! } - - its(:first) { should == '# disabled' } + it { apache.written?.should == false } + end end end diff --git a/spec/apache/rake/support_spec.rb b/spec/apache/rake/support_spec.rb index 12904cc..b0c463f 100644 --- a/spec/apache/rake/support_spec.rb +++ b/spec/apache/rake/support_spec.rb @@ -6,14 +6,12 @@ describe Apache::Rake::Support do let(:source) { '/source' } let(:destination) { '/destination/available' } - let(:symlink) { '/destination/enabled' } describe 'config_paths!' do before { @config = { :source => 'cats', - :destination => 'dogs', - :symlink => 'cows' + :destination => 'dogs' } } @@ -21,79 +19,5 @@ describe Apache::Rake::Support do 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 = { - :source_path => source, - :destination_path => destination, - :symlink_path => symlink - } - } - - subject { symlink_configs! } - - context 'source does not exist' do - before { File.expects(:directory?).with(destination).returns(false) } - - it { expect { subject }.to raise_error(Errno::ENOENT) } - end - - context 'source does exist' do - before { - File.expects(:directory?).with(destination).returns(true) - FileUtils.expects(:rm_rf).with(symlink) - FileUtils.expects(:mkdir_p).with(symlink) - Dir.expects(:[]).with(File.join(destination, '**/*')).returns(dir_return) - } - - context 'with no configs' do - let(:dir_return) { [] } - - before { FileUtils.expects(:ln_sf).never } - - it { subject } - end - - context 'with one config' do - let(:filename) { File.join(destination, 'dogs/cats') } - let(:dir_return) { [ filename ] } - - before { File.expects(:file?).with(filename).returns(is_file_result) } - - context 'is a directory' do - let(:is_file_result) { false } - - it { subject } - end - - context 'is a file' do - let(:is_file_result) { true } - - before { File.expects(:read).with(filename).returns(read_result) } - - 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 end end \ No newline at end of file