Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional prefixing. #53

Merged
merged 2 commits into from
Aug 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/r10k/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def environments
private

def load_sources
@_sources = @config.setting(:sources).map do |(name, hash)|
R10K::Deployment::Source.vivify(name, hash)
sources = @config.setting(:sources)
prefix = sources.length > 1
@_sources = sources.map do |(name, hash)|
R10K::Deployment::Source.vivify(name, hash, prefix)
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/r10k/deployment/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ class Environment
# @param [String] remote
# @param [String] basedir
# @param [String] dirname The directory to clone the root into, defaults to ref
def initialize(ref, remote, basedir, dirname = nil)
# @param [String] source_name An additional string which may be used with ref to build dirname
def initialize(ref, remote, basedir, dirname = nil, source_name = "")
@ref = ref
@remote = remote
@basedir = basedir
@dirname = sanitize_dirname(dirname || ref)
alternate_name = source_name.empty? ? ref : source_name + "_" + ref
@dirname = sanitize_dirname(dirname || alternate_name)

@working_dir = R10K::Git::WorkingDir.new(@ref, @remote, @basedir, @dirname)

Expand Down
25 changes: 21 additions & 4 deletions lib/r10k/deployment/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ class Source
# @return [Array<R10K::Deployment::Environment>] All environments for this source
attr_reader :environments

def self.vivify(name, attrs)
def self.vivify(name, attrs, prefix = false)
remote = (attrs.delete(:remote) || attrs.delete('remote'))
basedir = (attrs.delete(:basedir) || attrs.delete('basedir'))
prefix_config = (attrs.delete(:prefix) || attrs.delete('prefix'))
prefix_outcome = prefix_config.nil? ? prefix : prefix_config

raise ArgumentError, "Unrecognized attributes for #{self.name}: #{attrs.inspect}" unless attrs.empty?
new(name, remote, basedir)
new(name, remote, basedir, prefix_outcome)
end

def initialize(name, remote, basedir)
def initialize(name, remote, basedir, prefix = nil)
@name = name
@remote = remote
@basedir = basedir
@prefix = prefix.nil? ? false : prefix

@cache = R10K::Git::Cache.new(@remote)

Expand All @@ -56,6 +59,16 @@ def managed_directory
@basedir
end

def current_contents
dir = self.managed_directory
glob_part = @prefix ? @name.to_s() + '_*' : '*'
glob_exp = File.join(dir, glob_part)

Dir.glob(glob_exp).map do |fname|
File.basename fname
end
end

# List all environments that should exist in the basedir for this source
# @note This implements a required method for the Purgeable mixin
# @return [Array<String>]
Expand All @@ -68,7 +81,11 @@ def desired_contents
def load_environments
if @cache.cached?
@environments = @cache.branches.map do |branch|
R10K::Deployment::Environment.new(branch, @remote, @basedir)
if @prefix
R10K::Deployment::Environment.new(branch, @remote, @basedir, nil, @name.to_s())
else
R10K::Deployment::Environment.new(branch, @remote, @basedir)
end
end
else
@environments = []
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/deployment/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
subject.dirname.should == 'master'
end

it 'uses the ref and a provided source name in the default dirname' do
subject = described_class.new(ref, remote, '/tmp', nil, "the")
subject.dirname.should == 'the_master'
end

it 'allows a specific dirname to be set' do
subject = described_class.new(ref, remote, '/tmp', 'sourcename_master')
subject.dirname.should == 'sourcename_master'
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/deployment/source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'
require 'r10k/deployment/source'

describe R10K::Deployment::Source do
let(:name) { 'do_not_name_a_branch_this' }
let(:remote) { 'git://github.com/adrienthebo/r10k-fixture-repo' }
let(:basedir) { '/tmp' }

describe 'environments' do
it 'uses the name as a prefix when told' do
subject = described_class.new(name, remote, basedir, true)
subject.fetch_remote()
subject.environments.length.should > 0
subject.environments.first.dirname.should start_with name
end

it 'avoids using the name as a prefix when told' do
subject = described_class.new(name, remote, basedir, false)
subject.fetch_remote()
subject.environments.length.should > 0
subject.environments.first.dirname.should_not start_with name
end
end
end