Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tl: add build options #177

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
536c26f
tl: add build options
euclidianAce Jun 23, 2020
c2df4d9
tl: completely ignore .d.tl files when traversing directory
euclidianAce Jun 23, 2020
747e9ca
docs: remove typescript reference, rockspec: fix lfs dependency
euclidianAce Jun 23, 2020
14b8a66
tl run: exit when load/loadstring reports error
euclidianAce Jun 24, 2020
7216cb9
tl: fix include/exclude logic and **/ not matching current dir
euclidianAce Jun 25, 2020
3822d84
spec: Add initial tests for globbing
euclidianAce Jul 3, 2020
4a0b73a
Apply review comments.
euclidianAce Jul 3, 2020
6720776
tl: Fix some magic characters not being escaped properly
euclidianAce Jul 3, 2020
07136a2
ci: add luafilesystem dependency
euclidianAce Jul 3, 2020
33cd417
rockspec: fix missing comma
euclidianAce Jul 3, 2020
0b15964
spec: add initial build dir tests
euclidianAce Jul 6, 2020
af1db4a
spec: add initial source_dir test
euclidianAce Jul 8, 2020
c90c704
spec: add initial files test
euclidianAce Jul 8, 2020
67085e8
tl: Huge refactors + add build command
euclidianAce Jul 10, 2020
653375b
tl: fix project:files not matching the full path of files
euclidianAce Jul 10, 2020
16b3855
tl: fix no files being included when source_dir is present
euclidianAce Jul 10, 2020
d40bba5
tl config: fix files option
euclidianAce Jul 10, 2020
40f1703
tl build: fix issue with cleanup_file_name
euclidianAce Jul 10, 2020
cc0236a
tl config: add skip_compat53 as a config key
euclidianAce Jul 10, 2020
54dd7b3
tl gen: implement --output
euclidianAce Jul 10, 2020
5b909f6
tl: implement --pretend and tidy up --output when there are multiple …
euclidianAce Jul 10, 2020
f7e2b5c
spec: fix up some interaction tests
euclidianAce Jul 10, 2020
7f98cbf
tl: downgrade --option to only take 1 script + move some options around
euclidianAce Jul 11, 2020
1132abe
tl build: actually type check before compiling
euclidianAce Jul 11, 2020
a58f8e8
docs: update compiler options
euclidianAce Jul 12, 2020
a8985ab
spec.util: add chdir_setup+chdir_teardown to make tests take place in…
euclidianAce Jul 12, 2020
3d4194b
spec/config: small tweaks
euclidianAce Jul 12, 2020
a1707f4
spec: add initial --output tests
euclidianAce Jul 12, 2020
75dbd36
tl: use path_separator in glob pattern
euclidianAce Jul 12, 2020
409e23d
tl build: die when no config is found
euclidianAce Jul 14, 2020
9330863
tl build: remove metatables from pattern arrays
euclidianAce Jul 14, 2020
a5986d8
tl build+spec: die when source_dir doesn't exist or isn't a dir
euclidianAce Jul 14, 2020
8f98af8
tl build: project:find() now correctly returns the current dir
euclidianAce Jul 15, 2020
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ install:
- luarocks install busted
- luarocks install compat53
- luarocks install argparse
- luarocks install luafilesystem

script:
- luarocks lint tl-dev*rockspec
Expand Down
91 changes: 85 additions & 6 deletions docs/compiler_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ When running `tl`, the compiler will try to read the compilation options from a
Here is an example of a `tlconfig.lua` file:
```lua
return {
include = {
include_dir = {
"folder1/",
"folder2/"
},
Expand All @@ -21,8 +21,87 @@ return {

## List of compiler options

| Command line option | Config key | Type | Description |
| --- | --- | --- | --- |
| `-l --preload` | `preload_modules` | `{string}` | Execute the equivalent of `require('modulename')` before executing the tl script(s). |
| `-I --include` | `include` | `{string}` | Prepend this directory to the module search path.
| `--skip-compat53` | | | Skip compat53 insertions.
| Command line option | Config key | Type | Relevant Commands | Description |
| --- | --- | --- | --- | --- |
| `-l --preload` | `preload_modules` | `{string}` | `build` `check` `gen` `run` | Execute the equivalent of `require('modulename')` before executing the tl script(s). |
| `-I --include-dir` | `include_dir` | `{string}` | `build` `check` `gen` `run` | Prepend this directory to the module search path.
| `--skip-compat53` | `skip_compat53` | `boolean` | `build` `gen` | Skip compat53 insertions.
|| `include` | `{string}` | `build` | The set of files to compile/check. See below for details on patterns.
|| `exclude` | `{string}` | `build` | The set of files to exclude. See below for details on patterns.
| `-s --source-dir` | `source_dir` | `string` | `build` | Set the directory to be searched for files. `gen` will compile every .tl file in every subdirectory by default.
| `-b --build-dir` | `build_dir` | `string` | `build` | Set the directory for generated files, mimicking the file structure of the source files.
|| `files` | `{string}` | `build` | The names of files to be compiled. Does not accept patterns like `include`.
| `-p --pretend --dry-run` ||| `build` `gen` | Don't compile/write to any files, but type check and log what files would be written to.

### Include/Exclude patterns

The `include` and `exclude` fields can have glob-like patterns in them:
- `*`: Matches any number of characters (excluding directory separators)
- `**/`: Matches any number subdirectories

In addition
- setting the `source_dir` has the effect of prepending `source_dir` to all patterns.
- currently, `include` will only include `.tl` files even if the extension isn't specified

For example:
If our project was laid out as such:
```
tlconfig.lua
src/
| foo/
| | bar.tl
| | baz.tl
| bar/
| | a/
| | | foo.tl
| | b/
| | | foo.tl
```

and our tlconfig.lua contained the following:
```lua
return {
source_dir = "src",
build_dir = "build",
include = {
"foo/*.tl",
"bar/**/*.tl"
},
exclude = {
"foo/bar.tl"
}
}
```

Running `tl build -p` will type check the `include`d files and show what would be written to.
Running `tl build` will produce the following files.
```
tlconfig.lua
src/
| foo/
| | bar.tl
| | baz.tl
| bar/
| | a/
| | | foo.tl
| | b/
| | | foo.tl
build/
| foo/
| | baz.lua
| bar/
| | a/
| | | foo.lua
| | b/
| | | foo.lua
```

Additionally, complex patterns can be used for whatever convoluted file structure we need.
```lua
return {
include = {
"foo/**/bar/**/baz/**/*.tl"
}
}
```
This will compile any `.tl` file with a sequential `foo`, `bar`, and `baz` directory in its path.
73 changes: 73 additions & 0 deletions spec/cli/build_dir_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
local util = require("spec.util")

describe("-b --build-dir argument", function()
it("generates files in the given directory", function()
util.run_mock_project(finally, {
dir_name = "build_dir_test",
dir_structure = {
["tlconfig.lua"] = [[return {
build_dir = "build",
include = {
"foo.tl", "bar.tl"
},
}]],
["foo.tl"] = [[print "foo"]],
["bar.tl"] = [[print "bar"]],
},
cmd = "build",
generated_files = {
["build"] = {
"foo.lua",
"bar.lua",
}
},
})
end)
it("replicates the directory structure of the source", function()
util.run_mock_project(finally, {
dir_name = "build_dir_nested_test",
dir_structure = {
["tlconfig.lua"] = [[return {
build_dir = "build",
include = {"**/*.tl"}
}]],
["foo.tl"] = [[print "foo"]],
["bar.tl"] = [[print "bar"]],
["baz"] = {
["foo.tl"] = [[print "foo"]],
["bar"] = {
["foo.tl"] = [[print "foo"]],
}
}
},
cmd = "build",
generated_files = {
["build"] = {
"foo.lua",
"bar.lua",
["baz"] = {
"foo.lua",
["bar"] = {
"foo.lua",
}
}
}
},
})
end)
it("dies when no config is found", function()
util.run_mock_project(finally, {
dir_name = "build_dir_die_test",
dir_structure = {},
cmd = "build",
generated_files = {},
popen = {
status = nil,
exit = "exit",
code = 1,
},
cmd_output = "Build error: tlconfig.lua not found\n"
})
end)

end)
40 changes: 27 additions & 13 deletions spec/cli/gen_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local lfs = require("lfs")
local util = require("spec.util")

local input_file = [[
Expand Down Expand Up @@ -66,10 +67,17 @@ end
local c = 100
]]

local function tl_to_lua(name)
return (name:gsub("%.tl$", ".lua"))
end

describe("tl gen", function()
setup(util.chdir_setup)
teardown(util.chdir_teardown)
describe("on .tl files", function()
it("reports 0 errors and code 0 on success", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = "add.tl"
util.write_tmp_file(finally, name, [[
local function add(a: number, b: number): number
return a + b
end
Expand All @@ -79,7 +87,7 @@ describe("tl gen", function()
local pd = io.popen("./tl gen " .. name, "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = name:gsub("%.tl$", ".lua")
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
util.assert_line_by_line([[
local function add(a, b)
Expand All @@ -91,7 +99,8 @@ describe("tl gen", function()
end)

it("ignores type errors", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = "add.tl"
util.write_tmp_file(finally, name, [[
local function add(a: number, b: number): number
return a + b
end
Expand All @@ -103,7 +112,7 @@ describe("tl gen", function()
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.same("", output)
local lua_name = name:gsub("%.tl$", ".lua")
local lua_name = tl_to_lua(name)
util.assert_line_by_line([[
local function add(a, b)
return a + b
Expand All @@ -115,7 +124,8 @@ describe("tl gen", function()
end)

it("reports number of errors in stderr and code 1 on syntax errors", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = "add.tl"
util.write_tmp_file(finally, name, [[
print(add("string", 20))))))
]])
local pd = io.popen("./tl gen " .. name .. " 2>&1 1>/dev/null", "r")
Expand All @@ -125,7 +135,8 @@ describe("tl gen", function()
end)

it("ignores unknowns code 0 if no errors", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = "add.tl"
util.write_tmp_file(finally, name, [[
local function unk(x, y): number, number
return a + b
end
Expand All @@ -134,7 +145,7 @@ describe("tl gen", function()
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.same("", output)
local lua_name = name:gsub("%.tl$", ".lua")
local lua_name = tl_to_lua(name)
util.assert_line_by_line([[
local function unk(x, y)
return a + b
Expand All @@ -143,26 +154,28 @@ describe("tl gen", function()
end)

it("does not mess up the indentation (#109)", function()
local name = util.write_tmp_file(finally, "add.tl", input_file)
local name = "add.tl"
util.write_tmp_file(finally, name, input_file)
local pd = io.popen("./tl gen " .. name, "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = name:gsub("%.tl$", ".lua")
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
assert.equal(output_file, util.read_file(lua_name))
end)
end)

describe("with --skip-compat53", function()
it("does not add compat53 insertions", function()
local name = util.write_tmp_file(finally, "test.tl", [[
local name = "test.tl"
util.write_tmp_file(finally, name, [[
local t = {1, 2, 3, 4}
print(table.unpack(t))
]])
local pd = io.popen("./tl --skip-compat53 gen " .. name, "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = name:gsub("%.tl$", ".lua")
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
util.assert_line_by_line([[
local t = { 1, 2, 3, 4 }
Expand All @@ -173,14 +186,15 @@ describe("tl gen", function()

describe("without --skip-compat53", function()
it("adds compat53 insertions by default", function()
local name = util.write_tmp_file(finally, "test.tl", [[
local name = "test.tl"
util.write_tmp_file(finally, name, [[
local t = {1, 2, 3, 4}
print(table.unpack(t))
]])
local pd = io.popen("./tl gen " .. name, "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = name:gsub("%.tl$", ".lua")
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
util.assert_line_by_line([[
local _tl_compat53 = ((tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3) and require('compat53.module'); local table = _tl_compat53 and _tl_compat53.table or table; local _tl_table_unpack = unpack or table.unpack; local t = { 1, 2, 3, 4 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local util = require("spec.util")

describe("-I --include argument", function()
describe("-I --include-dir argument", function()
it("adds a directory to package.path", function()
local name = util.write_tmp_file(finally, "foo.tl", [[
require("add")
Expand Down
Loading