|
1 | | -module Lua |
| 1 | +module Geode |
2 | 2 | class Runner |
3 | 3 | @script : String |
4 | 4 | @output : IO |
5 | 5 | @state : Lua::State |
| 6 | + getter? uses_shell : Bool = false |
6 | 7 |
|
7 | 8 | def initialize(@script, @output) |
8 | 9 | @state = Lua::State.new |
9 | | - @state.open :all |
| 10 | + @state.open Lua::Library[:string, :table, :math] |
10 | 11 | end |
11 | 12 |
|
12 | | - def load_normal_env : Nil |
| 13 | + def run_normal_env : Nil |
| 14 | + Lua.create_function @state, "print" do |args| |
| 15 | + @output.puts args |
| 16 | + end |
| 17 | + |
13 | 18 | Lua.create_function @state, "current_dir" do |_| |
14 | 19 | Dir.current |
15 | 20 | end |
16 | 21 |
|
17 | | - Lua.create_function @state, "run_command" do |args| |
18 | | - command, *rest = args.map(&.as_s) rescue raise "expected all string arguments to 'run_command'" |
19 | | - Process.run(command, rest).exit_code |
| 22 | + Lua.create_function @state, "dir_exists" do |path| |
| 23 | + Dir.exists? path.as_s |
| 24 | + end |
| 25 | + |
| 26 | + Lua.create_function @state, "dir_list" do |path| |
| 27 | + Dir.children path.as_s |
| 28 | + end |
| 29 | + |
| 30 | + Lua.create_function @state, "dir_make" do |path| |
| 31 | + Dir.mkdir_p path.as_s |
| 32 | + nil |
| 33 | + end |
| 34 | + |
| 35 | + Lua.create_function @state, "dir_remove" do |path, recurse| |
| 36 | + if recurse.as_bool? |
| 37 | + FileUtils.rm_rf path.as_s |
| 38 | + else |
| 39 | + Dir.delete path.as_s |
| 40 | + end |
20 | 41 | end |
| 42 | + |
| 43 | + Lua.create_function @state, "run_command" do |command| |
| 44 | + Process.run(command.as_s).exit_code |
| 45 | + end |
| 46 | + |
| 47 | + @state.run_string @script |
21 | 48 | end |
22 | 49 |
|
23 | | - def load_test_env : Nil |
24 | | - Lua.create_function @state, "current_dir" do |_| |
| 50 | + def run_test_env : Nil |
| 51 | + Lua.create_function @state, "dir_current" do |_| |
25 | 52 | Dir.current |
26 | 53 | end |
27 | 54 |
|
28 | 55 | Lua.create_function @state, "run_command" do |_| |
| 56 | + @uses_shell = true |
29 | 57 | 0 |
30 | 58 | end |
31 | | - end |
32 | | - |
33 | | - def load_checks_env : Nil |
34 | | - # TODO |
35 | | - end |
36 | 59 |
|
37 | | - def run : Nil |
| 60 | + @state.run_string @script |
38 | 61 | end |
39 | 62 | end |
40 | 63 | end |
0 commit comments