{"id":189,"date":"2016-03-30T21:00:49","date_gmt":"2016-03-30T20:00:49","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=189"},"modified":"2023-05-12T10:30:15","modified_gmt":"2023-05-12T09:30:15","slug":"lua-add-package-path","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/lua-add-package-path\/","title":{"rendered":"Lua add package path"},"content":{"rendered":"\n<p>The add_package_path function:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"lua\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">local gmatch = string.gmatch\nlocal append = table.insert\nlocal join = table.concat\n\nfunction to_array(iterator)\n    local arr = {}\n    for v in iterator do\n        append(arr, v)\n    end\n    return arr\nend\n\nfunction add_path(package_path, path)\n  local paths = to_array(gmatch(package_path, \"[^;]+\"))\n  -- check if the path does not exist?\n  append(paths, path)\n  return join(paths, \";\")\nend\n\nfunction add_package_path(path)\n  package.path = add_path(package.path, path)\nend\n\nfunction add_package_cpath(path)\n  package.cpath = add_path(package.cpath, path)\nend\n\n\nprint(add_path(package.path, '.\/lua_modules\/?.lua'))<\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">\n<\/pre>\n\n\n\n<p>Luarocks example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir app\ncd app\/\nluarocks install --tree=lua_modules promise-es6 \nluarocks install --tree=lua_modules luv\nluarocks list --tree=lua_modules\n<\/pre>\n\n\n\n<p>Example usage of the add_package_path function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_package_path(\".\/lua_modules\/share\/lua\/5.3\/?.lua\")\nadd_package_cpath(\".\/lua_modules\/lib\/lua\/5.3\/?.so\")\n\nlocal Promise = require(\"Promise\")\nlocal luv = require(\"luv\")\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The add_package_path function: Luarocks example: mkdir app cd app\/ luarocks install &#8211;tree=lua_modules promise-es6 luarocks install &#8211;tree=lua_modules luv luarocks list &#8211;tree=lua_modules Example usage of the add_package_path function: add_package_path(&#8220;.\/lua_modules\/share\/lua\/5.3\/?.lua&#8221;) add_package_cpath(&#8220;.\/lua_modules\/lib\/lua\/5.3\/?.so&#8221;) local Promise = require(&#8220;Promise&#8221;) local luv = require(&#8220;luv&#8221;)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-lua"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=189"}],"version-history":[{"count":10,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/189\/revisions"}],"predecessor-version":[{"id":7781,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/189\/revisions\/7781"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}