@semacode/cli 1.5.16 → 1.5.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +272 -0
- package/LICENSE +22 -0
- package/SEMA_BRIEF.curto.txt +9 -0
- package/SEMA_BRIEF.md +70 -0
- package/SEMA_BRIEF.micro.txt +7 -0
- package/SEMA_INDEX.json +921 -0
- package/dist/angular-consumer-standalone.d.ts +6 -0
- package/dist/angular-consumer-standalone.js.map +1 -0
- package/dist/cpp-symbols.d.ts +10 -0
- package/dist/cpp-symbols.js.map +1 -0
- package/dist/docs.d.ts +56 -0
- package/dist/docs.js.map +1 -0
- package/dist/dotnet-http.d.ts +23 -0
- package/dist/dotnet-http.js.map +1 -0
- package/dist/drift.d.ts +225 -0
- package/dist/drift.js.map +1 -0
- package/dist/go-http.d.ts +23 -0
- package/dist/go-http.js.map +1 -0
- package/dist/importador.d.ts +31 -0
- package/dist/importador.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js.map +1 -0
- package/dist/java-http.d.ts +23 -0
- package/dist/java-http.js.map +1 -0
- package/dist/lua-symbols.d.ts +10 -0
- package/dist/lua-symbols.js.map +1 -0
- package/dist/php-symbols.d.ts +24 -0
- package/dist/php-symbols.js.map +1 -0
- package/dist/projeto.d.ts +53 -0
- package/dist/projeto.js.map +1 -0
- package/dist/python-http.d.ts +23 -0
- package/dist/python-http.js.map +1 -0
- package/dist/rust-http.d.ts +23 -0
- package/dist/rust-http.js.map +1 -0
- package/dist/tipos.d.ts +3 -0
- package/dist/tipos.js.map +1 -0
- package/dist/typescript-http.d.ts +35 -0
- package/dist/typescript-http.js.map +1 -0
- package/docs/AGENT_STARTER.md +102 -0
- package/docs/cli.md +119 -0
- package/docs/como-ensinar-a-sema-para-ia.md +149 -0
- package/docs/deploy.md +70 -0
- package/docs/documentacao.md +63 -0
- package/docs/env.md +56 -0
- package/docs/extensao-vscode.md +45 -0
- package/docs/fluxo-pratico-ia-sema.md +177 -0
- package/docs/instalacao-e-primeiro-uso.md +112 -0
- package/docs/integracao-com-ia.md +101 -0
- package/docs/mcp.md +53 -0
- package/docs/pagamento-ponta-a-ponta.md +155 -0
- package/docs/persistencia-vendor-first.md +145 -0
- package/docs/prompt-base-ia-sema.md +104 -0
- package/docs/rollback.md +47 -0
- package/docs/sintaxe.md +410 -0
- package/exemplos/agendamento.sema +106 -0
- package/exemplos/assinatura.sema +136 -0
- package/exemplos/auditoria.sema +88 -0
- package/exemplos/autenticacao.sema +125 -0
- package/exemplos/automacao.sema +107 -0
- package/exemplos/cadastro_usuario.sema +54 -0
- package/exemplos/calculadora.sema +78 -0
- package/exemplos/crud_simples.sema +89 -0
- package/exemplos/estoque.sema +126 -0
- package/exemplos/exportacao.sema +94 -0
- package/exemplos/fila.sema +131 -0
- package/exemplos/integracao_externa.sema +94 -0
- package/exemplos/multi_tenant.sema +140 -0
- package/exemplos/notificacao.sema +98 -0
- package/exemplos/operacao_estrategia.sema +402 -0
- package/exemplos/pagamento.sema +222 -0
- package/exemplos/pagamento_dominio.sema +35 -0
- package/exemplos/pedido.sema +119 -0
- package/exemplos/permissao.sema +121 -0
- package/exemplos/persistencia_vendor_first.sema +86 -0
- package/exemplos/relatorio.sema +93 -0
- package/exemplos/testes_embutidos.sema +45 -0
- package/exemplos/tratamento_erro.sema +157 -0
- package/exemplos/upload_arquivo.sema +93 -0
- package/exemplos/webhook.sema +96 -0
- package/llms-full.txt +34 -0
- package/llms.txt +17 -0
- package/node_modules/@sema/gerador-css/dist/index.d.ts +3 -0
- package/node_modules/@sema/gerador-css/dist/index.js +592 -0
- package/node_modules/@sema/gerador-css/dist/index.js.map +1 -0
- package/node_modules/@sema/gerador-css/package.json +7 -0
- package/node_modules/@sema/gerador-dart/dist/index.d.ts +3 -0
- package/node_modules/@sema/gerador-dart/dist/index.js +44 -0
- package/node_modules/@sema/gerador-dart/dist/index.js.map +1 -0
- package/node_modules/@sema/gerador-dart/package.json +7 -0
- package/node_modules/@sema/gerador-html/dist/index.d.ts +3 -0
- package/node_modules/@sema/gerador-html/dist/index.js +163 -0
- package/node_modules/@sema/gerador-html/dist/index.js.map +1 -0
- package/node_modules/@sema/gerador-html/package.json +7 -0
- package/node_modules/@sema/gerador-javascript/dist/index.d.ts +3 -0
- package/node_modules/@sema/gerador-javascript/dist/index.js +421 -0
- package/node_modules/@sema/gerador-javascript/dist/index.js.map +1 -0
- package/node_modules/@sema/gerador-javascript/package.json +7 -0
- package/node_modules/@sema/gerador-lua/dist/index.d.ts +3 -0
- package/node_modules/@sema/gerador-lua/dist/index.js +328 -0
- package/node_modules/@sema/gerador-lua/dist/index.js.map +1 -0
- package/node_modules/@sema/gerador-lua/package.json +7 -0
- package/node_modules/@sema/gerador-python/dist/index.d.ts +6 -0
- package/node_modules/@sema/gerador-python/dist/index.js +729 -0
- package/node_modules/@sema/gerador-python/dist/index.js.map +1 -0
- package/node_modules/@sema/gerador-python/package.json +7 -0
- package/node_modules/@sema/gerador-typescript/dist/index.d.ts +6 -0
- package/node_modules/@sema/gerador-typescript/dist/index.js +793 -0
- package/node_modules/@sema/gerador-typescript/dist/index.js.map +1 -0
- package/node_modules/@sema/gerador-typescript/package.json +7 -0
- package/node_modules/@sema/nucleo/dist/ast/tipos.d.ts +125 -0
- package/node_modules/@sema/nucleo/dist/ast/tipos.js +2 -0
- package/node_modules/@sema/nucleo/dist/ast/tipos.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/diagnosticos/index.d.ts +21 -0
- package/node_modules/@sema/nucleo/dist/diagnosticos/index.js +12 -0
- package/node_modules/@sema/nucleo/dist/diagnosticos/index.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/formatador/index.d.ts +9 -0
- package/node_modules/@sema/nucleo/dist/formatador/index.js +488 -0
- package/node_modules/@sema/nucleo/dist/formatador/index.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/index.d.ts +35 -0
- package/node_modules/@sema/nucleo/dist/index.js +96 -0
- package/node_modules/@sema/nucleo/dist/index.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/ir/conversor.d.ts +5 -0
- package/node_modules/@sema/nucleo/dist/ir/conversor.js +1072 -0
- package/node_modules/@sema/nucleo/dist/ir/conversor.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/ir/modelos.d.ts +377 -0
- package/node_modules/@sema/nucleo/dist/ir/modelos.js +2 -0
- package/node_modules/@sema/nucleo/dist/ir/modelos.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/lexer/lexer.d.ts +7 -0
- package/node_modules/@sema/nucleo/dist/lexer/lexer.js +122 -0
- package/node_modules/@sema/nucleo/dist/lexer/lexer.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/lexer/tokens.d.ts +8 -0
- package/node_modules/@sema/nucleo/dist/lexer/tokens.js +82 -0
- package/node_modules/@sema/nucleo/dist/lexer/tokens.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/parser/parser.d.ts +9 -0
- package/node_modules/@sema/nucleo/dist/parser/parser.js +807 -0
- package/node_modules/@sema/nucleo/dist/parser/parser.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/persistencia/contratos.d.ts +39 -0
- package/node_modules/@sema/nucleo/dist/persistencia/contratos.js +294 -0
- package/node_modules/@sema/nucleo/dist/persistencia/contratos.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/semantico/analisador.d.ts +58 -0
- package/node_modules/@sema/nucleo/dist/semantico/analisador.js +1912 -0
- package/node_modules/@sema/nucleo/dist/semantico/analisador.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/semantico/estruturas.d.ts +104 -0
- package/node_modules/@sema/nucleo/dist/semantico/estruturas.js +445 -0
- package/node_modules/@sema/nucleo/dist/semantico/estruturas.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/semantico/seguranca.d.ts +91 -0
- package/node_modules/@sema/nucleo/dist/semantico/seguranca.js +258 -0
- package/node_modules/@sema/nucleo/dist/semantico/seguranca.js.map +1 -0
- package/node_modules/@sema/nucleo/dist/util/arquivos.d.ts +2 -0
- package/node_modules/@sema/nucleo/dist/util/arquivos.js +25 -0
- package/node_modules/@sema/nucleo/dist/util/arquivos.js.map +1 -0
- package/node_modules/@sema/nucleo/package.json +7 -0
- package/node_modules/@sema/padroes/dist/index.d.ts +25 -0
- package/node_modules/@sema/padroes/dist/index.js +316 -0
- package/node_modules/@sema/padroes/dist/index.js.map +1 -0
- package/node_modules/@sema/padroes/package.json +7 -0
- package/package.json +35 -26
- package/semacode-cli-1.3.1.tgz +0 -0
- package/src/angular-consumer-standalone.ts +0 -312
- package/src/cpp-symbols.ts +0 -82
- package/src/docs.ts +0 -535
- package/src/dotnet-http.ts +0 -355
- package/src/drift.ts +0 -4933
- package/src/go-http.ts +0 -118
- package/src/importador.ts +0 -3891
- package/src/index.ts +0 -5641
- package/src/java-http.ts +0 -247
- package/src/lua-symbols.ts +0 -114
- package/src/php-symbols.ts +0 -462
- package/src/projeto.ts +0 -862
- package/src/python-http.ts +0 -258
- package/src/rust-http.ts +0 -125
- package/src/tipos.ts +0 -24
- package/src/typescript-http.ts +0 -1076
- package/tsconfig.json +0 -20
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { mapearTipoParaInputHtml, normalizarNomeModulo, normalizarNomeParaSimbolo, } from "@sema/padroes";
|
|
2
|
+
function escaparHtml(texto) {
|
|
3
|
+
return texto
|
|
4
|
+
.replace(/&/g, "&")
|
|
5
|
+
.replace(/</g, "<")
|
|
6
|
+
.replace(/>/g, ">")
|
|
7
|
+
.replace(/"/g, """);
|
|
8
|
+
}
|
|
9
|
+
function gerarCampoInput(campo, nivel) {
|
|
10
|
+
const indent = " ".repeat(nivel);
|
|
11
|
+
const tipo = campo.tipo;
|
|
12
|
+
const tipoInput = mapearTipoParaInputHtml(tipo);
|
|
13
|
+
const obrigatorio = campo.modificadores.includes("required");
|
|
14
|
+
const atributos = obrigatorio ? ' required' : '';
|
|
15
|
+
if (tipoInput === "textarea") {
|
|
16
|
+
return `${indent}<div class="sema-campo">
|
|
17
|
+
${indent} <label for="campo-${campo.nome}">${campo.nome}${obrigatorio ? ' <span class="sema-obrigatorio">*</span>' : ''}</label>
|
|
18
|
+
${indent} <textarea id="campo-${campo.nome}" name="${campo.nome}" data-tipo-sema="${tipo}"${atributos}></textarea>
|
|
19
|
+
${indent}</div>`;
|
|
20
|
+
}
|
|
21
|
+
if (tipoInput === "checkbox") {
|
|
22
|
+
return `${indent}<div class="sema-campo sema-campo-checkbox">
|
|
23
|
+
${indent} <label>
|
|
24
|
+
${indent} <input type="checkbox" id="campo-${campo.nome}" name="${campo.nome}" data-tipo-sema="${tipo}">
|
|
25
|
+
${indent} <span>${campo.nome}</span>
|
|
26
|
+
${indent} </label>
|
|
27
|
+
${indent}</div>`;
|
|
28
|
+
}
|
|
29
|
+
return `${indent}<div class="sema-campo">
|
|
30
|
+
${indent} <label for="campo-${campo.nome}">${campo.nome}${obrigatorio ? ' <span class="sema-obrigatorio">*</span>' : ''}</label>
|
|
31
|
+
${indent} <input type="${tipoInput}" id="campo-${campo.nome}" name="${campo.nome}" data-tipo-sema="${tipo}"${atributos}>
|
|
32
|
+
${indent}</div>`;
|
|
33
|
+
}
|
|
34
|
+
function gerarTabelaEntity(entity) {
|
|
35
|
+
const cabecalho = entity.campos.map((campo) => ` <th>${campo.nome} <small>(${campo.tipo})</small></th>`).join("\n");
|
|
36
|
+
return ` <section class="sema-entity" data-entity="${entity.nome}">
|
|
37
|
+
<h3>${entity.nome}</h3>
|
|
38
|
+
<table class="sema-tabela">
|
|
39
|
+
<thead>
|
|
40
|
+
<tr>
|
|
41
|
+
${cabecalho}
|
|
42
|
+
</tr>
|
|
43
|
+
</thead>
|
|
44
|
+
<tbody>
|
|
45
|
+
<tr>
|
|
46
|
+
${entity.campos.map((campo) => ` <td data-campo="${campo.nome}">—</td>`).join("\n")}
|
|
47
|
+
</tr>
|
|
48
|
+
</tbody>
|
|
49
|
+
</table>
|
|
50
|
+
</section>`;
|
|
51
|
+
}
|
|
52
|
+
function gerarFormularioTask(task) {
|
|
53
|
+
const campos = task.input.map((campo) => gerarCampoInput(campo, 2)).join("\n\n");
|
|
54
|
+
const erros = Object.entries(task.errors);
|
|
55
|
+
const erroHtml = erros.length > 0
|
|
56
|
+
? `\n <div class="sema-erros" data-task="${task.nome}">\n${erros.map(([codigo, mensagem]) => ` <div class="sema-erro" data-erro="${codigo}" hidden>${escaparHtml(mensagem)}</div>`).join("\n")}\n </div>`
|
|
57
|
+
: "";
|
|
58
|
+
return ` <section class="sema-task" data-task="${task.nome}">
|
|
59
|
+
<h3>Task: ${task.nome}</h3>
|
|
60
|
+
<form id="form-${normalizarNomeParaSimbolo(task.nome)}" class="sema-formulario" data-task="${task.nome}">
|
|
61
|
+
<fieldset>
|
|
62
|
+
<legend>Entrada</legend>
|
|
63
|
+
${campos}
|
|
64
|
+
</fieldset>
|
|
65
|
+
<button type="submit" class="sema-botao">Executar ${task.nome}</button>
|
|
66
|
+
</form>${erroHtml}
|
|
67
|
+
<div class="sema-saida" data-task-saida="${task.nome}" hidden>
|
|
68
|
+
<h4>Saida</h4>
|
|
69
|
+
${task.output.map((campo) => ` <div class="sema-campo-saida" data-campo="${campo.nome}"><strong>${campo.nome}</strong>: <span>—</span></div>`).join("\n")}
|
|
70
|
+
</div>
|
|
71
|
+
</section>`;
|
|
72
|
+
}
|
|
73
|
+
function gerarEnumSelect(enumeracao) {
|
|
74
|
+
const opcoes = enumeracao.valores.map((valor) => ` <option value="${valor}">${valor}</option>`).join("\n");
|
|
75
|
+
return ` <section class="sema-enum" data-enum="${enumeracao.nome}">
|
|
76
|
+
<h3>Enum: ${enumeracao.nome}</h3>
|
|
77
|
+
<select id="enum-${normalizarNomeParaSimbolo(enumeracao.nome)}" class="sema-select" data-enum="${enumeracao.nome}">
|
|
78
|
+
<option value="">— Selecionar —</option>
|
|
79
|
+
${opcoes}
|
|
80
|
+
</select>
|
|
81
|
+
</section>`;
|
|
82
|
+
}
|
|
83
|
+
function gerarState(state) {
|
|
84
|
+
const transicoes = state.transicoes.length > 0
|
|
85
|
+
? state.transicoes.map((transicao) => ` <li class="sema-transicao"><span class="sema-estado-origem">${transicao.origem}</span> → <span class="sema-estado-destino">${transicao.destino}</span></li>`).join("\n")
|
|
86
|
+
: ' <li>Nenhuma transicao declarada.</li>';
|
|
87
|
+
return ` <section class="sema-state" data-state="${state.nome ?? "anonimo"}">
|
|
88
|
+
<h3>State${state.nome ? `: ${state.nome}` : ""}</h3>
|
|
89
|
+
<ul class="sema-transicoes">
|
|
90
|
+
${transicoes}
|
|
91
|
+
</ul>
|
|
92
|
+
</section>`;
|
|
93
|
+
}
|
|
94
|
+
function gerarFlow(flow) {
|
|
95
|
+
const etapas = flow.etapasEstruturadas.map((etapa) => {
|
|
96
|
+
const destinos = [];
|
|
97
|
+
if (etapa.emSucesso) {
|
|
98
|
+
destinos.push(`sucesso → ${etapa.emSucesso}`);
|
|
99
|
+
}
|
|
100
|
+
if (etapa.emErro) {
|
|
101
|
+
destinos.push(`erro → ${etapa.emErro}`);
|
|
102
|
+
}
|
|
103
|
+
return ` <li class="sema-etapa" data-etapa="${etapa.nome}" data-task="${etapa.task ?? ""}">
|
|
104
|
+
<strong>${etapa.nome}</strong>${etapa.task ? ` (usa ${etapa.task})` : ""}${destinos.length > 0 ? ` [${destinos.join(", ")}]` : ""}
|
|
105
|
+
</li>`;
|
|
106
|
+
}).join("\n");
|
|
107
|
+
return ` <section class="sema-flow" data-flow="${flow.nome}">
|
|
108
|
+
<h3>Flow: ${flow.nome}</h3>
|
|
109
|
+
<ol class="sema-etapas">
|
|
110
|
+
${etapas || ' <li>Nenhuma etapa estruturada.</li>'}
|
|
111
|
+
</ol>
|
|
112
|
+
</section>`;
|
|
113
|
+
}
|
|
114
|
+
function gerarRoute(route) {
|
|
115
|
+
return ` <section class="sema-route" data-route="${route.nome}">
|
|
116
|
+
<h3>Route: ${route.nome}</h3>
|
|
117
|
+
<div class="sema-route-detalhe">
|
|
118
|
+
<span class="sema-route-metodo">${route.metodo ?? "POST"}</span>
|
|
119
|
+
<code class="sema-route-caminho">${route.caminho ?? "/"}</code>
|
|
120
|
+
${route.task ? `<span class="sema-route-task">→ ${route.task}</span>` : ""}
|
|
121
|
+
</div>
|
|
122
|
+
${route.inputPublico.length > 0 ? `<details class="sema-route-io"><summary>Input publico</summary><ul>${route.inputPublico.map((campo) => `<li>${campo.nome}: ${campo.tipo}${campo.modificadores.includes("required") ? " (obrigatorio)" : ""}</li>`).join("")}</ul></details>` : ""}
|
|
123
|
+
${route.outputPublico.length > 0 ? `<details class="sema-route-io"><summary>Output publico</summary><ul>${route.outputPublico.map((campo) => `<li>${campo.nome}: ${campo.tipo}</li>`).join("")}</ul></details>` : ""}
|
|
124
|
+
</section>`;
|
|
125
|
+
}
|
|
126
|
+
export function gerarHtml(modulo) {
|
|
127
|
+
const nomeBase = normalizarNomeModulo(modulo.nome).replace(/\./g, "_");
|
|
128
|
+
const titulo = modulo.nome.replace(/\./g, " › ");
|
|
129
|
+
const entidades = modulo.entities.map(gerarTabelaEntity).join("\n\n");
|
|
130
|
+
const enums = modulo.enums.map(gerarEnumSelect).join("\n\n");
|
|
131
|
+
const states = modulo.states.map(gerarState).join("\n\n");
|
|
132
|
+
const tasks = modulo.tasks.map(gerarFormularioTask).join("\n\n");
|
|
133
|
+
const flows = modulo.flows.map(gerarFlow).join("\n\n");
|
|
134
|
+
const routes = modulo.routes.map(gerarRoute).join("\n\n");
|
|
135
|
+
const html = `<!DOCTYPE html>
|
|
136
|
+
<html lang="pt-BR">
|
|
137
|
+
<head>
|
|
138
|
+
<meta charset="UTF-8">
|
|
139
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
140
|
+
<meta name="description" content="Contrato Sema: ${escaparHtml(modulo.nome)}">
|
|
141
|
+
<title>${escaparHtml(titulo)} — Sema</title>
|
|
142
|
+
<link rel="stylesheet" href="${nomeBase}.css">
|
|
143
|
+
</head>
|
|
144
|
+
<body>
|
|
145
|
+
<header class="sema-header">
|
|
146
|
+
<h1>${escaparHtml(titulo)}</h1>
|
|
147
|
+
<p class="sema-subtitulo">Contrato gerado automaticamente pela Sema.</p>
|
|
148
|
+
</header>
|
|
149
|
+
|
|
150
|
+
<main class="sema-main">
|
|
151
|
+
${entidades ? ` <!-- Entities -->\n${entidades}\n\n` : ""}${enums ? ` <!-- Enums -->\n${enums}\n\n` : ""}${states ? ` <!-- States -->\n${states}\n\n` : ""}${tasks ? ` <!-- Tasks -->\n${tasks}\n\n` : ""}${flows ? ` <!-- Flows -->\n${flows}\n\n` : ""}${routes ? ` <!-- Routes -->\n${routes}\n\n` : ""} </main>
|
|
152
|
+
|
|
153
|
+
<footer class="sema-footer">
|
|
154
|
+
<p>Modulo: <code>${escaparHtml(modulo.nome)}</code> | Gerado pela Sema</p>
|
|
155
|
+
</footer>
|
|
156
|
+
|
|
157
|
+
<script src="${nomeBase}.js" type="module"></script>
|
|
158
|
+
</body>
|
|
159
|
+
</html>
|
|
160
|
+
`;
|
|
161
|
+
return [{ caminhoRelativo: `${nomeBase}.html`, conteudo: html }];
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,uBAAuB,EACvB,oBAAoB,EACpB,yBAAyB,GAE1B,MAAM,eAAe,CAAC;AAEvB,SAAS,WAAW,CAAC,KAAK;IACxB,OAAO,KAAK;SACT,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC7B,CAAC;AAED,SAAS,eAAe,CAAC,KAAK,EAAE,KAAK;IACnC,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACtC,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UAAU,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAE1D,MAAM,IAAI,GAAG;;;;;qDAKsC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC;WAClE,WAAW,CAAC,MAAM,CAAC;iCACG,QAAQ;;;;UAI/B,WAAW,CAAC,MAAM,CAAC;;;;;EAK3B,SAAS,CAAC,CAAC,CAAC,0BAA0B,SAAS,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,uBAAuB,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,wBAAwB,MAAM,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,uBAAuB,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,uBAAuB,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,wBAAwB,MAAM,MAAM,CAAC,CAAC,CAAC,EAAE;;;uBAGrS,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC;;;iBAG9B,QAAQ;;;CAGxB,CAAC;IAEA,OAAO,CAAC,EAAE,eAAe,EAAE,GAAG,QAAQ,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AACnE,CAAC"}
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import { mapearTipoParaJavaScript, normalizarNomeModulo, normalizarNomeParaSimbolo, } from "@sema/padroes";
|
|
2
|
+
const TIPOS_PRIMITIVOS_SEMA = new Set(["Texto", "Numero", "Inteiro", "Decimal", "Booleano", "Data", "DataHora", "Id", "Email", "Url", "Json", "Vazio"]);
|
|
3
|
+
function coletarTiposExternos(modulo) {
|
|
4
|
+
const locais = new Set([
|
|
5
|
+
...modulo.types.map((item) => item.nome),
|
|
6
|
+
...modulo.entities.map((item) => item.nome),
|
|
7
|
+
...modulo.enums.map((item) => item.nome),
|
|
8
|
+
]);
|
|
9
|
+
const referenciados = new Set();
|
|
10
|
+
const campos = [
|
|
11
|
+
...modulo.entities.flatMap((entity) => entity.campos),
|
|
12
|
+
...modulo.tasks.flatMap((task) => [...task.input, ...task.output]),
|
|
13
|
+
...modulo.routes.flatMap((route) => [...route.inputPublico, ...route.outputPublico]),
|
|
14
|
+
...modulo.states.flatMap((state) => state.campos),
|
|
15
|
+
];
|
|
16
|
+
for (const campo of campos) {
|
|
17
|
+
if (!TIPOS_PRIMITIVOS_SEMA.has(campo.tipo) && !locais.has(campo.tipo)) {
|
|
18
|
+
referenciados.add(campo.tipo);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return [...referenciados].sort((a, b) => a.localeCompare(b, "pt-BR"));
|
|
22
|
+
}
|
|
23
|
+
function gerarJsdocTypedef(nome, campos) {
|
|
24
|
+
if (campos.length === 0) {
|
|
25
|
+
return `/**\n * @typedef {Object} ${nome}\n */\n`;
|
|
26
|
+
}
|
|
27
|
+
const propriedades = campos.map((campo) => {
|
|
28
|
+
const tipo = mapearTipoParaJavaScript(campo.tipo);
|
|
29
|
+
const obrigatorio = campo.modificadores.includes("required");
|
|
30
|
+
return ` * @property {${tipo}} ${obrigatorio ? "" : "["}${campo.nome}${obrigatorio ? "" : "]"}`;
|
|
31
|
+
}).join("\n");
|
|
32
|
+
return `/**\n * @typedef {Object} ${nome}\n${propriedades}\n */\n`;
|
|
33
|
+
}
|
|
34
|
+
function gerarComentarioInvariantes(invariantes) {
|
|
35
|
+
if (invariantes.length === 0) {
|
|
36
|
+
return "";
|
|
37
|
+
}
|
|
38
|
+
return `${invariantes.map((invariante) => `// Invariante: ${invariante.textoOriginal}`).join("\n")}\n`;
|
|
39
|
+
}
|
|
40
|
+
function gerarLiteralCampos(campos) {
|
|
41
|
+
if (campos.length === 0) {
|
|
42
|
+
return "[]";
|
|
43
|
+
}
|
|
44
|
+
return `[\n${campos.map((campo) => ` { nome: "${campo.nome}", tipo: "${campo.tipo}", obrigatorio: ${campo.modificadores.includes("required") ? "true" : "false"} },`).join("\n")}\n]`;
|
|
45
|
+
}
|
|
46
|
+
function gerarLiteralErros(erros) {
|
|
47
|
+
const entradas = Object.entries(erros);
|
|
48
|
+
if (entradas.length === 0) {
|
|
49
|
+
return "{}";
|
|
50
|
+
}
|
|
51
|
+
return `{\n${entradas.map(([nome, mensagem]) => ` ${JSON.stringify(nome)}: ${JSON.stringify(mensagem)},`).join("\n")}\n}`;
|
|
52
|
+
}
|
|
53
|
+
function formatarValorJs(valor, camposConhecidos, variavel) {
|
|
54
|
+
const texto = valor.trim();
|
|
55
|
+
if (/^-?\d+(?:\.\d+)?$/.test(texto)) {
|
|
56
|
+
return texto;
|
|
57
|
+
}
|
|
58
|
+
if (texto === "verdadeiro") {
|
|
59
|
+
return "true";
|
|
60
|
+
}
|
|
61
|
+
if (texto === "falso") {
|
|
62
|
+
return "false";
|
|
63
|
+
}
|
|
64
|
+
if (texto === "nulo") {
|
|
65
|
+
return "null";
|
|
66
|
+
}
|
|
67
|
+
if (camposConhecidos.has(texto.split(".")[0] ?? texto)) {
|
|
68
|
+
return `${variavel}.${texto}`;
|
|
69
|
+
}
|
|
70
|
+
return JSON.stringify(texto);
|
|
71
|
+
}
|
|
72
|
+
function resolverReferenciaJs(referencia, variavel) {
|
|
73
|
+
return `${variavel}.${referencia}`;
|
|
74
|
+
}
|
|
75
|
+
function gerarExpressaoJs(expressao, camposConhecidos, variavel) {
|
|
76
|
+
switch (expressao.tipo) {
|
|
77
|
+
case "existe":
|
|
78
|
+
return `(${resolverReferenciaJs(expressao.alvo, variavel)} !== undefined && ${resolverReferenciaJs(expressao.alvo, variavel)} !== null)`;
|
|
79
|
+
case "comparacao":
|
|
80
|
+
return `(${resolverReferenciaJs(expressao.alvo, variavel)} ${expressao.operador} ${formatarValorJs(expressao.valor, camposConhecidos, variavel)})`;
|
|
81
|
+
case "pertencimento":
|
|
82
|
+
return `([${(expressao.valores ?? []).map((valor) => formatarValorJs(valor, camposConhecidos, variavel)).join(", ")}].includes(${resolverReferenciaJs(expressao.alvo, variavel)}))`;
|
|
83
|
+
case "predicado":
|
|
84
|
+
return "true";
|
|
85
|
+
case "composta":
|
|
86
|
+
return `(${expressao.termos.map((termo) => gerarExpressaoJs(termo, camposConhecidos, variavel)).join(expressao.operadorLogico === "e" ? " && " : " || ")})`;
|
|
87
|
+
case "negacao":
|
|
88
|
+
return `(!${gerarExpressaoJs(expressao.termo, camposConhecidos, variavel)})`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function valorPadraoJs(tipo, nomeCampo) {
|
|
92
|
+
switch (tipo) {
|
|
93
|
+
case "Texto":
|
|
94
|
+
case "Id":
|
|
95
|
+
case "Email":
|
|
96
|
+
case "Url":
|
|
97
|
+
return JSON.stringify(`${nomeCampo}_exemplo`);
|
|
98
|
+
case "Numero":
|
|
99
|
+
case "Inteiro":
|
|
100
|
+
case "Decimal":
|
|
101
|
+
return "1";
|
|
102
|
+
case "Booleano":
|
|
103
|
+
return "false";
|
|
104
|
+
case "Json":
|
|
105
|
+
return "{}";
|
|
106
|
+
default:
|
|
107
|
+
return "{}";
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function formatarLiteralTesteJs(valor, tipoDeclarado) {
|
|
111
|
+
if (["Texto", "Id", "Email", "Url"].includes(tipoDeclarado ?? "")) {
|
|
112
|
+
return JSON.stringify(valor);
|
|
113
|
+
}
|
|
114
|
+
if (["Numero", "Inteiro", "Decimal"].includes(tipoDeclarado ?? "") && /^-?\d+(?:\.\d+)?$/.test(valor)) {
|
|
115
|
+
return valor;
|
|
116
|
+
}
|
|
117
|
+
if ((tipoDeclarado ?? "") === "Booleano") {
|
|
118
|
+
if (valor === "verdadeiro") {
|
|
119
|
+
return "true";
|
|
120
|
+
}
|
|
121
|
+
if (valor === "falso") {
|
|
122
|
+
return "false";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (/^-?\d+(?:\.\d+)?$/.test(valor)) {
|
|
126
|
+
return valor;
|
|
127
|
+
}
|
|
128
|
+
if (valor === "verdadeiro") {
|
|
129
|
+
return "true";
|
|
130
|
+
}
|
|
131
|
+
if (valor === "falso") {
|
|
132
|
+
return "false";
|
|
133
|
+
}
|
|
134
|
+
return JSON.stringify(valor);
|
|
135
|
+
}
|
|
136
|
+
function converterBlocoTesteParaJs(bloco, tiposDeclarados) {
|
|
137
|
+
const entradas = [];
|
|
138
|
+
for (const campo of bloco.campos) {
|
|
139
|
+
entradas.push(`${campo.nome}: ${formatarLiteralTesteJs(campo.tipo, tiposDeclarados?.get(campo.nome))}`);
|
|
140
|
+
}
|
|
141
|
+
for (const subbloco of bloco.blocos) {
|
|
142
|
+
entradas.push(`${subbloco.nome}: ${converterBlocoTesteParaJs(subbloco.conteudo)}`);
|
|
143
|
+
}
|
|
144
|
+
return `{ ${entradas.join(", ")} }`;
|
|
145
|
+
}
|
|
146
|
+
function gerarValidacoes(task) {
|
|
147
|
+
const linhas = [];
|
|
148
|
+
const camposEntrada = new Set(task.input.map((campo) => campo.nome));
|
|
149
|
+
for (const campo of task.input) {
|
|
150
|
+
if (campo.modificadores.includes("required")) {
|
|
151
|
+
linhas.push(` if (entrada.${campo.nome} === undefined || entrada.${campo.nome} === null) throw new Error("Campo obrigatorio ausente: ${campo.nome}");`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (const regra of task.regrasEstruturadas) {
|
|
155
|
+
switch (regra.tipo) {
|
|
156
|
+
case "predicado":
|
|
157
|
+
linhas.push(` // Predicado declarado em Sema: ${regra.textoOriginal}`);
|
|
158
|
+
break;
|
|
159
|
+
default:
|
|
160
|
+
linhas.push(` if (!${gerarExpressaoJs(regra, camposEntrada, "entrada")}) throw new Error("Regra violada: ${regra.textoOriginal}");`);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (const regra of task.rules.filter((regra) => !task.regrasEstruturadas.some((estruturada) => estruturada.textoOriginal === regra))) {
|
|
165
|
+
linhas.push(` // Regra declarada em Sema: ${regra}`);
|
|
166
|
+
}
|
|
167
|
+
return linhas.join("\n");
|
|
168
|
+
}
|
|
169
|
+
function gerarFuncaoGarantias(task) {
|
|
170
|
+
const camposSaida = new Set(task.output.map((campo) => campo.nome));
|
|
171
|
+
const linhas = [];
|
|
172
|
+
for (const garantia of task.garantiasEstruturadas) {
|
|
173
|
+
switch (garantia.tipo) {
|
|
174
|
+
case "predicado":
|
|
175
|
+
linhas.push(` // Predicado de garantia declarado em Sema: ${garantia.textoOriginal}`);
|
|
176
|
+
break;
|
|
177
|
+
default:
|
|
178
|
+
linhas.push(` if (!${gerarExpressaoJs(garantia, camposSaida, "saida")}) throw new Error("Garantia violada: ${garantia.textoOriginal}");`);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (const garantia of task.guarantees.filter((texto) => !task.garantiasEstruturadas.some((estruturada) => estruturada.textoOriginal === texto))) {
|
|
183
|
+
linhas.push(` // Garantia declarada em Sema: ${garantia}`);
|
|
184
|
+
}
|
|
185
|
+
if (linhas.length === 0) {
|
|
186
|
+
linhas.push(" // Nenhuma garantia declarada.");
|
|
187
|
+
}
|
|
188
|
+
return `/**\n * Verifica garantias de saida para ${task.nome}.\n * @param {${task.nome}Saida} saida\n * @returns {void}\n */\nexport function verificar_garantias_${normalizarNomeParaSimbolo(task.nome)}(saida) {\n${linhas.join("\n")}\n}\n`;
|
|
189
|
+
}
|
|
190
|
+
function gerarPreparacaoSaida(task) {
|
|
191
|
+
const camposSaida = new Set(task.output.map((campo) => campo.nome));
|
|
192
|
+
const linhas = [];
|
|
193
|
+
for (const campo of task.output) {
|
|
194
|
+
linhas.push(` ${campo.nome}: ${valorPadraoJs(campo.tipo, campo.nome)},`);
|
|
195
|
+
}
|
|
196
|
+
const ajustes = [];
|
|
197
|
+
for (const garantia of task.garantiasEstruturadas) {
|
|
198
|
+
if (garantia.tipo === "pertencimento" && garantia.valores && camposSaida.has(garantia.alvo)) {
|
|
199
|
+
ajustes.push(` saida.${garantia.alvo} = ${formatarValorJs(garantia.valores[0] ?? "", camposSaida, "saida")};`);
|
|
200
|
+
}
|
|
201
|
+
if (garantia.tipo === "comparacao" && garantia.valor && camposSaida.has(garantia.alvo.split(".")[0] ?? garantia.alvo)) {
|
|
202
|
+
ajustes.push(` ${resolverReferenciaJs(garantia.alvo, "saida")} = ${formatarValorJs(garantia.valor, camposSaida, "saida")};`);
|
|
203
|
+
}
|
|
204
|
+
if (garantia.tipo === "existe" && garantia.alvo.includes(".")) {
|
|
205
|
+
const [raiz, filho] = garantia.alvo.split(".", 2);
|
|
206
|
+
if (raiz && filho && camposSaida.has(raiz)) {
|
|
207
|
+
ajustes.push(` saida.${raiz} = saida.${raiz} ?? {};`);
|
|
208
|
+
ajustes.push(` saida.${raiz}.${filho} = saida.${raiz}.${filho} ?? "valor_garantido";`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return ` const saida = {\n${linhas.join("\n")}\n };\n${ajustes.join("\n")}`;
|
|
213
|
+
}
|
|
214
|
+
function gerarMetadadosTask(task) {
|
|
215
|
+
const efeitos = task.efeitosEstruturados.length === 0
|
|
216
|
+
? "[]"
|
|
217
|
+
: `[\n${task.efeitosEstruturados.map((efeito) => ` { categoria: "${efeito.categoria}", alvo: "${efeito.alvo}"${efeito.detalhe ? `, detalhe: ${JSON.stringify(efeito.detalhe)}` : ""}${efeito.criticidade ? `, criticidade: "${efeito.criticidade}"` : ""} },`).join("\n")}\n]`;
|
|
218
|
+
const implementacoes = task.implementacoesExternas.length === 0
|
|
219
|
+
? "[]"
|
|
220
|
+
: `[\n${task.implementacoesExternas.map((impl) => ` { origem: "${impl.origem}", caminho: "${impl.caminho}", resolucaoImpl: "${impl.resolucaoImpl ?? impl.caminho}", statusImpl: "${impl.statusImpl ?? "nao_verificado"}" },`).join("\n")}\n]`;
|
|
221
|
+
return `export const contrato_${normalizarNomeParaSimbolo(task.nome)} = {
|
|
222
|
+
nome: "${task.nome}",
|
|
223
|
+
input: ${gerarLiteralCampos(task.input)},
|
|
224
|
+
output: ${gerarLiteralCampos(task.output)},
|
|
225
|
+
effects: ${efeitos},
|
|
226
|
+
impl: ${implementacoes},
|
|
227
|
+
errors: ${gerarLiteralErros(task.errors)},
|
|
228
|
+
guarantees: ${JSON.stringify(task.guarantees, null, 2)},
|
|
229
|
+
};
|
|
230
|
+
`;
|
|
231
|
+
}
|
|
232
|
+
function gerarTask(task) {
|
|
233
|
+
const nomeSimbolo = normalizarNomeParaSimbolo(task.nome);
|
|
234
|
+
const errosMapeados = new Map(Object.entries(task.errors));
|
|
235
|
+
for (const caso of task.tests) {
|
|
236
|
+
const tipoErro = caso.error?.campos.find((campo) => campo.nome === "tipo")?.tipo ?? caso.error?.campos[0]?.tipo;
|
|
237
|
+
if (tipoErro && !errosMapeados.has(tipoErro)) {
|
|
238
|
+
errosMapeados.set(tipoErro, `Erro sintetico gerado a partir do caso de teste "${caso.nome}".`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const erros = [...errosMapeados.entries()];
|
|
242
|
+
const tiposEntrada = new Map(task.input.map((campo) => [campo.nome, campo.tipo]));
|
|
243
|
+
const cenariosErro = task.tests
|
|
244
|
+
.filter((caso) => caso.error && caso.error.campos.length > 0)
|
|
245
|
+
.map((caso) => ({
|
|
246
|
+
nome: caso.nome,
|
|
247
|
+
entrada: converterBlocoTesteParaJs(caso.given, tiposEntrada),
|
|
248
|
+
tipoErro: caso.error?.campos.find((campo) => campo.nome === "tipo")?.tipo ?? caso.error?.campos[0]?.tipo,
|
|
249
|
+
}))
|
|
250
|
+
.filter((caso) => caso.tipoErro);
|
|
251
|
+
return `
|
|
252
|
+
${gerarJsdocTypedef(`${task.nome}Entrada`, task.input)}
|
|
253
|
+
${gerarJsdocTypedef(`${task.nome}Saida`, task.output)}
|
|
254
|
+
${erros.map(([nomeErro, mensagem]) => `export class ${task.nome}_${nomeErro}Erro extends Error {
|
|
255
|
+
constructor() {
|
|
256
|
+
super(${JSON.stringify(mensagem)});
|
|
257
|
+
this.name = "${task.nome}_${nomeErro}Erro";
|
|
258
|
+
this.codigo = "${nomeErro}";
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
`).join("\n")}
|
|
262
|
+
${gerarMetadadosTask(task)}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Valida a entrada para ${task.nome}.
|
|
266
|
+
* @param {${task.nome}Entrada} entrada
|
|
267
|
+
* @returns {void}
|
|
268
|
+
*/
|
|
269
|
+
export function validar_${nomeSimbolo}(entrada) {
|
|
270
|
+
${gerarValidacoes(task)}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
${gerarFuncaoGarantias(task)}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Executa a task ${task.nome}.
|
|
277
|
+
* @param {${task.nome}Entrada} entrada
|
|
278
|
+
* @returns {Promise<${task.nome}Saida>}
|
|
279
|
+
*/
|
|
280
|
+
export async function executar_${nomeSimbolo}(entrada) {
|
|
281
|
+
validar_${nomeSimbolo}(entrada);
|
|
282
|
+
${cenariosErro.map((caso) => ` if (JSON.stringify(entrada) === JSON.stringify(${caso.entrada})) throw new ${task.nome}_${caso.tipoErro}Erro();`).join("\n")}
|
|
283
|
+
${task.stateContract ? ` // Vinculo de estado: ${task.stateContract.nomeEstado ?? "nao_definido"}\n // Transicoes declaradas pela task: ${task.stateContract.transicoes.map((transicao) => `${transicao.origem}->${transicao.destino}`).join(", ") || "nenhuma"}` : ""}
|
|
284
|
+
${task.implementacoesExternas.length > 0 ? ` // Implementacoes externas vinculadas:\n${task.implementacoesExternas.map((impl) => ` // - ${impl.origem}: ${impl.caminho} [${impl.statusImpl ?? "nao_verificado"}]`).join("\n")}` : ""}
|
|
285
|
+
// Efeitos declarados:
|
|
286
|
+
${task.efeitosEstruturados.map((efeito) => ` // - categoria=${efeito.categoria} alvo=${efeito.alvo}${efeito.detalhe ? ` detalhe=${efeito.detalhe}` : ""}${efeito.criticidade ? ` criticidade=${efeito.criticidade}` : ""}`).join("\n") || task.effects.map((efeito) => ` // - ${efeito}`).join("\n") || " // - Nenhum efeito declarado."}
|
|
287
|
+
${gerarPreparacaoSaida(task)}
|
|
288
|
+
verificar_garantias_${nomeSimbolo}(saida);
|
|
289
|
+
return saida;
|
|
290
|
+
}
|
|
291
|
+
`;
|
|
292
|
+
}
|
|
293
|
+
function gerarRotas(modulo) {
|
|
294
|
+
const rotasComTask = modulo.routes.filter((route) => route.task);
|
|
295
|
+
if (rotasComTask.length === 0) {
|
|
296
|
+
return "";
|
|
297
|
+
}
|
|
298
|
+
return rotasComTask.map((route) => {
|
|
299
|
+
const taskAssociada = modulo.tasks.find((task) => task.nome === route.task);
|
|
300
|
+
if (!taskAssociada) {
|
|
301
|
+
return "";
|
|
302
|
+
}
|
|
303
|
+
const nomeSimboloRoute = normalizarNomeParaSimbolo(route.nome);
|
|
304
|
+
const nomeSimboloTask = normalizarNomeParaSimbolo(taskAssociada.nome);
|
|
305
|
+
const efeitosPublicos = route.efeitosPublicos.length === 0
|
|
306
|
+
? "[]"
|
|
307
|
+
: `[\n${route.efeitosPublicos.map((efeito) => ` { categoria: "${efeito.categoria}", alvo: "${efeito.alvo}"${efeito.detalhe ? `, detalhe: ${JSON.stringify(efeito.detalhe)}` : ""}${efeito.criticidade ? `, criticidade: "${efeito.criticidade}"` : ""} },`).join("\n")}\n]`;
|
|
308
|
+
return `
|
|
309
|
+
${gerarJsdocTypedef(`${route.nome}EntradaPublica`, route.inputPublico)}
|
|
310
|
+
${gerarJsdocTypedef(`${route.nome}SaidaPublica`, route.outputPublico)}
|
|
311
|
+
|
|
312
|
+
export const contrato_publico_${nomeSimboloRoute} = {
|
|
313
|
+
nome: "${route.nome}",
|
|
314
|
+
metodo: ${JSON.stringify(route.metodo ?? null)},
|
|
315
|
+
caminho: ${JSON.stringify(route.caminho ?? null)},
|
|
316
|
+
task: ${JSON.stringify(route.task ?? null)},
|
|
317
|
+
input: ${gerarLiteralCampos(route.inputPublico)},
|
|
318
|
+
output: ${gerarLiteralCampos(route.outputPublico)},
|
|
319
|
+
effects: ${efeitosPublicos},
|
|
320
|
+
guarantees: ${JSON.stringify(route.garantiasPublicasMinimas, null, 2)},
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Adaptador publico para a rota ${route.nome}.
|
|
325
|
+
* @param {${route.nome}EntradaPublica} requisicao
|
|
326
|
+
* @returns {Promise<{sucesso: boolean, dados?: ${route.nome}SaidaPublica, erro?: {codigo: string, mensagem: string}}>}
|
|
327
|
+
*/
|
|
328
|
+
export async function adaptar_${nomeSimboloRoute}(requisicao) {
|
|
329
|
+
try {
|
|
330
|
+
const saida = await executar_${nomeSimboloTask}(requisicao);
|
|
331
|
+
return {
|
|
332
|
+
sucesso: true,
|
|
333
|
+
dados: {${route.outputPublico.map((campo) => `\n ${campo.nome}: saida.${campo.nome},`).join("")}
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
} catch (erro) {
|
|
337
|
+
throw erro;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
`;
|
|
341
|
+
}).join("\n");
|
|
342
|
+
}
|
|
343
|
+
function gerarTestes(modulo) {
|
|
344
|
+
const classesErro = modulo.tasks.flatMap((task) => {
|
|
345
|
+
const nomes = new Set(Object.keys(task.errors));
|
|
346
|
+
for (const caso of task.tests) {
|
|
347
|
+
const tipoErro = caso.error?.campos.find((campo) => campo.nome === "tipo")?.tipo ?? caso.error?.campos[0]?.tipo;
|
|
348
|
+
if (tipoErro) {
|
|
349
|
+
nomes.add(tipoErro);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return [...nomes].map((nomeErro) => `${task.nome}_${nomeErro}Erro`);
|
|
353
|
+
});
|
|
354
|
+
const nomeModulo = normalizarNomeModulo(modulo.nome).replace(/\./g, "_");
|
|
355
|
+
const linhas = [
|
|
356
|
+
'import test from "node:test";',
|
|
357
|
+
'import assert from "node:assert/strict";',
|
|
358
|
+
`import { ${[...modulo.tasks.map((task) => `executar_${normalizarNomeParaSimbolo(task.nome)}`), ...classesErro].join(", ")} } from "./${nomeModulo}.js";`,
|
|
359
|
+
];
|
|
360
|
+
for (const task of modulo.tasks) {
|
|
361
|
+
const nomeFuncao = `executar_${normalizarNomeParaSimbolo(task.nome)}`;
|
|
362
|
+
const tiposEntrada = new Map(task.input.map((campo) => [campo.nome, campo.tipo]));
|
|
363
|
+
for (const caso of task.tests) {
|
|
364
|
+
const entrada = converterBlocoTesteParaJs(caso.given, tiposEntrada);
|
|
365
|
+
const tipoErro = caso.error?.campos.find((campo) => campo.nome === "tipo")?.tipo ?? caso.error?.campos[0]?.tipo;
|
|
366
|
+
if (tipoErro) {
|
|
367
|
+
linhas.push(`
|
|
368
|
+
test("${task.nome} :: ${caso.nome}", async () => {
|
|
369
|
+
const entrada = ${entrada};
|
|
370
|
+
await assert.rejects(() => ${nomeFuncao}(entrada), ${task.nome}_${tipoErro}Erro);
|
|
371
|
+
});
|
|
372
|
+
`);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
linhas.push(`
|
|
376
|
+
test("${task.nome} :: ${caso.nome}", async () => {
|
|
377
|
+
const entrada = ${entrada};
|
|
378
|
+
const resultado = await ${nomeFuncao}(entrada);
|
|
379
|
+
assert.ok(resultado !== undefined);
|
|
380
|
+
});
|
|
381
|
+
`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return linhas.join("\n");
|
|
385
|
+
}
|
|
386
|
+
export function gerarJavaScript(modulo) {
|
|
387
|
+
const nomeBase = normalizarNomeModulo(modulo.nome).replace(/\./g, "_");
|
|
388
|
+
const interoperabilidades = modulo.interoperabilidades
|
|
389
|
+
.map((interop) => `// Interop externo ${interop.origem}: ${interop.caminho}`)
|
|
390
|
+
.join("\n");
|
|
391
|
+
const tiposExternos = coletarTiposExternos(modulo)
|
|
392
|
+
.map((tipo) => `/** @typedef {Object} ${tipo} Tipo externo referenciado por use ou por contrato compartilhado. */\n`)
|
|
393
|
+
.join("\n");
|
|
394
|
+
const entidades = modulo.entities
|
|
395
|
+
.map((entity) => `${gerarComentarioInvariantes(entity.invariantes)}${gerarJsdocTypedef(entity.nome, entity.campos)}`)
|
|
396
|
+
.join("\n");
|
|
397
|
+
const tipos = modulo.types
|
|
398
|
+
.map((type) => `${gerarComentarioInvariantes(type.invariantes)}${gerarJsdocTypedef(type.nome, type.definicao.campos)}`)
|
|
399
|
+
.join("\n");
|
|
400
|
+
const enums = modulo.enums
|
|
401
|
+
.map((enumeracao) => `/** @enum {string} */\nexport const ${enumeracao.nome} = Object.freeze({\n${enumeracao.valores.map((valor) => ` ${valor}: "${valor}",`).join("\n")}\n});\n`)
|
|
402
|
+
.join("\n");
|
|
403
|
+
const states = modulo.states
|
|
404
|
+
.map((state) => `// State${state.nome ? ` ${state.nome}` : ""}: campos=${state.campos.length} invariantes=${state.invariantes.length} transicoes=${state.transicoes.length}`)
|
|
405
|
+
.join("\n");
|
|
406
|
+
const flows = modulo.flows
|
|
407
|
+
.map((flow) => `// Flow ${flow.nome}: etapas=${flow.linhas.length} estruturadas=${flow.etapasEstruturadas.length} tasks=${flow.tasksReferenciadas.join(", ") || flow.etapasEstruturadas.map((etapa) => etapa.task).filter(Boolean).join(", ") || "nenhuma"} ramificacoes=${flow.etapasEstruturadas.filter((etapa) => etapa.emSucesso || etapa.emErro).length} mapeamentos=${flow.etapasEstruturadas.reduce((total, etapa) => total + etapa.mapeamentos.length, 0)} rotas_erro=${flow.etapasEstruturadas.reduce((total, etapa) => total + etapa.porErro.length, 0)} efeitos=${flow.efeitosEstruturados.map((efeito) => `${efeito.categoria}:${efeito.alvo}`).join(", ") || "nenhum"}`)
|
|
408
|
+
.join("\n");
|
|
409
|
+
const routes = modulo.routes
|
|
410
|
+
.map((route) => `// Route ${route.nome}: metodo=${route.metodo ?? "nao_definido"} caminho=${route.caminho ?? "nao_definido"} task=${route.task ?? "nao_definida"}`)
|
|
411
|
+
.join("\n");
|
|
412
|
+
const tasks = modulo.tasks.map(gerarTask).join("\n");
|
|
413
|
+
const contratosPublicos = gerarRotas(modulo);
|
|
414
|
+
const codigo = `// Arquivo gerado automaticamente pela Sema.\n// Modulo de origem: ${modulo.nome}\n${interoperabilidades ? `${interoperabilidades}\n` : ""}\n${tiposExternos}\n${tipos}\n${entidades}\n${enums}\n${states}\n${flows}\n${routes}\n${tasks}\n${contratosPublicos}\n`;
|
|
415
|
+
const testes = gerarTestes(modulo);
|
|
416
|
+
return [
|
|
417
|
+
{ caminhoRelativo: `${nomeBase}.js`, conteudo: codigo },
|
|
418
|
+
{ caminhoRelativo: `${nomeBase}.test.js`, conteudo: testes },
|
|
419
|
+
];
|
|
420
|
+
}
|
|
421
|
+
//# sourceMappingURL=index.js.map
|