Skip to content

Commit

Permalink
Move script-specific references from init command code.
Browse files Browse the repository at this point in the history
Encapsulate that functionality as methods on ScriptConfigCommand, instead.
  • Loading branch information
theory committed Aug 4, 2015
1 parent bb193ef commit 8984cbd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
18 changes: 4 additions & 14 deletions lib/App/Sqitch/Command/init.pm
Expand Up @@ -63,9 +63,7 @@ sub configure {

sub make_directories {
my $self = shift;
my $target = $self->default_target;
for my $attr (map { ("$_\_dir", "reworked_$_\_dir") } qw(deploy revert verify)) {
my $dir = $self->$attr || $target->$attr;
for my $dir ($self->directories_for( $self->default_target )) {
$self->_mkdir($dir) unless -e $dir;
}
return $self;
Expand Down Expand Up @@ -194,17 +192,9 @@ sub write_config {
}

# Add in options passed to the init command.
for my $attr (
'reworked_dir',
(map { ("$_\_dir", "reworked_$_\_dir") } qw(deploy revert verify)),
'extension'
) {
if (my $val = $self->$attr) {
push @vars => {
key => "core.$attr",
value => $val
};
}
my $script_config = $self->script_config;
while (my ($attr, $val) = each %{ $script_config }) {
push @vars => { key => "core.$attr", value => $val };
}

# Emit them.
Expand Down
45 changes: 45 additions & 0 deletions lib/App/Sqitch/Role/ScriptConfigCommand.pm
Expand Up @@ -67,6 +67,33 @@ around configure => sub {
return $class->$orig($config, $opt);
};

sub script_config {
my $self = shift;
my $config = {};
for my $attr (
'reworked_dir',
(map { ("$_\_dir", "reworked_$_\_dir") } qw(deploy revert verify)),
'extension'
) {
if (my $val = $self->$attr) {
$config->{$attr} = $val;
}
}
return $config;
}

sub directories_for {
my ($self, $target) = @_;
return (
$self->deploy_dir || $target->deploy_dir,
$self->revert_dir || $target->revert_dir,
$self->verify_dir || $target->verify_dir,
$self->reworked_deploy_dir || $target->reworked_deploy_dir,
$self->reworked_revert_dir || $target->reworked_revert_dir,
$self->reworked_verify_dir || $target->reworked_verify_dir,
);
}

1;

__END__
Expand Down Expand Up @@ -135,6 +162,24 @@ Path to the directory containing reworked verify scripts.
The file extension to use for change script files.
=head2 Instance Methods
=head3 C<script_config>
my $config = $cmd->script_config;
Returns a hash reference of script configuration values. They keys are
suitable for use in L<App::Sqitch::Config> sections, while the keys are the
values. All but the C<extension> key are L<Path::Class::Dir> objects.
=head3 C<directories_for>
my @dirs = $cmd->directories_for($target);
Returns a list of script directories for the target. Options passed to the
command are preferred. Paths are pulled from the command only when they have
not been passed as options.
=head1 See Also
=over
Expand Down
14 changes: 6 additions & 8 deletions t/init.t
Expand Up @@ -118,14 +118,12 @@ for my $attr (map { "$_\_dir"} qw(top deploy revert verify)) {
}
my $sep = dir('')->stringify;
is_deeply +MockOutput->get_info, [
map {
my $attr = "$_\_dir";
my $rwrk = "reworked_$_\_dir";
(
[__x "Created {file}", file => $target->$attr . $sep],
[__x "Created {file}", file => $init->$rwrk . $sep]
)
} qw(deploy revert verify)
[__x "Created {file}", file => $target->deploy_dir . $sep],
[__x "Created {file}", file => $target->revert_dir . $sep],
[__x "Created {file}", file => $target->verify_dir . $sep],
[__x "Created {file}", file => $init->reworked_deploy_dir . $sep],
[__x "Created {file}", file => $init->reworked_revert_dir . $sep],
[__x "Created {file}", file => $init->reworked_verify_dir . $sep],
], 'Each should have been sent to info';

# Do it again.
Expand Down

0 comments on commit 8984cbd

Please sign in to comment.