chore(utils): configurable file extensions for renamer util

This commit is contained in:
David Mosbach 2024-05-27 14:05:42 +00:00
parent 51a3a25042
commit 5200d88f75
2 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,5 @@
{
"/defaultExtension": "svg",
"seedling": "new",
"check": "ok",
"xmark": "not-ok",

View File

@ -19,6 +19,7 @@ usage() unless -f $renamefile;
mkdir $todir;
my %did = ();
my %params = ();
my @errNex = ();
@ -29,10 +30,26 @@ close $fh;
if($cont!~m#^\s*\{(.*)\}\s*$#s) { die "'$renamefile' not in an expected format; it should be an json-object" }
my $core = $1;
while($core=~s#^\s*,?\s*"([^"/]+)"\s*:\s*"([^"/]+)"##) {
my %toRename = ();
while($core=~s#^\s*,?\s*"(/?[^"/]+)"\s*:\s*"([^"/]+)"##) {
my ($from, $to) = ($1, $2);
my $pfrom = "$fromdir/$from.svg";
my $pto = "$todir/$to.svg";
if ($from =~ m#^/#) {
$params{$from} = $to;
} else {
$toRename{$from} = $to;
}
}
my $defaultExtension = $params{"/defaultExtension"};
for my $from (keys %toRename) {
my $to = $toRename{$from};
my $pfrom = "$fromdir/$from";
my $pto = "$todir/$to";
if (defined $defaultExtension and $from !~ m#\.#) {
$_ .= ".$defaultExtension" for $pfrom, $pto;
}
if(-e $pfrom) {
print "Renaming '$pfrom' to '$pto'\n";
system("cp", $pfrom, $pto);