store-modules.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. local lfs
  2. -- checks if lua file system is installed
  3. local using_lfs = pcall(function() lfs = require "lfs" end)
  4. -- switches to legacy module loading if lfs is not available
  5. if using_lfs then
  6. -- loads file names to ignore into a table
  7. function fetch_ignores(module_path)
  8. local ignore_list = {}
  9. -- checks if ignore.txt exists
  10. local f_test = io.open(module_path .. "/ignore.txt", "r")
  11. if not f_test then return {} end
  12. -- puts each line of ignore.txt into the table
  13. local f = io.lines(module_path .. "/ignore.txt")
  14. for line in f do
  15. table.insert(ignore_list, line)
  16. end
  17. return ignore_list
  18. end
  19. -- checks if a string is in a list
  20. function check_list(ignore_list, name)
  21. for i, v in ipairs(ignore_list) do
  22. if v == name then
  23. return true
  24. end
  25. end
  26. return false
  27. end
  28. modpol.load_modules = function(module_path)
  29. local loaded = 0
  30. local ignored = 0
  31. local ignores = fetch_ignores(module_path)
  32. for file in lfs.dir(module_path) do
  33. if file == "." or file == ".." then
  34. -- ignoring current and parent directory
  35. else
  36. -- only looks for .lua files
  37. if string.sub(file, -4, -1) == ".lua" then
  38. -- doesn't load files in the ignore.txt
  39. if check_list(ignores, file) then
  40. ignored = ignored + 1
  41. else
  42. dofile(module_path .. "/" .. file)
  43. loaded = loaded + 1
  44. end
  45. end
  46. end
  47. end
  48. print(loaded .. " modules loaded (" .. ignored .. " ignored)")
  49. end
  50. else
  51. dofile (modpol.topdir .. "/storage/store-modules-legacy.lua")
  52. end