From 0d148e404334f7039ca79add9d4908aac95c1ab2 Mon Sep 17 00:00:00 2001 From: othelarian Date: Tue, 5 Mar 2024 21:41:23 +0100 Subject: [PATCH] Addig a live command, similar to the cake in CoffeeScript, reading a Livefile at the package root --- Livefile | 4 ++ Makefile | 3 ++ bin-src/live.ls | 43 +++++++++++++++++++ bin/live | 102 +++++++++++++++++++++++++++++++++++++++++++++ scripts/build-live | 15 +++++++ 5 files changed, 167 insertions(+) create mode 100644 Livefile create mode 100644 bin-src/live.ls create mode 100644 bin/live create mode 100644 scripts/build-live diff --git a/Livefile b/Livefile new file mode 100644 index 000000000..89ab79ddf --- /dev/null +++ b/Livefile @@ -0,0 +1,4 @@ +option \option \o \Boolean 'an option' + +task 'valid', 'validating the Livefile', !-> + console.log 'this is a Livefile!' diff --git a/Makefile b/Makefile index b337e49a6..79385d04e 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,9 @@ browser/livescript.js: $(LIB) browser scripts/preroll browser/livescript-min.js: browser/livescript.js $(UGLIFYJS) browser/livescript.js --mangle --comments "all" > browser/livescript-min.js +live: bin/live + ./scripts/build-live + package.json: package.json.ls $(LSC) --compile package.json.ls diff --git a/bin-src/live.ls b/bin-src/live.ls new file mode 100644 index 000000000..20f5b90bf --- /dev/null +++ b/bin-src/live.ls @@ -0,0 +1,43 @@ +require! { + fs, optionator, path + 'prelude-ls': { List, Obj } +} + +lspath = path.join path.dirname(__filename), '../lib' +ls = require lspath + +options = [{ option: \help, alias: \h, type: \Boolean, description: '' }] +option = (option, alias, type, description) -> + options.push { option, alias, type, description } + +tasks = {} +task = (name, desc, fn) -> tasks[name] = { desc, fn } + +global.option = option +global.task = task + +try + fs.readFileSync \Livefile \utf-8 |> ls.run + prepend = 'Usage: live [options]\n\nOptions:' + mlgth = Obj.keys tasks |> List.maximum-by (.length) |> (.length) + filler = (str) -> ' ' * (mlgth - str.length) + line = (acc, elt) -> "#{acc}#{elt.0}#{filler elt.0} #{elt.1.desc}\n" + append = Obj.obj-to-pairs tasks |> List.fold line, 'Commands:\n\n' + find-lsc = List.find-index ((elt) -> elt == /lsc$/), process.argv + args = + if find-lsc? then List.drop (1 + find-lsc), process.argv + else process.argv + args = List.reject ((elt) -> elt == /live.ls$/), process.argv + op = optionator { append, options, prepend } + opts = op.parseArgv args + switch + | opts.help => console.log op.generateHelp! + | opts._.length is 0 => console.log 'ERROR: no task to run!' + | opts._.length > 1 => console.log 'ERROR: too many tasks selected!' + | opts._.0 not in Obj.keys tasks + console.log "ERROR: task does not exist (#{opts._.0})!" + | _ => tasks[opts._.0].fn opts +catch e + switch e.code + | \ENOENT => console.log 'There is no Livefile at this package root' + | otherwise => console.log e \ No newline at end of file diff --git a/bin/live b/bin/live new file mode 100644 index 000000000..39ff570b1 --- /dev/null +++ b/bin/live @@ -0,0 +1,102 @@ +#!/usr/bin/env node + +// Generated by LiveScript 1.6.1 +var fs, optionator, path, ref$, List, Obj, lspath, ls, options, option, tasks, task, prepend, mlgth, filler, line, append, findLsc, args, op, opts, e; +fs = require('fs'); +optionator = require('optionator'); +path = require('path'); +ref$ = require('prelude-ls'), List = ref$.List, Obj = ref$.Obj; +lspath = path.join(path.dirname(__filename), '../lib'); +ls = require(lspath); +options = [{ + option: 'help', + alias: 'h', + type: 'Boolean', + description: '' +}]; +option = function(option, alias, type, description){ + return options.push({ + option: option, + alias: alias, + type: type, + description: description + }); +}; +tasks = {}; +task = function(name, desc, fn){ + return tasks[name] = { + desc: desc, + fn: fn + }; +}; +global.option = option; +global.task = task; +try { + ls.run( + fs.readFileSync('Livefile', 'utf-8')); + prepend = 'Usage: live [options]\n\nOptions:'; + mlgth = function(it){ + return it.length; + }( + List.maximumBy(function(it){ + return it.length; + })( + Obj.keys(tasks))); + filler = function(str){ + return repeatString$(' ', mlgth - str.length); + }; + line = function(acc, elt){ + return acc + "" + elt[0] + filler(elt[0]) + " " + elt[1].desc + "\n"; + }; + append = List.fold(line, 'Commands:\n\n')( + Obj.objToPairs(tasks)); + findLsc = List.findIndex(function(elt){ + return /lsc$/.exec(elt); + }, process.argv); + args = findLsc != null + ? List.drop(1 + findLsc, process.argv) + : process.argv; + args = List.reject(function(elt){ + return /live.ls$/.exec(elt); + }, process.argv); + op = optionator({ + append: append, + options: options, + prepend: prepend + }); + opts = op.parseArgv(args); + switch (false) { + case !opts.help: + console.log(op.generateHelp()); + break; + case opts._.length !== 0: + console.log('ERROR: no task to run!'); + break; + case !(opts._.length > 1): + console.log('ERROR: too many tasks selected!'); + break; + case in$(opts._[0], Obj.keys(tasks)): + console.log("ERROR: task does not exist (" + opts._[0] + ")!"); + break; + default: + tasks[opts._[0]].fn(opts); + } +} catch (e$) { + e = e$; + switch (e.code) { + case 'ENOENT': + console.log('There is no Livefile at this package root'); + break; + default: + console.log(e); + } +} +function repeatString$(str, n){ + for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str; + return r; +} +function in$(x, xs){ + var i = -1, l = xs.length >>> 0; + while (++i < l) if (x === xs[i]) return true; + return false; +} \ No newline at end of file diff --git a/scripts/build-live b/scripts/build-live new file mode 100644 index 000000000..e9fff76c8 --- /dev/null +++ b/scripts/build-live @@ -0,0 +1,15 @@ +#!/usr/bin/env node + +var fs = require("fs"); +var path = require("path"); + +var realpath = path.dirname(fs.realpathSync(__filename)); +var lib = path.join(realpath, "../lib"); +var target = path.resolve(realpath, "../bin-src/live.ls"); +var output = path.resolve(realpath, "../bin/live"); + +var ls = require(lib); + +code = fs.readFileSync(target, "utf-8"); +res = ls.compile(code, {bare: true}); +fs.writeFileSync(output, "#!/usr/bin/env node\n\n" + res); \ No newline at end of file