Complete output file handling for kdesrc-build-setup

Now it supports:
* Backup existing configuration and overwrite
* Custom configuration filename.
* It even automatically falls back to a tempfile if backup fails.
wilder
Michael Pyne 15 years ago
parent ec32bc4fa2
commit ee439a1ffc
  1. 62
      kdesrc-build-setup

@ -3,6 +3,8 @@
use strict;
use 5.010;
use IO::Pipe;
use File::Copy;
use File::Temp qw/tempfile/;
sub runDialogExecutable
{
@ -193,7 +195,56 @@ my @chosenModules = getListOptions(
my $numCpus = getUserInput(
'How many CPU cores do you wish to use for building?', '2');
open my $output, '>', '/tmp/kdesrc-buildrc' or die "$!";
my $outputFileName = "$ENV{HOME}/.kdesrc-buildrc";
my $output; # Will be output filehandle.
while (-e $outputFileName) {
(my $printableName = $outputFileName) =~ s/^$ENV{HOME}/~/;
my $outputChoice = getMenuOption(
"$printableName already exists, what do you want to do?",
[
backup => 'Make a backup, then overwrite with the new configuration',
custom => 'Write the new configuration to a different file',
cancel => 'Cancel setup',
],
);
if ($outputChoice eq 'cancel') {
showInfo('Setup canceled');
exit 0;
}
if ($outputChoice eq 'custom') {
$outputFileName = getUserInput('Enter desired configuration file name.');
$outputFileName =~ s/^~/$ENV{HOME}/;
}
if ($outputChoice eq 'backup') {
copy($outputFileName, "$outputFileName~") or do {
my $error = "$!";
showInfo(<<EOF);
Failed to make backup of $outputFileName, due to error $error.
Configuration will be written to a temporary file instead.
EOF
($output, $outputFileName) = tempfile("kdesrc-buildrc-XXXX");
};
last;
}
}
# Filehandle could already be opened as a tempfile.
if (!$output) {
open ($output, '>', $outputFileName) or do {
my $error = "$!";
showInfo (<<EOF);
Unable to open output file $outputFileName for writing due to error $error.
EOF
die "$!";
}
}
print $output <<EOF;
# Autogenerated by kdesrc-build-setup. You may modify this file if desired.
@ -349,3 +400,12 @@ end module-set
EOF
}
close($output);
$outputFileName =~ s/^$ENV{HOME}/~/;
showInfo("Generated configuration has been written to $outputFileName");
# Say same thing in text mode just in case.
system('clear');
say "Generated configuration has been written to $outputFileName";
exit 0;

Loading…
Cancel
Save