Skip to content

Commit

Permalink
restructure commands, add help
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Jun 16, 2013
1 parent 04b54a8 commit 64bcb22
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.rock
todo
153 changes: 97 additions & 56 deletions moonrocks/actions.moon
@@ -1,11 +1,14 @@

import Api from require "moonrocks.api"
import File from require "moonrocks.multipart"
import columnize from require "moonrocks.util"

colors = require "ansicolors"

pretty = require "pl.pretty"

local *

load_rockspec = (fname) ->
rockspec = {}
fn = assert loadfile fname
Expand All @@ -25,72 +28,110 @@ prompt = (msg) ->
return true if line == "Y"

actions = {
login: =>
api = Api @
api\login!

help: =>

install: =>
escaped_args = for arg in *@original_args
-- hope this is good enough ;)
if arg\match "%s"
"'" ..arg\gsub("'", "'\'") .. "'"
{
name: "help"
help: "Show this text"
->
print "MoonRocks #{require "moonrocks.version"} (using #{Api.server})"
print "usage: moonrocks <action> [arguments]"

print!
print "Available actions:"
print!
print columnize [ { t.usage or t.name, t.help } for t in *actions ]
print!
}

{
name: "login"
help: "Set or change api key"

=>
api = Api @
api\login!
}

{
name: "install"
help: "Install a rock using `luarocks`, sets server to rocks.moonscript.org"
=>
escaped_args = for arg in *@original_args
-- hope this is good enough ;)
if arg\match "%s"
"'" ..arg\gsub("'", "'\'") .. "'"
else
arg

server = Api.server
server = "http://" .. server unless server\match "^%w+://"

table.insert escaped_args, 1, "--server=#{server}"

cmd = "luarocks #{table.concat escaped_args, " "}"
os.execute cmd
}

{
name: "upload"
usage: "upload <rockspec>"
help: "Pack source rock, upload rockspec and source rock to server. Pass --skip-pack to skip sending source rock"

(fname) =>
unless fname
error "missing rockspec (moonrocks #{get_action"upload".usage})"

assert fname, "missing rockspec (moonrocks upload my-package.rockspec)"
api = Api @
rockspec = load_rockspec fname

rock_fname = unless @["skip-pack"]
print colors "%{cyan}Packing %{reset}#{rockspec.package}"
ret = os.execute "luarocks pack '#{fname}'"
unless ret == 0
print colors "%{bright red}Failed to pack source rock!%{reset} (--skip-pack to disable)"
return

fname\gsub "rockspec$", "src.rock"

print colors "%{cyan}Sending%{reset} #{fname}..."

res = api\method "check_rockspec", {
package: rockspec.package
version: rockspec.version
}

unless res.module
print colors "%{magenta}Will create new module.%{reset} (#{rockspec.package})"

if res.version
print colors "%{bright yellow}A version of this module already exists.%{reset} (#{rockspec.package} #{rockspec.version})"
return unless prompt "Overwite?"
else
arg

server = Api.server
server = "http://" .. server unless server\match "^%w+://"

table.insert escaped_args, 1, "--server=#{server}"

cmd = "luarocks #{table.concat escaped_args, " "}"
os.execute cmd

upload: (fname) =>
api = Api @
rockspec = load_rockspec fname

rock_fname = unless @["skip-pack"]
print colors "%{cyan}Packing %{reset}#{rockspec.package}"
ret = os.execute "luarocks pack '#{fname}'"
unless ret == 0
print colors "%{bright red}Failed to pack source rock!%{reset} (--skip-pack to disable)"
return
print colors "%{magenta}Will create new version.%{reset} (#{rockspec.version})"

fname\gsub "rockspec$", "src.rock"
res = api\method "upload", nil, rockspec_file: File(fname)

print colors "%{cyan}Sending%{reset} #{fname}..."
if res.is_new and #res.manifests == 0
print colors "%{bright yellow}Warning: module not added to root manifest due to name taken"

res = api\method "check_rockspec", {
package: rockspec.package
version: rockspec.version
}
if rock_fname
print colors "%{cyan}Sending%{reset} #{rock_fname}..."
api\method "upload_rock/#{res.version.id}", nil, rock_file: File(rock_fname)

unless res.module
print colors "%{magenta}Will create new module.%{reset} (#{rockspec.package})"
print colors "%{bright green}Success:%{reset} #{res.module_url}"

if res.version
print colors "%{bright yellow}A version of this module already exists.%{reset} (#{rockspec.package} #{rockspec.version})"
return unless prompt "Overwite?"
else
print colors "%{magenta}Will create new version.%{reset} (#{rockspec.version})"

res = api\method "upload", nil, rockspec_file: File(fname)

if res.is_new and #res.manifests == 0
print colors "%{bright yellow}Warning: module not added to root manifest due to name taken"

if rock_fname
print colors "%{cyan}Sending%{reset} #{rock_fname}..."
api\method "upload_rock/#{res.version.id}", nil, rock_file: File(rock_fname)

print colors "%{bright green}Success:%{reset} #{res.module_url}"
}
}

get_action = (name) ->
for action in *actions
if action.name == name
return action

run = (params, flags) ->
action_name = assert params[1], "missing command"
fn = assert actions[action_name], "unknown action `#{action_name}`"
fn = assert get_action(action_name)[1], "unknown action `#{action_name}`"

params = [p for p in *params[2,]]

xpcall (-> fn flags, unpack params), (err) ->
Expand Down
60 changes: 60 additions & 0 deletions moonrocks/util.moon
@@ -0,0 +1,60 @@
import insert, concat from table

escape_pattern = do
punct = "[%^$()%.%[%]*+%-?%%]"
(str) -> (str\gsub punct, (p) -> "%"..p)

split = (str, delim using nil) ->
str ..= delim
[part for part in str\gmatch "(.-)" .. escape_pattern delim]

-- wrap test based on tokens
wrap_text = (text, indent=0, max_width=80) ->
width = max_width - indent
words = split text, " "
pos = 1
lines = {}
while pos <= #words
line_len = 0
line = {}
while true
word = words[pos]
break if word == nil
error "can't wrap text, words too long" if #word > width
break if line_len + #word > width

pos += 1
insert line, word
line_len += #word + 1 -- +1 for the space

insert lines, concat line, " "

concat lines, "\n" .. (" ")\rep indent

columnize = (rows, indent=2, padding=4) ->
max = 0
max = math.max max, #row[1] for row in *rows

left_width = indent + padding + max

formatted = for row in *rows
padd = (max - #row[1]) + padding
concat {
(" ")\rep indent
row[1]
(" ")\rep padd
wrap_text row[2], left_width
}

concat formatted, "\n"

if ... == "test"
print columnize {
{"hello", "here is some info"}
{"what is going on", "this is going to be a lot of text so it wraps around the end"}
{"this is something", "not so much here"}
{"else", "yeah yeah yeah not so much okay goodbye"}
}

{ :columnize, :split, :escape_pattern }

1 change: 1 addition & 0 deletions moonrocks/version.moon
@@ -0,0 +1 @@
"0.0.1"

0 comments on commit 64bcb22

Please sign in to comment.