Skip to content

Commit 9f3b92a

Browse files
committed
feat(cli): bin/tomlua for quick cli access
1 parent 8a9c4f1 commit 9f3b92a

17 files changed

Lines changed: 3912 additions & 3055 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: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
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

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+
1931
BENCH_ITERS ?= 100000
2032

2133
check_lua_incdir = \
@@ -30,15 +42,34 @@ 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+
3363
build: $(SRC)/src/*
3464
$(check_lua_incdir)
3565
@mkdir -p $(DESTDIR)
3666
$(CC) $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS)
3767

3868
bear: # used to generate compile_commands.json, which editor tools such as clangd and ccls use
3969
$(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
70+
@$(BEAR) -- $(CC) -### $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS) > /dev/null 2>&1;
71+
@echo '$(subst $(newline), ,$(FIX_BEAR_RESULT))' | $(LUA) -;
72+
@echo "Created compile_commands.json";
4273

4374
test: $(SRC)/src/* $(TESTDIR)/*
4475
$(check_so_was_built)
@@ -52,19 +83,31 @@ bench: $(SRC)/src/* $(TESTDIR)/*
5283
$(check_so_was_built)
5384
$(LUA) "$(TESTDIR)/test.lua" "$(DESTDIR)" 2 $(BENCH_ITERS) $(SKIP_TOML_EDIT)
5485

55-
install: $(SRC)/lua/tomlua/meta.lua
86+
install: $(SRC)/lua/tomlua/meta.lua $(SRC)/bin/tomlua
5687
ifdef LIBDIR
5788
$(check_so_was_built)
5889
@mkdir -p "$(LIBDIR)";
59-
cp "$(DESTDIR)/tomlua.so" "$(LIBDIR)/";
60-
@echo "Installed to $(LIBDIR)";
90+
@cp "$(DESTDIR)/tomlua.so" "$(LIBDIR)/";
91+
@echo "Installed library to $(LIBDIR)";
6192
ifdef LUADIR
6293
@mkdir -p "$(LUADIR)/tomlua";
63-
cp "$(SRC)/lua/tomlua/meta.lua" "$(LUADIR)/tomlua/";
94+
@cp "$(SRC)/lua/tomlua/meta.lua" "$(LUADIR)/tomlua/";
95+
@echo "Installed type definitions to $(LUADIR)";
96+
endif
97+
ifdef BINDIR
98+
@mkdir -p "$(BINDIR)";
99+
ifeq ($(filter /%,$(LUA)),)
100+
@printf '%s\npackage.cpath = '\''%q/?.so'\''\n-- ' "#!/usr/bin/env $(LUA)" "$(LIBDIR)" > "$(BINDIR)/tomlua";
101+
else
102+
@printf '%s\npackage.cpath = '\''%q/?.so'\''\n-- ' "#!$(LUA)" "$(LIBDIR)" > "$(BINDIR)/tomlua";
103+
endif
104+
@cat "$(SRC)/bin/tomlua" >> "$(BINDIR)/tomlua";
105+
@chmod +x "$(BINDIR)/tomlua";
106+
@echo "Installed binary to $(BINDIR)";
64107
endif
65108
else
66109
@echo "LIBDIR not set, skipping install"
67110
endif
68111

69112
clean:
70-
rm -rf $(DESTDIR) compile_commands.json compile_commands.tmp
113+
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)