-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructorGenerator.pl
171 lines (127 loc) · 4.13 KB
/
constructorGenerator.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use strict;
use warnings;
use Getopt::Std;
use 5.018;
use vars qw($VERSION %HEADER);
local $SIG{__WARN__} = sub { }; # Supress warnings.
$VERSION = "1.13-1";
%HEADER=(
author => 'Micael Levi L. C.',
contact => 'mllc<a>icomp.ufam.edu.br',
name => 'constructorGenerator',
description => 'Imprime (STDOUT) um metodo construtor parametrizado, para codigos em Java',
url => 'http://github.com/micalevisk',
changed => '11-13-2016'
);
################# [ GLOBAIS ] #################
my $BLUE="\033[36;1m";
my $RED="\033[40;31m";
my $YELLOW="\033[40;33m";
my $WHITE="\033[40;37;1m";
my $PURPLE="\033[0;35m";
my $NC="\033[m";
my $classe = ""; # nome do construtor.
my @PARAMETROS = (); # parâmetros do construtor.
my @CORPO = (); # conteúdo do escopo do construtor.
my %MODIFICADORES_ACESSO = (0 => 'private', 1 => 'public', 2 => 'protected', 3 => 'default');
my $modificador = "";
my $identacao="\t\t";
###############################################
################# [ DEFAULT ] #################
my $delim=':'; # variableName${delim}typeVariable
my $quiet=0; # não inserir o 'Auto-generated'
my $oneLine=0; # a função terá apenas uma linha
my $acesso='1';
###############################################
###########################################################
############### [ LENDO E DEFININDO PÇÕES ] ###############
###########################################################
my %options=();
getopts('hqld:m:', \%options);
help(), if defined($options{h});
$delim = $options{d}, if defined($options{d});
$quiet = $options{q}, if defined($options{q});
$oneLine = $options{l}, $identacao="\t", if defined($options{l});
$acesso = $options{m}%(keys %MODIFICADORES_ACESSO), if defined($options{m});
###########################################################
####################################
###### VERIFICAÇÃO DA ENTRADA ######
####################################
my $nomeArquivo = $0; $nomeArquivo =~ s/^.\///;
my $nArgs = $#ARGV + 1;
if($nArgs < 2){
showDetails();
help();
}
my $argumentos = join("," ,@ARGV);
help(), if( $argumentos !~ m/(\w+)(?:\.java)?,(.+)/i );
####################################
####### DEFININDO VARIÁVEIS #######
####################################
$classe="$1";
@PARAMETROS=split(/,/, $2);
foreach (@PARAMETROS){
next, if( $_ !~ m/^([^$delim]+)$delim([^$delim]+)$/ );
my $name = $1;
my $type = $2;
my $linha = "${identacao}this.$name = $name;";
push(@CORPO, $linha);
$_ =~ s/.*/$type $name/;
}
###################################################
############ IMPRESSÃO DO RESULTADO ###############
###################################################
# print "\tpublic";
print "\t";
print "$modificador " if defined( $modificador = $MODIFICADORES_ACESSO{$acesso} );
print "$classe(", join(", ", @PARAMETROS), ")"; ### prototipo.
print "{";
if($oneLine){
print join("", @CORPO);
}
else{
print "\n";
print "\t\t// TODO Auto-generated constructor method.\n", if not $quiet;
print join("\n", @CORPO);
printf "\n";
}
print "\t";
print "}";
print "\n";
############################################
############## FUNÇÕES EXTRAS ##############
############################################
sub cor {
return "$_[0]$_[1]$NC";
}
sub showDetails {
# system "sed -n '/%HEADER=/,/);/{/%HEADER=/b; /);/q; p;}' $nomeArquivo";
foreach my $key (sort (keys(%HEADER))) { print cor($PURPLE, $key)," ",cor($WHITE, $HEADER{$key}), "\n"; }
# while( my( $key, $value ) = each %HEADER ){ print "$key: $value\n"; }
}
sub help {
print <<EOL;
USO:
====
perl $BLUE$0$NC $YELLOW [-q | -l] [-d DELIMITER] [-m (0..3)]$NC $RED filename[.java] <var1Name>DELIMITER<var1Type> ...$NC
EXEMPLO DE USO:
===============
\$ perl $0 -q Mestre.java nome:String anoNascimento:int afiliacao:String posto:String
SAIDA:
======
public Mestre(String nome, int anoNascimento, String afiliacao, String posto){
\tthis.nome = nome;
\tthis.anoNascimento = anoNascimento;
\tthis.afiliacao = afiliacao;
\tthis.posto = posto;
}
EOL
exit 1;
}
q# ------------------------ NOTES ------------------------ #//q#
//
// [constructorGenerator.pl]
// Created by Micael Levi on 10/22/2016
// Copyright (c) 2016 mllc@icomp.ufam.edu.br; All rights reserved.
//
#;