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
44 changes: 44 additions & 0 deletions .github/workflows/lua-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: lua-test

on:
push:
branches:
- main
- master
tags:
- '*'
pull_request:
branches:
- main
paths:
- "lua/**"
- ".github/workflows/lua-test.yaml"
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

permissions:
contents: read

jobs:
build-and-test:
defaults:
run:
working-directory: "lua"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup lua toolchain
run: |
sudo apt-get update
sudo apt-get install -y lua-busted luarocks liblua5.2-dev
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.79
override: true
components: clippy, rustfmt
- name: Build
run: make
19 changes: 19 additions & 0 deletions lua/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "kcl-lib-lua"
version = "0.10.0-beta.1"
edition = "2021"
publish = false

[features]
default = ["mlua/lua52"]
lua52 = ["mlua", "mlua/lua52"]

[lib]
crate-type = ["cdylib"]

[dependencies]
mlua = { version = "0.9", features = [
"module",
"macros",
], default-features = false, optional = true }
kclvm-api = { git = "https://github.com/kcl-lang/kcl", version = "0.10.0-beta.1" }
18 changes: 18 additions & 0 deletions lua/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: all
all: build

.PHONY: build
build:
cargo build -r

.PHONY: fmt
fmt:
cargo fmt

.PHONY: check
check:
cargo check

.PHONY: clean
clean:
cargo clean
29 changes: 29 additions & 0 deletions lua/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# KCL Artifact Library for Lua

This repo is under development, PRs welcome!

## Build from Source

**Prerequisites**

+ Lua
+ Cargo

### Lua version

You have to enable one of the features: lua54, lua53, lua52, lua51, luajit(52) or luau in `Cargo.toml`, according to the chosen Lua version. **Default Lua version is 5.2**.

If you build on macos, you can set the environment to prevent link errors.

```shell
# Set cargo build target on macos
export MACOSX_DEPLOYMENT_TARGET='10.13'
```

### Linux

```shell
make
# copy to lua share library directory
cp ./target/release/libkcl_lib_lua.so /usr/lib/lua/5.2/kcl_lib.so
```
28 changes: 28 additions & 0 deletions lua/kcl_lib-0.10.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package = "kcl_lib"
version = "0.10.0-1"

source = {
url = "git+https://github.com/kcl-lang/kcl",
}

description = {
summary = "KCL Lua Bindings",
detailed = [[
KCL Lua Bindings
]],
homepage = "https://kcl-lang.io/",
license = " Apache-2.0"
}

dependencies = {
"lua >= 5.1",
"luarocks-build-rust-mlua = 0.2.0",
}

build = {
type = "rust-mlua",
modules = {
["kcl_lib"] = "kcl_lib_lua",
},
target_path = "target",
}
35 changes: 35 additions & 0 deletions lua/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
extern crate kclvm_api;

use mlua::prelude::*;

/// Execute KCL file with arguments and return the JSON/YAML result.
fn exec_program<'a>(lua: &'a Lua, args: LuaTable<'a>) -> LuaResult<LuaTable<'a>> {
let api = kclvm_api::API::default();
let work_dir: String = args.get("work_dir")?;
let k_filename_list: Vec<String> = args.get("k_filename_list")?;
let k_code_list: Vec<String> = args.get("k_code_list")?;

let result = match api.exec_program(&kclvm_api::ExecProgramArgs {
work_dir,
k_filename_list,
k_code_list,
..Default::default()
}) {
Ok(r) => r,
Err(e) => return Err(LuaError::external(e)),
};

let t = lua.create_table()?;
t.set("json_result", lua.create_string(result.json_result)?)?;
t.set("yaml_result", lua.create_string(result.yaml_result)?)?;
t.set("log_message", lua.create_string(result.log_message)?)?;
t.set("err_message", lua.create_string(result.err_message)?)?;
Ok(t)
}

#[mlua::lua_module]
fn kcl_lib(lua: &Lua) -> LuaResult<LuaTable> {
let module = lua.create_table()?;
module.set("exec_program", lua.create_function(exec_program)?)?;
Ok(module)
}
11 changes: 11 additions & 0 deletions lua/test/exec_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local kcl_lib = require("kcl_lib")

describe("kcl lua lib unit test", function()
describe("exec_program", function()
it("operator function in fs schema", function()
local result, err = kcl.exec_program({k_filename_list=["./test_data/schema.k"]})
assert.is_nil(err)
assert.are.equal(result.yaml_result, "app:\n replicas: 2")
end)
end)
end)
6 changes: 6 additions & 0 deletions lua/test/test_data/schema.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schema AppConfig:
replicas: int

app: AppConfig {
replicas: 2
}
4 changes: 2 additions & 2 deletions swift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ This repo is under development, PRs welcome!

## Developing

If you build on macos, you can set the environment to prevent link errors.

**Prerequisites**

+ Swift 5.8+
+ Cargo

If you build on macos, you can set the environment to prevent link errors.

```shell
# Set cargo build target on macos
export MACOSX_DEPLOYMENT_TARGET='10.13'
Expand Down