Move all exec operations to the Shell class

This commit is contained in:
Sebastian Morr 2020-09-08 16:46:12 +02:00
parent 5a8c377160
commit f09abe36e9
3 changed files with 21 additions and 25 deletions

20
game.gd
View file

@ -1,32 +1,12 @@
extends Node
var tmp_prefix = "/tmp/"
var cwd
var global_shell
func _ready():
global_shell = Shell.new()
global_shell.cd(tmp_prefix)
cwd = global_shell.run("pwd")
# Remove trailing newline.
cwd = cwd.substr(0,len(cwd)-1)
# Run a simple command with arguments, blocking, using OS.execute.
func exec(command, args=[]):
var debug = true
if debug:
print("game.exec: %s [%s]" % [command, PoolStringArray(args).join(", ")])
var output = []
OS.execute(command, args, true, output, true)
output = output[0]
if debug:
print(output)
return output
func read_file(path):
print ("reading " + path)
var file = File.new()

View file

@ -67,8 +67,8 @@ func load_level(id):
get_tree().quit()
# Danger zone!
game.exec("rm", ["-rf", active_repository_path])
game.exec("rm", ["-rf", goal_repository_path])
game.global_shell.run("rm -rf '%s'" % active_repository_path)
game.global_shell.run("rm -rf '%s'" % goal_repository_path)
construct_repo(goal_script, goal_repository_path)
construct_repo(active_script, active_repository_path)

View file

@ -9,7 +9,7 @@ func _init():
_fake_editor = game.tmp_prefix + "fake-editor"
var content = game.read_file("res://scripts/fake-editor")
game.write_file(_fake_editor, content)
run("chmod u+x " + _fake_editor)
_exec("chmod", ["u+x", _fake_editor])
func cd(dir):
_cwd = dir
@ -17,7 +17,7 @@ func cd(dir):
# Run a shell command given as a string. Run this if you're interested in the
# output of the command.
func run(command):
var debug = false
var debug = true
if debug:
print("$ %s" % command)
@ -31,7 +31,23 @@ func run(command):
hacky_command += "cd '%s';" % _cwd
hacky_command += command
var output = game.exec("/bin/sh", ["-c", hacky_command])
var output = _exec("/bin/sh", ["-c", hacky_command])
if debug:
print(output)
return output
# Run a simple command with arguments, blocking, using OS.execute.
func _exec(command, args=[]):
var debug = false
if debug:
print("exec: %s [%s]" % [command, PoolStringArray(args).join(", ")])
var output = []
OS.execute(command, args, true, output, true)
output = output[0]
if debug:
print(output)