diff --git a/export-elx.bat b/export-elx.bat index e2a72bf..9587d66 100644 --- a/export-elx.bat +++ b/export-elx.bat @@ -20,7 +20,7 @@ IF "%~1"=="" ( set ModuleTag=%~1 ) -@powershell -NoProfile -ExecutionPolicy Bypass -Command .\scripts\export-elx.ps1 -ModuleTag %ModuleTag% +@pwsh -NoProfile -ExecutionPolicy Bypass -Command .\scripts\export-elx.ps1 -ModuleTag %ModuleTag% IF DEFINED iscommandline IF NOT DEFINED ispowershell pause ENDLOCAL diff --git a/scripts/elx-tools-module.psm1 b/scripts/elx-tools-module.psm1 index 8d2e83b..6d7e02e 100644 --- a/scripts/elx-tools-module.psm1 +++ b/scripts/elx-tools-module.psm1 @@ -1,7 +1,7 @@ # Script-level parameters and cmdlet binding to support -Verbose and common parameters [CmdletBinding()] param ( - [switch]$Quiet + [switch]$Quiet = $false ) function Write-LogMessage { @@ -56,6 +56,16 @@ function New-EnvFileContent { [ValidateNotNullOrEmpty()][Parameter(Mandatory)][string]$ModuleTag ) + $envValues = Get-EnvironmentFromFile -ModuleTag $ModuleTag + + $out = ($envValues.Keys | ForEach-Object { "$_='$($envValues[$_])'" }) -join "`n" +} + +function Get-EnvironmentFromFile { + param( + [ValidateNotNullOrEmpty()][Parameter(Mandatory)][string]$ModuleTag + ) + $envFileScriptPath = Join-Path $PSScriptRoot "../" # Load existing .env if present $envPath = Join-Path $envFileScriptPath '.env' @@ -67,7 +77,7 @@ function New-EnvFileContent { $envValues = @{} $envLines = Get-Content -Raw -Path $envPath -ErrorAction Stop - Write-Host $envLines + foreach ($line in $envLines -split "`n") { $l = $line.Trim() if ($l -eq '' -or $l.StartsWith('#') -or $l.StartsWith(';')) { continue } @@ -81,8 +91,8 @@ function New-EnvFileContent { $envValues[$name] = $ModuleTag } } - $out = foreach ($name in $settingNames) { "$name='$($envValues[$name])'" } - $out -join "`n" + + $envValues } function Export-ElxModule { @@ -98,30 +108,45 @@ function Export-ElxModule { $seleniumToolsScriptPath = Join-Path $PSScriptRoot "../tools/selenium/" Push-Location -StackName Selenium $seleniumToolsScriptPath - if (-not $Quiet) { - Write-Host "Executing module export for '$ModuleTag'..." + try { + if (-not $Quiet) { + Write-Host "Executing module export for '$ModuleTag'..." + } + + $NpmPath = (Get-Command npm).Source + if (-not $NpmPath) { + Write-Error "npm is not installed or not found in PATH. Please install Node.js and npm, then try again." + exit 1 + } + Start-Process pwsh -ArgumentList "$NpmPath run export" -Environment (Get-EnvironmentFromFile -ModuleTag $ModuleTag) -NoNewWindow -Wait -PassThru | Out-Null + + if ($LASTEXITCODE -and ($LASTEXITCODE -ne 0)) { + Write-Error "Module export for '$ModuleTag' failed with exit code $LASTEXITCODE." + exit 1 + } + + if (-not $Quiet) { + Write-Host "Module export for '$ModuleTag' completed successfully!" -ForegroundColor Green + Write-Host "Switching back to original directory..." + } + + $seleniumDownloadDir = Join-Path $seleniumToolsScriptPath "downloads" + if (-not (Test-Path -Path $seleniumDownloadDir)) { + Write-Error "Selenium download directory not found at '$seleniumDownloadDir'." + exit 1 + } + + $latestDownloadedFile = Get-ChildItem -Path $seleniumDownloadDir -Filter "*.elx" | Sort-Object LastWriteTime -Descending | Select-Object -First 1 + if (-not $latestDownloadedFile) { + Write-Error "No .elx file found in Selenium download directory '$seleniumDownloadDir'." + exit 1 + } + + Move-Item -Path $latestDownloadedFile.FullName -Destination (Join-Path $ModulePath $latestDownloadedFile.Name) -Force } - - #HACK: il tool prende in ingresso un env file - $tmpEnvFile = [System.IO.Path]::GetTempFileName() - - New-EnvFileContent -ModuleTag $ModuleTag | Set-Content -LiteralPath $tmpEnvFile -Encoding UTF8 - - & npx env-cmd --file "$tmpEnvFile" -- npm run export - - Remove-Item -LiteralPath $tmpEnvFile -Force -ErrorAction SilentlyContinue - - if ($LASTEXITCODE -ne 0) { - Write-Error "Module export for '$ModuleTag' failed with exit code $LASTEXITCODE." - exit 1 + finally { + Pop-Location -StackName Selenium } - - if (-not $Quiet) { - Write-Host "Module export for '$ModuleTag' completed successfully!" -ForegroundColor Green - Write-Host "Switching back to original directory..." - } - - Pop-Location -StackName Selenium } function Convert-UrlToElixAutocompleteFormat { @@ -169,4 +194,5 @@ function Convert-UrlToElixAutocompleteFormat { Export-ModuleMember -Function Convert-UrlToElixAutocompleteFormat Export-ModuleMember -Function Save-ElxInternals -Export-ModuleMember -Function Export-ElxModule \ No newline at end of file +Export-ModuleMember -Function Export-ElxModule +Export-ModuleMember -Function New-EnvFileContent \ No newline at end of file diff --git a/scripts/export-elx.ps1 b/scripts/export-elx.ps1 index 6654750..1873b52 100644 --- a/scripts/export-elx.ps1 +++ b/scripts/export-elx.ps1 @@ -3,17 +3,26 @@ param( [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$ModuleTag ) -Write-Host "Checking module folder existence..." -$modulesSrcPath = Join-Path -Resolve $PSScriptRoot "../src/$ModuleTag" -if (-not (Test-Path -Path "$modulesSrcPath")) { - Write-Host "Module folder not found. Creating folder for module tag '$ModuleTag'..." -ForegroundColor Yellow - New-Item -ItemType Directory -Path "$modulesSrcPath" -Force | Out-Null +$psVersion = $PSVersionTable.PSVersion +if ($psVersion.Major -lt 7 -or ($psVersion.Major -eq 7 -and $psVersion.Minor -lt 4)) { + Write-Host "This script requires PowerShell 7.4 or higher. Please install PowerShell >= 7.4 and try again." -ForegroundColor Red + exit 1 } -Write-Host "Exporting module tag '$ModuleTag' from elixForms..." +Write-Host "Checking module folder existence..." +$modulesSrcPath = Join-Path $PSScriptRoot "../src/$ModuleTag" +if (-not (Test-Path -Path $modulesSrcPath)) { + Write-Host "Module folder not found. Creating folder for module tag '$ModuleTag'..." -ForegroundColor Yellow + New-Item -ItemType Directory -Path $modulesSrcPath -Force | Out-Null +} +$modulesSrcPath = Join-Path -Resolve $PSScriptRoot "../src/$ModuleTag" + +Write-Host "Module tag '$ModuleTag' will be exported to '$modulesSrcPath'" Import-Module $(Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Definition) "elx-tools-module.psm1") -Force -Export-ElxModule -ModuleTag $ModuleTag -ModulePath "$modulesSrcPath" +Write-Host "Exporting module tag '$ModuleTag' from elixForms..." + +Export-ElxModule -ModuleTag $ModuleTag -ModulePath $modulesSrcPath Write-Host "Module tag '$ModuleTag' exported successfully!" -ForegroundColor Green