Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ci

on:
push:
branches: [main]
branches: [main, develop]
pull_request:

permissions: {}
Expand Down Expand Up @@ -43,6 +43,9 @@ jobs:
- name: go vet
run: go vet ./...

- name: shell syntax
run: bash -n scripts/*.sh

# O -race precisa de CGO; o binario distribuido continua sem CGO (passos abaixo).
- name: go test
run: go test -race ./...
Expand All @@ -68,10 +71,10 @@ jobs:
GOARCH: amd64
run: go build -trimpath -o /dev/null ./cmd/slime

# Testes no Windows rodam apenas uma vez por merge na main (1.7x preco de Linux).
# Testes no Windows rodam na PR de release (develop -> main) e no merge na main (1.7x preco de Linux).
test-windows:
name: test (windows-2025)
if: github.event_name == 'push'
if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || (github.event_name == 'pull_request' && github.base_ref == 'main')
needs: test
runs-on: windows-2025
timeout-minutes: 15
Expand Down
77 changes: 77 additions & 0 deletions .github/workflows/package-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: package-preview

on:
pull_request:
types: [labeled, synchronize]

permissions: {}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
# Apenas sincronizacao de commit ou adicao da label alvo cancela uma execucao em andamento.
cancel-in-progress: ${{ github.event.action == 'synchronize' || github.event.label.name == 'build-binaries' }}

jobs:
package-preview:
name: package-preview
if: >-
contains(github.event.pull_request.labels.*.name, 'build-binaries') &&
(github.event.action == 'synchronize' || github.event.label.name == 'build-binaries')
runs-on: ubuntu-24.04
timeout-minutes: 15
permissions:
contents: read # checkout do codigo do repositorio
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: go.mod
cache-dependency-path: go.sum

- name: Build preview binaries and AUR metadata
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
SHA: ${{ github.sha }}
run: |
set -euo pipefail
tag="v0.0.0-pr.${PR_NUMBER}"
ver="${tag#v}"
commit="${SHA:0:7}"
build_date="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
ldflags="-s -w -X main.version=${ver} -X main.commit=${commit} -X main.date=${build_date}"

mkdir -p dist aur

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags "$ldflags" \
-o "dist/slime-${ver}-linux-amd64" ./cmd/slime

CGO_ENABLED=0 GOOS=linux GOARCH=arm64 \
go build -trimpath -ldflags "$ldflags" \
-o "dist/slime-${ver}-linux-arm64" ./cmd/slime

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \
go build -trimpath -ldflags "$ldflags" \
-o "dist/slime-${ver}-windows-amd64.exe" ./cmd/slime

(
cd dist
sha256sum "slime-${ver}-linux-amd64" "slime-${ver}-linux-arm64" "slime-${ver}-windows-amd64.exe" > SHA256SUMS
)

sha_amd64="$(awk '/linux-amd64$/ {print $1}' dist/SHA256SUMS)"
sha_arm64="$(awk '/linux-arm64$/ {print $1}' dist/SHA256SUMS)"
bash scripts/render_aur.sh "$tag" "$sha_amd64" "$sha_arm64" aur
cp aur/PKGBUILD dist/PKGBUILD
cp aur/.SRCINFO dist/.SRCINFO

- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: slime-preview-pr-${{ github.event.pull_request.number }}
path: dist/*
include-hidden-files: true
retention-days: 1
if-no-files-found: error
153 changes: 131 additions & 22 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: release

on:
push:
branches: [main]
tags:
- 'v*.*.*'
- '[0-9]*.*.*'
Expand All @@ -21,13 +22,84 @@ jobs:
timeout-minutes: 15
cache-mode: none # release: nunca restaura nem grava cache
permissions:
contents: read # checkout do codigo do repositorio
id-token: write # assina o atestado SLSA de proveniencia
attestations: write # grava o atestado de proveniencia
contents: read # checkout do codigo do repositorio
outputs:
tag: ${{ steps.version.outputs.tag }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 0 # necessario para calcular o proximo SemVer a partir das tags e commits na main

- name: Resolve release version
id: version
env:
EVENT_NAME: ${{ github.event_name }}
REF_NAME: ${{ github.ref_name }}
REF_TYPE: ${{ github.ref_type }}
run: |
set -euo pipefail
if [ "$REF_TYPE" = "tag" ]; then
tag="$REF_NAME"
elif [ "$EVENT_NAME" = "push" ] && [ "$REF_NAME" = "main" ]; then
head_tag="$(
git tag --points-at HEAD \
| grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' \
| sed -E 's/^v(.*)$/\1\tv\1/; t; s/^(.*)$/\1\t\1/' \
| sort -k1,1V \
| tail -n1 \
| cut -f2 || true
)"
if [ -n "$head_tag" ]; then
tag="$head_tag"
else
last_tag="$(
git tag --merged HEAD \
| grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' \
| sed -E 's/^v(.*)$/\1\tv\1/; t; s/^(.*)$/\1\t\1/' \
| sort -k1,1V \
| tail -n1 \
| cut -f2 || true
)"
if [ -z "$last_tag" ]; then
tag="0.1.0"
else
prefix=""
ver="$last_tag"
if [[ "$ver" == v* ]]; then
prefix="v"
ver="${ver#v}"
fi
IFS='.' read -r major minor patch <<< "$ver"
range="${last_tag}..HEAD"
subjects="$(git log --format=%s "$range")"
bodies="$(git log --format=%b "$range")"

if printf '%s\n' "$subjects" | grep -Eq '^[a-zA-Z0-9_-]+(\([^)]+\))?!:' \
|| printf '%s\n' "$bodies" | grep -Eq '^BREAKING[ -]CHANGE:'; then
major=$((major + 1))
minor=0
patch=0
elif printf '%s\n' "$subjects" | grep -Eq '^feat(\([^)]+\))?:'; then
minor=$((minor + 1))
patch=0
else
patch=$((patch + 1))
fi

tag="${prefix}${major}.${minor}.${patch}"
while git rev-parse -q --verify "refs/tags/${tag}" >/dev/null 2>&1; do
patch=$((patch + 1))
tag="${prefix}${major}.${minor}.${patch}"
done
fi
fi
else
tag="v0.0.0-snapshot"
fi

echo "Resolved release tag: $tag"
echo "tag=${tag}" >> "$GITHUB_OUTPUT"

- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
Expand All @@ -36,21 +108,24 @@ jobs:

- name: Verify and test before packaging
run: |
unformatted=$(gofmt -l .)
if [ -n "$unformatted" ]; then
echo "::error::Arquivos fora do gofmt:"
echo "$unformatted"
exit 1
fi
go mod verify
go vet ./...
go test ./...
bash -n scripts/*.sh
go test -race ./...

- name: Build release binaries and AUR metadata
env:
REF_NAME: ${{ github.ref_name }}
REF_TYPE: ${{ github.ref_type }}
TAG: ${{ steps.version.outputs.tag }}
SHA: ${{ github.sha }}
run: |
set -euo pipefail
if [ "$REF_TYPE" = "tag" ]; then
tag="$REF_NAME"
else
tag="v0.0.0-snapshot"
fi
tag="$TAG"
ver="${tag#v}"
commit="${SHA:0:7}"
build_date="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
Expand Down Expand Up @@ -80,10 +155,6 @@ jobs:
bash scripts/render_aur.sh "$tag" "$sha_amd64" "$sha_arm64" aur
cp aur/PKGBUILD dist/PKGBUILD

- uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2
with:
subject-path: dist/*

- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: dist
Expand All @@ -101,15 +172,36 @@ jobs:
retention-days: 7
if-no-files-found: error

# Assinatura isolada: codigo de dependencias em `build` nao pode emitir token OIDC.
attest:
name: attest
needs: build
if: github.ref_type == 'tag' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
runs-on: ubuntu-slim
timeout-minutes: 5
cache-mode: none # possui permissao de assinatura OIDC: sem cache
permissions:
id-token: write # assina o atestado SLSA de proveniencia
attestations: write # grava o atestado de proveniencia
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: dist
path: dist/

- uses: actions/attest@1e69f48acb82d1966a394da916b4c1698aa569d6 # v4.2.2
with:
subject-path: dist/*

github-release:
name: github-release
needs: build
if: github.ref_type == 'tag'
needs: [build, attest]
if: github.ref_type == 'tag' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
runs-on: ubuntu-slim
timeout-minutes: 5
cache-mode: none # possui permissao de escrita: sem cache
permissions:
contents: write # cria ou atualiza a GitHub Release e envia os binarios
contents: write # cria a tag (se ainda nao existir) e publica a GitHub Release com os binarios
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
Expand All @@ -120,23 +212,40 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
TAG: ${{ github.ref_name }}
TAG: ${{ needs.build.outputs.tag }}
TARGET_SHA: ${{ github.sha }}
run: |
set -euo pipefail
if [[ ! "$TAG" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::Tag invalida: $TAG"
exit 1
fi

if gh release view "$TAG" >/dev/null 2>&1; then
gh release upload "$TAG" dist/* --clobber
gh release edit "$TAG" --draft=false
else
gh release create "$TAG" dist/* --verify-tag --generate-notes
prerelease_flag=()
if [[ "$TAG" == *-* ]]; then
prerelease_flag+=(--prerelease)
fi
if gh api "repos/${GH_REPO}/git/ref/tags/${TAG}" >/dev/null 2>&1; then
gh release create "$TAG" dist/* --verify-tag --generate-notes "${prerelease_flag[@]}"
else
gh release create "$TAG" dist/* --target "$TARGET_SHA" --generate-notes "${prerelease_flag[@]}"
fi
fi

aur-publish:
name: aur-publish
needs: [build, github-release]
if: github.ref_type == 'tag'
if: (github.ref_type == 'tag' || (github.event_name == 'push' && github.ref == 'refs/heads/main')) && !contains(needs.build.outputs.tag, '-')
runs-on: ubuntu-slim
timeout-minutes: 5
cache-mode: none # publica no AUR: sem cache
concurrency:
group: aur-publish
cancel-in-progress: false
environment:
name: aur
url: https://aur.archlinux.org/packages/slime-rpg-bin
Expand All @@ -150,7 +259,7 @@ jobs:
- name: Push PKGBUILD and .SRCINFO to AUR
env:
AUR_SSH_PRIVATE_KEY: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
TAG: ${{ github.ref_name }}
TAG: ${{ needs.build.outputs.tag }}
run: |
set -euo pipefail
if [ -z "$AUR_SSH_PRIVATE_KEY" ]; then
Expand Down
3 changes: 2 additions & 1 deletion pkg/langcode/langcode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package langcode_test

import (
"path/filepath"
"testing"

"github.com/BGLuis/slime-rpg/pkg/langcode"
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestTargetFilename(t *testing.T) {
{"loc_en.csv", "es", "loc_es.csv"},
{"text_en_US.csv", "pt", "text_pt.csv"},
{"items.csv", "pt", "items_pt.csv"},
{"lang/en.csv", "pt", "lang/pt.csv"},
{"lang/en.csv", "pt", filepath.Join("lang", "pt.csv")},
}
for _, c := range cases {
if got := langcode.TargetFilename(c.source, c.lang); got != c.want {
Expand Down
18 changes: 16 additions & 2 deletions pkg/ui/tui/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ func (f *fakeProvider) TranslateBatch(ctx context.Context, texts []string, _, _

func testMemory(t *testing.T) MemoryOpener {
t.Helper()
dir := t.TempDir()
return func() (*memory.TranslationMemory, error) {
return memory.New(filepath.Join(t.TempDir(), "memory.db"))
mem, err := memory.New(filepath.Join(dir, "memory.db"))
if err != nil {
return nil, err
}
t.Cleanup(func() { _ = mem.Close() })
return mem, nil
}
}

Expand Down Expand Up @@ -219,7 +225,15 @@ func TestMemoryOnlyModeNeverCreatesProvider(t *testing.T) {
t.Error("o modo só-memória não deve criar provedor")
return nil, errors.New("não deveria ser chamado")
}),
WithMemoryOpener(func() (*memory.TranslationMemory, error) { return memory.New(dbPath) }))
WithMemoryOpener(func() (*memory.TranslationMemory, error) {
opened, err := memory.New(dbPath)
if err != nil {
return nil, err
}
t.Cleanup(func() { _ = opened.Close() })
return opened, nil
}))
t.Cleanup(m.stopJob)
m.view = ViewConfigure
m.selectedProvider = len(m.providers) - 1
p := newPump(t, m)
Expand Down