Skip to content

Commit

Permalink
Add help command to script/extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
seancribbs committed Jul 15, 2008
1 parent 88ccd2b commit 5b7c8a1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
67 changes: 64 additions & 3 deletions lib/radiant/extension/script.rb
Expand Up @@ -121,11 +121,11 @@ class Git < Checkout
def checkout_command
"git clone #{url} #{name}"
end

def abstract?
false
end

def matches?
self.url =~ /\.?git/
end
Expand Down Expand Up @@ -175,7 +175,7 @@ class Extension
module Script
class << self
def execute(args)
command = args.shift
command = args.shift || 'help'
const_get(command.camelize).new(args)
end
end
Expand Down Expand Up @@ -232,6 +232,67 @@ def initialize(args=[])
end
end
end

class Help
include Util
def initialize(args=[])
helpcmd = args.shift || 'basic'
send helpcmd
end

def basic
output <<-HELP
Usage: script/extension [command] [arguments]
Commands:
install Install an extension from the registry.
uninstall Uninstall a previously installed extension.
help Display help for commands
Type 'script/extension help [command]' for information about that
command.
HELP
end
alias :help :basic

def install
output <<-HELP
Usage: script/extension install extension_name
- Installs an extension from the information in the registry.
HELP
end

def uninstall
output <<-HELP
Usage: script/extension uninstall extension_name
- Uninstalls a previously installed extension.
HELP
end

private
def output(string='')
$stdout.puts strip_leading_whitespace(string)
end

def strip_leading_whitespace(text)
text = text.dup
text.gsub!("\t", " ")
lines = text.split("\n")
leading = lines.map do |line|
unless line =~ /^\s*$/
line.match(/^(\s*)/)[0].length
else
nil
end
end.compact.min
lines.inject([]) {|ary, line| ary << line.sub(/^[ ]{#{leading}}/, "")}.join("\n")
end
end
end
end
end
Empty file modified script/extension 100644 → 100755
Empty file.
19 changes: 19 additions & 0 deletions spec/lib/radiant/extension/script_spec.rb
Expand Up @@ -8,12 +8,20 @@

Radiant::Extension::Script::Uninstall.should_receive(:new)
Radiant::Extension::Script.execute ['uninstall']

Radiant::Extension::Script::Help.should_receive(:new)
Radiant::Extension::Script.execute ['help']
end

it "should pass the command-line args to the subscript" do
Radiant::Extension::Script::Install.should_receive(:new).with(['page_attachments'])
Radiant::Extension::Script.execute ['install', 'page_attachments']
end

it "should run the help command if no command is passed" do
Radiant::Extension::Script::Help.should_receive(:new).with([])
Radiant::Extension::Script.execute []
end
end

describe "Radiant::Extension::Script::Util" do
Expand Down Expand Up @@ -98,6 +106,17 @@
end
end


describe "Radiant::Extension::Script::Help" do
it "should output a help message for each command" do
messages = %w{basic install uninstall help}
$stdout.should_receive(:puts).exactly(messages.size).times
messages.each do |m|
Radiant::Extension::Script::Help.new([m])
end
end
end

describe "Registry::Action" do
before :each do
@action = Registry::Action.new
Expand Down

0 comments on commit 5b7c8a1

Please sign in to comment.