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