Skip to content

Commit 0464f99

Browse files
authored
Merge pull request #65 from BirdeeHub/cli
feat(cli): tomlua command
2 parents 8a9c4f1 + 3ffc19f commit 0464f99

18 files changed

Lines changed: 3973 additions & 3098 deletions

.github/rockspec.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ build = {
3939
},
4040
install_variables = {
4141
LIBDIR="$(LIBDIR)",
42+
BINDIR="$(BINDIR)",
4243
LUADIR="$(LUADIR)",
4344
},
4445
}

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
**/result
2-
**/lib
2+
**/result-*
3+
**/repl-result-*
34
**/.cache
5+
build/
46
**/compile_commands.json

Makefile

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
src ?= .
22
SRC ?= $(src)
33
SRC := $(abspath $(SRC))
4-
DESTDIR ?= $(SRC)/lib
4+
DESTDIR ?= ./build/lib
55
CC ?= gcc
66
LUA ?= lua
77
BEAR ?= bear
8-
GREP ?= grep
98
CFLAGS ?= -fPIC -x c -O3 -flto -Wl,-s -Winline
109
LIBFLAG ?= -shared
1110

11+
ifdef LUA_INC
12+
LUA_INCDIR ?= $(LUA_INC)
13+
endif
14+
1215
CFLAGS += $(LIBFLAG) -I"$(LUA_INCDIR)"
1316
TESTDIR := $(SRC)/tests
1417
SRCS := $(SRC)/src/tomlua.c \
1518
$(SRC)/src/decode.c \
1619
$(SRC)/src/encode.c \
1720
$(SRC)/src/dates.c
1821

19-
BENCH_ITERS ?= 100000
22+
ifdef out
23+
PREFIX ?= $(out)
24+
endif
25+
ifdef PREFIX
26+
BINDIR ?= $(PREFIX)/bin
27+
LUADIR ?= $(PREFIX)/lua
28+
LIBDIR ?= $(PREFIX)/lib
29+
endif
30+
31+
FIX_SHEBANG := print(string.format("\#!%s\\npackage.cpath = %q .. [[/?.so;]] .. package.cpath\\n-- ", arg[1], arg[2]))
2032

2133
check_lua_incdir = \
2234
@if [ -z "$(LUA_INCDIR)" ]; then \
@@ -30,15 +42,36 @@ check_so_was_built = \
3042
false; \
3143
fi
3244

45+
define newline
46+
47+
48+
endef
49+
define FIX_BEAR_RESULT
50+
local input, tmp = "compile_commands.json", "compile_commands.tmp";
51+
local infile = assert(io.open(input, "r"));
52+
local outfile = assert(io.open(tmp, "w"));
53+
for line in infile:lines() do
54+
if not line:find("-###", 1, true) then
55+
outfile:write(line, "\n");
56+
end
57+
end
58+
infile:close();
59+
outfile:close();
60+
assert(os.rename(tmp, input));
61+
endef
62+
63+
BENCH_ITERS ?= 100000
64+
3365
build: $(SRC)/src/*
3466
$(check_lua_incdir)
3567
@mkdir -p $(DESTDIR)
3668
$(CC) $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS)
3769

3870
bear: # used to generate compile_commands.json, which editor tools such as clangd and ccls use
3971
$(check_lua_incdir)
40-
$(BEAR) -- $(CC) -### $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS) > /dev/null 2>&1
41-
$(GREP) -v -- "-###" compile_commands.json > compile_commands.tmp && mv compile_commands.tmp compile_commands.json
72+
@$(BEAR) -- $(CC) -### $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS) > /dev/null 2>&1;
73+
@echo '$(subst $(newline), ,$(FIX_BEAR_RESULT))' | $(LUA) -;
74+
@echo "Created compile_commands.json";
4275

4376
test: $(SRC)/src/* $(TESTDIR)/*
4477
$(check_so_was_built)
@@ -52,19 +85,31 @@ bench: $(SRC)/src/* $(TESTDIR)/*
5285
$(check_so_was_built)
5386
$(LUA) "$(TESTDIR)/test.lua" "$(DESTDIR)" 2 $(BENCH_ITERS) $(SKIP_TOML_EDIT)
5487

55-
install: $(SRC)/lua/tomlua/meta.lua
88+
install: $(SRC)/lua/tomlua/meta.lua $(SRC)/bin/tomlua
5689
ifdef LIBDIR
5790
$(check_so_was_built)
5891
@mkdir -p "$(LIBDIR)";
59-
cp "$(DESTDIR)/tomlua.so" "$(LIBDIR)/";
60-
@echo "Installed to $(LIBDIR)";
92+
@cp "$(DESTDIR)/tomlua.so" "$(LIBDIR)/";
93+
@echo "Installed library to $(LIBDIR)";
6194
ifdef LUADIR
6295
@mkdir -p "$(LUADIR)/tomlua";
63-
cp "$(SRC)/lua/tomlua/meta.lua" "$(LUADIR)/tomlua/";
96+
@cp "$(SRC)/lua/tomlua/meta.lua" "$(LUADIR)/tomlua/";
97+
@echo "Installed type definitions to $(LUADIR)";
98+
endif
99+
ifdef BINDIR
100+
@mkdir -p "$(BINDIR)";
101+
ifeq ($(filter /%,$(LUA)),)
102+
@echo '$(FIX_SHEBANG)' | $(LUA) - "/usr/bin/env $(LUA)" "$(LIBDIR)" > "$(BINDIR)/tomlua"
103+
else
104+
@echo '$(FIX_SHEBANG)' | $(LUA) - "$(LUA)" "$(LIBDIR)" > "$(BINDIR)/tomlua"
105+
endif
106+
@cat "$(SRC)/bin/tomlua" >> "$(BINDIR)/tomlua";
107+
@chmod +x "$(BINDIR)/tomlua";
108+
@echo "Installed binary to $(BINDIR)";
64109
endif
65110
else
66111
@echo "LIBDIR not set, skipping install"
67112
endif
68113

69114
clean:
70-
rm -rf $(DESTDIR) compile_commands.json compile_commands.tmp
115+
rm -rf $(DESTDIR) compile_commands.json compile_commands.tmp build

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ It is not intended to replace packages like [toml\_edit](https://github.com/nvim
1313
* Compatible with Lua 5.1+.
1414
* Some advanced TOML compliance features are optional (`fancy_dates`, etc.).
1515
* Allows you to read directly into an existing lua table of defaults. This cuts out the step of merging them yourself, making it even more performant and easier to use!
16+
* Command for using the library from the command line. (`tomlua --help` to see the list of options).
1617

1718
## Limitations
1819

0 commit comments

Comments
 (0)