Skip to content

Commit

Permalink
feat: add support for luarocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Crazyokd committed Jan 18, 2025
1 parent bcdadbf commit c8cc712
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: build

on:
push:
branches: [ "master", "main" ]
pull_request:
branches: [ "master", "main" ]
workflow_call: # enable calling this workflow inside other (such as the deploy one)
jobs:
build:
runs-on: ubuntu-latest
env:
LUA_ROCKS_API_KEY: ${{ secrets.LUA_ROCKS_API_KEY }}

steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- name: Build the Docker test image
run: docker-compose build test
- name: Run tests
run: docker-compose run --rm test
27 changes: 27 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: deploy

# How to fire this workflow
## After you are ready to deploy your rock, you need create a git tag in the format vX.Y.Z
## git tag vX.Y.Z && git push --tags

on:
push:
tags:
- 'v*.*-*'

jobs:
build:
uses: ./.github/workflows/build.yml
deploy:
name: Deploy rock to luarocks.org
env:
LUA_ROCKS_API_KEY: ${{ secrets.LUA_ROCKS_API_KEY }}
needs: [ build ] # build must pass to deploy
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build the Docker luarocks upload image
run: docker-compose build upload
- name: Deploy to luarocks.org
run: docker-compose run --rm upload
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.PHONY: clean run1 run2
.PHONY: clean all run1 run2

all: run1 run2

run1:
lua test.lua
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ lua <=> xml **with order**
## Example
ref [test.lua](test.lua) and [test2.lua](test2.lua)

## Feature
- restore with order.
- get/set xml node like member call.

## TODO
- add support for chain call.

## LICENSE
This code is freely distributable under the terms of the [MIT license](LICENSE).
34 changes: 34 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.9'

services:
test:
command: bash -c "luarocks make && busted --shuffle"
build:
context: .
dockerfile: test.Dockerfile
volumes:
- ".:/lua/"
working_dir: "/lua"

lint:
command: luacheck -q .
build:
context: .
dockerfile: test.Dockerfile
volumes:
- ".:/lua/"
working_dir: "/lua"

upload:
environment:
- LUA_ROCKS_API_KEY
#the command doesn't work without the bash -c, even being a single command
command: bash -c "luarocks upload xml2lua-*.rockspec --force --api-key=$LUA_ROCKS_API_KEY"
build:
context: .
dockerfile: test.Dockerfile
volumes:
- ".:/lua/"
working_dir: "/lua"
depends_on:
- test
23 changes: 23 additions & 0 deletions luaxml-1.0-0.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package = "luaxml"
version = "1.0-0"
source = {
url = "git://github.com/Crazyokd/luaxml",
tag = "v1.0-0"
}
description = {
summary = "Expressing xml with lua",
detailed = [[
lua <=> xml with order.
]],
homepage = "https://github.com/Crazyokd/luaxml",
license = "MIT"
}
dependencies = {
"lua >= 5.1, <= 5.4"
}
build = {
type = "builtin",
modules = {
luaxml = "luaxml.lua",
}
}
35 changes: 35 additions & 0 deletions spec/test2_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local function print_r(root)
local cache = { [root] = "." }

local function _dump(t, space, name)
local temp = {}
for k, v in pairs(t) do
local key = tostring(k)
if cache[v] then
table.insert(temp, "+" .. key .. " {" .. cache[v] .. "}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
table.insert(temp,
"+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. string.rep(" ", #key), new_key))
else
table.insert(temp, "+" .. key .. " [" .. tostring(v) .. "]")
end
end
return table.concat(temp, "\n" .. space)
end

print(_dump(root, "", ""))
end

local f = assert(io.open("test.xml", "r"))
local xml = f:read("a")
f:close()

local lxml = require "luaxml"
local lx1 = lxml.new()

lx1:load(xml)
-- print_r(lx1.xt)
print(lx1)
lx1:save('test2.xml')
90 changes: 90 additions & 0 deletions spec/test_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
local function print_r(root)
local cache = { [root] = "." }

local function _dump(t, space, name)
local temp = {}
for k, v in pairs(t) do
local key = tostring(k)
if cache[v] then
table.insert(temp, "+" .. key .. " {" .. cache[v] .. "}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
table.insert(temp,
"+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. string.rep(" ", #key), new_key))
else
table.insert(temp, "+" .. key .. " [" .. tostring(v) .. "]")
end
end
return table.concat(temp, "\n" .. space)
end

print(_dump(root, "", ""))
end


local luaxml = require "luaxml"

local lx1 = luaxml.new()
lx1.xt = {
["@meta"] = {},
["root@1"] = {
["key1@1"] = {
["@val"] = "value1",
["@next"] = "key2@1",
},
["key2@1"] = {
["@val"] = 123,
["@attr"] = { type = "string" },
["@next"] = "key3@1",
},
["key3@1"] = {
["@val"] = 31,
["@next"] = "key3@2",
},
["key3@2"] = {
["@val"] = 32,
["@next"] = "key4@1",
},
["key4@1"] = {
["key41@1"] = {
["@val"] = 123,
["@next"] = "key42@1"
},
["key42@1"] = {
["@val"] = 123,
},
["@head"] = "key41@1",
["@attr"] = { type = "map" },
},
["@head"] = "key1@1",
},
["@head"] = "root@1",
}


print(lx1)

print(assert(lx1:get("/root/key1") == "value1"))
lx1:set("/root/key1", 456)
print(assert(lx1:get("/root/key1") == 456))


print(lx1:get("/root/key3[1]")) -- 31
lx1["/root/key3[1]"] = 789
print(lx1:get("/root/key3[1]")) -- 789
print(lx1:get("/root/key5")) -- nil

-- lx1:set("/root/key3[3]", 1024)
lx1["/root/key3[3]"] = 1024

-- iterate attrs
assert(lx1["/root/key4/@type"] == "map")
local key4attrs = lx1:get_attrs("/root/key4")
for k, v in pairs(key4attrs) do
print(k, v)
end

print(lx1)

print(lx1["/root/key1"])
11 changes: 11 additions & 0 deletions test.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM nickblah/lua:5.4.6-luarocks-ubuntu

RUN apt-get update -qq > /dev/null \
&& apt-get install build-essential git zip -qq > /dev/null \
&& luarocks install dkjson > /dev/null \
&& luarocks install luacheck > /dev/null \
&& luarocks install luacov > /dev/null \
&& luarocks install luacov-coveralls > /dev/null \
&& luarocks install busted > /dev/null

CMD ["busted"]

0 comments on commit c8cc712

Please sign in to comment.