Lubyk utilities
Miscellanous utility functions in lk module.
Script
.scriptSource (level)
Find the source of the current file or the file up x levels in the call chain (-1 = up one level).
.scriptPath (level)
Find the path of the current script or the script up x levels in the call chain (-1 = up one level). The path is relative to the current directory.
-- get path of running script local my_path = lk.scriptPath() --> 'test/lk_test.lua'
If you need an absolute path, use absolutizePath.
.scriptDir (level)
Find the directory of the current script or the directory of the script up x levels in the call chain (-1 = up one level). This is the same as scriptPath but it returns the parent directory.
.deprecation (lib_name, old, new, ...)
Declare a method as being deprecated. This should be used when method names are changed to avoid breaking code without warnings. The syntax is:
function lib.badName(...) return lib.deprecation('lk', 'badName', 'betterName', ...) end
When someone uses the "badName" method, a deprecation warning is printed:
[DEPRECATION] {traceback} 'lk.badName' is deprecated. Please use 'lk.betterName' instead.
Filesystem
.fileType (path)
Test for file/directory existence at @path@. If the element exists, returns the file type such as 'directory' or 'file'. Returns nil if there is nothing at the given path.
.exist lib.fileType
Return true if the file or folder pointed by @path@ exists.
.content (basepath, path)
Return the content of a file as a lua string (not suitable for very long content where parsing the file chunks by chunks is better). The method accepts either a single @path@ argument or a @basepath@ and relative @path@.
.writeall (filepath, data, check_diff)
Write @data@ to @filepath@, creating path folders if necessary. If @check_diff@ is true, only write if the content has changed to avoid changing stat information on the file.
.makePath (path)
Build necessary folders to create a given @path@.
.move (path, new_path)
Move a file or directory from @path@ to @new_path@. This is like "os.rename" but it also builds necessary path components in new_path.
.copy (path, new_path)
Copy a file from @path@ to @new_path@. Builds necessary path components in new_path.
.rmFile (path)
Delete the file located at @path@. Does nothing if the element at @path@ does not exist (already removed).
.rmTree (path, recursive)
Remove the directory at @path@. If @recursive@ is true, remove recursively. This is a dangerous method if the source path is not ensured to be exempt of mistakes.
.pathDir (filepath)
Return the parent folder and filename from a @filepath@. Usage:
local base, file = lk.pathDir(filepath)
This can also be used on urls.
.absolutizePath (path, basepath)
Return an absolute path from a @path@ and optional @basepath@. If basepath is not provided, the method uses the current directory. This method resolves @/../@ and @/./@ in paths and returns a clean path. Can also be used with urls.
For example:
local abs_path = lk.absolutizePath('foo/bar/../baz', '/home') --> '/home/foo/baz'
.absToRel (abs_string, base)
Transform an absolute path or url to a relative path with given base. For example:
local rel = lk.absToRel('/home/foo/bar', '/home/foo/baz') --> '/home/foo/bar' local rel = lk.absToRel('/home/foo/bar', '/home/foo') --> 'bar'
Strings, Arrays
.strip (str)
Removes white spaces at the beginning and end of the string @str@.
.split (str, pat)
Split a string @str@ into elements separated by the pattern @pat@. The function returns a table.
local t = lk.split('foo/bar/baz', '/') --> {'foo', 'bar', 'baz'}
.join (list, sep)
Join elements of a table @list@ with separator @sep@. Returns a string.
local x = lk.join({'foo', 'bar', 'baz'}, '.') --> 'foo.bar.baz'
.insertSorted (list, elem, key)
Insert @elem@ into a @list@, keeping entries in "list" sorted. If elem is not a string, the @elem[key]@ is used to get a string for sorting.
.merge (source, table)
Merge values in @table@ inside @source@. All keys are considered (array and hash).
.deepMerge = deepMerge
nothing changed
TODO MISSING DOCUMENTATION
.deepMerge (base, key, value)
Deep merge @value@ into @base@ at @key@. Returns true if base has been changed. For example:
local base = {a = { b = 4, c = 5 }} lk.deepMerge(base, 'a', { c = 5 }) --> false (nothing changed) lk.deepMerge(base, 'a', { c = 6 }) --> true, base = {a = { b = 4, c = 6 }}
Debugging
.traceRequire ()
Print calls to @require@. This can be used to list all code dependencies. Usage:
lk.traceRequire() -- From now on, all require statements are printed.
If you simply want to make sure no require is not called to autoload code after some point, you should use Autoload.strict.
.log (...)
Print a log message with the current file name and line. This is better then using "print" because it gives us some information on the context of the print statement.
lk.log('foo') --> lk/util.lua:426: foo
Miscellanous
.print = print
TODO MISSING DOCUMENTATION
.spawn (code, ...)
Start a new process with @code@, encoding arguments with yaml.
Here is an example that passes a table of arguments.
local pid = lk.spawn([[ require 'lubyk' lk.Doc.make(%s) ]], {target = 'doc', sources = 'lib', format = 'html'})
.shellQuote (str)
Quote string @str@ so that it can be inserted in a shell command. Example:
os.execute(string.format('latex %s', lk.shellQuote(filepath)))
TODO
Strings,-ArraysTODO MISSING DOCUMENTATION
MiscellanousTODO MISSING DOCUMENTATION