2020-09-08 16:00:18 +02:00
|
|
|
extends Node
|
|
|
|
class_name Shell
|
|
|
|
|
|
|
|
var _cwd
|
2020-10-20 18:44:33 +02:00
|
|
|
var os = OS.get_name()
|
2020-09-08 16:00:18 +02:00
|
|
|
|
2020-09-15 12:36:22 +02:00
|
|
|
#signal output(text)
|
2020-09-08 22:16:18 +02:00
|
|
|
|
2020-09-08 16:00:18 +02:00
|
|
|
func _init():
|
2020-09-29 16:12:58 +02:00
|
|
|
_cwd = game.tmp_prefix_inside
|
2020-09-08 16:00:18 +02:00
|
|
|
|
|
|
|
func cd(dir):
|
|
|
|
_cwd = dir
|
|
|
|
|
|
|
|
# Run a shell command given as a string. Run this if you're interested in the
|
|
|
|
# output of the command.
|
2020-09-29 18:36:24 +02:00
|
|
|
func run(command, crash_on_fail=true):
|
2020-10-22 14:01:08 +02:00
|
|
|
var debug = true
|
2020-09-08 16:00:18 +02:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print("$ %s" % command)
|
|
|
|
|
|
|
|
var env = {}
|
2020-10-06 16:50:31 +02:00
|
|
|
if game.fake_editor:
|
|
|
|
env["GIT_EDITOR"] = game.fake_editor.replace(" ", "\\ ")
|
2020-09-09 18:32:57 +02:00
|
|
|
env["GIT_AUTHOR_NAME"] = "You"
|
|
|
|
env["GIT_COMMITTER_NAME"] = "You"
|
|
|
|
env["GIT_AUTHOR_EMAIL"] = "you@example.com"
|
|
|
|
env["GIT_COMMITTER_EMAIL"] = "you@example.com"
|
2020-09-22 16:18:56 +02:00
|
|
|
env["GIT_TEMPLATE_DIR"] = ""
|
2020-09-08 16:00:18 +02:00
|
|
|
|
|
|
|
var hacky_command = ""
|
|
|
|
for variable in env:
|
|
|
|
hacky_command += "export %s='%s';" % [variable, env[variable]]
|
2020-09-29 16:12:58 +02:00
|
|
|
hacky_command += "cd '%s' || exit 1;" % _cwd
|
2020-09-08 16:00:18 +02:00
|
|
|
hacky_command += command
|
|
|
|
|
2020-09-11 12:23:26 +02:00
|
|
|
# Godot's OS.execute wraps each argument in double quotes before executing.
|
|
|
|
# Because we want to be in a single-quote context, where nothing is evaluated,
|
|
|
|
# we end those double quotes and start a single quoted string. For each single
|
|
|
|
# quote appearing in our string, we close the single quoted string, and add
|
|
|
|
# a double quoted string containing the single quote. Ooooof!
|
|
|
|
#
|
|
|
|
# Example: The string
|
|
|
|
#
|
|
|
|
# test 'fu' "bla" blubb
|
|
|
|
#
|
|
|
|
# becomes
|
|
|
|
#
|
2020-09-29 14:26:20 +02:00
|
|
|
# "'test '"'"'fu'"'"' "bla" blubb"
|
2020-10-20 18:44:33 +02:00
|
|
|
#
|
|
|
|
# Quoting Magic is not needed for Windows!
|
|
|
|
if os == "X11" or os == "OSX":
|
|
|
|
hacky_command = '"\''+hacky_command.replace("'", "'\"'\"'")+'\'"'
|
2020-09-11 12:23:26 +02:00
|
|
|
|
2020-09-29 18:36:24 +02:00
|
|
|
var output = helpers.exec(_shell_binary(), ["-c", hacky_command], crash_on_fail)
|
2020-09-08 16:00:18 +02:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print(output)
|
|
|
|
|
|
|
|
return output
|
2020-09-09 11:01:30 +02:00
|
|
|
|
|
|
|
func _shell_binary():
|
2020-10-20 18:44:33 +02:00
|
|
|
|
2020-09-09 11:01:30 +02:00
|
|
|
|
2020-10-06 16:50:31 +02:00
|
|
|
if os == "X11" or os == "OSX":
|
2020-09-11 13:12:12 +02:00
|
|
|
return "bash"
|
2020-09-09 11:01:30 +02:00
|
|
|
elif os == "Windows":
|
2020-09-11 13:12:12 +02:00
|
|
|
return "dependencies\\windows\\git\\bin\\bash.exe"
|
2020-09-09 11:01:30 +02:00
|
|
|
else:
|
2020-09-29 14:53:00 +02:00
|
|
|
helpers.crash("Unsupported OS: %s" % os)
|
2020-09-08 16:46:12 +02:00
|
|
|
|
2020-09-08 22:16:18 +02:00
|
|
|
var _t
|
|
|
|
func run_async(command):
|
|
|
|
_t = Thread.new()
|
|
|
|
_t.start(self, "run_async_thread", command)
|
|
|
|
|
|
|
|
func run_async_thread(command):
|
|
|
|
var port = 1000 + (randi() % 1000)
|
|
|
|
var s = TCP_Server.new()
|
|
|
|
s.listen(port)
|
2020-09-11 10:33:44 +02:00
|
|
|
var _pid = OS.execute("ncat", ["127.0.0.1", str(port), "-c", command], false, [], true)
|
2020-09-08 22:16:18 +02:00
|
|
|
while not s.is_connection_available():
|
|
|
|
pass
|
|
|
|
var c = s.take_connection()
|
|
|
|
while c.get_status() == StreamPeerTCP.STATUS_CONNECTED:
|
2020-09-15 09:30:19 +02:00
|
|
|
read_from(c)
|
|
|
|
OS.delay_msec(1000/30)
|
|
|
|
read_from(c)
|
2020-09-08 22:16:18 +02:00
|
|
|
c.disconnect_from_host()
|
|
|
|
s.stop()
|
|
|
|
|
2020-09-15 09:30:19 +02:00
|
|
|
func read_from(c):
|
|
|
|
var total_available = c.get_available_bytes()
|
|
|
|
print(str(total_available)+" bytes available")
|
|
|
|
while total_available > 0:
|
|
|
|
var available = min(1024, total_available)
|
|
|
|
total_available -= available
|
|
|
|
print("reading "+str(available))
|
|
|
|
var data = c.get_utf8_string(available)
|
|
|
|
#emit_signal("output", data)
|
|
|
|
print(data.size())
|