-
Notifications
You must be signed in to change notification settings - Fork 2
/
mygen.pl
executable file
·52 lines (41 loc) · 1.16 KB
/
mygen.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
#! /usr/bin/perl
use YAML::Syck;
use strict;
print Dump( \@ARGV );
print "@ARGV\n";
my $usage = "$0 infile outfile FOO=REPLACEMENT_FOO BAR=REPLACEMENT_BAR ...";
my $source = shift @ARGV || die $usage;
my $dest = shift @ARGV || die $usage;
my @replacements;
foreach (@ARGV) {
die $usage unless ( $_ =~ /=/ );
my ( $k, $v ) = split( /=/, $_, 2 );
my $re = qr/\Q$k/;
push( @replacements, [ $k, $v ] );
}
open( SOURCE, "<$source" ) || die "Failed to open $source : $!";
open( DEST, ">$dest.new" ) || die "Failed to open $dest.new for write: $!";
while (<SOURCE>) {
if ( $_ =~ m#//go:generate# ) {
$_ =~ s#//go:generate#//\\\\//go:generated#;
print DEST;
next;
}
if ( $_ =~ m#//\+build generate# ) {
print DEST "//AUTO GENERATED CODE\n";
next;
}
while (1) {
my $original = $_;
foreach my $aref (@replacements) {
my $ref = $aref->[0];
my $val = $aref->[1];
$_ =~ s/$ref/$val/g;
}
last if ( $original eq $_ );
}
print DEST;
}
close DEST;
close SOURCE;
rename( "$dest.new", "$dest" ) || die "Failed to rename $dest.new $dest: $!"