1 | import { Erro, Severity } from './models/Erro';
|
2 | import { Duplicados } from './models/Duplicados';
|
3 | import { Fonte, Funcao, Tipos } from './fonte';
|
4 | import { ItemModel } from './models/ItemProject';
|
5 | import * as globby from 'globby';
|
6 | import * as fileSystem from 'fs';
|
7 | import { ValidaAdvpl } from './validaAdvpl';
|
8 | import { version } from './package.json';
|
9 |
|
10 | export class ValidaProjeto {
|
11 | public projeto: ItemModel[];
|
12 | public comentFontPad: string[];
|
13 | public ownerDb: string[];
|
14 | public empresas: string[];
|
15 | public local;
|
16 | public version: string = version;
|
17 | private advplExtensions = ['prw', 'prx', 'prg', 'apw', 'apl', 'tlpp'];
|
18 | protected listaDuplicados = { files: [], functions: [] };
|
19 |
|
20 | constructor(
|
21 | comentFontePad: string[],
|
22 | local: string,
|
23 | private log: boolean = true
|
24 | ) {
|
25 | this.local = local;
|
26 | this.comentFontPad = comentFontePad;
|
27 | this.ownerDb = [];
|
28 | this.empresas = [];
|
29 | }
|
30 | public validaProjeto(pathsProject: string[]): Promise<ValidaProjeto> {
|
31 | return new Promise((resolve: Function) => {
|
32 | this.projeto = [];
|
33 | let startTime: any = new Date();
|
34 | if (this.log) {
|
35 | console.log(startTime);
|
36 | console.log('Analise de Projeto');
|
37 | }
|
38 |
|
39 | console.log('criando');
|
40 | this.criaPromises(pathsProject, startTime).finally(() => {
|
41 | resolve();
|
42 | });
|
43 | console.log('esperando');
|
44 | });
|
45 | }
|
46 |
|
47 | criaPromises(pathsProject: string[], startTime?: any) {
|
48 | return new Promise((resolve: Function) => {
|
49 | let promisses: Promise<ValidaAdvpl>[] = [];
|
50 |
|
51 |
|
52 | let globexp: any[] = [];
|
53 | for (var i = 0; i < this.advplExtensions.length; i++) {
|
54 | globexp.push(`**/*.${this.advplExtensions[i]}`);
|
55 | }
|
56 |
|
57 | let promissesGlobby = [];
|
58 | for (var i = 0; i < pathsProject.length; i++) {
|
59 | let pathProject: string = pathsProject[i];
|
60 |
|
61 |
|
62 | promissesGlobby.push(
|
63 | globby.default(globexp, {
|
64 | cwd: pathProject,
|
65 | caseSensitiveMatch: false
|
66 | })
|
67 | );
|
68 | }
|
69 |
|
70 | Promise.all(promissesGlobby).then((folder: any[]) => {
|
71 | for (var x = 0; x < folder.length; x++) {
|
72 | let files = folder[x];
|
73 | for (var j = 0; j < files.length; j++) {
|
74 | let fileName: string = files[j];
|
75 | let valida: ValidaAdvpl = new ValidaAdvpl(
|
76 | this.comentFontPad,
|
77 | this.local,
|
78 | this.log
|
79 | );
|
80 | valida.ownerDb = this.ownerDb;
|
81 | valida.empresas = this.empresas;
|
82 |
|
83 | if (this.log) {
|
84 | console.log('Arquivo: ' + fileName);
|
85 | }
|
86 | let conteudo = fileSystem.readFileSync(
|
87 | pathsProject + '\\' + fileName,
|
88 | 'latin1'
|
89 | );
|
90 |
|
91 | promisses.push(
|
92 | valida.validacao(conteudo, pathsProject + '\\' + fileName)
|
93 | );
|
94 | }
|
95 | }
|
96 |
|
97 | Promise.all(promisses).then((validacoes: ValidaAdvpl[]) => {
|
98 | for (var idx = 0; idx < validacoes.length; idx++) {
|
99 | let validacao: ValidaAdvpl = validacoes[idx];
|
100 | let itemProjeto = new ItemModel();
|
101 | itemProjeto.content = validacao.conteudoFonte;
|
102 | itemProjeto.errors = validacao.aErros;
|
103 | itemProjeto.fonte = validacao.fonte;
|
104 |
|
105 | this.projeto.push(itemProjeto);
|
106 | }
|
107 |
|
108 | this.verificaDuplicados().then(() => {
|
109 | if (this.log && startTime) {
|
110 |
|
111 | let endTime: any = new Date();
|
112 | let timeDiff = endTime - startTime;
|
113 |
|
114 | timeDiff /= 1000;
|
115 |
|
116 |
|
117 | let seconds = Math.round(timeDiff);
|
118 | console.log('Terminou! (' + seconds + ' segundos)');
|
119 | resolve();
|
120 | }
|
121 | });
|
122 | });
|
123 | });
|
124 | });
|
125 | }
|
126 |
|
127 | public verificaDuplicados(): Promise<Duplicados> {
|
128 | return new Promise((resolve: Function) => {
|
129 | let startTime: any = new Date();
|
130 | console.log('Start Duplicados');
|
131 | let listaFuncoes: string[] = [];
|
132 | let funcoesDuplicadas: string[] = [];
|
133 | let listaArquivos: string[] = [];
|
134 | let arquivosDuplicados: string[] = [];
|
135 |
|
136 | for (var idx = 0; idx < this.projeto.length; idx++) {
|
137 | let item: ItemModel = this.projeto[idx];
|
138 | let fonte: Fonte = item.fonte;
|
139 |
|
140 | try {
|
141 | fileSystem.statSync(fonte.fonte);
|
142 |
|
143 | for (var idx2 = 0; idx2 < fonte.funcoes.length; idx2++) {
|
144 | let funcao: Funcao = fonte.funcoes[idx2];
|
145 |
|
146 | if (
|
147 | funcao.tipo !== Tipos['Static Function'] &&
|
148 | funcao.tipo !== Tipos.Method
|
149 | ) {
|
150 | let functionName: string = (
|
151 | funcao.nome + funcao.tipo
|
152 | ).toUpperCase();
|
153 |
|
154 | if (listaFuncoes.indexOf(functionName) === -1) {
|
155 | listaFuncoes.push(functionName);
|
156 | } else if (funcoesDuplicadas.indexOf(functionName) === -1) {
|
157 | funcoesDuplicadas.push(functionName);
|
158 | }
|
159 | }
|
160 | }
|
161 |
|
162 | let fileName = fonte.fonte
|
163 | .replace(/\\/g, '/')
|
164 | .substring(fonte.fonte.replace(/\\/g, '/').lastIndexOf('/') + 1)
|
165 | .toUpperCase();
|
166 |
|
167 | if (listaArquivos.indexOf(fileName) === -1) {
|
168 | listaArquivos.push(fileName);
|
169 | } else if (arquivosDuplicados.indexOf(fileName) === -1) {
|
170 | arquivosDuplicados.push(fileName);
|
171 | }
|
172 | } catch (e) {
|
173 | if (e.code === 'ENOENT') {
|
174 | item.content = '';
|
175 | item.errors = [];
|
176 | item.fonte.funcoes = [];
|
177 | } else {
|
178 | console.log(`Erro ao validar : ${fonte.fonte}`);
|
179 | console.log(e);
|
180 | }
|
181 | }
|
182 | }
|
183 |
|
184 |
|
185 | let duplicadosOld = JSON.parse(JSON.stringify(this.listaDuplicados));
|
186 | this.listaDuplicados.files = JSON.parse(
|
187 | JSON.stringify(arquivosDuplicados)
|
188 | );
|
189 | this.listaDuplicados.functions = JSON.parse(
|
190 | JSON.stringify(funcoesDuplicadas)
|
191 | );
|
192 |
|
193 |
|
194 | let filesIncluidos = this.listaDuplicados.files.filter(
|
195 | x => duplicadosOld.files.indexOf(x) === -1
|
196 | );
|
197 | let filesExcluidos = duplicadosOld.files.filter(
|
198 | x => this.listaDuplicados.files.indexOf(x) === -1
|
199 | );
|
200 |
|
201 | let functionsIncluidos = this.listaDuplicados.functions.filter(
|
202 | x => duplicadosOld.functions.indexOf(x) === -1
|
203 | );
|
204 | let functionsExcluidos = duplicadosOld.functions.filter(
|
205 | x => this.listaDuplicados.functions.indexOf(x) === -1
|
206 | );
|
207 |
|
208 | // marca duplicados
|
209 | for (var idx = 0; idx < this.projeto.length; idx++) {
|
210 | let item: ItemModel = this.projeto[idx];
|
211 | let fonte: Fonte = item.fonte;
|
212 |
|
213 | for (var idx2 = 0; idx2 < fonte.funcoes.length; idx2++) {
|
214 | let funcao: Funcao = fonte.funcoes[idx2];
|
215 | let functionName: string = (funcao.nome + funcao.tipo).toUpperCase();
|
216 |
|
217 | if (functionsIncluidos.indexOf(functionName) > -1) {
|
218 | item.errors.push(
|
219 | new Erro(
|
220 | funcao.linha,
|
221 | funcao.linha,
|
222 | traduz('validaAdvpl.functionDuplicate', this.local),
|
223 | Severity.Error
|
224 | )
|
225 | );
|
226 | }
|
227 | if (functionsExcluidos.indexOf(functionName) > -1) {
|
228 | item.errors = item.errors.filter((erro: Erro) => {
|
229 | return (
|
230 | erro.message !==
|
231 | traduz('validaAdvpl.functionDuplicate', this.local) ||
|
232 | funcao.linha !== erro.startLine
|
233 | );
|
234 | });
|
235 | }
|
236 | }
|
237 |
|
238 | let fileName = fonte.fonte
|
239 | .replace(/\\/g, '/')
|
240 | .substring(fonte.fonte.replace(/\\/g, '/').lastIndexOf('/') + 1)
|
241 | .toUpperCase();
|
242 |
|
243 | if (filesIncluidos.indexOf(fileName) > -1) {
|
244 | item.errors.push(
|
245 | new Erro(
|
246 | 0,
|
247 | 0,
|
248 | traduz('validaAdvpl.fileDuplicate', this.local),
|
249 | Severity.Error
|
250 | )
|
251 | );
|
252 | } else if (filesExcluidos.indexOf(fileName) > -1) {
|
253 | item.errors = item.errors.filter((erro: Erro) => {
|
254 | return (
|
255 | erro.message !== traduz('validaAdvpl.fileDuplicate', this.local)
|
256 | );
|
257 | });
|
258 | }
|
259 | }
|
260 | if (this.log) {
|
261 | let errosContagem: any = this.contaErros();
|
262 |
|
263 | console.log(`\t${errosContagem.errors} Errors`);
|
264 | console.log(`\t${errosContagem.warnings} Warnings`);
|
265 | console.log(`\t${errosContagem.information} Informations`);
|
266 | console.log(`\t${errosContagem.hint} Hints`);
|
267 | }
|
268 | if (this.log) {
|
269 |
|
270 | let endTime: any = new Date();
|
271 | let timeDiff = endTime - startTime;
|
272 |
|
273 | timeDiff /= 1000;
|
274 |
|
275 |
|
276 | let seconds = Math.round(timeDiff);
|
277 | console.log('Terminou! (' + seconds + ' segundos) Duplicados');
|
278 | }
|
279 | resolve();
|
280 | });
|
281 | }
|
282 | public contaErros(): any {
|
283 | let erros: any = { errors: 0, warnings: 0, information: 0, hint: 0 };
|
284 |
|
285 | for (var idx = 0; idx < this.projeto.length; idx++) {
|
286 | let item: ItemModel = this.projeto[idx];
|
287 | for (var idx2 = 0; idx2 < item.errors.length; idx2++) {
|
288 | let erro: Erro = item.errors[idx2];
|
289 | if (erro.severity === Severity.Error) {
|
290 | erros.errors++;
|
291 | } else if (erro.severity === Severity.Warning) {
|
292 | erros.warnings++;
|
293 | } else if (erro.severity === Severity.Information) {
|
294 | erros.information++;
|
295 | } else if (erro.severity === Severity.Hint) {
|
296 | erros.hint++;
|
297 | }
|
298 | }
|
299 | }
|
300 | return erros;
|
301 | }
|
302 | }
|
303 |
|
304 | function traduz(key, local) {
|
305 | let locales: string[] = ['en', 'pt-br'];
|
306 | let i18n = require('i18n');
|
307 | i18n.configure({
|
308 | locales: locales,
|
309 | directory: __dirname + '/locales'
|
310 | });
|
311 | i18n.setLocale(locales.indexOf(local) + 1 ? local : 'en');
|
312 | return i18n.__(key);
|
313 | }
|