diff --git a/lib/App/Sqitch/Command/init.pm b/lib/App/Sqitch/Command/init.pm index 2b7ac54e8..f2d9f2c8d 100644 --- a/lib/App/Sqitch/Command/init.pm +++ b/lib/App/Sqitch/Command/init.pm @@ -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; @@ -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. diff --git a/lib/App/Sqitch/Role/ScriptConfigCommand.pm b/lib/App/Sqitch/Role/ScriptConfigCommand.pm index 6e13a279e..24028fb9c 100644 --- a/lib/App/Sqitch/Role/ScriptConfigCommand.pm +++ b/lib/App/Sqitch/Role/ScriptConfigCommand.pm @@ -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__ @@ -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 + + my $config = $cmd->script_config; + +Returns a hash reference of script configuration values. They keys are +suitable for use in L sections, while the keys are the +values. All but the C key are L objects. + +=head3 C + + 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 diff --git a/t/init.t b/t/init.t index b3486681d..1b0701661 100644 --- a/t/init.t +++ b/t/init.t @@ -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.