/
Upmake.pm
78 lines (57 loc) · 1.4 KB
/
Upmake.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package Text::Upmake;
use strict;
use warnings;
use autodie;
use Exporter qw(import);
our @EXPORT = qw(read_files_list upmake);
=head1 NAME
Text::Upmake - Update make files.
=head1 SYNOPSIS
=head1 AUTHOR
Vadim Zeitlin
=cut
# Reads the file containing the file lists definitions and returns a hash ref
# with variable names as keys and refs to arrays of the file names as values.
#
# Takes an (open) file handle as argument.
sub read_files_list
{
my ($fh) = @_;
my ($var, %vars);
while (<$fh>) {
chomp;
s/#.*$//;
s/^\s+//;
s/\s+$//;
next if !$_;
if (/^(\w+)\s*=$/) {
$var = $1;
} else {
die "Unexpected contents outside variable definition at line $.\n"
unless defined $var;
push @{$vars{$var}}, $_;
}
}
return \%vars;
}
# Update the file with the given name in place using the specified function and
# passing it the rest of the arguments.
#
# This is meant to be used with update_xxx() below.
sub upmake
{
my ($fname, $updater, @args) = @_;
my $fname_new = "$fname.upmake.new"; # TODO make it more unique
open my $in, '<', $fname;
open my $out, '>', $fname_new;
my $changed = $updater->($in, $out, @args);
close $in;
close $out;
if ($changed) {
rename $fname_new, $fname;
} else {
unlink $fname_new;
}
$changed
}
1;