Fixes to AddonLoader for Windows
[io.git] / libs / iovm / io / AddonLoader.io
blob63057cf767bb3f978973904b30e69c13e638091e
1 Addon := Object clone do(
2 newSlot("rootPath")
3 newSlot("name")
5 platform := System platform asLowercase
7 dllSuffix := method(
8 list("cygwin", "mingw", "windows") detect(dllPlatform, platform containsSeq(dllPlatform)) ifNonNil(return("dll"))
9 if(platform == "darwin", return "dylib")
10 "so"
13 dllName := method("libIo" .. name .. "." .. dllSuffix)
15 addonPath := method(Path with(rootPath, name))
17 dllPath := method(Path with(addonPath, "_build/dll", dllName))
18 sourcePath := method(Path with(addonPath, "source"))
20 ioFiles := method(
21 d := Directory with(addonPath) folderNamed("io")
22 if(d == nil, return list())
23 files := d files select(path endsWithSeq(".io"))
24 files map(name) sort map(name, d fileNamed(name))
27 dependencies := method(
28 File with(Path with(addonPath, "depends")) contents split(" ")
31 loadDependencies := method(
32 //writeln(name, " depends on ", dependencies)
33 dependencies foreach(d,
34 if(Lobby getSlot(d) == nil,
35 //writeln("loading dependency ", d)
36 AddonLoader loadAddonNamed(d)
41 load := method(
42 //writeln("Addon ", name, " loading from ", addonPath)
43 loadDependencies
44 context := Object clone
45 Protos Addons setSlot(name, context)
46 Protos appendProto(context)
47 //writeln(dllPath)
48 if(File with(dllPath) exists,
49 DynLib clone setPath(dllPath) open call("Io" .. name .. "Init", context)
51 // check for C files, if found then addon didn't compile
52 if(Directory with(sourcePath) size > 1,
53 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.")
56 //ioFiles foreach(f, writeln("loading ", f path))
57 ioFiles foreach(file, context doFile(file path))
58 Lobby getSlot(name)
61 exists := method(Directory with(addonPath) exists)
63 addonProtos := method(
64 f := File with(Path with(addonPath, "protos"))
65 if(f exists, f contents split, list())
69 AddonLoader := Object clone do(
70 searchPaths := list("io/addons", System installPrefix .. "/lib/io/addons")
71 appendSearchPath := method(p, searchPaths appendIfAbsent(p); self)
73 addons := method(
74 searchFolders := searchPaths map(path, Directory with(path)) select(exists)
75 addonFolders := searchFolders map(folders) flatten select(isAccessible) select(fileNames contains("build.io"))
76 addons := addonFolders map(f, Addon clone setRootPath(f path pathComponent) setName(f path lastPathComponent))
77 addons
80 addonFor := method(name,
81 r := addons detect(name == name)
82 if(r, return r)
83 addons detect(addonProtos contains(name))
86 hasAddonNamed := method(name, addonFor(name) != nil)
88 loadAddonNamed := method(name,
89 //writeln("loadAddonNamed(", name, ")")
90 addon := addonFor(name)
91 if(addon, addon load, nil)
92 Lobby getSlot(name)