Skip to content

Instantly share code, notes, and snippets.

@FYHenry
Last active June 25, 2024 21:40
Show Gist options
  • Save FYHenry/e0e04d6b0c86f640c7ea3667057e2845 to your computer and use it in GitHub Desktop.
Save FYHenry/e0e04d6b0c86f640c7ea3667057e2845 to your computer and use it in GitHub Desktop.
Inote additional files
#!/usr/bin/perl
use warnings;
use strict;
use constant ROOT_DIR => "./Inote/Inote_RestAPI";
use constant PATHS => (
ROOT_DIR."/src/main/java",
ROOT_DIR."/src/test/java"
);
use constant EX_FLAG => defined($ARGV[0])
&& (("$ARGV[0]" eq "-e")
|| ("$ARGV[0]" eq "--execute"));
use constant {
RESET => "\x1b[0m",
GREEN => "\x1b[32m",
YELLOW => "\x1b[33m"
};
use File::Find;
use File::Copy;
my (@all_files, @all_dirs, @bad_dirs, @good_dirs, @bad_names, @good_names);
print(YELLOW."Execution mode!\n".RESET) if EX_FLAG;
# $FILE_PATH $OLD_TEXT [$OLD_TEXT…] => void
sub fix_pkg_names {
my ($fp, @olds) = @_;
my ($stream, @tmp);
open($stream, "<", $fp) || die "$0 : Can't open ${fp} ! : $!";
while (my $line = <$stream>) {
push @tmp, $line;
}
close($stream) || warn "$0 : Can't close ${fp} ! : $!";
open($stream, ">", $fp) || die "$0 : Can't open ${fp} ! : $!";
for my $line (@tmp) {
for my $old (@olds) {
my $new = $old =~ s/([A-Z])(?=[a-z0-9]*)/_\l${1}/gr;
$line =~ s/${old}/${new}/g;
}
print $stream $line;
}
close($stream) || warn "$0 : Can't close ${fp} ! : $!";
}
# $FILE_NAME => void
sub push_filenames {
my $file_name=$_;
my $full_name=$File::Find::name;
push(@all_files, $full_name) if -f $file_name;
if (-d $file_name){
push(@all_dirs, $full_name);
if ($file_name !~ m/^\.$|^[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*$/) {
my $is_copy = 0;
if ($#bad_names) {
for my $name (@bad_names) {
$is_copy = 1 if "$name" eq "$file_name";
}
}
push(@bad_names, $file_name) if ! $is_copy;
push(@bad_dirs, $full_name);
}
}
}
find(\&push_filenames, (PATHS));
print GREEN."Files : ".RESET.$#all_files."\n";
print GREEN."Directories : ".RESET."$#all_dirs"."\n";
print GREEN."Bad directories :\n".RESET;
for my $bad_dir (@bad_dirs) {
print " ${bad_dir}\n";
}
print GREEN."Bad names :\n".RESET;
for my $bad_name (@bad_names) {
print " ${bad_name}\n";
}
if (EX_FLAG) {
for my $file (@all_files) {
fix_pkg_names($file, @bad_names);
}
}
print(GREEN."Renamed directories :\n".RESET);
for my $bad_dir (reverse(@bad_dirs)) {
my $good_dir =
$bad_dir =~
s/(?<=\/)([A-Za-z0-9]*)([A-Z])([A-Za-z0-9]*)$/${1}_\l${2}${3}/gr;
print("${bad_dir} ".YELLOW."=>".RESET." ${good_dir}\n");
move("$bad_dir", "$good_dir") if EX_FLAG;
}
@FYHenry
Copy link
Author

FYHenry commented Jun 16, 2024

The program rename_packages.pl renames Java packages to kebab case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment