| 1 | package Makerelease::Step::Prompt; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Makerelease::Step; | ||
| 5 | use IO::File; | ||
| 6 | |||
| 7 | our $VERSION = '0.1'; | ||
| 8 | |||
| 9 | our @ISA=qw(Makerelease::Step); | ||
| 10 | |||
| 11 | # the only time we allow things to be skipped are with -n | ||
| 12 | sub possibly_skip { | ||
| 13 | |||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | my ($step, $parentstep, $counter) = @_; | ||
| 17 | |||
| 18 | # handle -n | ||
| 19 | return 1 if ($self->possibly_skip_dryrun(@_)); | ||
| 20 | |||
| 21 | return 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub test { | ||
| 25 | my ($self, $step, $parentstep, $counter) = @_; | ||
| 26 | my $ret = 0; | ||
| 27 | $ret = 1 if ($self->require_attribute($step, $parentstep, $counter, | ||
| 28 | 'parameter')); | ||
| 29 | return $ret; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub step { | ||
| 33 | my ($self, $step, $parentstep, $counter) = @_; | ||
| 34 | |||
| 35 | my $done = 0; | ||
| 36 | my $answer; | ||
| 37 | |||
| 38 | # default to anything specified previously (or via command line) | ||
| 39 | my $default; | ||
| 40 | |||
| 41 | $default = $self->{'parameters'}{$step->{'parameter'}} | ||
| 42 | if (exists($self->{'parameters'}{$step->{'parameter'}})); | ||
| 43 | |||
| 44 | # allow a default on the param token | ||
| 45 | $default = $step->{'default'} | ||
| 46 | if (exists($step->{'default'}) && !defined($default)); | ||
| 47 | |||
| 48 | |||
| 49 | # set up the prompt | ||
| 50 | my $prompt = $step->{'prompt'}; | ||
| 51 | |||
| 52 | # modify the prompt if there was a default | ||
| 53 | $prompt = "$prompt [$default]" if ($default); | ||
| 54 | |||
| 55 | |||
| 56 | # ask the question till the answer is valid | ||
| 57 | while (!$done) { | ||
| 58 | $done = 1; | ||
| 59 | |||
| 60 | $answer = $self->getinput($prompt); | ||
| 61 | |||
| 62 | $answer = $default if ($answer eq ''); | ||
| 63 | |||
| 64 | if ($step->{'values'} && $answer !~ $step->{'values'}) { | ||
| 65 | $self->output("Illegal value; must match: $step->{'values'}"); | ||
| 66 | $done = 0; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | $self->{'parameters'}{$step->{'parameter'}} = $answer; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub document_step { | ||
| 73 | my ($self, $step, $parentstep, $counter) = @_; | ||
| 74 | |||
| 75 | $self->output("Decide on a value for parameter '$step->{parameter}'"); | ||
| 76 | $self->output(" parameter: $step->{parameter}"); | ||
| 77 | $self->output(" prompt: $step->{prompt}"); | ||
| 78 | $self->output(" legal: $step->{values}") if ($self->{'values'}); | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 |