From effb2ed486f3d390823cf72534d6de33293aae3c Mon Sep 17 00:00:00 2001
From: Alexander Weber <web.alexander@web.de>
Date: Tue, 13 Sep 2016 00:49:18 +0200
Subject: [PATCH] framework enhancement

- multi modules support in chat command
- robust against syntax or runtime errors in module
- output in addition to the chat trough new function qa_block.out(...)
---
 checks/same_recipe.lua |  4 +-
 init.lua               | 89 ++++++++++++++++++++++++++----------------
 2 files changed, 58 insertions(+), 35 deletions(-)

diff --git a/checks/same_recipe.lua b/checks/same_recipe.lua
index 6e04bff..62edabb 100644
--- a/checks/same_recipe.lua
+++ b/checks/same_recipe.lua
@@ -78,14 +78,14 @@ for name, def in pairs(minetest.registered_items) do
 		local recipes_for_node = minetest.get_all_craft_recipes(name)
 		if recipes_for_node == nil then
 			if print_no_recipe then
-				print(name, "no_recipe")
+				qa_block.out(name, "no_recipe")
 			end
 		else
 	        	for kn, vn in ipairs(recipes_for_node) do
                 		for ku, vu in ipairs(known_recipes) do
                         		if vu.output ~= vn.output and
 		                      	   is_same_recipe(vu, vn) == true then
-        	             			print('same recipe', vu.output, vn.output)
+						qa_block.out('same recipe', vu.output, vn.output)
 --	        	                      	print (dump(vu),dump(vn))   --debug
 	                	        end
 	        	        end
diff --git a/init.lua b/init.lua
index 9678733..9dc1912 100644
--- a/init.lua
+++ b/init.lua
@@ -1,18 +1,64 @@
 print("initialize Starting QA Block")
 
+qa_block = {}
+
+function qa_block.out(...)
+	local outsting = ""
+	local out
+	local x
+	for x, out in ipairs(arg) do
+		print(out)
+		outsting = (outsting..tostring(out)..'\t')
+	end
+	print(outsting)
+	minetest.chat_send_all(outsting)
+end
+
+
+local filepath = minetest.get_modpath("qa_block").."/checks/"
+local defaultmodule = "same_recipe"
+
+
+local function do_module( module )
+	qa_block.out("QA checks started")
+--- TODO: some selectoin of executed check
+	local file = filepath..module..".lua"
+
+	local f=io.open(file,"r")
+	if not f then
+		qa_block.out("file "..file.." not found")
+	else
+		io.close(f)
+		local compiled
+		local executed
+		local err
+		local compiled, err = loadfile(file)
+		if not compiled then
+			qa_block.out("syntax error in module file"..file)
+			qa_block.out(err)
+		else
+			executed, err = pcall(compiled)
+			if not executed then
+				qa_block.out("runtime error appears")
+				qa_block.out(err)
+			end
+		end
+	end
+	qa_block.out("QA checks finished")
+
+end
 
-local filepath = minetest.get_modpath("qa_block").."/checks/same_recipe.lua"
 minetest.register_chatcommand("qa_block", {
-	params = "",
+	params = "<checkmodule>",
 	description = "Perform qa block check",
 	privs = {interact = true},
-	func = function()
-		print("QA checks started")
-
---- TODO: some selectoin of executed check
-		dofile(filepath)
-		print("QA checks finished. Have a look to the debug.txt")
-		return true, "QA checks finished."
+	func = function(name, param)
+	if param and param ~= "" then
+		do_module(param)
+	else
+		do_module(defaultmodule)
+	end
+	return true, "QA checks finished."
 	end,
 })
 
@@ -24,34 +70,11 @@ minetest.register_node("qa_block:block", {
 })
 
 
-
---qa = {}
---function qa.list_variables(var,recursive)
--------------------------------------------------------------
----- dump variables in tables. can be reduced in dump depth
----- get all variables in memory
-----  the top node in LUA is "_G" ;)
---		if recursive == nil then
---			recursive = false
---		end
---
---		for k,v in pairs(var) do
---			print(k,v)
---
---		        if type(v) == "table" and recursive == true  then
---				qa.dump_variables(v,false, "->")
---			end
---		end
---	end
-
-
 minetest.register_on_placenode(function (pos, node)
         if node.name == "qa_block:block" then
-		print("QA checks started")
 
 --- TODO: some selectoin of executed check
-		dofile(filepath)
-		print("QA checks finished. Have a look to the debug.txt")
+		do_module(defaultmodule)
 		minetest.env:add_node(pos, {name="air"})
         end
 end)