| 1 | package Makefile::Update; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Update make files. | ||||||||||
| 4 | |||||||||||
| 5 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 8 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 8 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 8 | our @EXPORT = qw(read_files_list upmake); | |||||||||
| 12 | |||||||||||
| 13 | 8 | our $VERSION = '0.4'; # VERSION | |||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | sub read_files_list | ||||||||||
| 18 | 1 | ▶ | { | ||||||||
|
Subroutine
read_files_list: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||
| 19 | 1 | my ($fh) = @_; | |||||||||
| 20 | |||||||||||
| 21 | 1 | my ($var, %vars); | |||||||||
| 22 | 1 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 14 | chomp; | |||||||||
| 24 | 14 | s/#.*$//; | |||||||||
| 25 | 14 | s/^\s+//; | |||||||||
| 26 | 14 | s/\s+$//; | |||||||||
| 27 | 14 | ▶ | next if !$_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | |||||||||||
| 29 | 9 | ▶ | if (/^(\w+)\s*=$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 3 | $var = $1; | |||||||||
| 31 | } else { | ||||||||||
| 32 | 6 | ▶ | die "Unexpected contents outside variable definition at line $.\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | unless defined $var; | ||||||||||
| 34 | 6 | ▶ | if (/^\$(\w+)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | my $name = $1; | |||||||||
| 36 | die qq{Reference to undefined variable "$name" in the } . | ||||||||||
| 37 | qq{assignment to "$var" at line $.\n} | ||||||||||
| 38 | 2 | ▶ | unless exists $vars{$name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 2 | my $value = $vars{$name}; | |||||||||
| 40 | 2 | push @{$vars{$var}}, $_ for @$value; | |||||||||
| 41 | } else { | ||||||||||
| 42 | 4 | push @{$vars{$var}}, $_; | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 1 | return \%vars; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | sub upmake | ||||||||||
| 52 | 12 | ▶ | { | ||||||||
|
Subroutine
upmake: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||
| 53 | 12 | my $file_or_options = shift; | |||||||||
| 54 | 12 | my ($updater, @args) = @_; | |||||||||
| 55 | |||||||||||
| 56 | 12 | my ($fname, $verbose, $quiet, $dryrun); | |||||||||
| 57 | 12 | ▶ | if (ref $file_or_options eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 10 | $fname = $file_or_options->{file}; | |||||||||
| 59 | 10 | $verbose = $file_or_options->{verbose}; | |||||||||
| 60 | 10 | $quiet = $file_or_options->{quiet}; | |||||||||
| 61 | 10 | $dryrun = $file_or_options->{dryrun}; | |||||||||
| 62 | } else { | ||||||||||
| 63 | 2 | $fname = $file_or_options; | |||||||||
| 64 | 2 | $verbose = | |||||||||
| 65 | $quiet = | ||||||||||
| 66 | $dryrun = 0; | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 12 | ▶ | if ($dryrun) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 2 | my $old = do { | |||||||||
| 71 | 2 | local $/; | |||||||||
| 72 | 2 | open my $f, '<', $fname; | |||||||||
| 73 | <$f> | ||||||||||
| 74 | 2 | }; | |||||||||
| 75 | 2 | my $new = ''; | |||||||||
| 76 | |||||||||||
| 77 | 2 | open my $in, '<', \$old; | |||||||||
| 78 | 2 | open my $out, '>', \$new; | |||||||||
| 79 | |||||||||||
| 80 | 2 | ▶ | if ($updater->($in, $out, @args)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 1 | print qq{Would update "$fname"}; | |||||||||
| 82 | |||||||||||
| 83 | 1 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | ▶ | if (eval { require Text::Diff; }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | print " with the following changes:\n"; | |||||||||
| 86 | |||||||||||
| 87 | 0 | print Text::Diff::diff(\$old, \$new, { | |||||||||
| 88 | FILENAME_A => $fname, | ||||||||||
| 89 | FILENAME_B => "$fname.new" | ||||||||||
| 90 | }); | ||||||||||
| 91 | } else { | ||||||||||
| 92 | 0 | print ".\n"; | |||||||||
| 93 | |||||||||||
| 94 | 0 | warn qq{Can't display diff of the changes, please install Text::Diff module.\n}; | |||||||||
| 95 | } | ||||||||||
| 96 | } else { | ||||||||||
| 97 | 1 | print ".\n"; | |||||||||
| 98 | } | ||||||||||
| 99 | } else { | ||||||||||
| 100 | 1 | print qq{Wouldn't change the file "$fname".\n}; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 2 | return 0; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 10 | my $fname_new = "$fname.upmake.new"; # TODO make it more unique | |||||||||
| 107 | |||||||||||
| 108 | 10 | open my $in, '<', $fname; | |||||||||
| 109 | 10 | open my $out, '>', $fname_new; | |||||||||
| 110 | |||||||||||
| 111 | 10 | my $changed = $updater->($in, $out, @args); | |||||||||
| 112 | |||||||||||
| 113 | 10 | close $in; | |||||||||
| 114 | 10 | close $out; | |||||||||
| 115 | |||||||||||
| 116 | 10 | ▶ | if ($changed) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 6 | rename $fname_new, $fname; | |||||||||
| 118 | } else { | ||||||||||
| 119 | 4 | unlink $fname_new; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 10 | ▶ | if ($changed) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 6 | ▶ | print qq{File "$fname" successfully updated.\n} unless $quiet; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 6 | return 1; | |||||||||
| 125 | } else { | ||||||||||
| 126 | 4 | ▶ | print qq{No changes in the file "$fname".\n} if $verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 4 | return 0; | |||||||||
| 128 | } | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 8 | 1; | |||||||||
| 132 | |||||||||||
| 133 | __END__ | ||||||||||