Apply Perltidy to translation-tool.pl (#6326)

This commit is contained in:
Basil Crow 2022-03-05 07:34:35 -08:00 committed by GitHub
parent ad72f4d9e3
commit f3b8ad3d5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 209 additions and 74 deletions

104
.perltidyrc Normal file
View File

@ -0,0 +1,104 @@
#################################################
# Categories outlined in this config
# are based on the categories in the
# perltidy man page:
# http://perltidy.sourceforge.net/perltidy.html
#################################################
###################################
# I/O Control
###################################
# Send all errors to standard output rather than a file.
--standard-error-output
# All non-critical warnings will be reported as errors.
--warning-output
###################################
# Basic Options
###################################
# Maximum number of characters per line.
--maximum-line-length=80
###################################
# Code Indentation Control
###################################
# The number of spaces to indent a line when a new block starts.
--indent-columns=4
# If a line continues, it should be indented 2 spaces.
--continuation-indentation=4
# If a comment is longer than the maximum line length, break it up for
# readability.
--outdent-long-comments
# If a quoted string is longer than the maximum line length, do not break it up
# for readability.
--no-outdent-long-quotes
###################################
# Whitespace Control
###################################
# Stack opening braces in order to avoid having a brace sitting by itself on
# a line.
--stack-opening-tokens
# Stack closing tokens in order to avoid having a brace sitting by itself on
# a line.
--stack-closing-tokens
# Spaces between parentheses e.g. if ((my $len_tab = length($tabstr)) > 0) {
--paren-tightness=2
# Spaces between brackets e.g. $width = $col[$j + $k] - $col[$j];
--square-bracket-tightness=2
# Spaces between braces in expression e.g. $obj->{$parsed_sql->{'table'}[0]};
--brace-tightness=2
# Spaces between braches with blocks of code
# e.g. %bf = map { $_ => -M $_ } grep { /\.deb$/ } dirents '.';
--block-brace-tightness=0
# Do not add spaces between semicolons within for loops.
--nospace-for-semicolon
###################################
# Comment Controls
###################################
# Indent comments to be at the same level as the code.
--indent-block-comments
###################################
# Line Break Control
###################################
# The else is on the same line as the brace.
--cuddled-else
# Create a break after -> and period if a break is required.
--want-break-after='-> .'
###################################
# Blank Line Control
###################################
# Do not force blank lines before full line comments.
--noblanks-before-comments
# Do not force blank lines before blocks starting with for, foreach, while,
# until, and if,unless.
--noblanks-before-blocks
###################################
# Vertical Alignment
###################################
# Turn off vertical alignment.
-novalign

View File

@ -1,7 +1,8 @@
#!/usr/bin/perl -w
# The MIT License
#
# Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number of other of contributors
# Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number
# of other of contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@ -27,19 +28,22 @@
# Perl script to generate missing translation keys and missing properties files,
# to remove unused keys, and to convert utf8 properties files to iso or ascii.
#
# 1.- It recursively looks for files in a folder, and analyzes them to extract the
# keys being used in the application.
# 2.- If --add=true, it generates the appropriate file for the desired language and adds
# these keys to it, adding the english text as a reference.
# If the properties file already exists the script update it with the new keys.
# 3.- When --remove=true and there are unused keys in our file, the script removes them.
# 4.- If an editor is passed as argument, the script edits each modified file after adding new keys.
# 5.- Finally, when --toiso=true or --toascii=true, the script is able to convert utf-8
# properties files to iso or unicode hex representation is ascii.
# 1.- It recursively looks for files in a folder, and analyzes them to extract
# the keys being used in the application.
# 2.- If --add=true, it generates the appropriate file for the desired language
# and adds these keys to it, adding the english text as a reference. If the
# properties file already exists the script update it with the new keys.
# 3.- When --remove=true and there are unused keys in our file, the script
# removes them.
# 4.- If an editor is passed as argument, the script edits each modified file
# after adding new keys.
# 5.- Finally, when --toiso=true or --toascii=true, the script is able to
# convert utf-8 properties files to iso or unicode hex representation is
# ascii.
#
# Note, while the migration to Jenkins this file will report the keys which should point
# to Jenkins instead of the old name.
# Note, while the migration to Jenkins this file will report the keys which
# should point to Jenkins instead of the old name.
use warnings;
use strict;
@ -47,18 +51,26 @@ use File::Basename;
use File::Find;
use File::Path;
my ($lang, $editor, $dir, $toiso, $toascii, $add, $remove, $reuse, $counter, $target) = (undef, undef, "./", undef, undef, undef, undef, undef, undef, "./");
my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins, $countervalue) = (0, 0, 0, 0, 0, 0, 0, 1);
my (
$lang, $editor, $dir, $toiso, $toascii,
$add, $remove, $reuse, $counter, $target
) = (undef, undef, "./", undef, undef, undef, undef, undef, undef, "./");
my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins,
$countervalue)
= (0, 0, 0, 0, 0, 0, 0, 1);
## read arguments
foreach (@ARGV) {
if (/^--lang=(.*)$/) {
$lang = $1;
} elsif (/^--editor=(.*)$/) {
$editor = $1; $add = 1;
$editor = $1;
$add = 1;
} elsif (/^--toiso$/ || /^--toiso=true$/) {
$toiso = 1; $toascii = 0;
$toiso = 1;
$toascii = 0;
} elsif (/^--toascii$/ || /^--toascii=true$/) {
$toascii = 1; $toiso = 0;
$toascii = 1;
$toiso = 0;
} elsif (/^--add$/ || /^--add=true$/) {
$add = 1;
} elsif (/^--remove$/ || /^--remove=true$/) {
@ -85,7 +97,8 @@ print STDERR "Finding files ...\n";
my @files = findTranslatableFiles($dir);
print STDERR "Found " . (scalar keys @files) . " files\n";
## load a cache with keys already translated to utilize in the case the same key is used
## load a cache with keys already translated to utilize in the case the same key
## is used
my %cache = loadAllTranslatedKeys($reuse, $lang) if ($reuse && -e $reuse);
## process each file
@ -113,8 +126,14 @@ if ($tkeys != 0) {
$pnojenkins = $tnojenkins / $tkeys * 100;
}
my @formatParameters = ($tfiles, $tkeys, $tdone, $pdone, $tmissing, $pmissing, $tunused, $punused, $tempty, $pempty, $tsame, $psame, $tnojenkins, $pnojenkins);
printf "\nTOTAL: Files: %d Keys: %d Done: %d(%.2f%%)\n Missing: %d(%.2f%%) Orphan: %d(%.2f%%) Empty: %d(%.2f%%) Same: %d(%.2f%%) NoJenkins: %d(%.2f%%)\n\n", (@formatParameters);
my @formatParameters = (
$tfiles, $tkeys, $tdone, $pdone, $tmissing,
$pmissing, $tunused, $punused, $tempty, $pempty,
$tsame, $psame, $tnojenkins, $pnojenkins
);
printf
"\nTOTAL: Files: %d Keys: %d Done: %d(%.2f%%)\n Missing: %d(%.2f%%) Orphan: %d(%.2f%%) Empty: %d(%.2f%%) Same: %d(%.2f%%) NoJenkins: %d(%.2f%%)\n\n",
(@formatParameters);
## end
exit();
@ -131,15 +150,17 @@ sub processFile {
# keys -> Hash of keys used in jelly or Message.properties files
# ekeys -> Hash of key/values in English
# okeys -> Hash of key/values in the desired language which are already present in the file
# okeys -> Hash of key/values in the desired language which are already
# present in the file
my (%keys, %okeys, %ekeys);
# Read .jelly or Message.properties files, and fill a hash with the keys found
# Read .jelly or Message.properties files, and fill a hash with the keys
# found
if ($file =~ m/.jelly$/) {
%keys = loadJellyFile($file);
%ekeys = loadPropertiesFile($efile);
} else {
%keys = %ekeys = loadPropertiesFile($file)
%keys = %ekeys = loadPropertiesFile($file);
}
# load keys already present in the desired locale
@ -186,7 +207,8 @@ sub processFile {
}
# Show Alerts
print "\nFile: $ofile\n$missing$unused$same$nj" if ($missing ne "" || $unused ne '' || $same ne '' || $nj ne '');
print "\nFile: $ofile\n$missing$unused$same$nj"
if ($missing ne "" || $unused ne '' || $same ne '' || $nj ne '');
# write new keys in our file adding the English translation as a reference
if ($add && $missing ne "") {
@ -200,9 +222,9 @@ sub processFile {
print F $cache{$_} . "\n";
} else {
if ($counter) {
# add unique value for each added translation
print F "---TranslateMe ".$countervalue."--- ".($ekeys{$_} ? $ekeys{$_} : $_)."\n";
print F "---TranslateMe " . $countervalue . "--- " .
($ekeys{$_} ? $ekeys{$_} : $_) . "\n";
} else {
print F "\n";
}
@ -214,8 +236,10 @@ sub processFile {
close(F);
}
# open the editor if the user has specified it and there are changes to manage
system("$editor $ofile") if ($editor && $add && ($missing ne "" || $same ne "" || $nj ne ''));
# open the editor if the user has specified it and there are changes to
# manage
system("$editor $ofile")
if ($editor && $add && ($missing ne "" || $same ne "" || $nj ne ''));
# write new keys in our file adding the English translation as a reference
removeUnusedKeys($ofile, %keys) if ($remove && $unused ne "");
@ -246,10 +270,15 @@ sub findTranslatableFiles {
my $dir = shift;
die "Folder doesn't exist: $dir\n" unless (-e $dir);
my @ret;
find(sub {
find(
sub {
my $file = $File::Find::name;
push(@ret, $file) if ($file !~ m#(/src/test/)|(/target/)|(\.svn)# && $file =~ /(Messages.properties)$|(.*\.jelly)$/);
}, $dir);
push(@ret, $file)
if ($file !~ m#(/src/test/)|(/target/)|(\.svn)#
&& $file =~ /(Messages.properties)$|(.*\.jelly)$/);
},
$dir
);
return @ret;
}
@ -261,7 +290,9 @@ sub loadJellyFile {
while (<F>) {
next if (!/\$\{.*?\%([^\(]+?).*\}/);
my $line = $_;
while ($line =~ /^.*?\$\{\%([^\(\}]+)(.*)$/ || $line=~ /^.*?\$\{.*?['"]\%([^\(\}\"\']+)(.*)$/ ) {
while ($line =~ /^.*?\$\{\%([^\(\}]+)(.*)$/
|| $line =~ /^.*?\$\{.*?['"]\%([^\(\}\"\']+)(.*)$/)
{
$line = $2;
my $word = $1;
$word =~ s/\(.+$//g;
@ -329,7 +360,8 @@ sub removeUnusedKeys {
sub convert {
my ($ofile, $toiso, $toascii) = @_;
if (isUtf8($ofile) && ($toiso || $toascii)) {
print "\nConverting file $ofile to " . ($toiso ? "ISO-8859" : "ASCII") . "\n";
print "\nConverting file $ofile to " .
($toiso ? "ISO-8859" : "ASCII") . "\n";
my $back = $ofile . "~~";
if (rename($ofile, $back) && open(FI, $back) && open(FO, ">$ofile")) {
while (<FI>) {
@ -371,6 +403,7 @@ sub isUtf8 {
# print MIT license in new files
# Note: the license is read from the head of this file
my $license;
sub printLicense {
my $file = shift;
if (!$license && open(F, $0)) {
@ -395,8 +428,7 @@ sub printLicense {
}
# trim function to remove whitespace from the start and end of the string
sub trim($)
{
sub trim($) {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
@ -436,4 +468,3 @@ Usage: $0 --lang=xx [options] [dir]
";
exit();
}