| 1 | package Makerelease; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '0.1'; | ||
| 6 | |||
| 7 | # conditionally use Text::Wrap | ||
| 8 | my $havewrap = eval { require Text::Wrap; }; | ||
| 9 | if ($havewrap) { | ||
| 10 | import Text::Wrap qw(wrap); | ||
| 11 | } | ||
| 12 | |||
| 13 | |||
| 14 | # note: this new clause is used by most sub-modules too, altering it | ||
| 15 | # will alter them. | ||
| 16 | sub new { | ||
| 17 | my $type = shift; | ||
| 18 | my ($class) = ref($type) || $type; | ||
| 19 | my $self = {}; | ||
| 20 | %$self = @_; | ||
| 21 | bless($self, $class); | ||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | # this loads the needed perl module (dynamically) for a given step | ||
| 26 | sub load_step { | ||
| 27 | my ($self, $step) = @_; | ||
| 28 | # make sure we can load it before bailing with -n | ||
| 29 | my $steptype = $step->{'type'}; | ||
| 30 | my $steptypecap = $steptype; | ||
| 31 | $steptypecap =~ s/^(.)/uc($1)/e; | ||
| 32 | my $module = "Makerelease::Step::$steptypecap"; | ||
| 33 | my $haveit = eval "require $module"; | ||
| 34 | if (!$haveit) { | ||
| 35 | print STDERR | ||
| 36 | "Could not load a module for release step type \"$steptype\";\n"; | ||
| 37 | print STDERR | ||
| 38 | " Tried: $module\n"; | ||
| 39 | print STDERR | ||
| 40 | " Error: $@\n"; | ||
| 41 | die; | ||
| 42 | } | ||
| 43 | |||
| 44 | # create a module instance | ||
| 45 | my $stepmodule = eval "new $module"; | ||
| 46 | if (!$stepmodule) { | ||
| 47 | print STDERR | ||
| 48 | "Can't create an instance of the \"step\" module\n"; | ||
| 49 | print STDERR | ||
| 50 | " Tried: $module\n"; | ||
| 51 | print STDERR | ||
| 52 | " Error: $@\n"; | ||
| 53 | die; | ||
| 54 | } | ||
| 55 | |||
| 56 | # auto-inherit some parameters | ||
| 57 | $stepmodule->{'opts'} = $self->{'opts'}; | ||
| 58 | $stepmodule->{'parameters'} = $self->{'parameters'}; | ||
| 59 | $stepmodule->{'master'} = $self; | ||
| 60 | |||
| 61 | return $stepmodule; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub start_step { | ||
| 65 | my ($self, $vernum, $vername) = @_; | ||
| 66 | $self->output_raw("STEP: $vernum: $vername\n\n"); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub test_steps { | ||
| 70 | my ($self, $relinfo, $parentstep) = @_; | ||
| 71 | my $counter; | ||
| 72 | my $result = 0; | ||
| 73 | foreach my $step (@{$relinfo->{'steps'}[0]{'step'}}) { | ||
| 74 | $counter++; | ||
| 75 | my $stepmodule = $self->load_step($step); | ||
| 76 | $result = 1 if ($stepmodule->test($step, "$parentstep$counter")); | ||
| 77 | } | ||
| 78 | return $result; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub process_steps { | ||
| 82 | my ($self, $relinfo, $parentstep) = @_; | ||
| 83 | my $counter; | ||
| 84 | foreach my $step (@{$relinfo->{'steps'}[0]{'step'}}) { | ||
| 85 | $counter++; | ||
| 86 | |||
| 87 | # print the step header | ||
| 88 | $self->start_step("$parentstep$counter", $step->{'title'}); | ||
| 89 | |||
| 90 | my $stepmodule = $self->load_step($step); | ||
| 91 | |||
| 92 | # print description of the step if it exists | ||
| 93 | $stepmodule->print_description($step); | ||
| 94 | |||
| 95 | next if ($stepmodule->possibly_skip($step, $parentstep, $counter)); | ||
| 96 | |||
| 97 | # XXX: skip up to step based on number here | ||
| 98 | |||
| 99 | # XXX: skip up to step based on nmae here | ||
| 100 | |||
| 101 | $self->DEBUG("processing step: $parentstep.$counter: type=$step->{'type'}\n"); | ||
| 102 | |||
| 103 | $stepmodule->step($step, $parentstep, $counter); | ||
| 104 | $stepmodule->finish_step($step, $parentstep, $counter); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub print_toc { | ||
| 109 | my ($self, $relinfo, $parentstep) = @_; | ||
| 110 | my $counter; | ||
| 111 | foreach my $step (@{$relinfo->{'steps'}[0]{'step'}}) { | ||
| 112 | $counter++; | ||
| 113 | |||
| 114 | my $stepmodule = $self->load_step($step); | ||
| 115 | |||
| 116 | # print the step header | ||
| 117 | $stepmodule->print_toc_header($step, $parentstep, $counter); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | sub print_toc_header { | ||
| 122 | my ($self, $step, $parentstep, $counter) = @_; | ||
| 123 | $self->output_raw(sprintf("%-15.15s %s\n", | ||
| 124 | "$parentstep$counter", $step->{title})); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub getinput { | ||
| 128 | my ($self, $prompt) = @_; | ||
| 129 | $self->output_raw("$prompt ") if ($prompt); | ||
| 130 | my $bogus = <STDIN>; | ||
| 131 | chomp($bogus); | ||
| 132 | return $bogus; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub DEBUG { | ||
| 136 | my ($self, @args) = @_; | ||
| 137 | if ($self->{'opts'}{'v'}) { | ||
| 138 | $self->output(@args); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | sub output { | ||
| 143 | my $self = shift; | ||
| 144 | my @text = @_; | ||
| 145 | my @output; | ||
| 146 | |||
| 147 | if ($havewrap) { | ||
| 148 | foreach my $text (@text) { | ||
| 149 | foreach my $section (split(/\n\n/,$text)) { | ||
| 150 | push @output, wrap(" ", " ", $section); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | print STDERR join("\n\n",@output),"\n\n"; | ||
| 154 | } else { | ||
| 155 | print STDERR " ",@_; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub output_raw { | ||
| 160 | my $self = shift; | ||
| 161 | print STDERR @_; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub ensure_array { | ||
| 165 | my ($self, $something) = @_; | ||
| 166 | return $something if (ref($something) eq 'ARRAY'); | ||
| 167 | return [$something]; | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; | ||
| 171 | |||
| 172 | =head1 NAME | ||
| 173 | |||
| 174 | Makerelease - A base perl module for Makerelease Plugins | ||
| 175 | |||
| 176 | =head1 SYNOPSIS | ||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | =head1 DESCRIPTION | ||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | |||
| 187 |