-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgetExtraSourceFiles.pl
executable file
·83 lines (73 loc) · 1.51 KB
/
getExtraSourceFiles.pl
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
79
80
81
82
83
#!/usr/bin/env perl
use File::Basename;
use Getopt::Std;
use warnings;
use strict;
sub usage()
{
print STDERR << "EOF";
Usage: $0 -i INFILE -o OUTFILE
Will read and process 'INSERT_SRC <PATH>' tags in input file,
expand them with Cabal friendly wildcards.
EOF
exit;
}
my %opt;
getopts("hi:o:", \%opt) or usage();
usage() if($opt{"h"} || !exists($opt{"i"}) || $opt{"i"} eq '');
my $templateFile = $opt{"i"};
my $outFile = $templateFile;
$outFile =~ s/\.template$/.release/;
$outFile = $opt{"o"} if(exists($opt{"o"}));
warn "Reading $templateFile, writing to $outFile\n";
open(INF, $templateFile) or die "Cannot open $templateFile";
open(OUTF, '>', $outFile) or die "Cannot open $outFile to write";
while(<INF>)
{
chomp;
if(/^(\s*)--\s+\@INSERT_SRC\s+(.+)$/)
{
my $indent = $1;
my $dirName = $2;
my %dirMap;
getExtraSrcEntries($dirName, \%dirMap);
foreach my $dir (sort (keys %dirMap))
{
foreach my $wcard (sort (keys %{$dirMap{$dir}}))
{
print OUTF "$indent\"$dir/$wcard\"\n";
}
}
}
else
{
print OUTF "$_\n";
}
}
close(OUTF);
close(INF);
sub getWildCard
{
my $fname = shift;
if($fname =~ /[^\/]+?\.([^\/]+)$/)
{
return "*.$1";
}
else
{
return basename($fname);
}
}
sub getExtraSrcEntries
{
my ($path, $dirMap) = @_;
warn "Scanning $path..\n";
my $inf;
open($inf, "find -L $path -type f |") or die("Cannot exec 'find $path -type f'");
while(<$inf>)
{
chomp;
${$dirMap}{dirname $_}{getWildCard $_} = 1;
}
close($inf);
}