Build

filter: Statementstmt 100% 1 / 1 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 1 / 1 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/DoitX/Example.pm ↑ Summary
1 #! /usr/local/bin/perl
2 # MD5: 78764185c2434b4cdc13b7ad35206b8e
3  
4 1 $Build_PL::ARGV = []
5 ;
6  
7 # line 1 "Build.PL"
8 #!/usr/bin/perl -w
9 # -*- perl -*-
10  
11 #
12 # Author: Slaven Rezic
13 #
14 # Copyright (C) 2017,2018,2019,2020,2021,2022 Slaven Rezic. All rights reserved.
15 # This program is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 # Mail: slaven@rezic.de
19 # WWW: http://www.rezic.de/eserte/
20 #
21  
22 use strict;
23 use FindBin;
24 use lib "$FindBin::RealBin/lib";
25  
26 use Config;
27 use Cwd qw(realpath getcwd);
28 use Digest::MD5 qw(md5_hex);
29 use File::Basename;
30 use Doit;
31 use Doit::Log;
32 use Doit::Util qw(in_directory);
33  
34 my $doit = Doit->init;
35  
36 my $Build_PL_file_contents = do {
37 open my $fh, '<', 'Build.PL'
38 or error "Error opening Build.PL: $!";
39 local $/ = undef;
40 <$fh>;
41 };
42 my $Build_PL_md5hex = md5_hex $Build_PL_file_contents;
43  
44 for (@ARGV) {
45 if (/^[^-].*=.*/) { # looks like oldfashioned option without leading dash (e.g. "installdirs=vendor")
46 s/^/--/;
47 }
48 }
49  
50 if (basename($0) eq 'Build.PL') {
51 _Build_PL_mode();
52 }
53  
54 # Check if Build is up-to-date (md5 check, no timestamp check)
55 {
56 open my $fh, '<', $0
57 or error "Can't open $0: $!";
58 my $shebang = <$fh>;
59 my $md5_line = <$fh>;
60 if (my($old_md5hex) = $md5_line =~ m{^# MD5: (\S+)}) {
61 if ($old_md5hex ne $Build_PL_md5hex) {
62 my $perl;
63 if (($perl) = $shebang =~ m{^#!\s*(.*)}) {
64 # parsed it
65 } else {
66 warning "Cannot parse perl interpreter path out of '$shebang', fallback to 'perl'";
67 $perl = "perl";
68 }
69 error "Build.PL changed, please run '$perl Build.PL' again";
70 }
71 } else {
72 error "Unexpected: no MD5 found in '$md5_line'";
73 }
74 }
75  
76 require Getopt::Long;
77 my %opt = (verbose => 0, uninst => 0, destdir => '', create_packlist => 1);
78 $Build_PL::ARGV=$Build_PL::ARGV if 0; # cease -w
79 @ARGV = (@$Build_PL::ARGV, @ARGV);
80 if ($ENV{PERL_MB_OPT}) {
81 require Text::ParseWords;
82 push @ARGV, Text::ParseWords::shellwords($ENV{PERL_MB_OPT});
83 }
84 Getopt::Long::GetOptions(
85 \%opt,
86 'allow_mb_mismatch=i',
87 'config=s%',
88 'create_packlist=i',
89 'destdir=s',
90 'installdirs=s',
91 'install_base=s',
92 'install_path=s%',
93 'prefix=s',
94 'uninst:1',
95 'verbose:1',
96 'versionlib=s',
97 'version=s',
98 # The following are just ignored, but currently set by cover
99 'extra_compiler_flags=s',
100 'extra_linker_flags=s',
101 )
102 or error "usage: $0 [options]";
103  
104 my $action = shift || 'build';
105 $action =~ s/-/_/g;
106 if (@ARGV) {
107 error "No arguments allowed";
108 }
109 {
110 no strict 'refs';
111 &$action;
112 }
113  
114 sub build {
115 build_libs();
116 manifypods();
117 }
118  
119 sub build_libs {
120 $doit->make_path('blib/lib');
121 $doit->make_path('blib/arch'); # not used, but keep ExtUtils::Install quiet
122 require File::Find;
123 my @pm_files;
124 File::Find::find(sub { no warnings 'once'; push @pm_files, $File::Find::name if /\.(pm|pod)$/ && -f $_ }, "lib");
125 for my $file (@pm_files) {
126 my $dest = 'blib/'.$file;
127 if (!-e $dest || -M $dest > -M $file) {
128 $doit->make_path(dirname($dest));
129 $doit->copy($file, $dest);
130 }
131 }
132 }
133  
134 sub manifypods {
135 # Handles only Pods in .pod files (which is the case in the Doit distribution)
136 require File::Find;
137 require Pod::Man;
138 my $mansep = $^O =~ m{^(MSWin32|cygwin)$} ? '.' : '::';
139 File::Find::find
140 ({
141 wanted => sub {
142 if (-f $_ && /\.pod$/) {
143 no warnings 'once';
144 my $pod = $File::Find::name;
145 my $section = 3; # no scripts yet, so we can hardcode here
146 my %options = (section => $section);
147 (my $man = $pod) =~ s{^lib.}{};
148 $man =~ s{/+}{$mansep}g;
149 $man =~ s{\.pod$}{.$Config{"man${section}ext"}};
150 $man = "blib/man$section/$man";
151 if (!-e $man || -M $man > -M $pod || -M $man > -M "Build") {
152 $doit->make_path(dirname($man));
153 my $parser = Pod::Man->new(%options);
154 if ($doit->is_dry_run) {
155 info "$pod -> $man (dry-run)";
156 } else {
157 info "$pod -> $man";
158 $parser->parse_from_file($pod, $man)
159 or error "Could not install $man";
160 }
161 $doit->chmod(0644, $man); # XXX should this be changeable? like $PERM_RW in Makefile.PL?
162 }
163 }
164 },
165 no_chdir => 1,
166 },
167 "lib");
168 }
169  
170 sub clean {
171 $doit->remove_tree('blib');
172 }
173  
174 sub realclean { &clean }
175  
176 sub test {
177 local $ENV{PERL_DL_NONLAZY} = 1;
178 require File::Glob;
179 require File::Spec;
180 $doit->system($^X, "-MExtUtils::Command::MM", "-MTest::Harness", "-e", 'undef *Test::Harness::Switches; test_harness(0, "blib/lib", "blib/arch")', File::Glob::bsd_glob(File::Spec->catfile("t", "*.t")));
181 # $doit->system(_prove_path(), '-b', 't'); # use right perl?
182 }
183  
184 sub test_xt {
185 $doit->system(_prove_path(), '-b', 'xt'); # use right perl?
186 }
187  
188 sub test_installed {
189 my $t_dir = "$FindBin::RealBin/t";
190 chdir "/"
191 or error "Cannot chdir to /: $!";
192 $doit->system(_prove_path(), $t_dir);
193 }
194  
195 sub test_in_docker {
196 my $distro_spec = $ENV{DISTRO_SPEC}; # XXX this should be a proper option!
197 if (!$distro_spec || $distro_spec !~ m{^.*:.*$}) {
198 error "Please set DISTRO_SPEC environment variable to something like 'debian:jessie'";
199 }
200 my $more_testing = $ENV{XXX_MORE_TESTING}; # XXX this will be a proper option in future!
201 Doit::Log::set_label("[$distro_spec" . ($more_testing ? " (more)" : "") . "]");
202 for my $tool (qw(docker)) {
203 $doit->qx('which', $tool); # existence check
204 }
205 require File::Temp;
206 my $dir = File::Temp::tempdir("Doit_test_in_docker_XXXXXXXX", TMPDIR => 1, CLEANUP => 1);
207 my $dockerfile = <<"EOF";
208 FROM $distro_spec
209  
210 EOF
211 if ($ENV{XXX_INVALIDATE_CACHE}) { # XXX this will be a proper option in future!
212 _add_Dockerfile_invalidate_cmd(\$dockerfile);
213 }
214 my $add_opts = {};
215 $dockerfile .= _fix_Dockerfile_for_dist($distro_spec, $add_opts);
216 if ($distro_spec =~ m{^centos:}) {
217 ######################################################################
218 # CentOS
219 # From https://fedoraproject.org/wiki/EPEL/FAQ#How_can_I_install_the_packages_from_the_EPEL_software_repository.3F
220 if ($distro_spec =~ m{:6$}) {
221 $dockerfile .= <<"EOF";
222 RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
223 EOF
224 } elsif ($distro_spec =~ m{:7$}) {
225 $dockerfile .= <<"EOF";
226 RUN yum -y install epel-release
227 EOF
228 } elsif ($distro_spec =~ m{:8$}) {
229 $dockerfile .= <<"EOF";
230 RUN yum -y install epel-release
231 EOF
232 } else {
233 info "No EPEL support for $distro_spec";
234 }
235 $dockerfile .= <<"EOF";
236 RUN yum -y install \\
237 sudo \\
238 rsync \\
239 "perl(ExtUtils::MakeMaker)" \\
240 "perl(Digest::MD5)" \\
241 "perl(Test::More)" \\
242 make \\
243 git
244  
245 EOF
246 if ($distro_spec =~ m{:8$}) {
247 # Requires also PowerTools repo additionally to EPEL
248 $dockerfile .= <<"EOF";
249 RUN dnf -y install 'dnf-command(config-manager)'
250 RUN yum config-manager --set-enabled PowerTools
251 EOF
252 }
253 $dockerfile .= <<"EOF";
254 RUN yum -y install \\
255 "perl(IPC::Run)"
256 EOF
257 if ($distro_spec !~ m{:8$}) {
258 # Currently not available in CentOS8
259 $dockerfile .= <<"EOF";
260 RUN yum -y install \\
261 "perl(Net::OpenSSH)"
262 EOF
263 }
264 if ($distro_spec =~ m{:6$}) {
265 $dockerfile .= <<"EOF";
266 RUN curl -L https://cpanmin.us | perl - App::cpanminus
267  
268 # Not available as an RPM
269 RUN cpanm --quiet --notest CPAN::Meta
270 EOF
271 } else {
272 $dockerfile .= <<"EOF";
273 RUN yum -y install \\
274 "perl(CPAN::Meta)"
275 EOF
276 }
277 if ($more_testing) {
278 $dockerfile .= <<"EOF";
279 RUN yum -y install \\
280 "perl(Capture::Tiny)" \\
281 "perl(Term::ANSIColor)" \\
282 "perl(BSD::Resource)" \\
283 "perl(CPAN::Meta::Validator)" \\
284 "perl(LWP::UserAgent)" \\
285 file
286  
287 ENV TRAVIS 1
288 ENV DOIT_TEST_WITH_SUDO 1
289  
290 EOF
291 if ($distro_spec =~ m{:6$}) {
292 $dockerfile .= <<"EOF";
293 # Term::ANSIColor exists, but is too old (no colorstrip function)
294 RUN cpanm --quiet --notest "Term::ANSIColor~>=2.01"
295  
296 RUN yum -y install python34-pip
297  
298 EOF
299 } else {
300 $dockerfile .= <<"EOF";
301 RUN yum -y install python3-pip
302 EOF
303 }
304 }
305 } elsif ($distro_spec =~ m{^alpine(:|$)}) {
306 $dockerfile .= <<"EOF";
307 RUN apk update
308 # perl-utils for prove
309 # shadow for useradd
310 RUN apk add sudo rsync git perl perl-utils make shadow
311 EOF
312 if ($more_testing) {
313 $dockerfile .= <<"EOF";
314 RUN apk add py3-pip
315  
316 #ENV TRAVIS 1
317 ENV DOIT_TEST_WITH_SUDO 1
318  
319 EOF
320 }
321  
322 } else {
323 ######################################################################
324 # Debian-like (Debian, Ubuntu, LinuxMint ...)
325 my @packages = (
326 'libipc-run-perl',
327 ($distro_spec eq 'ubuntu:precise' ? () : 'libnet-openssh-perl'),
328 'sudo',
329 'rsync',
330 'git',
331 );
332 my $packages_spec = join(" \\\n", map { " $_" } @packages);
333 if ($distro_spec =~ m{^(ubuntu:precise)$}) {
334 # See https://gist.github.com/dergachev/f5da514802fcbbb441a1
335 $dockerfile .= <<'EOF'
336 RUN sed -i.bak -r 's/(archive|security).ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
337 EOF
338 }
339 $dockerfile .= <<"EOF";
340 ENV DEBIAN_FRONTEND noninteractive
341  
342 RUN apt-get$add_opts->{apt_get_update_opts} update && apt-get install -y --no-install-recommends \\
343 $packages_spec
344  
345 EOF
346 if ($more_testing) {
347 my @more_packages = (
348 'libbsd-resource-perl',
349 'libcapture-tiny-perl',
350 'libcpan-meta-perl',
351 'libwww-perl',
352 'perl-modules',
353 'locales',
354 'file',
355 ($distro_spec eq 'ubuntu:precise' ? () : 'python3-pip'),
356 );
357 my $more_packages_spec = join(" \\\n", map { " $_" } @more_packages);
358 $dockerfile .= <<"EOF";
359 RUN apt-get install -y --no-install-recommends \\
360 $more_packages_spec
361  
362 ENV TRAVIS 1
363 ENV DOIT_TEST_WITH_SUDO 1
364  
365 EOF
366 }
367 }
368  
369 for my $env_key (qw(HARNESS_OPTIONS)) {
370 if (defined $ENV{$env_key}) {
371 $dockerfile .= <<"EOF";
372 ENV $env_key "$ENV{$env_key}"
373 EOF
374 }
375 }
376  
377 $doit->write_binary("$dir/Dockerfile", $dockerfile);
378 in_directory {
379 (my $tag = $distro_spec) =~ s{:}{-}g;
380 $tag = 'doit-test-' . $tag;
381 $doit->system(qw(docker build -t), $tag, qw(.));
382 $doit->system(
383 qw(docker run), '-v', "$FindBin::RealBin:/data:ro", $tag,
384 'sh', '-c', 'rsync -a --no-owner --no-group --exclude=blib "--exclude=Doit-*.tar.gz" /data/ /tmp/Doit/ && cd /tmp/Doit && perl Build.PL && ./Build && ./Build generate_META_json && ./Build test && ./Build test_xt && ./Build dist_install_and_test'
385 );
386 } $dir;
387  
388 Doit::Log::set_label(undef);
389 }
390  
391 # Run docker-based tests on a number of Linux distributions:
392 # the stable ones of Debian, Ubuntu and CentOS, and some
393 # older versions of these distributions.
394 sub test_standard {
395 Doit::Log::set_label("[local]");
396 $doit->system($^X, 'Build.PL');
397 $doit->system('./Build');
398 $doit->system('./Build', 'test');
399 Doit::Log::set_label(undef);
400 for my $distro_spec ('ubuntu:jammy', 'debian:bullseye', 'ubuntu:focal', 'debian:buster', 'ubuntu:bionic', 'centos:7', 'debian:stretch', 'debian:jessie', 'ubuntu:xenial', 'ubuntu:precise', 'centos:6', 'alpine:latest') {
401 local $ENV{DISTRO_SPEC} = $distro_spec;
402 for my $more_testing (0, 1) {
403 next if $more_testing && $distro_spec eq 'centos:6'; # centos:6 is unmaintained since Nov 2020, and some yum operations don't work anymore. See also https://support.plesk.com/hc/en-us/articles/360017922619-After-November-30th-2020-updating-system-packages-on-Plesk-server-with-CentOS-6-fails-Cannot-find-a-valid-baseurl-for-repo-base
404 local $ENV{XXX_MORE_TESTING} = $more_testing;
405 $doit->system('./Build', 'test_in_docker');
406 }
407 }
408 }
409  
410 sub test_kwalitee (;$) {
411 my($distdir) = @_;
412 # If cwd is used then more tests fail,
413 # because META files are not available here.
414 $distdir = $FindBin::RealBin if !defined $distdir;
415  
416 if (eval { require Test::Kwalitee; 1 }) {
417 in_directory {
418 local $ENV{RELEASE_TESTING} = 1;
419 eval { $doit->system($^X, '-MTest::More', '-MTest::Kwalitee=kwalitee_ok', '-e', 'kwalitee_ok(qw(-has_manifest -has_meta_yml)); done_testing') };
420 } $distdir;
421 } else {
422 warning "Test::Kwalitee is not installed";
423 }
424 }
425  
426 sub test_pod (;$) {
427 my($distdir) = @_;
428 $distdir = $FindBin::RealBin if !defined $distdir;
429  
430 if (eval { require Test::Pod; 1 }) {
431 in_directory {
432 eval { $doit->system($^X, '-MTest::Pod', '-e', 'all_pod_files_ok()') };
433 } $distdir;
434 } else {
435 warning "Test::Pod is not installed";
436 }
437 }
438  
439 sub test_cpan_versions {
440 my($distdir) = @_;
441  
442 if ($doit->which("cpan_check_versions")) {
443 in_directory {
444 $doit->system('cpan_check_versions', '-remote');
445 } $distdir;
446 } else {
447 warning <<EOF;
448 No cpan_check_versions found in PATH, cannot check for properly
449 incremented VERSIONs. Please check git://github.com/eserte/srezic-misc.git
450 EOF
451 }
452 }
453  
454 sub git_checks {
455 $doit->add_component('git');
456  
457 my $status = $doit->git_short_status;
458 if ($status eq '<<') {
459 error 'Working directory has uncomitted changes: ' . `git status`;
460 }
461 if ($status eq '*') {
462 error 'Working directory has files not under git control (and not in .gitignore or .git/info/exclude): ' . `git status`;
463 }
464  
465 my $Doit_VERSION = _get_Doit_VERSION();
466  
467 my $out = $doit->info_qx(qw(git tag -l), $Doit_VERSION);
468 if (defined $out && $out ne '') {
469 error "A git tag $Doit_VERSION already exists";
470 }
471 }
472  
473 sub distdir {
474 my(%options) = @_;
475 my $temporary = delete $options{temporary};
476 error "Unhandled options: " . join(" ", %options) if %options;
477  
478 require File::Temp;
479 my $tempdir = File::Temp::tempdir("Doit_distdir_XXXXXXXX", TMPDIR => 1, CLEANUP => $temporary);
480 my $Doit_VERSION = _get_Doit_VERSION();
481 my $distdir = "$tempdir/Doit-$Doit_VERSION";
482 $doit->mkdir($distdir);
483 for my $line (split /\0/, $doit->info_qx({quiet=>1}, 'git', 'ls-files', '-z')) {
484 next if ($line =~ m{^( \.travis\.yml
485 | \.?appveyor\.yml
486 | \.gitignore
487 )$}x);
488 # XXX maybe implement also MANIFEST.SKIP?
489 my $dirname = dirname $line;
490 if ($dirname ne '.') {
491 $doit->make_path("$distdir/$dirname");
492 }
493 $doit->copy($line, "$distdir/$dirname/");
494 }
495  
496 generate_META_json("$distdir/META.json");
497 generate_META_yml("$distdir/META.yml" );
498  
499 test_kwalitee($distdir);
500 test_pod($distdir);
501  
502 # If not temporary, then it is assumed that the
503 # interactive distdir action is called, and
504 # provide information to the user.
505 if (!$temporary) {
506 info "Distribution directory is $distdir";
507 }
508  
509 $distdir;
510 }
511  
512 sub dist {
513 if (!$ENV{DOIT_TEST_SKIP_SOME_CHECKS}) {
514 git_checks();
515 }
516 # else skip because Doit version is usually not incremented in
517 # this phase and the git tag already exists
518  
519  
520 my $tarfile = _get_tarfilename();
521 if (-e $tarfile) {
522 error "$tarfile already exists";
523 }
524 my $distdir = distdir(temporary => 1);
525  
526 if (!$ENV{DOIT_TEST_SKIP_SOME_CHECKS}) {
527 test_cpan_versions($distdir);
528 }
529 # else because versions of all Doit modules are usually not
530 # incremented in this phase
531  
532 in_directory {
533 # Note: tar cfvz C:/... ... does not seem to work on Windows
534 # (for all or some tar versions?). Error message is:
535 # tar (child): Cannot connect to C: resolve failed
536 # So create in cwd first, and move to final location.
537 $doit->system('tar', 'cfvz', basename($tarfile), basename($distdir));
538 $doit->move(basename($tarfile), $tarfile);
539 } "$distdir/..";
540 }
541  
542 sub dist_install_and_test {
543 local $ENV{DOIT_TEST_SKIP_SOME_CHECKS} = 1; # XXX is there a better interface?
544 my $tarfile = _get_tarfilename();
545 if (!-e $tarfile) {
546 dist();
547 }
548 require File::Temp;
549 my $dir = File::Temp::tempdir("Doit_dist_install_and_test_XXXXXXXX", TMPDIR => 1, CLEANUP => 1);
550 my $Doit_VERSION = _get_Doit_VERSION();
551 in_directory {
552 # Note: see above for tar under Windows problems
553 $doit->move($tarfile, basename($tarfile));
554 $doit->system('tar', 'xfz', basename($tarfile));
555 in_directory {
556 $doit->system($^X, 'Build.PL');
557 my @Build = $^O eq 'MSWin32' ? ($^X, 'Build') : ('./Build');
558 $doit->system(@Build);
559 $doit->system(@Build, 'test');
560 $doit->system(@Build, 'install');
561 $doit->system(@Build, 'test-installed');
562 } "Doit-$Doit_VERSION";
563 } $dir;
564 }
565  
566 sub dist_install_with_cpanm {
567 my $tarfile = _get_tarfilename();
568 if (!-e $tarfile) {
569 dist();
570 }
571 $doit->system('cpanm', $tarfile);
572 test_installed();
573 }
574  
575 sub look {
576 my $distdir = distdir(temporary => 1);
577 in_directory {
578 info "Spawning $ENV{SHELL} in $distdir...";
579 local $ENV{DOIT_BUILD_SHELL_LEVEL} = $ENV{DOIT_BUILD_SHELL_LEVEL};
580 $ENV{DOIT_BUILD_SHELL_LEVEL}++;
581 $doit->system($ENV{SHELL});
582 } $distdir;
583 }
584  
585 sub cover {
586 $doit->system(_cover_path(), '--test');
587 }
588  
589 sub show_cover {
590 cover();
591 my $browser = $^O eq 'darwin' ? 'open' : $doit->which('xdg-open') ? 'xdg-open' : 'firefox';
592 $doit->system($browser, "$FindBin::RealBin/cover_db/coverage.html");
593 }
594  
595 sub install {
596 build();
597 # XXX check if test suite was run?
598  
599 # MOD_INSTALL = $(ABSPERLRUN) -MExtUtils::Install -e 'install([ from_to => {@ARGV}, verbose => '\''$(VERBINST)'\'', uninstall_shadows => '\''$(UNINST)'\'', dir_mode => '\''$(PERM_DIR)'\'' ]);' --
600 # $(MOD_INSTALL) \
601  
602 # for a perl install:
603 # read "$(PERL_ARCHLIB)/auto/$(FULLEXT)/.packlist" \
604 # write "$(DESTINSTALLARCHLIB)/auto/$(FULLEXT)/.packlist" \
605 # "$(INST_LIB)" "$(DESTINSTALLPRIVLIB)" \
606 # "$(INST_ARCHLIB)" "$(DESTINSTALLARCHLIB)" \
607 # "$(INST_BIN)" "$(DESTINSTALLBIN)" \
608 # "$(INST_SCRIPT)" "$(DESTINSTALLSCRIPT)" \
609 # "$(INST_MAN1DIR)" "$(DESTINSTALLMAN1DIR)" \
610 # "$(INST_MAN3DIR)" "$(DESTINSTALLMAN3DIR)"
611  
612 # for a site install:
613 # read "$(SITEARCHEXP)/auto/$(FULLEXT)/.packlist" \
614 # write "$(DESTINSTALLSITEARCH)/auto/$(FULLEXT)/.packlist" \
615 # "$(INST_LIB)" "$(DESTINSTALLSITELIB)" \
616 # "$(INST_ARCHLIB)" "$(DESTINSTALLSITEARCH)" \
617 # "$(INST_BIN)" "$(DESTINSTALLSITEBIN)" \
618 # "$(INST_SCRIPT)" "$(DESTINSTALLSITESCRIPT)" \
619 # "$(INST_MAN1DIR)" "$(DESTINSTALLSITEMAN1DIR)" \
620 # "$(INST_MAN3DIR)" "$(DESTINSTALLSITEMAN3DIR)"
621
622 require Data::Dumper;
623 require ExtUtils::Install;
624 my $FULLEXT = 'Doit';
625 my $INST_LIB = 'blib/lib';
626 my $INST_ARCHLIB = 'blib/arch';
627 my $INST_BIN = 'blib/bin';
628 my $INST_SCRIPT = 'blib/script';
629 my $INST_MAN1DIR = 'blib/man1';
630 my $INST_MAN3DIR = 'blib/man3';
631 my $PERM_DIR = '755';
632 my @euii_args = (
633 from_to => {
634 (
635 $opt{install_base}
636 ?
637 (
638 read => "$Config{sitearchexp}/auto/$FULLEXT/.packlist",
639 ($opt{create_packlist} ? (write => "$opt{install_base}/lib/perl5/$Config{archname}/auto/$FULLEXT/.packlist") : ()),
640 $INST_LIB => "$opt{install_base}/lib/perl5",
641 $INST_ARCHLIB => "$opt{install_base}/lib/perl5",
642 $INST_BIN => "$opt{install_base}/bin",
643 $INST_SCRIPT => "$opt{install_base}/bin",
644 $INST_MAN1DIR => "$opt{install_base}/man/man1",
645 $INST_MAN3DIR => "$opt{install_base}/man/man3",
646 )
647 :
648 ($opt{installdirs}||'') eq 'core'
649 ?
650 (
651 read => "$Config{archlib}/auto/$FULLEXT/.packlist",
652 ($opt{create_packlist} ? (write => "$opt{destdir}$Config{installarchlib}/auto/$FULLEXT/.packlist") : ()),
653 $INST_LIB => "$opt{destdir}$Config{installprivlib}",
654 $INST_ARCHLIB => "$opt{destdir}$Config{installarchlib}",
655 $INST_BIN => "$opt{destdir}$Config{installbin}",
656 $INST_SCRIPT => "$opt{destdir}$Config{installscript}",
657 $INST_MAN1DIR => "$opt{destdir}$Config{installman1dir}",
658 $INST_MAN3DIR => "$opt{destdir}$Config{installman3dir}",
659 )
660 :
661 ($opt{installdirs}||'') eq 'vendor'
662 ?
663 (
664 read => "$Config{vendorlib}/auto/$FULLEXT/.packlist",
665 ($opt{create_packlist} ? (write => "$opt{destdir}$Config{installvendorarch}/auto/$FULLEXT/.packlist") : ()),
666 $INST_LIB => "$opt{destdir}$Config{installvendorlib}",
667 $INST_ARCHLIB => "$opt{destdir}$Config{installvendorarch}",
668 $INST_BIN => "$opt{destdir}$Config{installvendorbin}",
669 $INST_SCRIPT => "$opt{destdir}$Config{installvendorscript}",
670 $INST_MAN1DIR => "$opt{destdir}$Config{installvendorman1dir}",
671 $INST_MAN3DIR => "$opt{destdir}$Config{installvendorman3dir}",
672 )
673 : # default is site
674 (
675 read => "$Config{sitearchexp}/auto/$FULLEXT/.packlist",
676 ($opt{create_packlist} ? (write => "$opt{destdir}$Config{installsitearch}/auto/$FULLEXT/.packlist") : ()),
677 $INST_LIB => "$opt{destdir}$Config{installsitelib}",
678 $INST_ARCHLIB => "$opt{destdir}$Config{installsitearch}",
679 $INST_BIN => "$opt{destdir}$Config{installsitebin}",
680 $INST_SCRIPT => "$opt{destdir}$Config{installsitescript}",
681 $INST_MAN1DIR => "$opt{destdir}$Config{installsiteman1dir}",
682 $INST_MAN3DIR => "$opt{destdir}$Config{installsiteman3dir}",
683 )
684 )
685 },
686 verbose => $opt{verbose},
687 uninstall_shadows => $opt{uninst},
688 dir_mode => $PERM_DIR,
689 dry_run => $doit->is_dry_run,
690 );
691 my $euii_args_dump = Data::Dumper->new([{ @euii_args }],[qw()])->Indent(1)->Useqq(1)->Sortkeys(1)->Terse(1)->Dump;
692 info "Run ExtUtils::Install::install with parameters\n" . $euii_args_dump;
693 ExtUtils::Install::install([@euii_args]);
694 if ($doit->is_dry_run) {
695 info "(this was dry-run mode)";
696 }
697 }
698  
699 {
700 my $Doit_VERSION;
701  
702 sub _get_Doit_VERSION () {
703 return $Doit_VERSION if defined $Doit_VERSION;
704  
705 {
706 my $Doit_pm = $INC{"Doit.pm"};
707 open my $fh, $Doit_pm or die "Cannot open $Doit_pm: $!";
708 while(<$fh>) {
709 if (/\$VERSION\s*=\s*'(.*)'/) {
710 $Doit_VERSION = $1;
711 last;
712 }
713 }
714 if (!defined $Doit_VERSION) {
715 error "Fatal: Cannot find \$VERSION in $Doit_pm";
716 }
717 }
718 if ($Doit_VERSION !~ m{^\d+\.[\d_]+$}) {
719 error "Doit.pm VERSION $Doit_VERSION does not look as expected";
720 }
721 # Check also for numerical value of version
722 if (!defined $Doit::VERSION) {
723 error "Unexpected: cannot find \$VERSION in loaded Doit.pm";
724 }
725 {
726 (my $Doit_numerical_version = $Doit_VERSION) =~ s{_}{}g;
727 if ($Doit_numerical_version != $Doit::VERSION) {
728 error "Unexpected: parsed version $Doit_VERSION is not numerically equal to loaded version $Doit::VERSION";
729 }
730 }
731 $Doit_VERSION;
732 }
733 }
734  
735 sub _get_tarfilename () {
736 my $Doit_VERSION = _get_Doit_VERSION();
737 getcwd . "/Doit-" . $Doit_VERSION . ".tar.gz";
738 }
739  
740 sub _generate_META ($;$) {
741 my($destfile, $meta_version) = @_;
742  
743 require CPAN::Meta;
744 require ExtUtils::MakeMaker;
745 my $meta = CPAN::Meta->new({
746 name => 'Doit',
747 author => 'Slaven Rezic <srezic@cpan.org>',
748 abstract => 'a scripting framework',
749 license => 'perl',
750 version => MM->parse_version("$FindBin::RealBin/lib/Doit.pm"),
751 prereqs => {
752 configure => {
753 requires => {
754 'Digest::MD5' => 0,
755 'Exporter' => 5.57, # use Exporter "import"
756 'File::Path' => 2.0, # make_path
757 },
758 },
759 runtime => {
760 recommends => {
761 'IPC::Run' => 0,
762 'Net::OpenSSH' => 0,
763 },
764 requires => {
765 'perl' => 5.006,
766 'Exporter' => 5.57, # use Exporter "import"
767 'File::Path' => 2.07, # make_path
768 },
769 },
770 test => {
771 requires => {
772 'Test::More' => 0,
773 },
774 },
775 },
776 resources => {
777 repository => { url => 'git://github.com/eserte/Doit' },
778 },
779 generated_by => "Doit version " . _get_Doit_VERSION(),
780 });
781 if ($doit->is_dry_run) {
782 info "Would create $destfile" . (defined $meta_version ? " (version $meta_version)" : "") . " (dry-run)";
783 } else {
784 $meta->save($destfile, (defined $meta_version ? { version => $meta_version } : ()));
785 }
786 }
787  
788 sub generate_META_json (;$) {
789 my $destfile = shift || 'META.json';
790 _generate_META $destfile;
791 }
792  
793 sub generate_META_yml (;$) {
794 my $destfile = shift || 'META.yml';
795 _generate_META $destfile, 1.4;
796 }
797  
798 # XXX the debian package build functionality should go into
799 # a separate Doit component
800  
801 sub debian_package {
802 _debian_package('--depends' => 'perl, libnet-openssh-perl, libipc-run-perl', '--add-distro-version' => '1');
803 }
804  
805 sub _debian_package {
806 my(%opts) = @_;
807  
808 for my $tool (qw(dh-make-perl)) { # XXX maybe add git here?
809 $doit->qx('which', $tool); # existence check
810 }
811  
812 my $distdir = distdir(temporary => 1);
813  
814 my $version = delete $opts{'--version'};
815 my $add_distro_version = delete $opts{'--add-distro-version'};
816 if (!defined $version) {
817 chomp(my $git_describe = eval { $doit->info_qx('git', 'describe') }); # XXX what if this fails? Optionally made fatal?
818 if (defined $git_describe) {
819 if ($git_describe =~ m{^([0-9\._]+)$}) {
820 $version = $1;
821 } elsif ($git_describe =~ m{^([0-9\._]+)-(\d+)-g(.*)}) {
822 $version = $1."+git".$2."+".$3."-1"; # XXX make "1" configurable?
823 } else {
824 error "Cannot parse output from git describe: '$git_describe'";
825 }
826 $version =~ s{_}{-}; # replace "devel" version specifier
827 if ($add_distro_version) {
828 chomp(my $dist_id = $doit->qx({quiet=>1}, 'lsb_release', '-si'));
829 chomp(my $rel = $doit->qx({quiet=>1}, 'lsb_release', '-sr'));
830 if ($dist_id eq 'Debian') {
831 $rel =~ s{^(\d+).*}{$1};
832 $version .= "+deb${rel}u${add_distro_version}";
833 } elsif ($dist_id eq 'LinuxMint') {
834 $version .= "+linuxmint${rel}u${add_distro_version}";
835 } elsif ($dist_id eq 'Ubuntu') {
836 $version .= "~ubuntu${rel}.${add_distro_version}";
837 } else {
838 error "No distro version support for '$dist_id'";
839 }
840 }
841 } else {
842 warning "No information using git-describe available --- use automatic version detection";
843 }
844 }
845  
846 my $deb;
847 in_directory {
848 $doit->system(qw(dh-make-perl --build --vcs none), (defined $version ? ('--version', $version) : ()), %opts);
849 require File::Glob;
850 my(@debs) = File::Glob::bsd_glob("$distdir/../*.deb");
851 if (@debs != 1) {
852 error "Expecting exactly one generated .deb, but got: <@debs>\n";
853 }
854 $deb = $debs[0];
855 } $distdir;
856 $doit->copy($deb, basename($deb));
857 print basename($deb), "\n";
858 }
859  
860 sub debian_package_with_docker {
861 my $distro_spec = $ENV{DISTRO_SPEC}; # XXX this should be a proper option!
862 if (!$distro_spec || $distro_spec !~ m{^.*:.*$}) {
863 error "Please set DISTRO_SPEC environment variable to something like 'debian:jessie'";
864 }
865 for my $tool (qw(docker)) {
866 $doit->qx('which', $tool); # existence check
867 }
868  
869 # Cannot access directories outside of /Users when working
870 # with docker-machine @ Mac, see for an explanation:
871 # https://stackoverflow.com/questions/37673140/docker-volume-located-in-tmp-on-osx-empty
872 my $limited_volume_availability = $^O eq 'darwin';
873  
874 require File::Temp;
875 my $dir = File::Temp::tempdir("Doit_debian_package_docker_XXXXXXXX", TMPDIR => !$limited_volume_availability, CLEANUP => 1);
876 $dir = realpath $dir; # docker volumes have to be specified with absolute paths
877  
878 my $pkgdir = $limited_volume_availability ? "$FindBin::RealBin/pkg" : "/tmp";
879 if (!-d $pkgdir) {
880 $doit->mkdir($pkgdir);
881 }
882  
883 my $use_workdir = $ENV{USE_WORKDIR}; # XXX this should be a proper option
884 if ($use_workdir) {
885 # no --cvs-exclude --- git-describe has to work
886 $doit->system('rsync', '-a', '--exclude=blib', '.', $dir);
887 } else {
888 $doit->add_component('git');
889 $doit->git_repo_update(
890 repository => $FindBin::RealBin,
891 directory => $dir,
892 # no: we need the full history for correct git version calculation: clone_opts => [qw(--depth=1)],
893 );
894 }
895 in_directory {
896 my $dockerfile = <<"EOF";
897 FROM $distro_spec
898 EOF
899 my $add_opts = {};
900 $dockerfile .= _fix_Dockerfile_for_dist($distro_spec, $add_opts);
901 $dockerfile .= <<"EOF";
902  
903 ENV DEBIAN_FRONTEND noninteractive
904  
905 RUN apt-get$add_opts->{apt_get_update_opts} update && apt-get install -y --no-install-recommends \\
906 dh-make-perl \\
907 lsb-release \\
908 git
909  
910 # XXX This is depending on the current perl module and should be configurable
911 RUN apt-get install -y --no-install-recommends \\
912 libipc-run-perl \\
913 libnet-openssh-perl
914  
915 EOF
916 for my $env_key (qw(DEBFULLNAME DEBEMAIL EMAIL)) {
917 if ($ENV{$env_key}) {
918 $dockerfile .= <<"EOF";
919 ENV $env_key "$ENV{$env_key}"
920 EOF
921 }
922 }
923 $doit->write_binary("Dockerfile", $dockerfile);
924  
925 (my $label = $distro_spec) =~ s{:}{-}g;
926 $label = "doit-deb-$label";
927 $doit->system(qw(docker build -t), $label, qw(.));
928 $doit->system(
929 qw(docker run), '-v', "$dir:/data", '-v', "$pkgdir:/pkg", $label,
930 'sh', '-c', 'git config --global --add safe.directory /data && cd /data && perl Build.PL && ./Build debian_package && cp *.deb /pkg'
931 );
932 } $dir;
933 info "Package is available in $pkgdir";
934 }
935  
936 sub release {
937 my $Doit_VERSION = _get_Doit_VERSION();
938 for my $existing_tag (split /\n/, $doit->info_qx({quiet=>1}, 'git', 'tag')) {
939 if ($Doit_VERSION eq $existing_tag) {
940 error "The version $Doit_VERSION is already tagged --- probably the release was already done";
941 }
942 }
943  
944 FIND_VERSION: {
945 open my $cfh, '<', 'Changes'
946 or error "Can't open Changes: $!";
947 while(<$cfh>) {
948 if (/^\Q$Doit_VERSION\E\s/) {
949 last FIND_VERSION;
950 }
951 }
952 error "Cannot find version $Doit_VERSION in Changes";
953 }
954  
955 $doit->system('git', 'fetch');
956  
957 $doit->add_component('git');
958 my $git_status = $doit->git_short_status;
959 if ($git_status ne '' && $git_status ne '<') {
960 error "Please check the git status";
961 }
962  
963 # XXX TODO: check first if the current version already exists at CPAN
964 my $tarfile = _get_tarfilename();
965 if (!-e $tarfile) {
966 dist();
967 } else {
968 info "Use existing tarball $tarfile";
969 }
970  
971 print STDERR "Upload $tarfile? (y/n) ";
972 if (!y_or_n()) {
973 error "exiting release process";
974 }
975  
976 $doit->system('cpan-upload', basename($tarfile));
977  
978 $doit->system('git', 'tag', '-a', '-m', "* $Doit_VERSION", $Doit_VERSION);
979 $doit->system('git', 'push', 'origin', 'master', $Doit_VERSION);
980 }
981  
982 sub _prove_path {
983 my @directory_candidates = ($Config{bin}, dirname(realpath $^X));
984 my @basename_candidates = ('prove', "prove$Config{version}");
985 my @candidates = map {
986 my $basename = $_;
987 map {
988 "$_/$basename";
989 } @directory_candidates;
990 } @basename_candidates;
991 if ($^O eq 'MSWin32') {
992 unshift @candidates, map { "$_.bat"} @candidates;
993 }
994 for my $candidate (@candidates) {
995 if (-x $candidate) {
996 return $candidate;
997 }
998 }
999 error "No 'prove' for the current perl found";
1000 }
1001  
1002 sub _cover_path {
1003 my @candidates = ("$Config{bin}/cover");
1004 if ($^O eq 'MSWin32') {
1005 unshift @candidates, map { "$_.bat"} @candidates;
1006 }
1007 for my $candidate (@candidates) {
1008 if (-x $candidate) {
1009 return $candidate;
1010 }
1011 }
1012 error "No 'cover' for the current perl found";
1013 }
1014  
1015 sub _Build_PL_mode {
1016 require Data::Dumper;
1017 my $argv_serialized = "\n" . '$Build_PL::ARGV = ' . Data::Dumper->new([\@ARGV], [])->Indent(1)->Useqq(1)->Sortkeys(1)->Terse(1)->Dump . ";\n\n";
1018 {
1019 if (-l "Build") { # formerly this used to be a symlink
1020 $doit->unlink("Build");
1021 }
1022 my $preamble = <<"EOF";
1023 #! $Config{perlpath}
1024 # MD5: $Build_PL_md5hex
1025 EOF
1026 $preamble .= $argv_serialized;
1027 $doit->write_binary({quiet=>1}, 'Build', $preamble . qq{# line 1 "Build.PL"\n} . $Build_PL_file_contents);
1028 $doit->chmod(0755, 'Build');
1029 }
1030 exit;
1031 }
1032  
1033 # Return a string for adding to Dockerfile after the FROM line. Also
1034 # the 2nd parameter (which has to be an empty hashref) is filled with
1035 # further instructions to amend the Dockerfile (currently only
1036 # apt_get_update_opts).
1037 sub _fix_Dockerfile_for_dist {
1038 my($distro_spec, $add_opts_ref) = @_;
1039 if ($distro_spec =~ m{^debian:(wheezy|7)$}) {
1040 # https://unix.stackexchange.com/questions/508724/failed-to-fetch-jessie-backports-repository
1041 $add_opts_ref->{apt_get_update_opts} = ' -o Acquire::Check-Valid-Until=false'; # with leading space
1042 <<EOF;
1043 RUN echo 'deb [check-valid-until=no] http://archive.debian.org/debian wheezy main' > /etc/apt/sources.list
1044 RUN echo 'deb [check-valid-until=no] http://archive.debian.org/debian-security/ wheezy/updates main' >> /etc/apt/sources.list
1045 EOF
1046 } else {
1047 $add_opts_ref->{apt_get_update_opts} = '';
1048 '';
1049 }
1050 }
1051  
1052 sub _add_Dockerfile_invalidate_cmd {
1053 my $dockerfile_ref = shift;
1054 require POSIX;
1055 $$dockerfile_ref .= <<"EOF";
1056 # Just a hack to make sure that the following lines
1057 # are executed at least once a day
1058 RUN echo @{[ POSIX::strftime("%F", localtime) ]}
1059 EOF
1060 }
1061  
1062 # REPO BEGIN
1063 # REPO NAME y_or_n /home/eserte/src/srezic-repository
1064 # REPO MD5 146cfcf8f954555fe0117a55b0ddc9b1
1065  
1066 #=head2 y_or_n
1067 #
1068 #Accept user input. Return true on 'y', return false on 'n', otherwise
1069 #ask again.
1070 #
1071 #A default may be supplied as an optional argument:
1072 #
1073 # y_or_n 'y';
1074 # y_or_n 'n';
1075 #
1076 #=cut
1077  
1078 sub y_or_n (;$) {
1079 my $default = shift;
1080 while () {
1081 chomp(my $yn = <STDIN>);
1082 if ($yn eq '' && defined $default) {
1083 $yn = $default;
1084 }
1085 if (lc $yn eq 'y') {
1086 return 1;
1087 } elsif (lc $yn eq 'n') {
1088 return 0;
1089 } else {
1090 print STDERR "Please answer y or n: ";
1091 }
1092 }
1093 }
1094 # REPO END
1095  
1096 __END__
« blib/lib/DoitX/Example.pm ↑ Summary