summaryrefslogtreecommitdiff
path: root/lib/App/Sqitch/Role/ContextCommand.pm
blob: 4795f83fbce30a442f390048f52d99e3fea1e1c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package App::Sqitch::Role::ContextCommand;

use 5.010;
use strict;
use warnings;
use utf8;
use Moo::Role;
use Path::Class;
use App::Sqitch::Types qw(ArrayRef);
use Locale::TextDomain qw(App-Sqitch); # XXX Until deprecation removed below.

our $VERSION = 'v1.4.0'; # VERSION

requires 'options';
requires 'configure';
requires 'target_params';
requires 'command'; # XXX Until deprecation removed below.

has _cx => (
    is  => 'ro',
    isa => ArrayRef,
    default => sub { [] },
);

around options => sub {
    my $orig = shift;
    return $orig->(@_), qw(
        plan-file|f=s
        top-dir=s
    );
};

around configure => sub {
    my ( $orig, $class, $config, $opt ) = @_;

    # DEPRECATTION: --top-dir deprecated in v0.9999. Remove at some point.
    App::Sqitch->warn(__x(
        "  Option --top-dir is deprecated for {command} and other non-configuration commands.\n  Use --chdir instead.",
        command => $class->command,
    )) if $opt->{top_dir};

    # Grab the target params.
    my @cx = (
        do { my $f = delete $opt->{top_dir};   $f ? ( top_dir   => dir($f))  : () },
        do { my $f = delete $opt->{plan_file}; $f ? ( plan_file => file($f)) : () },
    );

    # Let the command take care of its options.
    my $params = $class->$orig($config, $opt);

    # Hang on to the target parameters.
    $params->{_cx} = \@cx;
    return $params;
};

around target_params => sub {
    my ($orig, $self) = (shift, shift);
    return $self->$orig(@_), @{ $self->_cx };
};

1;

__END__

=head1 Name

App::Sqitch::Role::ContextCommand - A command that needs to know where things are

=head1 Synopsis

  package App::Sqitch::Command::add;
  extends 'App::Sqitch::Command';
  with 'App::Sqitch::Role::ContextCommand';

=head1 Description

This role encapsulates the options and target parameters required by commands
that need to know where to find project files.

=head1 Interface

=head2 Class Methods

=head3 C<options>

  my @opts = App::Sqitch::Command::add->options;

Adds contextual options C<--plan-file> and C<--top-dir>.

=head3 C<configure>

Configures the options used for target parameters.

=head2 Instance Methods

=head3 C<target_params>

Returns a list of parameters to be passed to App::Sqitch::Target's C<new>
and C<all_targets> methods.

=head1 See Also

=over

=item L<App::Sqitch::Command::add>

The C<add> command adds changes to the the plan and change scripts to the project.

=item L<App::Sqitch::Command::deploy>

The C<deploy> command deploys changes to a database.

=item L<App::Sqitch::Command::bundle>

The C<bundle> command bundles Sqitch changes for distribution.

=back

=head1 Author

David E. Wheeler <david@justatheory.com>

=head1 License

Copyright (c) 2012-2023 iovation Inc., David E. Wheeler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=cut