Skip to content

Commit 552e03c

Browse files
committed
refactor(template): additional env functions & simplify methods
1 parent a948a6e commit 552e03c

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/template/runner.cr

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,63 @@
1-
module Lua
1+
module Geode
22
class Runner
33
@script : String
44
@output : IO
55
@state : Lua::State
6+
getter? uses_shell : Bool = false
67

78
def initialize(@script, @output)
89
@state = Lua::State.new
9-
@state.open :all
10+
@state.open Lua::Library[:string, :table, :math]
1011
end
1112

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+
1318
Lua.create_function @state, "current_dir" do |_|
1419
Dir.current
1520
end
1621

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
2041
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
2148
end
2249

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 |_|
2552
Dir.current
2653
end
2754

2855
Lua.create_function @state, "run_command" do |_|
56+
@uses_shell = true
2957
0
3058
end
31-
end
32-
33-
def load_checks_env : Nil
34-
# TODO
35-
end
3659

37-
def run : Nil
60+
@state.run_string @script
3861
end
3962
end
4063
end

src/template/template.cr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ module Geode
6060

6161
def run_script(output : IO) : Nil
6262
script = File.read Config::TEMPLATES / name / "control.lua"
63-
runner = Runner.new script, output
64-
runner.load_standard_functions
63+
runner = Geode::Runner.new script, output
64+
runner.run_normal_env
6565
end
6666

6767
def test_script(output : IO) : Nil
68+
script = File.read Config::TEMPLATES / name / "control.lua"
69+
runner = Geode::Runner.new script, output
70+
runner.run_test_env
6871
end
6972
end
7073
end

0 commit comments

Comments
 (0)