From 431f540b429642277e2650a3b27e4d0212177302 Mon Sep 17 00:00:00 2001 From: Lewin Kelly Date: Wed, 31 Jan 2024 22:40:13 +0000 Subject: [PATCH] Improve Sync server and plugin --- Sync/Plugin/Plugin.lua | 50 +++++++++++++++++++++++++++++++------- Sync/Server/.darklua.json5 | 21 ++++++++++++++++ Sync/Server/compile.go | 5 +++- Sync/Server/main.go | 14 ++++++++++- 4 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 Sync/Server/.darklua.json5 diff --git a/Sync/Plugin/Plugin.lua b/Sync/Plugin/Plugin.lua index b9b4581..5afbf0c 100644 --- a/Sync/Plugin/Plugin.lua +++ b/Sync/Plugin/Plugin.lua @@ -191,10 +191,10 @@ end local function makeScript(s) -- because no continue local path = s.path -- { "ServerScriptService", "script" } local content = s.content - local type = s.type + local filetype = s.type local obj = game - local ok2 = pcall(function() + local ok, err = pcall(function() for i = 1, #path - 1 do obj = obj:FindFirstChild(path[i]) if not obj then @@ -205,12 +205,13 @@ local function makeScript(s) -- because no continue end end end) - if not ok2 then + if not ok then notify( "Failed to sync " .. table.concat(path, ".") .. "! Is the path correct?" ) + print("Failed to sync:", err) return end @@ -218,7 +219,18 @@ local function makeScript(s) -- because no continue local existingObj = obj:FindFirstChild(name) if existingObj then if existingObj:IsA "Script" then - existingObj.Source = content + local ok2, err2 = pcall(function() + existingObj.Source = content + end) + if not ok2 then + notify( + "Failed to sync " + .. table.concat(path, ".") + .. " to an existing file!" + ) + print("Failed to sync to existing:", err2) + return + end else notify( "Object already exists at path " @@ -230,17 +242,37 @@ local function makeScript(s) -- because no continue else local createScript - if type == "server" then + if filetype == "server" then createScript = Instance.new "Script" - elseif type == "client" then + elseif filetype == "client" then createScript = Instance.new "LocalScript" else return end - createScript.Name = name - createScript.Source = content - createScript.Parent = obj + print("Name", name) + print("Content", content, type(content)) + + if type(content) == "table" then + for i, v in pairs(content) do + print(i, v) + end + end + + local ok2, err2 = pcall(function() + createScript.Name = name + createScript.Source = content + createScript.Parent = obj + end) + if not ok2 then + notify( + "Failed to write " + .. table.concat(path, ".") + .. "! Is the content valid?" + ) + print("Failed to write:", err2) + return + end end end diff --git a/Sync/Server/.darklua.json5 b/Sync/Server/.darklua.json5 new file mode 100644 index 0000000..0483e2d --- /dev/null +++ b/Sync/Server/.darklua.json5 @@ -0,0 +1,21 @@ +{ + rules: [ + "convert_index_to_field", + "remove_spaces", + "remove_compound_assignment", + "remove_interpolated_string", + "group_local_assignment", + // "compute_expression", + "remove_unused_if_branch", + "remove_unused_while", + "remove_empty_do", + "remove_types", + // "remove_method_definition", + "remove_function_call_parens", + "filter_after_early_return", + { + rule: "rename_variables", + globals: ["$default", "$roblox"], + }, + ], +} diff --git a/Sync/Server/compile.go b/Sync/Server/compile.go index 868546c..2558a1c 100644 --- a/Sync/Server/compile.go +++ b/Sync/Server/compile.go @@ -12,7 +12,10 @@ func CompileLuau(sourcePath string) (string, error) { } cmd := exec.Command(path, "process", sourcePath, "./temp.lua") - cmd.Run() + err = cmd.Run() + if err != nil { + return "", err + } // Return the compiled file file, _ := os.ReadFile("./temp.lua") diff --git a/Sync/Server/main.go b/Sync/Server/main.go index 1f1fbc5..d02442b 100644 --- a/Sync/Server/main.go +++ b/Sync/Server/main.go @@ -93,6 +93,7 @@ func main() { // scripttype = "module" fmt.Println(c.InRed("Unknown script type: ") + c.InUnderline(c.InPurple(formatPath)) + c.InRed("!")) fmt.Println(c.InYellow("If you were trying to sync a ModuleScript, these are not supported by Mercury Sync. Please transpose them manually.")) + return nil } formatPath = strings.ReplaceAll(formatPath, string(os.PathSeparator), "$dot$") @@ -115,6 +116,12 @@ func main() { case "luau": fmt.Println(c.InBlue("Compiling ") + c.InUnderline(c.InPurple(dottedPath)) + c.InBlue("...")) content, err = CompileLuau(path) + + if content == "" { + fmt.Println(c.InYellow("After compilation, file ") + c.InUnderline(c.InPurple(dottedPath)) + c.InYellow(" was empty!")) + content = "-- Mercury Sync: Empty file" + } + if err != nil { fmt.Println(c.InRed("Error while compiling Luau file:"), err) if strings.Contains(err.Error(), "file does not exist") || @@ -131,6 +138,11 @@ func main() { return nil } content = string(file) + + if content == "" { + fmt.Println(c.InYellow("File ") + c.InUnderline(c.InPurple(dottedPath)) + c.InYellow(" is empty!")) + content = "-- Mercury Sync: Empty file" + } } fmt.Println(c.InGreen("Sending ") + c.InUnderline(c.InPurple(dottedPath)) + c.InGreen("...")) @@ -150,7 +162,7 @@ func main() { return nil }) - os.Remove("./temp.lua") + // os.Remove("./temp.lua") cx.JSON(200, Response) })