webmoney/scripts/lib/diretrizes.psm1

173 lines
5.2 KiB
PowerShell

# =============================================================================
# WEBMoney - Validador de Directrices (v3.0)
# =============================================================================
# Este modulo es importado por todos los scripts para garantizar compliance
# =============================================================================
$Script:ProjectRoot = "c:\Users\marco\OneDrive\Documents\WebMoney"
$Script:Server = "root@213.165.93.60"
$Script:ProductionUrl = "https://webmoney.cnxifly.com"
# Cargar directrices
function Get-Diretrizes {
$diretrizesFile = Join-Path $Script:ProjectRoot ".DIRETRIZES_DESENVOLVIMENTO_v3"
if (-not (Test-Path $diretrizesFile)) {
throw "ERRO FATAL: Arquivo de diretrizes nao encontrado!"
}
return Get-Content $diretrizesFile -Raw
}
# Validar que no hay cambios sin commit (Regra #6)
function Assert-NoUncommittedChanges {
Push-Location $Script:ProjectRoot
$status = git status --porcelain 2>$null
Pop-Location
if ($status) {
$count = @($status | Where-Object { $_ }).Count
return @{
Valid = $false
Message = "Existem $count arquivos modificados sem commit"
Files = $status
}
}
return @{ Valid = $true }
}
# Validar VERSION existe e esta correto
function Assert-VersionFile {
$versionFile = Join-Path $Script:ProjectRoot "VERSION"
if (-not (Test-Path $versionFile)) {
return @{ Valid = $false; Message = "Arquivo VERSION nao encontrado" }
}
$version = (Get-Content $versionFile).Trim()
if ($version -notmatch '^\d+\.\d+\.\d+$') {
return @{ Valid = $false; Message = "VERSION invalido: $version (deve ser X.Y.Z)" }
}
return @{ Valid = $true; Version = $version }
}
# Validar CHANGELOG tem entrada para versao atual
function Assert-ChangelogUpdated {
param([string]$Version)
$changelogFile = Join-Path $Script:ProjectRoot "CHANGELOG.md"
if (-not (Test-Path $changelogFile)) {
return @{ Valid = $false; Message = "Arquivo CHANGELOG.md nao encontrado" }
}
$content = Get-Content $changelogFile -Raw
if ($content -notmatch "\[$Version\]") {
return @{ Valid = $false; Message = "CHANGELOG nao contem entrada para versao $Version" }
}
return @{ Valid = $true }
}
# Validar conexao SSH com servidor
function Assert-ServerConnection {
$result = ssh -o ConnectTimeout=5 -o BatchMode=yes $Script:Server "echo OK" 2>$null
if ($result -ne "OK") {
return @{
Valid = $false
Message = "Nao foi possivel conectar ao servidor. Execute: .\scripts\setup-ssh.ps1"
}
}
return @{ Valid = $true }
}
# Validar que producao esta acessivel
function Assert-ProductionAccessible {
try {
$response = Invoke-WebRequest -Uri $Script:ProductionUrl -TimeoutSec 10 -UseBasicParsing
if ($response.StatusCode -eq 200) {
return @{ Valid = $true }
}
} catch {}
return @{
Valid = $false
Message = "Producao nao acessivel em $Script:ProductionUrl"
}
}
# Incrementar versao semantica
function Get-NextVersion {
param(
[string]$Current,
[ValidateSet('patch', 'minor', 'major')]
[string]$Type
)
$parts = $Current.Split('.')
$major = [int]$parts[0]
$minor = [int]$parts[1]
$patch = [int]$parts[2]
switch ($Type) {
'major' { $major++; $minor = 0; $patch = 0 }
'minor' { $minor++; $patch = 0 }
'patch' { $patch++ }
}
return "$major.$minor.$patch"
}
# Atualizar arquivo VERSION
function Update-VersionFile {
param([string]$NewVersion)
$versionFile = Join-Path $Script:ProjectRoot "VERSION"
Set-Content -Path $versionFile -Value $NewVersion -NoNewline
}
# Atualizar CHANGELOG
function Update-Changelog {
param(
[string]$Version,
[ValidateSet('Added', 'Changed', 'Fixed', 'Removed', 'Security')]
[string]$ChangeType,
[string]$Description
)
$changelogFile = Join-Path $Script:ProjectRoot "CHANGELOG.md"
$date = Get-Date -Format "yyyy-MM-dd"
$content = Get-Content $changelogFile -Raw
# Encontrar posicao para inserir
$insertPos = $content.IndexOf("`n## [")
if ($insertPos -eq -1) {
$insertPos = $content.IndexOf("---") + 4
}
$newEntry = @"
## [$Version] - $date
### $ChangeType
- $Description
"@
$newContent = $content.Insert($insertPos, $newEntry)
Set-Content -Path $changelogFile -Value $newContent -NoNewline
}
# Mostrar regras das directrizes
function Show-DiretrizesReminder {
Write-Host ""
Write-Host " DIRETRIZES v3.0 - REGRAS OBRIGATORIAS:" -ForegroundColor Yellow
Write-Host " ----------------------------------------" -ForegroundColor Yellow
Write-Host " 1. VERSION incrementado em cada commit" -ForegroundColor Gray
Write-Host " 2. CHANGELOG atualizado em cada commit" -ForegroundColor Gray
Write-Host " 3. Deploy via scripts (NUNCA manual)" -ForegroundColor Gray
Write-Host " 4. Teste em producao OBRIGATORIO" -ForegroundColor Gray
Write-Host " 5. Commit somente apos teste OK" -ForegroundColor Gray
Write-Host ""
}
# Exportar funcoes
Export-ModuleMember -Function *