Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Last active February 11, 2018 22:56
Show Gist options
  • Save aleclarson/759b5600974c6a8073eff2e3eb81699f to your computer and use it in GitHub Desktop.
Save aleclarson/759b5600974c6a8073eff2e3eb81699f to your computer and use it in GitHub Desktop.
lula - Lua package manager

lula

Try it:

wget https://gist.githubusercontent.com/aleclarson/759b5600974c6a8073eff2e3eb81699f/raw/lula.sh -O ~/bin/lula
chmod +x ~/bin/lula

package.lua

You must create a package.lua file in the root directory of your project. In it, you will declare a global variable for each configuration option you want to use.

Here are the configuration options:

  • main: string Used as the 2nd argument when you run lula or lula start
  • scripts: {} Contains named shell scripts that you run with lula run <name>
  • inject: {} An array of module paths to require before your "main" path
  • dependencies: {} An array of git urls to shallow clone into the ./lib directory

The main option defaults to init.lua.

The scripts.start option defaults to lua.

Usage

# Run the "start" script
lula
lula start

# Run any other script you wish
lula run <name>

# Install "dependencies" into the ./lib directory
lula install
lula i

Roadmap

  • Implement the "inject" feature
  • Implement the "dependencies" feature
  • Support adding a dependency via lula install <git-url>
  • Support adding a dependency by its luarocks name (by resolving it to a url)
  • Support passing extra arguments to lula start and lula run
  • Let scripts contain pairs where the value is an array of commands

Example package.lua

main = "init.lua"
inject = {
  "moonscript",
}
dependencies = {
  "https://github.com/tarantool/queue.git",
}
scripts = {
  start = "tarantool",
  clean = "rm data/backup/* &> /dev/null",
}
CMD=${1:-start}; shift
get_prop() {
SCRIPT="
package.path = '?.lua;' .. package.path
require('$PWD/package')
ok, res = pcall(function()
return $1 or ''
end)
if ok then print(res) end"
lua -e "$SCRIPT"
}
p() {
echo " $@"
}
if [ ! -f "$PWD/package.lua" ]; then
p "Cannot find ./package.lua"
exit 1
fi
if [ $CMD == "install" ] || [ $CMD == "i" ]; then
p "lula install: not yet implemented"
elif [ $CMD == "start" ]; then
CMD="$(get_prop scripts.start)"
CMD=${CMD:-lua}
MAIN=`get_prop main`
MAIN=${MAIN:-init.lua}
echo ""
printf "\e[1m$CMD $MAIN\n\e[0m"
echo ""
eval "$CMD $MAIN"
elif [ $CMD == "run" ]; then
SCRIPT=$1
if [ -z $SCRIPT ]; then
p "Must provide a script name"
exit 1
fi
CMD=`get_prop scripts.$SCRIPT`
if [ -z "$CMD" ]; then
p "Unknown script: $SCRIPT"
exit 1
fi
echo ""
printf "\e[1m$CMD\n\e[0m"
echo ""
eval "$CMD"
else
p "Unknown command: $CMD"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment