File Coverage

blib/lib/App/Sqitch/Role/ContextCommand.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 31 31 100.0


line stmt bran cond sub pod time code
1             package App::Sqitch::Role::ContextCommand;
2              
3 15     15   18755 use 5.010;
  15         80  
4 15     15   103 use strict;
  15         75  
  15         320  
5 15     15   124 use warnings;
  15         39  
  15         407  
6 15     15   94 use utf8;
  15         46  
  15         166  
7 15     15   391 use Moo::Role;
  15         66  
  15         140  
8 15     15   6413 use Path::Class;
  15         55  
  15         1023  
9 15     15   113 use App::Sqitch::Types qw(ArrayRef);
  15         41  
  15         213  
10 15     15   11331 use Locale::TextDomain qw(App-Sqitch); # XXX Until deprecation removed below.
  15         49  
  15         104  
11              
12             our $VERSION = 'v1.4.0'; # VERSION
13              
14             requires 'options';
15             requires 'configure';
16             requires 'target_params';
17             requires 'command'; # XXX Until deprecation removed below.
18              
19             has _cx => (
20             is => 'ro',
21             isa => ArrayRef,
22             default => sub { [] },
23             );
24              
25             around options => sub {
26             my $orig = shift;
27             return $orig->(@_), qw(
28             plan-file|f=s
29             top-dir=s
30             );
31             };
32              
33             around configure => sub {
34             my ( $orig, $class, $config, $opt ) = @_;
35              
36             # DEPRECATTION: --top-dir deprecated in v0.9999. Remove at some point.
37             App::Sqitch->warn(__x(
38             " Option --top-dir is deprecated for {command} and other non-configuration commands.\n Use --chdir instead.",
39             command => $class->command,
40             )) if $opt->{top_dir};
41              
42             # Grab the target params.
43             my @cx = (
44             do { my $f = delete $opt->{top_dir}; $f ? ( top_dir => dir($f)) : () },
45             do { my $f = delete $opt->{plan_file}; $f ? ( plan_file => file($f)) : () },
46             );
47              
48             # Let the command take care of its options.
49             my $params = $class->$orig($config, $opt);
50              
51             # Hang on to the target parameters.
52             $params->{_cx} = \@cx;
53             return $params;
54             };
55              
56             around target_params => sub {
57             my ($orig, $self) = (shift, shift);
58             return $self->$orig(@_), @{ $self->_cx };
59             };
60              
61             1;
62              
63             __END__
64              
65             =head1 Name
66              
67             App::Sqitch::Role::ContextCommand - A command that needs to know where things are
68              
69             =head1 Synopsis
70              
71             package App::Sqitch::Command::add;
72             extends 'App::Sqitch::Command';
73             with 'App::Sqitch::Role::ContextCommand';
74              
75             =head1 Description
76              
77             This role encapsulates the options and target parameters required by commands
78             that need to know where to find project files.
79              
80             =head1 Interface
81              
82             =head2 Class Methods
83              
84             =head3 C<options>
85              
86             my @opts = App::Sqitch::Command::add->options;
87              
88             Adds contextual options C<--plan-file> and C<--top-dir>.
89              
90             =head3 C<configure>
91              
92             Configures the options used for target parameters.
93              
94             =head2 Instance Methods
95              
96             =head3 C<target_params>
97              
98             Returns a list of parameters to be passed to App::Sqitch::Target's C<new>
99             and C<all_targets> methods.
100              
101             =head1 See Also
102              
103             =over
104              
105             =item L<App::Sqitch::Command::add>
106              
107             The C<add> command adds changes to the the plan and change scripts to the project.
108              
109             =item L<App::Sqitch::Command::deploy>
110              
111             The C<deploy> command deploys changes to a database.
112              
113             =item L<App::Sqitch::Command::bundle>
114              
115             The C<bundle> command bundles Sqitch changes for distribution.
116              
117             =back
118              
119             =head1 Author
120              
121             David E. Wheeler <david@justatheory.com>
122              
123             =head1 License
124              
125             Copyright (c) 2012-2023 iovation Inc., David E. Wheeler
126              
127             Permission is hereby granted, free of charge, to any person obtaining a copy
128             of this software and associated documentation files (the "Software"), to deal
129             in the Software without restriction, including without limitation the rights
130             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
131             copies of the Software, and to permit persons to whom the Software is
132             furnished to do so, subject to the following conditions:
133              
134             The above copyright notice and this permission notice shall be included in all
135             copies or substantial portions of the Software.
136              
137             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
138             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
139             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
140             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
141             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
142             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
143             SOFTWARE.
144              
145             =cut