lyqyd / cc-packman

A package manager for ComputerCraft.
MIT License
28 stars 14 forks source link

Place easy-shell in startup folder in CraftOS 1.8 #63

Closed JakobDev closed 6 years ago

JakobDev commented 7 years ago

Since CraftOS 1.8 startup can be a folder. So it would be nice to place easy-shell in this folder and not as file, if packman run on CraftOS 1.8. I have modified the code of esay-shell to do that:

local args = {...}

local exStr = [[shell.run("/usr/bin/easy-shell execute")]]

local function loadStartup(path)
    local lines = {}
    if fs.exists(path) then
        local handle = io.open(path, "r")
        if handle then
            for line in handle:lines() do
                table.insert(lines, line)
            end
            handle:close()
        end
    end
    return lines
end

local function writeStartup(contents,path)
    local handle = io.open(path, "w")
    if handle then
        for i, line in ipairs(contents) do
            handle:write(line.."\n")
        end
        handle:close()
    end
end

if #args >= 1 then
    if args[1] == "execute" then
        shell.setPath(shell.path()..":/usr/bin")

        local loadAPI = os.loadAPI

        os.loadAPI = function(path)
            if fs.exists(path) then
                return loadAPI(path)
            elseif fs.exists(fs.combine("/usr/apis", path)) then
                return loadAPI(fs.combine("/usr/apis", path))
            end
        end
    elseif args[1] == "install" then
        local path
        if tonumber(os.version():sub(9)) > 1.7 then
            path = "/startup/easyshell.lua"
        else
            path = "/startup"
        end
        local contents = loadStartup(path)
        local changed = false
        if fs.exists(path) then         
            local exists = false

            for i = 1, #contents do
                if contents[i] == exStr then
                    exists = true
                    break
                end
            end

            if not exists then
                table.insert(contents, 1, exStr)
                changed = true
            end
        else
            contents[1] = exStr
            changed = true
        end
        if changed then
            writeStartup(contents,path)
        end
        shell.run("usr/bin/easy-shell execute")
    elseif args[1] == "remove" then
        local contents = loadStartup()
        for i = #contents, 1, -1 do
             if contents[i] == exStr then
                table.remove(contents, i)
            end
        end
        writeStartup(contents)
    end
end

If it runs on CraftOS 1.7 and earlier, it write the file /startup. On CraftOS 1.8 it write the file /startup/easyshell.lua, so the User can put other Programs in the startup folder.

lyqyd commented 6 years ago

This was a good idea, I've implemented it.

Thanks!