| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # perl::module Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Perl::Module; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Shell::Command); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable build test install cpan cpanm) ], | ||
| 16 | commands => { | ||
| 17 | build => [ qw(directory|OPTIONAL) ], | ||
| 18 | test => [ qw(directory|OPTIONAL) ], | ||
| 19 | install => [ qw(module|$module_list|directory|OPTIONAL) ], | ||
| 20 | dist => [ qw(directory|OPTIONAL) ], | ||
| 21 | clean => [ qw(directory|OPTIONAL) ], | ||
| 22 | }, | ||
| 23 | attributes => { | ||
| 24 | use_test => [ qw(0|1) ], | ||
| 25 | use_sudo => [ qw(0|1) ], | ||
| 26 | }, | ||
| 27 | attributes_default => { | ||
| 28 | use_test => 0, | ||
| 29 | use_sudo => 1, | ||
| 30 | }, | ||
| 31 | require_binaries => { | ||
| 32 | 'cpanm' => [ ], | ||
| 33 | }, | ||
| 34 | }; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub build { | ||
| 38 | my $self = shift; | ||
| 39 | my ($directory) = @_; | ||
| 40 | |||
| 41 | my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp'; | ||
| 42 | |||
| 43 | $directory ||= ''; | ||
| 44 | if (length($directory)) { | ||
| 45 | $self->brik_help_run_directory_not_found('build', $directory) or return; | ||
| 46 | if (defined($self->shell)) { | ||
| 47 | $self->shell->run_cd($directory) or return; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | chdir($directory) or return $self->log->error("build: chdir: $!"); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | my @cmd = (); | ||
| 55 | if (-f 'Build.PL') { | ||
| 56 | @cmd = ( 'perl Build.PL', 'perl Build' ); | ||
| 57 | } | ||
| 58 | elsif (-f 'Makefile.PL') { | ||
| 59 | @cmd = ( 'perl Makefile.PL', 'make' ); | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | if (length($directory)) { | ||
| 63 | if (defined($self->shell)) { | ||
| 64 | $self->shell->run_cd($cwd) or return; | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | chdir($cwd) or return $self->log->error("build: chdir: $!"); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | return $self->log->error("build: neither Build.PL nor Makefile.PL were found, abort"); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $r; | ||
| 74 | $self->use_sudo(0); | ||
| 75 | for (@cmd) { | ||
| 76 | $r = $self->execute($_) or last; # Abord if one cmd failed | ||
| 77 | } | ||
| 78 | $self->use_sudo(1); | ||
| 79 | |||
| 80 | if (length($directory)) { | ||
| 81 | if (defined($self->shell)) { | ||
| 82 | $self->shell->run_cd($cwd) or return; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | chdir($cwd) or return $self->log->error("build: chdir: $!"); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | return $r; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub test { | ||
| 93 | my $self = shift; | ||
| 94 | my ($directory) = @_; | ||
| 95 | |||
| 96 | my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp'; | ||
| 97 | |||
| 98 | $directory ||= ''; | ||
| 99 | if (length($directory)) { | ||
| 100 | $self->brik_help_run_directory_not_found('test', $directory) or return; | ||
| 101 | if (defined($self->shell)) { | ||
| 102 | $self->shell->run_cd($directory) or return; | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | chdir($directory) or return $self->log->error("test: chdir: $!"); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | my $cmd; | ||
| 110 | if (-f 'Build') { | ||
| 111 | $cmd = 'perl Build test'; | ||
| 112 | } | ||
| 113 | elsif (-f 'Makefile') { | ||
| 114 | $cmd = 'make test'; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | if (length($directory)) { | ||
| 118 | if (defined($self->shell)) { | ||
| 119 | $self->shell->run_cd($cwd) or return; | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | chdir($cwd) or return $self->log->error("test: chdir: $!"); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | return $self->log->error("test: neither Build nor Makefile were found, abort"); | ||
| 126 | } | ||
| 127 | |||
| 128 | $self->use_sudo(0); | ||
| 129 | my $r = $self->execute($cmd); | ||
| 130 | $self->use_sudo(1); | ||
| 131 | |||
| 132 | if (length($directory)) { | ||
| 133 | if (defined($self->shell)) { | ||
| 134 | $self->shell->run_cd($cwd) or return; | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | chdir($cwd) or return $self->log->error("test: chdir: $!"); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | return $r; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub install { | ||
| 145 | my $self = shift; | ||
| 146 | my ($module) = @_; | ||
| 147 | |||
| 148 | my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp'; | ||
| 149 | |||
| 150 | my $cmd; | ||
| 151 | if ((defined($module) && -d $module) || (! defined($module))) { | ||
| 152 | my $directory = $module || ''; # We consider there is only one arg: the directory where | ||
| 153 | # to find the module to install | ||
| 154 | if (length($directory)) { | ||
| 155 | $self->brik_help_run_directory_not_found('install', $directory) or return; | ||
| 156 | if (defined($self->shell)) { | ||
| 157 | $self->shell->run_cd($directory) or return; | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | chdir($directory) or return $self->log->error("install: chdir: $!"); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | if (-f 'Build') { | ||
| 165 | $cmd = 'perl Build install'; | ||
| 166 | } | ||
| 167 | elsif (-f 'Makefile') { | ||
| 168 | $cmd = 'make install'; | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | if (length($directory)) { | ||
| 172 | if (defined($self->shell)) { | ||
| 173 | $self->shell->run_cd($cwd) or return; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | chdir($cwd) or return $self->log->error("install: chdir: $!"); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | return $self->log->error("install: neither Build nor Makefile were found, abort"); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | my $ref = $self->brik_help_run_invalid_arg('install', $module, 'ARRAY', 'SCALAR') | ||
| 184 | or return; | ||
| 185 | |||
| 186 | $cmd = $self->use_test ? 'cpanm' : 'cpanm -n'; | ||
| 187 | if ($ref eq 'ARRAY') { | ||
| 188 | $cmd = join(' ', $cmd, @$module); | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | $cmd = join(' ', $cmd, $module); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | my $r = $self->execute($cmd); | ||
| 196 | |||
| 197 | if (defined($self->shell)) { | ||
| 198 | $self->shell->run_cd($cwd) or return; | ||
| 199 | } | ||
| 200 | else { | ||
| 201 | chdir($cwd) or return $self->log->error("install: chdir: $!"); | ||
| 202 | } | ||
| 203 | |||
| 204 | return $r; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub dist { | ||
| 208 | my $self = shift; | ||
| 209 | my ($directory) = @_; | ||
| 210 | |||
| 211 | my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp'; | ||
| 212 | |||
| 213 | $directory ||= ''; | ||
| 214 | if (length($directory)) { | ||
| 215 | $self->brik_help_run_directory_not_found('dist', $directory) or return; | ||
| 216 | if (defined($self->shell)) { | ||
| 217 | $self->shell->run_cd($directory) or return; | ||
| 218 | } | ||
| 219 | else { | ||
| 220 | chdir($directory) or return $self->log->error("dist: chdir: $!"); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | my $cmd; | ||
| 225 | if (-f 'Build') { | ||
| 226 | $cmd = 'perl Build dist'; | ||
| 227 | } | ||
| 228 | elsif (-f 'Makefile') { | ||
| 229 | $cmd = 'make dist'; | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | if (length($directory)) { | ||
| 233 | if (defined($self->shell)) { | ||
| 234 | $self->shell->run_cd($cwd) or return; | ||
| 235 | } | ||
| 236 | else { | ||
| 237 | chdir($cwd) or return $self->log->error("dist: chdir: $!"); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | return $self->log->error("dist: neither Build nor Makefile were found, abort"); | ||
| 241 | } | ||
| 242 | |||
| 243 | $self->use_sudo(0); | ||
| 244 | my $r = $self->execute($cmd); | ||
| 245 | $self->use_sudo(1); | ||
| 246 | |||
| 247 | if (length($directory)) { | ||
| 248 | if (defined($self->shell)) { | ||
| 249 | $self->shell->run_cd($cwd) or return; | ||
| 250 | } | ||
| 251 | else { | ||
| 252 | chdir($cwd) or return $self->log->error("dist: chdir: $!"); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | return $r; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub clean { | ||
| 260 | my $self = shift; | ||
| 261 | my ($directory) = @_; | ||
| 262 | |||
| 263 | my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp'; | ||
| 264 | |||
| 265 | $directory ||= ''; | ||
| 266 | if (length($directory)) { | ||
| 267 | $self->brik_help_run_directory_not_found('clean', $directory) or return; | ||
| 268 | if (defined($self->shell)) { | ||
| 269 | $self->shell->run_cd($directory) or return; | ||
| 270 | } | ||
| 271 | else { | ||
| 272 | chdir($directory) or return $self->log->error("clean: chdir: $!"); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | my $cmd; | ||
| 277 | if (-f 'Build') { | ||
| 278 | $cmd = 'perl Build clean'; | ||
| 279 | } | ||
| 280 | elsif (-f 'Makefile') { | ||
| 281 | $cmd = 'make clean'; | ||
| 282 | } | ||
| 283 | else { | ||
| 284 | if (length($directory)) { | ||
| 285 | if (defined($self->shell)) { | ||
| 286 | $self->shell->run_cd($cwd) or return; | ||
| 287 | } | ||
| 288 | else { | ||
| 289 | chdir($cwd) or return $self->log->error("clean: chdir: $!"); | ||
| 290 | } | ||
| 291 | } | ||
| 292 | return $self->log->error("clean: neither Build nor Makefile were found, abort"); | ||
| 293 | } | ||
| 294 | |||
| 295 | $self->use_sudo(0); | ||
| 296 | my $r = $self->execute($cmd); | ||
| 297 | $self->use_sudo(1); | ||
| 298 | |||
| 299 | if (length($directory)) { | ||
| 300 | if (defined($self->shell)) { | ||
| 301 | $self->shell->run_cd($cwd) or return; | ||
| 302 | } | ||
| 303 | else { | ||
| 304 | chdir($cwd) or return $self->log->error("clean: chdir: $!"); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | return $r; | ||
| 309 | } | ||
| 310 | |||
| 311 | 1; | ||
| 312 | |||
| 313 | __END__ | ||