diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..04903fa --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,82 @@ +name: Test 🧪 + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + workflow_dispatch: + +jobs: + test: + name: Test and check ️✅ + runs-on: ubuntu-22.04 + strategy: + matrix: + include: + - lua-type: "lua" + lua-version: "5.1" + - lua-type: "lua" + lua-version: "5.2" + - lua-type: "lua" + lua-version: "5.3" + - lua-type: "lua" + lua-version: "5.4" + - lua-type: "luajit" + luajit-version: "2.0" + - lua-type: "luajit" + luajit-version: "2.1" + + steps: + - uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Create and activate virtual environment + run: | + python -m venv venv + source venv/bin/activate + echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV + echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH + + - name: Install hererocks + run: pip install hererocks + + - name: Setup Lua + run: | + mkdir -p lua_install + if [[ "${{ matrix.lua-type }}" == "lua" ]]; then + hererocks lua_install -r^ --lua=${{ matrix.lua-version }} + else + hererocks lua_install -r^ --luajit=${{ matrix.luajit-version }} + fi + echo "$PWD/lua_install/bin" >> $GITHUB_PATH + + - name: Install dependencies + run: | + luarocks install luacheck + luarocks install busted + luarocks install luacov + + - name: Run tests and checks + run: | + luacheck --no-unused-args --std max+busted smiti18n spec + busted --verbose --coverage + + - name: Generate and show coverage report + run: | + luacov + { + if [[ "${{ matrix.lua-type }}" == "lua" ]]; then + echo "# Test Coverage Summary - Lua ${{ matrix.lua-version }}" + else + echo "# Test Coverage Summary - LuaJIT ${{ matrix.luajit-version }}" + fi + echo "" + echo "| File | Hits | Missed | Coverage |" + echo "|------|------|---------|----------|" + grep "^smiti18n/.*[0-9]" luacov.report.out | sort -u | sed -E 's/^([^|]+[^ ])[[:space:]]+([0-9]+)[[:space:]]+([0-9]+)[[:space:]]+([0-9.]+)%[[:space:]]*$/| \1 | \2 | \3 | \4% |/' + } >> $GITHUB_STEP_SUMMARY diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..7f5d435 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,28 @@ +name: Release 🏷️ + +on: + push: + tags: + - "v?[0-9]+.[0-9]+.[0-9]+*" + workflow_dispatch: + inputs: + tag: + description: "The existing tag to publish" + type: "string" + required: true + +jobs: + publish-release: + name: "Publish Release 📤️" + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: Publish release ${{ github.ref }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh release create "${{ github.ref }}" --draft --generate-notes + if [ "$(gh release view "${{ github.ref }}" --json assets --template '{{len .assets}}')" -eq 0 ]; then + exit 1 + fi + gh release edit "${{ github.ref }}" --draft=false diff --git a/.luacov b/.luacov new file mode 100644 index 0000000..7d652db --- /dev/null +++ b/.luacov @@ -0,0 +1,10 @@ +return { + include = { + 'smiti18n/', + 'spec/' + }, + exclude = { + 'lua_install', + '.luarocks' + } +} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 17934dd..0000000 --- a/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -language: python -sudo: false - -env: - - LUA="lua=5.1" - - LUA="lua=5.2" - - LUA="lua=5.3" - - LUA="luajit=2.0" - - LUA="luajit=2.1" - -before_install: - - pip install hererocks - - hererocks lua_install -r^ --$LUA - - export PATH=$PATH:$PWD/lua_install/bin # Add directory with all installed binaries to PATH - -install: - - luarocks install luacheck - - luarocks install busted - - luarocks install luacov - - luarocks install luacov-coveralls - -script: - - luacheck --no-unused-args --std max+busted i18n spec - - busted --verbose --coverage - -after_success: - - luacov-coveralls --exclude $TRAVIS_BUILD_DIR/lua_install - -branches: - except: - - gh-pages - -notifications: - email: - on_success: change - on_failure: always diff --git a/smiti18n/init.lua b/smiti18n/init.lua index cc0cfd7..b594206 100644 --- a/smiti18n/init.lua +++ b/smiti18n/init.lua @@ -1,3 +1,4 @@ +local unpack = unpack or table.unpack -- lua 5.2 compat local i18n = {} local store @@ -67,17 +68,17 @@ local function assertFunctionOrNil(functionName, paramName, value) error(msg:format(functionName, paramName, tostring(value), type(value))) end -local function defaultPluralizeFunction(locale, count) - if not locale then - locale = i18n.getLocale() - if type(locale) == "table" then - locale = locale[1] +local function defaultPluralizeFunction(loc, count) + if not loc then + loc = i18n.getLocale() + if type(loc) == "table" then + loc = loc[1] end end - return plural.get(variants.root(locale), count) + return plural.get(variants.root(loc), count) end -local function pluralize(t, locale, data) +local function pluralize(t, loc, data) assertPresentOrPlural('interpolatePluralTable', 't', t) data = data or {} local key @@ -92,7 +93,7 @@ local function pluralize(t, locale, data) if customPluralizeFunction then plural_form = customPluralizeFunction(count) else - plural_form = defaultPluralizeFunction(locale, count) + plural_form = defaultPluralizeFunction(loc, count) end return t[plural_form] end @@ -102,7 +103,7 @@ local function treatNode(node, loc, data) local iter = {ipairs(node)} node = {} for k,v in unpack(iter) do - node[k] = treatNode(v, data) + node[k] = treatNode(v, loc, data) end elseif type(node) == 'string' then return interpolate(node, data) diff --git a/smiti18n/variants.lua b/smiti18n/variants.lua index b8a0b49..54e5d63 100644 --- a/smiti18n/variants.lua +++ b/smiti18n/variants.lua @@ -6,13 +6,6 @@ local function reverse(arr, length) return result, length end -local function concat(arr1, len1, arr2, len2) - for i = 1, len2 do - arr1[len1 + i] = arr2[i] - end - return arr1, len1 + len2 -end - function variants.ancestry(locale) local result, length, accum = {},0,nil locale:gsub("[^%-]+", function(c)