Merge AddonLoader with Steve's documentation additions
[io.git] / libs / iovm / io / AddonLoader.io
blob89351d4aab9bcd7043a3943389546dbecdaf2d22
1 Addon := Object clone do(
2 docSlot("rootPath", "Returns the rootPath of the addon's folder.")
3 docSlot("setRootPath(aSequence)", "Sets rootPath of the addon's folder. Returns self.")
4 newSlot("rootPath")
6 docSlot("name", "Returns the name of the addon.")
7 docSlot("setName(aSequence)", "Sets the name of the addon. Returns self.")
8 newSlot("name")
10 docSlot("platform", "Implemented as method(System platform).")
11 platform := System platform
13 docSlot("dllSuffix", "Returns the platform specific dll suffix.")
14 dllSuffix := method(
15 list("cygwin", "mingw", "windows") detect(dllPlatform, platform containsSeq(dllPlatform)) ifNonNil(return("dll"))
16 if(platform == "darwin", return "dylib")
17 "so"
20 docSlot("dllName", "Return the name of the dll for the addon.")
21 dllName := method("libIo" .. name .. "." .. dllSuffix)
23 docSlot("addonPath", "Implemented as Path with(rootPath, name).")
24 addonPath := method(Path with(rootPath, name))
26 docSlot("dllPath", "Returns the path to the dll for the addon. Note: not all addons have dlls - some just contain io files.")
27 dllPath := method(Path with(addonPath, "_build/dll", dllName))
29 docSlot("sourcePath", "Returns the path of the source folder for the addon.")
30 sourcePath := method(Path with(addonPath, "source"))
32 docSlot("ioFiles", "Return list of io File objects for the io files in the io folder of the addon.")
33 ioFiles := method(
34 d := Directory with(addonPath) folderNamed("io")
35 if(d == nil, return list())
36 files := d files select(path endsWithSeq(".io"))
37 files map(name) sort map(name, d fileNamed(name))
40 docSlot("dependencies", "Returns the list of dependencies from the addon's depends file.")
41 dependencies := method(
42 File with(Path with(addonPath, "depends")) contents split(" ")
45 docSlot("loadDependencies", "Loads the addon's dependencies. Called from the load method.")
46 loadDependencies := method(
47 //writeln(name, " depends on ", dependencies)
48 dependencies foreach(d,
49 if(Lobby getSlot(d) == nil,
50 //writeln("loading dependency ", d)
51 AddonLoader loadAddonNamed(d)
56 docSlot("load", "Loads the addon.")
57 load := method(
58 //writeln("Addon ", name, " loading from ", addonPath)
59 loadDependencies
60 context := Object clone
61 Protos Addons setSlot(name, context)
62 Protos appendProto(context)
63 //writeln(dllPath)
64 if(File with(dllPath) exists,
65 DynLib clone setPath(dllPath) open call("Io" .. name .. "Init", context)
67 // check for C files, if found then addon didn't compile
68 if(Directory with(sourcePath) size > 1,
69 Exception raise("Failed to load Addon " .. name .. " - it appears that the addon exists but was not compiled. You might try running 'make " .. name .. "' in the Io source folder.")
72 //ioFiles foreach(f, writeln("loading ", f path))
73 ioFiles foreach(file, context doFile(file path))
74 Lobby getSlot(name)
77 docSlot("exists", "Returns true if the addonPath exists, false otherwise.")
78 exists := method(Directory with(addonPath) exists)
80 docSlot("addonProtos", "Returns names of protos defined in the addon from the addon's protos file.")
81 addonProtos := method(
82 f := File with(Path with(addonPath, "protos"))
83 if(f exists, f contents split, list())
87 AddonLoader := Object clone do(
88 docSlot("searchPaths", "Returns the list of paths to search for addons.")
89 searchPaths := list("io/addons", System installPrefix .. "/lib/io/addons")
91 docSlot("appendSearchPath(aSequence)", "Appends the argument to the list of search paths.")
92 appendSearchPath := method(p, searchPaths appendIfAbsent(p); self)
94 docSlot("addons", "Looks for all addons which can be found and returns them as a list of Addon objects. Caches the result the first time it is called.")
95 addons := method(
96 searchFolders := searchPaths map(path, Directory with(path)) select(exists)
97 addonFolders := searchFolders map(folders) flatten select(isAccessible) select(fileNames contains("build.io"))
98 addons := addonFolders map(f, Addon clone setRootPath(f path pathComponent) setName(f path lastPathComponent))
99 addons
102 docSlot("addonFor(aName)", "Returns the Addon with the given name if it can be found or nil otherwise.")
103 addonFor := method(name,
104 r := addons detect(name == name)
105 if(r, return r)
106 addons detect(addonProtos contains(name))
109 docSlot("hasAddonNamed(aName)", "Returns true if the named addon can be found, false otherwise.")
110 hasAddonNamed := method(name, addonFor(name) != nil)
112 docSlot("loadAddonNamed(aName)", "Loads the Addon with the given name if it can be found or nil otherwise.")
113 loadAddonNamed := method(name,
114 //writeln("loadAddonNamed(", name, ")")
115 addon := addonFor(name)
116 if(addon, addon load, nil)
117 Lobby getSlot(name)