Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit eb611e4c681063fb287df35d480c486bdb14a0c1 @mgutz committed Feb 5, 2012
Showing with 579 additions and 0 deletions.
  1. +27 −0 .gitignore
  2. +52 −0 README.md
  3. +4 −0 bin/luv
  4. +1 −0 index.lua
  5. +110 −0 lib/luv.lua
  6. +56 −0 lib/util.lua
  7. +312 −0 support/lapp.lua
  8. +17 −0 test/Luvfile.lua
@@ -0,0 +1,27 @@
+*~
+*.bak
+*.swo
+*.swp
+.*.swp
+.*.swo
+.*.bak
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+pids
+logs
+result
+!.gitignore
+.sass-cache
+*DS_Store
+dump.rdb
+node_modules
+log/
+build/
+barc-app/
+dist/server/support/socket.io
@@ -0,0 +1,52 @@
+# Luv
+
+Build utility like Rake for [luvit](http://luvit.io)
+
+## Installation
+
+No npm-like utility yet :(
+
+ git clone https://github.com/mgutz/luv.git
+
+ # cp the luv script to your path
+ cp luv/bin/luv ~/bin
+
+
+## Usage
+
+Create a `Luvfile.lua` in your project's root directory.
+
+```lua
+local desc = ""
+
+-- Specify task depencencies in 3rd argument.
+task("build", "Builds the project.", {"clean"}, function()
+ print("build your project here")
+end).dependencies({"clean"})
+
+-- All tasks must have `name`, `description` and `action`.
+task("clean", "Cleans the project.", function()
+ print("cleaning")
+end)
+```
+
+See all tasks
+
+ luv
+
+Run a task
+
+ luv build
+
+Specify task options.
+
+```lua
+-- Encode tasks in the description and `lapp` style
+local desc = [[
+Creates distribution
+ -a,--archive-name (string) Archive name.
+]]
+task("dist", desc, {"build"}, function(self)
+ print("Creating distribution "..self.args["archive-name"])
+end)
+```
@@ -0,0 +1,4 @@
+#!/usr/bin/env luvit
+
+luv = require("../index")
+luv.run()
@@ -0,0 +1 @@
+return require("./lib/luv")
@@ -0,0 +1,110 @@
+local tasks = {}
+
+local lapp = require("../support/lapp")
+local fs = require("fs")
+local path = require("path")
+local os = require("os")
+local table = require("table")
+local string = require("string")
+local util = require("./util")
+
+--[[
+A build task object.
+
+name - The name of the task.
+description - The description with optional options encoded in `lapp` style.
+action - The action to take.
+--]]
+function Task(name, description, action)
+ local self = {
+ name = name,
+ description = description,
+ action = action,
+ invoked = false
+ }
+
+ if #util.split(description, "\n") > 1 then
+ self.hasOptions = true
+ end
+
+ return self
+end
+
+
+_G.task = function(name, description, ...)
+ local arg = {n=select('#',...),...}
+ local dependencies = nil
+ local action
+
+ if #arg == 1 then
+ action = arg[1]
+ elseif #arg == 2 then
+ dependencies = arg[1]
+ action = arg[2]
+ end
+
+ local t = Task(name, description, action)
+ t.dependencies = dependencies
+ tasks[name] = t
+ return t
+end
+
+
+_G.invoke = function(name)
+ local t = tasks[name]
+ if not t.invoked then
+ if t.hasOptions then
+ t.args = lapp(process.argv, t.description)
+ end
+
+ if t.dependencies then
+ for k, v in ipairs(t.dependencies) do
+ invoke(v)
+ end
+ end
+
+ t.action(t)
+ t.invoked = true
+ end
+end
+
+
+function detectLuvdir(dir)
+ if fs.existsSync(path.join(dir, "Luvfile.lua")) then return dir end
+ parent = path.normalize(path.join(dir, '..'))
+ if parent ~= dir then
+ return detectLuvDir(parent)
+ else
+ return nil
+ end
+end
+
+
+function usage()
+ print("usage: luv TASK [task_options]")
+ print("\nTasks:")
+ for task,_ in util.sortedPairs(tasks) do
+ local firstLine = util.split(tasks[task].description, "\n")[1]
+ print(string.format("%-12s %s", task, firstLine))
+ end
+end
+
+
+function run()
+ local argv = process.argv
+ local cwd = process.cwd()
+ local dir = detectLuvdir(cwd)
+
+ if not dir then
+ print("`Luvfile.lua` file not found in "..cwd)
+ elseif #argv > 0 then
+ require(dir.."/Luvfile")
+ invoke(argv[1])
+ else
+ --fs.chdir("dir")
+ require(dir.."/Luvfile")
+ usage()
+ end
+end
+
+return {run = run}
@@ -0,0 +1,56 @@
+local string = require("string")
+local table = require("table")
+
+--[[
+Splits `str` by `delim`.
+
+From http://lua-users.org/wiki/SplitJoin
+]]
+function split(str, delim, maxNb)
+ -- Eliminate bad cases...
+ if string.find(str, delim) == nil then
+ return { str }
+ end
+ if maxNb == nil or maxNb < 1 then
+ maxNb = 0 -- No limit
+ end
+ local result = {}
+ local pat = "(.-)" .. delim .. "()"
+ local nb = 0
+ local lastPos
+ for part, pos in string.gfind(str, pat) do
+ nb = nb + 1
+ result[nb] = part
+ lastPos = pos
+ if nb == maxNb then break end
+ end
+ -- Handle the last field
+ if nb ~= maxNb then
+ result[nb + 1] = string.sub(str, lastPos)
+ end
+ return result
+end
+
+--[[
+Sorts a table `t` by keys.
+
+From http://www.lua.org/pil/19.3.html
+]]
+function sortedPairs(t, f)
+ local a = {}
+ for n in pairs(t) do table.insert(a, n) end
+ table.sort(a, f)
+ local i = 0 -- iterator variable
+ local iter = function () -- iterator function
+ i = i + 1
+ if a[i] == nil then return nil
+ else return a[i], t[a[i]]
+ end
+ end
+ return iter
+end
+
+return {
+ split = split,
+ sortedPairs = sortedPairs
+}
Oops, something went wrong.

0 comments on commit eb611e4

Please sign in to comment.