glib-mkenums: add --output option to write output to a file

https://bugzilla.gnome.org/show_bug.cgi?id=770175
This commit is contained in:
Tim-Philipp Müller 2016-08-21 20:03:15 +01:00 committed by Matthias Clasen
parent c382da4819
commit ac54db2ee2

View File

@ -2,6 +2,9 @@
use warnings;
use File::Basename;
use File::Copy "move";
use File::Temp;
use Cwd;
use Safe;
# glib-mkenums.pl
@ -25,6 +28,8 @@ my $firstenum = 1; # Is this the first enumeration per file?
my @entries; # [ $name, $val ] for each entry
my $sandbox = Safe->new; # sandbox for safe evaluation of expressions
my $output_file; # Filename to write result into
sub parse_trigraph {
my $opts = shift;
my @opts;
@ -154,6 +159,7 @@ sub usage {
print " --vtail <text> Value tail, produced after iterating over enum values\n";
print " --comments <text> Comment structure\n";
print " --template file Template file\n";
print " --output file Output file\n";
print " -v, --version Print version informations\n\n";
print "Production text substitutions:\n";
print " \@EnumName\@ PrefixTheXEnum\n";
@ -247,12 +253,23 @@ while ($_=$ARGV[0],/^-/) {
elsif (/^--vprod$/) { $vprod = $vprod . shift }
elsif (/^--vtail$/) { $vtail = $vtail . shift }
elsif (/^--comments$/) { $comment_tmpl = shift }
elsif (/^--output$/) { $output = shift }
elsif (/^--help$/ || /^-h$/ || /^-\?$/) { usage; }
elsif (/^--version$/ || /^-v$/) { version; }
else { usage; }
last if not defined($ARGV[0]);
}
if (defined ($output)) {
my($out_fn, $out_dir, $out_suffix) = fileparse($output, qr{\.\w+$});
if ($out_dir eq '') { $out_dir = cwd(); }
$out_suffix =~ s/^\./_/; # .foo -> _foo
$OUTPUT = File::Temp->new("$out_fn$out_suffix\_XXXXXX", DIR => $out_dir, UNLINK => 0);
select $OUTPUT; # Make all print calls from here on go to OUTPUT
}
# put auto-generation comment
{
my $comment = $comment_tmpl;
@ -563,3 +580,12 @@ if (length($ftail)) {
$comment =~ s/\@comment\@/Generated data ends here/;
print "\n" . $comment . "\n\n";
}
if (defined ($output)) {
select STDOUT;
my $tmpfilename = $OUTPUT->filename;
close ($OUTPUT)
|| warn "Closing output file $tmpfilename failed: $!";
move ($tmpfilename, $output)
|| die "Could not rename $tmpfilename to $output: $!";
}