Jump to content

Running a tool from the Editor.


Recommended Posts

I've tried to make an extension that'll compile all lua code in the project, but the Command/Run File doesn't seem to work. The printed command line looks fine and returns true when finished.

local extension = {}

function extension:RunLuac(file)
    local luac = AppDir().."/Tools/luac.exe"
    local o = " -o " .. "\"" .. StripExt(file)..".luac\" "
    local f = "\"" .. file .. "\""

    if FileType(luac) == 0 then
        Print("Error: Failed to locate luac!")
        return
    end

    Print(luac .. o .. f)

    --Command("cd " .. CurrentDir())
    if RunFile("\"" .. luac .. "\"" .. o .. f ) ~= 0 then
        Print("Success!")
    end
end

function ExecuteDir(path)
    local dir = LoadDir(path)
    for _, file in ipairs(dir) do
        local filepath = path .. "/" .. file;
        local t = FileType(filepath)
        if t == 1 then
            if ExtractExt(file) == "lua" then extension:RunLuac(filepath) end
        elseif t == 2 then
            ExecuteDir(filepath)
        end
    end
end

function extension.hook(event, extension)
    if event.id == EVENT_WIDGETACTION then
        ExecuteDir(CurrentDir().."/Source")
    end
end

--------------------------------------------------------------------
-- Add menu item
--------------------------------------------------------------------

local menu = program.menu:FindChild("Scripting", false)
if menu ~= nil then
    local submenu = menu:FindChild("Utilities", false)
    if submenu == nil then
        submenu = CreateMenu("Utilities", menu)
    end
    extension.menuitem = CreateMenu("Compile Lua Scripts", submenu)
    ListenEvent(EVENT_WIDGETACTION, extension.menuitem, extension.hook, extension)
end

 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

I don't think you can pass command-line arguments with the RunFile() command. The Command() or CreateProcess() functions would probably be better.

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

25 minutes ago, Josh said:

Command() should also work fine, because it is just running a windows console command.

I tried that too as mentioned in the opening post but nothing seemed to happen.

Tried CreateProcess and it's still not outputting the luac files. I'll probably have better luck using a batch file. It's possible that UAC is getting in the way.

local extension = {}

function extension:RunLuac(file)
    local luac = AppDir().."/Tools/luac.exe"
    local o = " -o " .. "\"" .. StripExt(file)..".luac\" "
    local f = "\"" .. file .. "\""

    if FileType(luac) == 0 then
        Print("Error: Failed to locate luac!")
        return
    end

    Print(luac .. o .. f)

    --Command("cd " .. CurrentDir())
    CreateProcess("\"" .. luac .."\" " .. o .. f)
    --local exitcode = proc:Wait()
    --Print("Process ended (" .. tostring(exitcode) .. ")")
    --if RunFile("\"" .. luac .. "\"" .. o .. f ) ~= 0 then
    --    Print("Success!")
    --end
end

function ExecuteDir(path)
    local dir = LoadDir(path)
    for _, file in ipairs(dir) do
        local filepath = path .. "/" .. file;
        local t = FileType(filepath)
        if t == 1 then
            if ExtractExt(file) == "lua" then extension:RunLuac(filepath) end
        elseif t == 2 then
            ExecuteDir(filepath)
        end
    end
end

function extension.hook(event, extension)
    if event.id == EVENT_WIDGETACTION then
        ExecuteDir(CurrentDir().."/Source")
        ExecuteDir(CurrentDir().."/Scripts")
    end
end

--------------------------------------------------------------------
-- Add menu item
--------------------------------------------------------------------

local menu = program.menu:FindChild("Scripting", false)
if menu ~= nil then
    local submenu = menu:FindChild("Utilities", false)
    if submenu == nil then
        submenu = CreateMenu("Utilities", menu)
    end
    extension.menuitem = CreateMenu("Compile Lua Scripts", submenu)
    ListenEvent(EVENT_WIDGETACTION, extension.menuitem, extension.hook, extension)
end

 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

With CreateProcess, you pass your arguments in a second function parameter.

I tested with Command. If I print out the command string and paste it into a Windows console it works correctly, but if I run the string with Command(), which is just calling wsystem() under the hood, it seems to not like the space in the path:

Quote

'C:\Projects\Ultra' is not recognized as an internal or external command,
operable program or batch file.

There are quotation marks around the path, so I am not sure why it doesn't work.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

This will work in the next build that goes up:

function extension:RunLuac(file)
    local luac = AppDir().."/Tools/luac.exe"
    local o = " -o " .. "\"" .. StripExt(file)..".luac\" "
    local f = "\"" .. file .. "\""

    if FileType(luac) == 0 then
        Print("Error: Failed to locate luac!")
        return
    end

    local command = "\"" .. luac .."\"" .. o .. f
    command = Replace(command, "/", "\\")
    Print(command)
    Command(command)
end

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 4 weeks later...

This works great, thank you!

local extension = {}

function extension:RunLuac(file)
    local luac = AppDir().."/Tools/luac.exe"
    local o = " -o " .. "\"" .. StripExt(file)..".luac\" "
    local f = "\"" .. file .. "\""

    if FileType(luac) == 0 then
        Print("Error: Failed to locate luac!")
        return
    end

    local command = "\"" .. luac .."\"" .. o .. f
    command = Replace(command, "/", "\\")
    Print(command)
    Command(command)
end

function ExecuteDir(path)
    local dir = LoadDir(path)
    for _, file in ipairs(dir) do
        local filepath = path .. "/" .. file;
        local t = FileType(filepath)
        if t == 1 then
            if ExtractExt(file) == "lua" then extension:RunLuac(filepath) end
        elseif t == 2 then
            ExecuteDir(filepath)
        end
    end
end

function extension.hook(event, extension)
    if event.id == EVENT_WIDGETACTION then
        ExecuteDir(CurrentDir().."/Source")
        ExecuteDir(CurrentDir().."/Scripts")
    end
end

--------------------------------------------------------------------
-- Add menu item
--------------------------------------------------------------------

local menu = program.menu:FindChild("Scripting", false)
if menu ~= nil then
    local submenu = menu:FindChild("Utilities", false)
    if submenu == nil then
        submenu = CreateMenu("Utilities", menu)
    end
    extension.menuitem = CreateMenu("Compile Lua Scripts", submenu)
    ListenEvent(EVENT_WIDGETACTION, extension.menuitem, extension.hook, extension)
end

 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...