Feature Request: common code between projects

This is a feature I would like to see in a future version of Codea: A way to keep code that can be shared between projects.

I have a handful of .lua files I like to include in ever project I start. Some utility classes and functions I have created, MiddleClass, A crude UI library I created for Codea, etc. These files either never change, or I want the changes to be shared between all projects that use them.

So I would like it if there were a “common” or “library” project that would represent a body of lua files that would be shared with all Codea projects.

Saveglobaldata() is your freind

I have a library of personal functions as well, like coin(), sideof(), slicerender()… I just save them with saveglobaldata

@KMEB That is an interesting idea. Its got a number of downsides, but at least it works today.

That is pretty clever. I remember reading in a previous thread that code sharing between projects was on the radar, but this should do in the meantime.

@JockM my plan for this is to allow you to select any number of other projects as dependent projects — this would happen from the + button used to add new files. They would get loaded before your project runs.

If you then changed the dependent (library) project - would that be everywhere? Ie. are we copying, or pointing? And how would that work with the current cut-and-paste regime imposed by apple?

@Simeon perfect, that will do nicely

@Bortels it would just be a weak reference. An instruction to “Load the project with this name before running the current one”.

Please, please, please do this!

Yes! This! This would make things a ton easier when incorporating some of the libraries I’ve been writing. They are starting to get fairly large when it comes to the number of classes I am using.

-- This file is loaded before any user scripts, removing unsafe environment functions

------------------------------------------------
-- Block out any dangerous or insecure functions
------------------------------------------------

arg=nil

__loadedProjects = {}
import = function(projectName)
    if __loadedProjects[projectName] then return nil end -- prevents re-loading of a project
    __loadedProjects[projectName]=1

    -- load Info.plist
    local home = os.getenv("HOME")
    local dir = home.."/Documents/"..projectName..".codea/"
    local info_fname = dir.."Info.plist"
    local info_file = io.open(info_fname,"r")
    assert(info_file~=nil,"Could not open "..info_fname.." in "..projectName)
    local contents = ""
    for line in io.lines(info_fname) do
        contents = contents..line.."\
"
    end

    -- find the buffer order
    local bufferKey = "<key>Buffer Order</key>"
    local idx = contents:find(bufferKey)
    assert(idx~=nil,"Could not find buffer order in "..info_fname.." in "..projectName)
    contents = contents:sub(idx)

    -- find the end of the buffer order
    local idx2 = contents:find("</array>")
    assert(idx2~=nil,"Could not find the end of the buffer order in "..info_fname.." in "..projectName)
    contents = contents:sub(1,idx2)

    -- fin the buffer names
    local buffers = {}
    for buff in contents:gmatch("<string>([%a%s%d]+)</string>") do
        table.insert(buffers,buff)
    end

    -- load each buffer
    for _,buff in ipairs(buffers) do
        local buff_fname = dir..buff..".lua"
        local f = loadfile(buff_fname)
        assert(f~=nil,"Could not load file "..buff..".lua in "..projectName)
        f()
    end
end

--[[
rawget=nil
rawset=nil
rawequal=nil
setfenv=nil
getfenv=nil
string.dump=nil
dofile=nil
io={write=io.write}

load=nil
loadfile=nil

os.execute=nil
os.getenv=nil
os.remove=nil
os.rename=nil
os.tmpname=nil
os.exit=nil
--]]
--[[
-- We allow:
os.time
os.setlocale
os.difftime
os.date
os.clock
--]]
--[[
package.loaded.io=io
package.loaded.package=nil
package=nil
require=nil
--]]

Something I saw somewhere:

-- This file is loaded before any user scripts, removing unsafe environment functions

------------------------------------------------
-- Block out any dangerous or insecure functions
------------------------------------------------

arg=nil

__loadedProjects = {}
import = function(projectName)
    if __loadedProjects[projectName] then return nil end -- prevents re-loading of a project
    __loadedProjects[projectName]=1

    -- load Info.plist
    local home = os.getenv("HOME")
    local dir = home.."/Documents/"..projectName..".codea/"
    local info_fname = dir.."Info.plist"
    local info_file = io.open(info_fname,"r")
    assert(info_file~=nil,"Could not open "..info_fname.." in "..projectName)
    local contents = ""
    for line in io.lines(info_fname) do
        contents = contents..line.."\
"
    end

    -- find the buffer order
    local bufferKey = "<key>Buffer Order</key>"
    local idx = contents:find(bufferKey)
    assert(idx~=nil,"Could not find buffer order in "..info_fname.." in "..projectName)
    contents = contents:sub(idx)

    -- find the end of the buffer order
    local idx2 = contents:find("</array>")
    assert(idx2~=nil,"Could not find the end of the buffer order in "..info_fname.." in "..projectName)
    contents = contents:sub(1,idx2)

    -- fin the buffer names
    local buffers = {}
    for buff in contents:gmatch("<string>([%a%s%d]+)</string>") do
        table.insert(buffers,buff)
    end

    -- load each buffer
    for _,buff in ipairs(buffers) do
        local buff_fname = dir..buff..".lua"
        local f = loadfile(buff_fname)
        assert(f~=nil,"Could not load file "..buff..".lua in "..projectName)
        f()
    end
end

--[[
rawget=nil
rawset=nil
rawequal=nil
setfenv=nil
getfenv=nil
string.dump=nil
dofile=nil
io={write=io.write}

load=nil
loadfile=nil

os.execute=nil
os.getenv=nil
os.remove=nil
os.rename=nil
os.tmpname=nil
os.exit=nil
--]]
--[[
-- We allow:
os.time
os.setlocale
os.difftime
os.date
os.clock
--]]
--[[
package.loaded.io=io
package.loaded.package=nil
package=nil
require=nil
--]]

@TheRogueBatcher - please start a new discussion rather than resurrecting 18 month old threads. (I know you were trying to be helpful, it’s just that it is confusing to suddenly see such old discussions resurfacing).

If you include code, it will format correctly if you put three ~ before and after it, as I’ve done above. You also seem to have double posted, I’ll delete the second post, if that’s the case.

Finally, on this topic, Codea now has the ability to set dependencies to other projects, allowing you to share code between your projects. I think this is what was being discussed.