major refactor
- change autogenerated folder to autodocs/httpyac - renamed many environment variables - add config section to env template to manage different apps - cleanup bruno environments for easier automation of env files - add pre-request and post-response scripts from bruno - manage EFTL-specific scripts - add scripts to setup json template - add scripts to update environment files
This commit is contained in:
@@ -201,12 +201,24 @@ function Parse-HttpBlock ($text) {
|
||||
$body = $http.body
|
||||
}
|
||||
|
||||
$preRequestScript = $parsed.runtime?.scripts | Where-Object { $_.type -eq 'before-request' } | Select-Object -First 1 -ExpandProperty 'code'
|
||||
# $preRequestScript = $null
|
||||
# if ($parsed.runtime -and ($parsed.runtime -is [psobject] -or $parsed.runtime -is [hashtable])) {
|
||||
# }
|
||||
|
||||
$postResponseScript = $parsed.runtime?.scripts | Where-Object { $_.type -eq 'after-response' } | Select-Object -First 1 -ExpandProperty 'code'
|
||||
# $postResponseScript = $null
|
||||
# if ($parsed.runtime -and ($parsed.runtime -is [psobject] -or $parsed.runtime -is [hashtable])) {
|
||||
# }
|
||||
|
||||
return @{
|
||||
method = $http.method ?? 'GET'
|
||||
url = $http.url ?? ''
|
||||
params = $params
|
||||
headers = $headers
|
||||
body = $body
|
||||
preRequestScript = $preRequestScript
|
||||
postResponseScript = $postResponseScript
|
||||
}
|
||||
}
|
||||
|
||||
@@ -370,7 +382,13 @@ function Get-RequestConfigForFile ($yamlFile, $sourceDir) {
|
||||
$dirChain = @()
|
||||
$currentDir = Split-Path -LiteralPath $yamlFile
|
||||
|
||||
$result = @{}
|
||||
|
||||
while ($true) {
|
||||
if ($currentDir -match [regex]::Escape("EFTL processing")) {
|
||||
$result.hasEftlProcessing = $true
|
||||
}
|
||||
|
||||
$dirChain = ,$currentDir + $dirChain
|
||||
if ($currentDir -eq $sourceDir) {
|
||||
break
|
||||
@@ -390,7 +408,6 @@ function Get-RequestConfigForFile ($yamlFile, $sourceDir) {
|
||||
# Aggiungi il file principale
|
||||
Add-File $yamlFile
|
||||
|
||||
$result = @{}
|
||||
foreach ($config in $resolved.Value) {
|
||||
$result = Merge-RequestConfig $result $config
|
||||
}
|
||||
@@ -513,6 +530,38 @@ function Build-RequestContent ($request, $requestName, $requestConfig = @{}, [re
|
||||
})
|
||||
}
|
||||
|
||||
function Replace-PreRequestEftlScript ($inputText) {
|
||||
# Sostituisce tutto fino al primo [EFTL] con codice specifico
|
||||
$processedPreRequest = ($inputText -replace '^[\s\S]*?(?=\[EFTL\])', "exports.elixBase64EftlDocument = btoa(String.raw```n")
|
||||
# Sostituisce tutto fino al primo [EFTL] con codice specifico
|
||||
$processedPreRequest = ($processedPreRequest -replace '(?s)(.*\[/EFTL\]).*$', "`$1`n``);")
|
||||
return $processedPreRequest
|
||||
}
|
||||
|
||||
function Add-DefaultPostResponseEftlScript {
|
||||
return @"
|
||||
var status = response.statusCode;
|
||||
|
||||
if (status !== 200) {
|
||||
console.log("ERRORE HTTP! Response status code: " + status);
|
||||
return;
|
||||
}
|
||||
|
||||
var decodedDocument = "";
|
||||
var response = response.parsedBody;
|
||||
|
||||
if (response.value.code === "ERROR") {
|
||||
decodedDocument = "ERRORE EFTL!\n";
|
||||
decodedDocument += response.value.description.split("\n").slice(0, 3).join("\n");
|
||||
}
|
||||
else {
|
||||
decodedDocument = atob(response.value.processedDocument);
|
||||
}
|
||||
|
||||
console.log(decodedDocument);
|
||||
"@
|
||||
}
|
||||
|
||||
# -------------------------
|
||||
# Variabili da requestConfig
|
||||
# -------------------------
|
||||
@@ -679,6 +728,24 @@ function Build-RequestContent ($request, $requestName, $requestConfig = @{}, [re
|
||||
}
|
||||
}
|
||||
|
||||
# -------------------------
|
||||
# Super-special treatment for requests belonging to elixForms API v2\EFTL Processing folder
|
||||
# Here we blindly assume that ALL requests must be treated in the same way!
|
||||
# Pre-request script is taken from the original request and manipulated so it can be integrated into the generated .http file
|
||||
# -------------------------
|
||||
if ($request.preRequestScript) {
|
||||
$lines += ''
|
||||
$lines += "{{"
|
||||
$lines += "// Pre-request script"
|
||||
if ($requestConfig.hasEftlProcessing) {
|
||||
$lines += (Replace-PreRequestEftlScript $request.preRequestScript)
|
||||
}
|
||||
else {
|
||||
$lines += $request.preRequestScript
|
||||
}
|
||||
$lines += "}}"
|
||||
}
|
||||
|
||||
# -------------------------
|
||||
# Final request line
|
||||
# -------------------------
|
||||
@@ -703,6 +770,26 @@ function Build-RequestContent ($request, $requestName, $requestConfig = @{}, [re
|
||||
$lines += $requestBody
|
||||
}
|
||||
|
||||
# -------------------------
|
||||
# Super-special treatment for requests belonging to elixForms API v2\EFTL Processing folder
|
||||
# Here we blindly assume that ALL requests must be treated in the same way!
|
||||
# Post-response script is overwritten by default so it should magically work
|
||||
# -------------------------
|
||||
if ($requestConfig.hasEftlProcessing) {
|
||||
$lines += ''
|
||||
$lines += "{{"
|
||||
$lines += "// Post-response script (EFTL)"
|
||||
$lines += Add-DefaultPostResponseEftlScript
|
||||
$lines += "}}"
|
||||
}
|
||||
elseif ($request.postResponseScript) {
|
||||
$lines += ''
|
||||
$lines += "{{"
|
||||
$lines += "// Post-response script"
|
||||
$lines += $request.postResponseScript
|
||||
$lines += "}}"
|
||||
}
|
||||
|
||||
return ($lines -join "`n")
|
||||
}
|
||||
|
||||
@@ -729,7 +816,7 @@ function Clean-Folder($dir) {
|
||||
Remove-Item -LiteralPath $entry.FullName -Force
|
||||
}
|
||||
} else {
|
||||
if (!$entry.PSIsContainer -and $entry.Name -match '\.js$|\.http$|\.env\.template$') {
|
||||
if (!$entry.PSIsContainer -and $entry.Name -match '\.js$|\.http$|^\.env\..*$') {
|
||||
Remove-Item -LiteralPath $entry.FullName -Force
|
||||
}
|
||||
}
|
||||
@@ -922,7 +1009,7 @@ function Invoke-Main {
|
||||
throw "No collections found in workspace.yml"
|
||||
}
|
||||
|
||||
$outputBaseRoot = Join-Path $workspaceRoot 'autogen/httpyac_ps1'
|
||||
$outputBaseRoot = Join-Path $workspaceRoot 'autodocs/httpyac'
|
||||
Clean-Folder $outputBaseRoot
|
||||
|
||||
foreach ($collection in $collections) {
|
||||
|
||||
@@ -17,8 +17,9 @@ function Ask-YesNo($message) {
|
||||
while ($true) {
|
||||
$resp = Read-Host "$message [YySs/Nn]"
|
||||
switch ($resp.ToUpper()) {
|
||||
"N" { return $false }
|
||||
default { return $true }
|
||||
"Y" { return $true }
|
||||
"S" { return $true }
|
||||
default { return $false }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,14 +33,17 @@ function Ask-Value($key) {
|
||||
$newJson = [ordered]@{}
|
||||
|
||||
foreach ($topKey in $parsedJson.Keys) {
|
||||
if ($topKey -eq "Config") {
|
||||
# Copia Config senza modifiche, i parametri serviranno dopo la generazione
|
||||
$newJson["Config"] = $parsedJson["Config"]
|
||||
continue
|
||||
}
|
||||
|
||||
# Sezione API → trattamento speciale
|
||||
if ($topKey -eq "API") {
|
||||
|
||||
$newJson["API"] = [ordered]@{}
|
||||
|
||||
foreach ($sectionName in $parsedJson["API"].Keys) {
|
||||
|
||||
$section = $parsedJson["API"][$sectionName]
|
||||
|
||||
$shouldConfigure = Ask-YesNo "Vuoi configurare la sezione '$sectionName'?"
|
||||
@@ -69,6 +73,12 @@ foreach ($topKey in $parsedJson.Keys) {
|
||||
$newSection = [ordered]@{}
|
||||
|
||||
foreach ($key in $section.Keys) {
|
||||
if ($key -eq "_httpyac_only") {
|
||||
# Mantieni la chiave speciale senza chiedere input
|
||||
$newSection[$key] = $section[$key]
|
||||
continue
|
||||
}
|
||||
|
||||
$inputValue = Ask-Value $key
|
||||
|
||||
if ($existingEnv -and $existingEnv["API"] -and $existingEnv["API"].Contains($sectionName)) {
|
||||
@@ -0,0 +1,152 @@
|
||||
# Percorsi file
|
||||
$envFile = Join-Path $PSScriptRoot ".." "env.json" # JSON originale
|
||||
$collectionsRoot = Join-Path $PSScriptRoot ".." "collections" # cartella di partenza delle collezioni
|
||||
|
||||
# Costanti
|
||||
$ENV_JSON_FILENAME = "env.json"
|
||||
$CFG_SECTION_NAME = "Config"
|
||||
$EXCLUDE_BRUNO_CFG_NAME = "ExcludeFromBruno"
|
||||
$API_SECTION_NAME = "API"
|
||||
$ENV_TEMPLATE_FILENAME = ".env.template"
|
||||
$ENV_FILENAME = ".env"
|
||||
|
||||
# 1. Se il file non esiste → errore
|
||||
if (-not (Test-Path $envFile)) {
|
||||
Write-Error "File '$ENV_JSON_FILENAME' non trovato."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Carica JSON mantenendo ordine
|
||||
$raw = Get-Content $envFile -Raw
|
||||
$data = $raw | ConvertFrom-Json -AsHashtable
|
||||
|
||||
if (-not $data.ContainsKey($API_SECTION_NAME)) {
|
||||
Write-Error "Il file '$ENV_JSON_FILENAME' non contiene la sezione '$API_SECTION_NAME'."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Funzione per leggere file .env (KEY=VALUE)
|
||||
function Read-EnvFile($path) {
|
||||
$result = [ordered]@{}
|
||||
foreach ($line in Get-Content $path) {
|
||||
if ($line.Trim() -eq "" -or $line.Trim().StartsWith("#")) { continue }
|
||||
if ($line -match "^\s*([^=]+)\s*=\s*(.*)$") {
|
||||
$key = $matches[1].Trim()
|
||||
$value = $matches[2]
|
||||
$result[$key] = $value
|
||||
}
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
||||
# Funzione per scrivere file .env
|
||||
function Write-EnvFile($path, $hashtable) {
|
||||
$lines = foreach ($k in $hashtable.Keys) {
|
||||
"$k=$(Format-EnvValue $hashtable[$k])"
|
||||
}
|
||||
Set-Content $path -Value $lines -Encoding UTF8
|
||||
}
|
||||
|
||||
function Format-EnvValue($value) {
|
||||
if ($null -eq $value) {
|
||||
return ""
|
||||
}
|
||||
if ($value -match "#") {
|
||||
return "'$value'"
|
||||
}
|
||||
return $value
|
||||
}
|
||||
|
||||
function KeysMatch($a, $b) {
|
||||
$diff = Compare-Object -ReferenceObject ($a | Sort-Object -Unique) -DifferenceObject ($b | Sort-Object -Unique)
|
||||
|
||||
return -not $diff
|
||||
}
|
||||
|
||||
# 2a. Leggi la configurazione di base (Config) se presente
|
||||
$config = $null
|
||||
$noBrunoKeys = @()
|
||||
if ($data.ContainsKey($CFG_SECTION_NAME)) {
|
||||
$config = $data[$CFG_SECTION_NAME]
|
||||
if ($config.ContainsKey($EXCLUDE_BRUNO_CFG_NAME)) {
|
||||
$noBrunoKeys = $config[$EXCLUDE_BRUNO_CFG_NAME]
|
||||
}
|
||||
}
|
||||
|
||||
# 2. Scorri tutte le sezioni sotto API
|
||||
foreach ($sectionName in $data[$API_SECTION_NAME].Keys) {
|
||||
|
||||
Write-Host "`n--- Sezione: $sectionName ---"
|
||||
|
||||
$section = $data[$API_SECTION_NAME][$sectionName]
|
||||
|
||||
# a. Verifica esistenza cartella
|
||||
$folder = Join-Path $collectionsRoot $sectionName
|
||||
if (-not (Test-Path $folder)) {
|
||||
Write-Warning "La cartella '$folder' non esiste. Sezione ignorata."
|
||||
continue
|
||||
}
|
||||
|
||||
# b. Verifica .env.template
|
||||
$templatePath = Join-Path $folder $ENV_TEMPLATE_FILENAME
|
||||
if (Test-Path $templatePath) {
|
||||
$template = Read-EnvFile $templatePath
|
||||
|
||||
$templateKeys = $template.Keys
|
||||
$sectionKeys = $section.Keys | Where-Object { $_ -notin $noBrunoKeys }
|
||||
|
||||
if ($templateKeys.Count -ne $sectionKeys.Count -or -not (KeysMatch $templateKeys $sectionKeys)) {
|
||||
Write-Warning "Il file '$ENV_TEMPLATE_FILENAME' in '$sectionName' non ha le stesse chiavi della sezione."
|
||||
}
|
||||
}
|
||||
|
||||
# c/d/e. Gestione file .env
|
||||
$envPath = Join-Path $folder $ENV_FILENAME
|
||||
|
||||
if (-not (Test-Path $envPath)) {
|
||||
# c. Genera nuovo file .env
|
||||
Write-Host "Generazione nuovo file '$ENV_FILENAME' per '$sectionName'."
|
||||
$newEnv = [ordered]@{}
|
||||
foreach ($key in $sectionKeys) {
|
||||
$newEnv[$key] = $section[$key]
|
||||
}
|
||||
Write-EnvFile $envPath $newEnv
|
||||
continue
|
||||
}
|
||||
|
||||
# d. Verifica chiavi .env esistente
|
||||
$existingEnv = Read-EnvFile $envPath
|
||||
|
||||
$envKeys = $existingEnv.Keys
|
||||
$sectionKeys = $section.Keys | Where-Object { $_ -notin $noBrunoKeys }
|
||||
|
||||
if ($envKeys.Count -ne $sectionKeys.Count -or -not (KeysMatch $envKeys $sectionKeys)) {
|
||||
Write-Warning "Il file '$ENV_FILENAME' in '$sectionName' non ha le stesse chiavi della sezione."
|
||||
}
|
||||
|
||||
# e. Aggiorna valori
|
||||
$updatedEnv = [ordered]@{}
|
||||
|
||||
foreach ($key in $sectionKeys) {
|
||||
$valueFromJson = $section[$key]
|
||||
|
||||
if ($existingEnv.Contains($key)) {
|
||||
if ($null -ne $valueFromJson) {
|
||||
# aggiorna
|
||||
$updatedEnv[$key] = $valueFromJson
|
||||
} else {
|
||||
# mantieni
|
||||
$updatedEnv[$key] = $existingEnv[$key]
|
||||
}
|
||||
} else {
|
||||
# chiave mancante → aggiungi
|
||||
$updatedEnv[$key] = $valueFromJson
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Aggiornamento file '$ENV_FILENAME' per '$sectionName'..."
|
||||
Write-EnvFile $envPath $updatedEnv
|
||||
Write-Host "File '$(Resolve-Path -LiteralPath $envPath)' aggiornato."
|
||||
}
|
||||
|
||||
Write-Host "`nOperazione completata."
|
||||
+29
-14
@@ -1,11 +1,15 @@
|
||||
# Percorsi file
|
||||
$envFile = Join-Path $PSScriptRoot ".." "env.json" # JSON originale
|
||||
$collectionsRoot = Join-Path $PSScriptRoot ".." "collections" # cartella di partenza delle collezioni
|
||||
$envFile = Join-Path $PSScriptRoot ".." "env.json" # JSON originale
|
||||
$collectionsRoot = Join-Path $PSScriptRoot ".." "autodocs/httpyac" # cartella di partenza delle collezioni
|
||||
|
||||
# Costanti
|
||||
$API_SECTION_NAME = "API"
|
||||
$ENV_TEMPLATE_FILENAME = ".env.template"
|
||||
$ENV_FILENAME = ".env"
|
||||
|
||||
# 1. Se il file non esiste → errore
|
||||
if (-not (Test-Path $envFile)) {
|
||||
Write-Error "File env.json non trovato."
|
||||
Write-Error "File '$ENV_JSON_FILENAME' non trovato."
|
||||
exit 1
|
||||
}
|
||||
|
||||
@@ -13,8 +17,8 @@ if (-not (Test-Path $envFile)) {
|
||||
$raw = Get-Content $envFile -Raw
|
||||
$data = $raw | ConvertFrom-Json -AsHashtable
|
||||
|
||||
if (-not $data.ContainsKey("API")) {
|
||||
Write-Error "Il file env.json non contiene la sezione 'API'."
|
||||
if (-not $data.ContainsKey($API_SECTION_NAME)) {
|
||||
Write-Error "Il file '$ENV_JSON_FILENAME' non contiene la sezione '$API_SECTION_NAME'."
|
||||
exit 1
|
||||
}
|
||||
|
||||
@@ -57,11 +61,11 @@ function KeysMatch($a, $b) {
|
||||
}
|
||||
|
||||
# 2. Scorri tutte le sezioni sotto API
|
||||
foreach ($sectionName in $data["API"].Keys) {
|
||||
foreach ($sectionName in $data[$API_SECTION_NAME].Keys) {
|
||||
|
||||
Write-Host "`n--- Sezione: $sectionName ---"
|
||||
|
||||
$section = $data["API"][$sectionName]
|
||||
$section = $data[$API_SECTION_NAME][$sectionName]
|
||||
|
||||
# a. Verifica esistenza cartella
|
||||
$folder = Join-Path $collectionsRoot $sectionName
|
||||
@@ -71,7 +75,7 @@ foreach ($sectionName in $data["API"].Keys) {
|
||||
}
|
||||
|
||||
# b. Verifica .env.template
|
||||
$templatePath = Join-Path $folder ".env.template"
|
||||
$templatePath = Join-Path $folder $ENV_TEMPLATE_FILENAME
|
||||
if (Test-Path $templatePath) {
|
||||
$template = Read-EnvFile $templatePath
|
||||
|
||||
@@ -79,18 +83,18 @@ foreach ($sectionName in $data["API"].Keys) {
|
||||
$sectionKeys = $section.Keys
|
||||
|
||||
if ($templateKeys.Count -ne $sectionKeys.Count -or -not (KeysMatch $templateKeys $sectionKeys)) {
|
||||
Write-Warning "Il file .env.template in '$sectionName' non ha le stesse chiavi della sezione."
|
||||
Write-Warning "Il file '$ENV_TEMPLATE_FILENAME' in '$sectionName' non ha le stesse chiavi della sezione."
|
||||
}
|
||||
}
|
||||
|
||||
# c/d/e. Gestione file .env
|
||||
$envPath = Join-Path $folder ".env"
|
||||
$envPath = Join-Path $folder $ENV_FILENAME
|
||||
|
||||
if (-not (Test-Path $envPath)) {
|
||||
# c. Genera nuovo file .env
|
||||
Write-Host "Generazione nuovo file .env per '$sectionName'."
|
||||
Write-Host "Generazione nuovo file '$ENV_FILENAME' per '$sectionName'."
|
||||
$newEnv = [ordered]@{}
|
||||
foreach ($key in $section.Keys) {
|
||||
foreach ($key in $sectionKeys) {
|
||||
$newEnv[$key] = $section[$key]
|
||||
}
|
||||
Write-EnvFile $envPath $newEnv
|
||||
@@ -104,7 +108,7 @@ foreach ($sectionName in $data["API"].Keys) {
|
||||
$sectionKeys = $section.Keys
|
||||
|
||||
if ($envKeys.Count -ne $sectionKeys.Count -or -not (KeysMatch $envKeys $sectionKeys)) {
|
||||
Write-Warning "Il file .env in '$sectionName' non ha le stesse chiavi della sezione."
|
||||
Write-Warning "Il file '$ENV_FILENAME' in '$sectionName' non ha le stesse chiavi della sezione."
|
||||
}
|
||||
|
||||
# e. Aggiorna valori
|
||||
@@ -127,8 +131,19 @@ foreach ($sectionName in $data["API"].Keys) {
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Aggiornamento file .env per '$sectionName'."
|
||||
# f. Aggiungi chiavi mancanti da .env.template
|
||||
if (Test-Path $templatePath) {
|
||||
foreach ($key in $template.Keys) {
|
||||
if (-not $updatedEnv.Contains($key)) {
|
||||
Write-Host "Aggiunta chiave mancante '$key' da '$ENV_TEMPLATE_FILENAME'."
|
||||
$updatedEnv[$key] = $template[$key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Aggiornamento file '$ENV_FILENAME' per '$sectionName'..."
|
||||
Write-EnvFile $envPath $updatedEnv
|
||||
Write-Host "File '$(Resolve-Path -LiteralPath $envPath)' aggiornato."
|
||||
}
|
||||
|
||||
Write-Host "`nOperazione completata."
|
||||
Reference in New Issue
Block a user